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: can't assign pointer from generic type to generic interface satisfied by pointer receiver #47747

Closed
4ad opened this issue Aug 17, 2021 · 6 comments
Assignees
Labels
Milestone

Comments

@4ad
Copy link
Member

@4ad 4ad commented Aug 17, 2021

On tip (a304273) with -gcflags -G=3.

Repro:

package p

type Fooer[t any] interface {
	Foo(Barer[t])
}
type Barer[t any] interface {
	Bar(t)
}

type Foo[t any] t
type Bar[t any] t

func (l Foo[t]) Foo(v Barer[t]) { v.Bar(t(l)) }
func (b *Bar[t]) Bar(l t)       { *b = Bar[t](l) }

func f[t any](f Fooer[t]) t {
	var b Bar[t]
	f.Foo(&b)
	return t(b)
}

Compiler aborts with:

go build -gcflags -G=3 g.go
# command-line-arguments
./g.go:18:8: incompatible type: cannot use &b (value of type *Bar[t]) as Barer[t] value

Works with go2go: https://go2goplay.golang.org/p/HKqbj1Y6Ahh

@4ad 4ad added this to the Go1.18 milestone Aug 17, 2021
@4ad
Copy link
Member Author

@4ad 4ad commented Aug 17, 2021

Simpler repro:

package p

type Fooer[t any] interface {
	Foo()
}

type Foo[t any] t

func (f *Foo[t]) Foo() {}

func f[t any](v t) {
	var f = Foo[t](v)
	_ = Fooer[t](&f)
}

go build -gcflags -G=3 g.go
# command-line-arguments
./g.go:13:15: cannot convert &f (value of type *Foo[t]) to Fooer[t]

Works with go2go: https://go2goplay.golang.org/p/TSQHwwv_p3o

Loading

@4ad 4ad changed the title cmd/compile: compiler rejects assignment of value of generic type to satisfied interface cmd/compile: can't assign pointer from generic type to generic interface satisfied by pointer receiver Aug 17, 2021
@mknyszek
Copy link
Contributor

@mknyszek mknyszek commented Aug 17, 2021

CC @ianlancetaylor @griesemer because I'm not actually sure what the rules are around this case.

Loading

@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 17, 2021

Simpler reproducer: https://go2goplay.golang.org/p/NLPVJOJuWIY (works with go2go but not with current compiler).

Loading

@griesemer griesemer self-assigned this Aug 17, 2021
@griesemer
Copy link
Contributor

@griesemer griesemer commented Aug 17, 2021

Direct reproducer:

package p

type T[P any] P

func (T[_]) m() {}

func _[P any](x *T[P]) {
        x.m()
}

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 17, 2021

Change https://golang.org/cl/342990 mentions this issue: cmd/compile/internal/types2: fix lookup for type-parameter based type

Loading

@gopherbot
Copy link

@gopherbot gopherbot commented Aug 17, 2021

Change https://golang.org/cl/342991 mentions this issue: go/types: fix method lookup for type-parameter based types

Loading

gopherbot pushed a commit that referenced this issue Aug 17, 2021
…ed types

Pointers to type parameters don't have methods, but pointers to
defined types whose underlying types are type parameters may have
methods. Fix the respective test.

For #47747.

Change-Id: I1de47be094ed9297f0e7782538011657c37c5adc
Reviewed-on: https://go-review.googlesource.com/c/go/+/342990
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
@gopherbot gopherbot closed this in 0f85b0c Aug 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants