Skip to content

Commit 818515f

Browse files
griesemergopherbot
authored andcommitted
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 5276107 commit 818515f

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

src/cmd/compile/internal/types2/issues_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,3 +1121,23 @@ func f(x int) {
11211121
t.Errorf("got: %s want: %s", got, want)
11221122
}
11231123
}
1124+
1125+
func TestIssue68877(t *testing.T) {
1126+
const src = `
1127+
package p
1128+
1129+
type (
1130+
S struct{}
1131+
A = S
1132+
T A
1133+
)`
1134+
1135+
conf := Config{EnableAlias: true}
1136+
pkg := mustTypecheck(src, &conf, nil)
1137+
T := pkg.Scope().Lookup("T").(*TypeName)
1138+
got := T.String() // this must not panic (was issue)
1139+
const want = "type p.T struct{}"
1140+
if got != want {
1141+
t.Errorf("got %s, want %s", got, want)
1142+
}
1143+
}

src/cmd/compile/internal/types2/named.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ func (t *Named) cleanup() {
282282
if t.TypeArgs().Len() == 0 {
283283
panic("nil underlying")
284284
}
285-
case *Named:
285+
case *Named, *Alias:
286286
t.under() // t.under may add entries to check.cleaners
287287
}
288288
t.check = nil

src/go/types/issues_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,3 +1131,23 @@ func f(x int) {
11311131
t.Errorf("got: %s want: %s", got, want)
11321132
}
11331133
}
1134+
1135+
func TestIssue68877(t *testing.T) {
1136+
const src = `
1137+
package p
1138+
1139+
type (
1140+
S struct{}
1141+
A = S
1142+
T A
1143+
)`
1144+
1145+
t.Setenv("GODEBUG", "gotypesalias=1")
1146+
pkg := mustTypecheck(src, nil, nil)
1147+
T := pkg.Scope().Lookup("T").(*TypeName)
1148+
got := T.String() // this must not panic (was issue)
1149+
const want = "type p.T struct{}"
1150+
if got != want {
1151+
t.Errorf("got %s, want %s", got, want)
1152+
}
1153+
}

src/go/types/named.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)