]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.23] go/types, types2: Named.cleanup must also handle *Alias types
authorRobert Griesemer <gri@golang.org>
Thu, 15 Aug 2024 23:07:04 +0000 (16:07 -0700)
committerCherry Mui <cherryyz@google.com>
Wed, 21 Aug 2024 17:42:47 +0000 (17:42 +0000)
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 #68894.

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>
Reviewed-on: https://go-review.googlesource.com/c/go/+/606656

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

index 20e3f52facd9deb4f3f38ca8aec21cf6fd53f728..b339def7354e28b319c70452516492e6d938c78f 100644 (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)
+       }
+}
index 1859b27aa4edfbe156245e1e6e3a73ecd15cdaa3..02b5ecf1669ea565fdc1d21824c4745aaa63245b 100644 (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
index 3f459d3883017efcd4e78d482948588376d43e95..da0c0c1255b63ef4b084f6bb69d8970784f6b48c 100644 (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)
+       }
+}
index b44fa9d788c34587fc30c5a1b7febd6e2b8dbabe..d55b023812d10801b0c10df18ba72b50994278f5 100644 (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