Skip to content
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

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

Closed
DmitriyMV opened this issue Sep 21, 2021 · 4 comments
Closed

Comments

@DmitriyMV
Copy link

@DmitriyMV DmitriyMV commented Sep 21, 2021

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 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

Loading

@ALTree ALTree added this to the Go1.18 milestone Sep 21, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 21, 2021

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.

Loading

@griesemer griesemer closed this Sep 21, 2021
@DmitriyMV
Copy link
Author

@DmitriyMV DmitriyMV commented Sep 21, 2021

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

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Sep 21, 2021

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

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants