Skip to content

cmd/compile: pointer to concrete type doesn't satisfy generic type method set #48512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
DmitriyMV opened this issue Sep 21, 2021 · 4 comments
Closed
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@DmitriyMV
Copy link
Contributor

This code:

package main

import "fmt"

type Fooer interface{ Foo() }

type FooGetter[F Fooer] interface{ GetFooer() F }

type A struct{}

func (A) GetFooer() B { return B{} }

type B struct{}

func (B) Foo() {}

func fun[F Fooer, FG FooGetter[F]](a *FG) F {
	return a.GetFooer()
}

func main() {
	fmt.Printf("%T", fun[B, A](&A{}))
}

works on playground, but doesn't on latest tip. I suspect this is correct behavior since simplified version doesn't compile either on go2go playground. But then, why does go2go permits this code?

@ALTree
Copy link
Member

ALTree commented Sep 21, 2021

why does go2go permits this code?

go2go was only created to experiment with the type parameters proposal when no implementation existed, the fact that it does or does not permit some piece of code doesn't mean much in itself.

cc @griesemer

@ALTree ALTree added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 21, 2021
@ALTree ALTree added this to the Go1.18 milestone Sep 21, 2021
@griesemer
Copy link
Contributor

The error message is pretty clear:

x.go:18:11: a.GetFooer undefined (type *FG has no field or method GetFooer)

The type *FG (pointer to type parameter) doesn't have a GetFooer method, but the type FG does. This code compiles:

package main

import "fmt"

type Fooer interface{ Foo() }

type FooGetter[F Fooer] interface{ GetFooer() F }

type A struct{}

func (A) GetFooer() B { return B{} }

type B struct{}

func (B) Foo() {}

func fun[F Fooer, FG FooGetter[F]](a FG) F { // <<< FG not *FG
	return a.GetFooer()
}

func main() {
	fmt.Printf("%T", fun[B, A](A{})) // <<< A{} not &A{}
}

There's no automatic indirection for pointers to type parameters - maybe there should be (we're still fine-tuning those rules).

I don't know why your original version works on the go2go playground - possibly because the translated code does the automatic indirection (because it's been translated into regular Go types). But it doesn't really matter - we're not going to dig into that - the playground was for experimentation only is not kept up tp date. I recommend you use the latest compiler as the ground truth at this point. Thanks.

Closing as there is nothing to do here.

@DmitriyMV
Copy link
Contributor Author

@griesemer sorry to bother you a little more, but what is the correct way to require pointers on callee side?

@griesemer
Copy link
Contributor

Pass a pointer type argument in main: fmt.Printf("%T", fun[B, *A](&A{}))
Also, see the section on constraint type inference. Thanks.

@golang golang locked and limited conversation to collaborators Sep 21, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants