From 818515fd3ec69a3276d7d9753c148d8103888582 Mon Sep 17 00:00:00 2001
From: Robert Griesemer <gri@golang.org>
Date: Thu, 15 Aug 2024 16:07:04 -0700
Subject: [PATCH] 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>
---
 .../compile/internal/types2/issues_test.go    | 20 +++++++++++++++++++
 src/cmd/compile/internal/types2/named.go      |  2 +-
 src/go/types/issues_test.go                   | 20 +++++++++++++++++++
 src/go/types/named.go                         |  2 +-
 4 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/src/cmd/compile/internal/types2/issues_test.go b/src/cmd/compile/internal/types2/issues_test.go
index 20e3f52facd..b339def7354 100644
--- a/src/cmd/compile/internal/types2/issues_test.go
+++ b/src/cmd/compile/internal/types2/issues_test.go
@@ -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)
+	}
+}
diff --git a/src/cmd/compile/internal/types2/named.go b/src/cmd/compile/internal/types2/named.go
index 241371b72fe..92dedf51d56 100644
--- a/src/cmd/compile/internal/types2/named.go
+++ b/src/cmd/compile/internal/types2/named.go
@@ -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
diff --git a/src/go/types/issues_test.go b/src/go/types/issues_test.go
index 3f459d38830..da0c0c1255b 100644
--- a/src/go/types/issues_test.go
+++ b/src/go/types/issues_test.go
@@ -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)
+	}
+}
diff --git a/src/go/types/named.go b/src/go/types/named.go
index 316cdc9bba4..21d0f4f59f8 100644
--- a/src/go/types/named.go
+++ b/src/go/types/named.go
@@ -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
-- 
GitLab