]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: handle Alias types in substitution
authorRobert Griesemer <gri@golang.org>
Wed, 21 Feb 2024 20:54:10 +0000 (12:54 -0800)
committerGopher Robot <gobot@golang.org>
Wed, 21 Feb 2024 21:26:51 +0000 (21:26 +0000)
Fixes #65854.
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>
src/cmd/compile/internal/types2/subst.go
src/go/types/subst.go
src/internal/types/testdata/fixedbugs/issue65854.go [new file with mode: 0644]

index 09dc58527a11f1061cabe76fa20eb0a0baa91ebd..1ad73c41ce1f08df4e91adf5d864880d703a7211 100644 (file)
@@ -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 {
index 1934ebab2b1a4f962836d7fe0986b5db59e2e18f..178f71728358940b2a774bbdfcfcee84251aea87 100644 (file)
@@ -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 (file)
index 0000000..744777a
--- /dev/null
@@ -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]