]> Cypherpunks repositories - gostls13.git/commitdiff
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)
committerGopher Robot <gobot@golang.org>
Fri, 16 Aug 2024 13:54:51 +0000 (13:54 +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 #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>

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 241371b72fea4d19cb608b9c5ec6fb74361a1320..92dedf51d568a63d009b3293b66b152bfb69ef31 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 316cdc9bba41ff3ddd26dbcbf86319379ad89676..21d0f4f59f838893c63b06bba1b996a37cc5db2f 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