go/types: fix TypeName.IsAlias for type parameter names

Fixes #49213

Change-Id: I2bfc151b74b0d14efbd00e5d28584f4180126c5d
Reviewed-on: https://go-review.googlesource.com/c/go/+/359656
Trust: Robert Griesemer <gri@golang.org>
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
zhouguangyuan 2021-10-29 15:24:28 +08:00 committed by Robert Findley
parent 0ae4c7ff4a
commit 80bedb8480
3 changed files with 6 additions and 1 deletions

View File

@ -269,6 +269,8 @@ func (obj *TypeName) IsAlias() bool {
return obj.pkg != nil || t.name != obj.name || t == universeByte || t == universeRune
case *Named:
return obj != t.obj
case *TypeParam:
return obj != t.obj
default:
return true
}

View File

@ -30,6 +30,8 @@ func TestIsAlias(t *testing.T) {
pkg := NewPackage("p", "p")
t1 := NewTypeName(0, pkg, "t1", nil)
n1 := NewNamed(t1, new(Struct), nil)
t5 := NewTypeName(0, pkg, "t5", nil)
NewTypeParam(t5, nil)
for _, test := range []struct {
name *TypeName
alias bool
@ -43,6 +45,7 @@ func TestIsAlias(t *testing.T) {
{NewTypeName(0, nil, "int32", Typ[Int32]), false}, // type name refers to basic type with same name
{NewTypeName(0, pkg, "int32", Typ[Int32]), true}, // type name is declared in user-defined package (outside Universe)
{NewTypeName(0, nil, "rune", Typ[Rune]), true}, // type name refers to basic type rune which is an alias already
{t5, false}, // type name refers to type parameter and vice versa
} {
check(test.name, test.alias)
}

View File

@ -5,7 +5,7 @@
package issue45985
// TODO(rFindley): this error should be on app[int] below.
func app[S /* ERROR "type S = S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
func app[S /* ERROR "type S S does not match" */ interface{ ~[]T }, T any](s S, e T) S {
return append(s, e)
}