aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2024-02-21 12:54:10 -0800
committerThan McIntosh <thanm@google.com>2024-03-26 19:41:26 +0000
commite23707b59cc7842867cb6366e33eaf145b861f7c (patch)
tree2427641741b8ad75b7758f1f326303e832198591
parent3826650c99b177364670dad9766ea499cab48dfc (diff)
downloadgo-e23707b59cc7842867cb6366e33eaf145b861f7c.tar.gz
go-e23707b59cc7842867cb6366e33eaf145b861f7c.zip
[release-branch.go1.22] go/types, types2: handle Alias types in substitution
Fixes #65858. For #65778. // for x/tools/cmd/gotype Change-Id: I67d4644b28e831926fc6c233098aa1755c57162f Reviewed-on: https://go-review.googlesource.com/c/go/+/565835 Auto-Submit: Robert Griesemer <gri@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Robert Findley <rfindley@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/565840 Run-TryBot: Robert Griesemer <gri@google.com> TryBot-Result: Gopher Robot <gobot@golang.org>
-rw-r--r--src/cmd/compile/internal/types2/subst.go12
-rw-r--r--src/go/types/subst.go12
-rw-r--r--src/internal/types/testdata/fixedbugs/issue65854.go13
3 files changed, 37 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/types2/subst.go b/src/cmd/compile/internal/types2/subst.go
index 09dc58527a..1ad73c41ce 100644
--- a/src/cmd/compile/internal/types2/subst.go
+++ b/src/cmd/compile/internal/types2/subst.go
@@ -95,6 +95,18 @@ func (subst *subster) typ(typ Type) Type {
case *Basic:
// nothing to do
+ case *Alias:
+ rhs := subst.typ(t.fromRHS)
+ if rhs != t.fromRHS {
+ // This branch cannot be reached because the RHS of an alias
+ // may only contain type parameters of an enclosing function.
+ // Such function bodies are never "instantiated" and thus
+ // substitution is not called on locally declared alias types.
+ // TODO(gri) adjust once parameterized aliases are supported
+ panic("unreachable for unparameterized aliases")
+ // return subst.check.newAlias(t.obj, rhs)
+ }
+
case *Array:
elem := subst.typOrNil(t.elem)
if elem != t.elem {
diff --git a/src/go/types/subst.go b/src/go/types/subst.go
index 1934ebab2b..178f717283 100644
--- a/src/go/types/subst.go
+++ b/src/go/types/subst.go
@@ -97,6 +97,18 @@ func (subst *subster) typ(typ Type) Type {
case *Basic:
// nothing to do
+ case *Alias:
+ rhs := subst.typ(t.fromRHS)
+ if rhs != t.fromRHS {
+ // This branch cannot be reached because the RHS of an alias
+ // may only contain type parameters of an enclosing function.
+ // Such function bodies are never "instantiated" and thus
+ // substitution is not called on locally declared alias types.
+ // TODO(gri) adjust once parameterized aliases are supported
+ panic("unreachable for unparameterized aliases")
+ // return subst.check.newAlias(t.obj, rhs)
+ }
+
case *Array:
elem := subst.typOrNil(t.elem)
if elem != t.elem {
diff --git a/src/internal/types/testdata/fixedbugs/issue65854.go b/src/internal/types/testdata/fixedbugs/issue65854.go
new file mode 100644
index 0000000000..744777a94f
--- /dev/null
+++ b/src/internal/types/testdata/fixedbugs/issue65854.go
@@ -0,0 +1,13 @@
+// -gotypesalias=1
+
+// Copyright 2024 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+type A = int
+
+type T[P any] *A
+
+var _ T[int]