Skip to content

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

Closed

Description

@DmitriyMV

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?

Activity

  1. ALTree commented on Sep 21, 2021

    @ALTree
    Member

    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

  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Sep 21, 2021
  3. added this to the Go1.18 milestone on Sep 21, 2021
  4. griesemer commented on Sep 21, 2021

    @griesemer
    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.

  5. DmitriyMV commented on Sep 21, 2021

    @DmitriyMV
    ContributorAuthor

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

  6. griesemer commented on Sep 21, 2021

    @griesemer
    Contributor

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

  7. locked and limited conversation to collaborators on Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions