aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobert Griesemer <gri@google.com>2025-11-18 15:47:44 -0800
committerGopher Robot <gobot@golang.org>2025-11-20 10:25:17 -0800
commit790384c6c23f7ce44199ea3cd61c856d632b08aa (patch)
tree04d6b61b6eca359cbfffc62371d56bd78fb02159 /src
parenta49b0302d0e1d97b67a5f3f3beceafdcbc4c2ef0 (diff)
downloadgo-790384c6c23f7ce44199ea3cd61c856d632b08aa.tar.xz
spec: adjust rule for type parameter on RHS of alias declaration
Per discussion on issue #75885, a type parameter on the RHS of an alias declaration must not be declared in the same declaration (but it may be declared by an enclosing function). This relaxes the spec slightly and allows for (pre-existing) test cases. Add a corresponding check to the type checker (there was no check for type parameters on the RHS of alias declarations at all, before). Fixes #75884. Fixes #75885. Change-Id: I1e5675978e6423d626c068829d4bf5e90035ea82 Reviewed-on: https://go-review.googlesource.com/c/go/+/721820 Auto-Submit: Robert Griesemer <gri@google.com> Reviewed-by: Robert Griesemer <gri@google.com> Reviewed-by: Mark Freeman <markfreeman@google.com> Reviewed-by: Robert Findley <rfindley@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src')
-rw-r--r--src/cmd/compile/internal/types2/decl.go9
-rw-r--r--src/go/types/decl.go9
-rw-r--r--src/internal/types/testdata/fixedbugs/issue75885.go15
3 files changed, 29 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go
index 60371651ab..5cb52fdbe4 100644
--- a/src/cmd/compile/internal/types2/decl.go
+++ b/src/cmd/compile/internal/types2/decl.go
@@ -497,9 +497,14 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *TypeN
rhs = check.declaredType(tdecl.Type, obj)
assert(rhs != nil)
-
alias.fromRHS = rhs
- unalias(alias) // populate alias.actual
+
+ // spec: In an alias declaration the given type cannot be a type parameter declared in the same declaration."
+ // (see also go.dev/issue/75884, go.dev/issue/#75885)
+ if tpar, ok := rhs.(*TypeParam); ok && alias.tparams != nil && slices.Index(alias.tparams.list(), tpar) >= 0 {
+ check.error(tdecl.Type, MisplacedTypeParam, "cannot use type parameter declared in alias declaration as RHS")
+ alias.fromRHS = Typ[Invalid]
+ }
} else {
if !versionErr && tparam0 != nil {
check.error(tdecl, UnsupportedFeature, "generic type alias requires GODEBUG=gotypesalias=1 or unset")
diff --git a/src/go/types/decl.go b/src/go/types/decl.go
index 4b374fb66d..fffcb590e6 100644
--- a/src/go/types/decl.go
+++ b/src/go/types/decl.go
@@ -572,9 +572,14 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *ast.TypeSpec, def *TypeName
rhs = check.declaredType(tdecl.Type, obj)
assert(rhs != nil)
-
alias.fromRHS = rhs
- unalias(alias) // populate alias.actual
+
+ // spec: In an alias declaration the given type cannot be a type parameter declared in the same declaration."
+ // (see also go.dev/issue/75884, go.dev/issue/#75885)
+ if tpar, ok := rhs.(*TypeParam); ok && alias.tparams != nil && slices.Index(alias.tparams.list(), tpar) >= 0 {
+ check.error(tdecl.Type, MisplacedTypeParam, "cannot use type parameter declared in alias declaration as RHS")
+ alias.fromRHS = Typ[Invalid]
+ }
} else {
// With Go1.23, the default behavior is to use Alias nodes,
// reflected by check.enableAlias. Signal non-default behavior.
diff --git a/src/internal/types/testdata/fixedbugs/issue75885.go b/src/internal/types/testdata/fixedbugs/issue75885.go
new file mode 100644
index 0000000000..f0cf4a65ed
--- /dev/null
+++ b/src/internal/types/testdata/fixedbugs/issue75885.go
@@ -0,0 +1,15 @@
+// -gotypesalias=1
+
+// Copyright 2025 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[P any] = P // ERROR "cannot use type parameter declared in alias declaration as RHS"
+
+func _[P any]() {
+ type A[P any] = P // ERROR "cannot use type parameter declared in alias declaration as RHS"
+ type B = P
+ type C[Q any] = P
+}