1
0
mirror of https://github.com/golang/go synced 2024-11-22 05:34:39 -07:00

go/types, types2: Named.cleanup must also handle *Alias types

Named.cleanup is called at the end of type-checking to ensure that
a named type is fully set up; specifically that it's underlying
field is not (still) a Named type. Now it can also be an *Alias
type. Add this case to the respective type switch.

Fixes #68877.

Change-Id: I29bc0024ac9d8b0152a3d97c82dd28d09d5dbd66
Reviewed-on: https://go-review.googlesource.com/c/go/+/605977
Auto-Submit: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Robert Griesemer 2024-08-15 16:07:04 -07:00 committed by Gopher Robot
parent 527610763b
commit 818515fd3e
4 changed files with 42 additions and 2 deletions

View File

@ -1121,3 +1121,23 @@ func f(x int) {
t.Errorf("got: %s want: %s", got, want)
}
}
func TestIssue68877(t *testing.T) {
const src = `
package p
type (
S struct{}
A = S
T A
)`
conf := Config{EnableAlias: true}
pkg := mustTypecheck(src, &conf, nil)
T := pkg.Scope().Lookup("T").(*TypeName)
got := T.String() // this must not panic (was issue)
const want = "type p.T struct{}"
if got != want {
t.Errorf("got %s, want %s", got, want)
}
}

View File

@ -282,7 +282,7 @@ func (t *Named) cleanup() {
if t.TypeArgs().Len() == 0 {
panic("nil underlying")
}
case *Named:
case *Named, *Alias:
t.under() // t.under may add entries to check.cleaners
}
t.check = nil

View File

@ -1131,3 +1131,23 @@ func f(x int) {
t.Errorf("got: %s want: %s", got, want)
}
}
func TestIssue68877(t *testing.T) {
const src = `
package p
type (
S struct{}
A = S
T A
)`
t.Setenv("GODEBUG", "gotypesalias=1")
pkg := mustTypecheck(src, nil, nil)
T := pkg.Scope().Lookup("T").(*TypeName)
got := T.String() // this must not panic (was issue)
const want = "type p.T struct{}"
if got != want {
t.Errorf("got %s, want %s", got, want)
}
}

View File

@ -285,7 +285,7 @@ func (t *Named) cleanup() {
if t.TypeArgs().Len() == 0 {
panic("nil underlying")
}
case *Named:
case *Named, *Alias:
t.under() // t.under may add entries to check.cleaners
}
t.check = nil