mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
go/types: fix method lookup for type-parameter based types
This is a clean port of CL 342990. Fixes #47747. Change-Id: I2e86fb8b70d42a220ac1ba25798d9e58227ba5f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/342991 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>
This commit is contained in:
parent
9d9e3291fa
commit
0f85b0c0e1
2 changed files with 74 additions and 3 deletions
|
|
@ -76,9 +76,12 @@ func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (o
|
||||||
typ, isPtr := deref(T)
|
typ, isPtr := deref(T)
|
||||||
|
|
||||||
// *typ where typ is an interface or type parameter has no methods.
|
// *typ where typ is an interface or type parameter has no methods.
|
||||||
switch under(typ).(type) {
|
|
||||||
case *Interface, *TypeParam:
|
|
||||||
if isPtr {
|
if isPtr {
|
||||||
|
// don't look at under(typ) here - was bug (issue #47747)
|
||||||
|
if _, ok := typ.(*TypeParam); ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if _, ok := under(typ).(*Interface); ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
68
src/go/types/testdata/fixedbugs/issue47747.go2
vendored
Normal file
68
src/go/types/testdata/fixedbugs/issue47747.go2
vendored
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
// Copyright 2021 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package p
|
||||||
|
|
||||||
|
type T1[P any] P
|
||||||
|
|
||||||
|
func (T1[_]) m() {}
|
||||||
|
|
||||||
|
func _[P any](x *T1[P]) {
|
||||||
|
// x.m exists because x is of type *T1 where T1 is a defined type
|
||||||
|
// (even though under(T1) is a type parameter)
|
||||||
|
x.m()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func _[P interface{ m() }](x P) {
|
||||||
|
x.m()
|
||||||
|
// (&x).m doesn't exist because &x is of type *P
|
||||||
|
// and pointers to type parameters don't have methods
|
||||||
|
(&x).m /* ERROR \*P has no field or method m */ ()
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
type T2 interface{ m() }
|
||||||
|
|
||||||
|
func _(x *T2) {
|
||||||
|
// x.m doesn't exists because x is of type *T2
|
||||||
|
// and pointers to interfaces don't have methods
|
||||||
|
x.m /* ERROR \*T2 has no field or method m */()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 1 from issue
|
||||||
|
|
||||||
|
type Fooer1[t any] interface {
|
||||||
|
Foo(Barer[t])
|
||||||
|
}
|
||||||
|
type Barer[t any] interface {
|
||||||
|
Bar(t)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Foo1[t any] t
|
||||||
|
type Bar[t any] t
|
||||||
|
|
||||||
|
func (l Foo1[t]) Foo(v Barer[t]) { v.Bar(t(l)) }
|
||||||
|
func (b *Bar[t]) Bar(l t) { *b = Bar[t](l) }
|
||||||
|
|
||||||
|
func _[t any](f Fooer1[t]) t {
|
||||||
|
var b Bar[t]
|
||||||
|
f.Foo(&b)
|
||||||
|
return t(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test case 2 from issue
|
||||||
|
|
||||||
|
type Fooer2[t any] interface {
|
||||||
|
Foo()
|
||||||
|
}
|
||||||
|
|
||||||
|
type Foo2[t any] t
|
||||||
|
|
||||||
|
func (f *Foo2[t]) Foo() {}
|
||||||
|
|
||||||
|
func _[t any](v t) {
|
||||||
|
var f = Foo2[t](v)
|
||||||
|
_ = Fooer2[t](&f)
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue