<!--{
"Title": "The Go Programming Language Specification",
- "Subtitle": "Language version go1.26 (Nov 12, 2025)",
+ "Subtitle": "Language version go1.26 (Nov 18, 2025)",
"Path": "/ref/spec"
}-->
</pre>
<p>
-In an alias declaration the given type cannot be a type parameter.
+In an alias declaration the given type cannot be a type parameter declared in the same declaration.
</p>
<pre>
-type A[P any] = P // illegal: P is a type parameter
+type A[P any] = P // illegal: P is a type parameter declared in the declaration of A
+
+func f[P any]() {
+ type A = P // ok: T is a type parameter declared by the enclosing function
+}
</pre>
<h4 id="Type_definitions">Type definitions</h4>
<pre>
type T[P any] P // illegal: P is a type parameter
-func f[T any]() {
- type L T // illegal: T is a type parameter declared by the enclosing function
+func f[P any]() {
+ type L P // illegal: P is a type parameter declared by the enclosing function
}
</pre>
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")
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.
--- /dev/null
+// -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
+}