mirror of
https://github.com/golang/go
synced 2024-11-26 07:38:00 -07:00
cmd/compile/internal/types2: disallow aliases for generic types
The existing approach (alias name stands for generic type name) is an exception: it's the only place where a generic type could be used without explicit instantiation. The correct solution is currently under discussion (see proposal issue #46477). This CL requires that the RHS of an alias type declaration be an instantiated non-generic type. If #46477 is accepted, the implementation will require proper representation of alias types. Change-Id: Ie85b923213a64f39837e56e38e14757458272b93 Reviewed-on: https://go-review.googlesource.com/c/go/+/346294 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
605d1aaea2
commit
68152359fd
@ -555,7 +555,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
|
|||||||
}
|
}
|
||||||
|
|
||||||
obj.typ = Typ[Invalid]
|
obj.typ = Typ[Invalid]
|
||||||
rhs = check.anyType(tdecl.Type)
|
rhs = check.varType(tdecl.Type)
|
||||||
obj.typ = rhs
|
obj.typ = rhs
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -17,13 +17,15 @@ type T2[P any] struct {
|
|||||||
|
|
||||||
type List[P any] []P
|
type List[P any] []P
|
||||||
|
|
||||||
// Alias type declarations cannot have type parameters. Syntax error.
|
// Alias type declarations cannot have type parameters.
|
||||||
|
// Issue #46477 proposses to change that.
|
||||||
type A1[P any] = /* ERROR cannot be alias */ P
|
type A1[P any] = /* ERROR cannot be alias */ P
|
||||||
|
|
||||||
// But an alias may refer to a generic, uninstantiated type.
|
// Pending clarification of #46477 we disallow aliases
|
||||||
type A2 = List
|
// of generic types.
|
||||||
|
type A2 = List // ERROR cannot use generic type
|
||||||
var _ A2[int]
|
var _ A2[int]
|
||||||
var _ A2 /* ERROR without instantiation */
|
var _ A2
|
||||||
|
|
||||||
type A3 = List[int]
|
type A3 = List[int]
|
||||||
var _ A3
|
var _ A3
|
||||||
|
@ -5,9 +5,9 @@
|
|||||||
package p
|
package p
|
||||||
|
|
||||||
type T[P any] P
|
type T[P any] P
|
||||||
type A = T
|
type A = T // ERROR cannot use generic type
|
||||||
var x A[int]
|
var x A[int]
|
||||||
var _ A /* ERROR cannot use generic type */
|
var _ A
|
||||||
|
|
||||||
type B = T[int]
|
type B = T[int]
|
||||||
var y B = x
|
var y B = x
|
||||||
@ -16,5 +16,5 @@ var _ B /* ERROR not a generic type */ [int]
|
|||||||
// test case from issue
|
// test case from issue
|
||||||
|
|
||||||
type Vector[T any] []T
|
type Vector[T any] []T
|
||||||
type VectorAlias = Vector
|
type VectorAlias = Vector // ERROR cannot use generic type
|
||||||
var v Vector[int]
|
var v Vector[int]
|
||||||
|
@ -8,7 +8,7 @@ type T[P any] struct{}
|
|||||||
|
|
||||||
func (T[P]) m1()
|
func (T[P]) m1()
|
||||||
|
|
||||||
type A1 = T
|
type A1 = T // ERROR cannot use generic type
|
||||||
|
|
||||||
func (A1[P]) m2() {}
|
func (A1[P]) m2() {}
|
||||||
|
|
||||||
|
@ -38,12 +38,15 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
case universeAny, universeComparable:
|
case universeAny, universeComparable:
|
||||||
// complain if necessary but keep going
|
// complain if necessary
|
||||||
if !check.allowVersion(check.pkg, 1, 18) {
|
if !check.allowVersion(check.pkg, 1, 18) {
|
||||||
check.softErrorf(e, "undeclared name: %s (requires version go1.18 or later)", e.Value)
|
check.errorf(e, "undeclared name: %s (requires version go1.18 or later)", e.Value)
|
||||||
} else if obj == universeAny {
|
return // avoid follow-on errors
|
||||||
|
}
|
||||||
|
if obj == universeAny {
|
||||||
// If we allow "any" for general use, this if-statement can be removed (issue #33232).
|
// If we allow "any" for general use, this if-statement can be removed (issue #33232).
|
||||||
check.softErrorf(e, "cannot use any outside constraint position")
|
check.softErrorf(e, "cannot use any outside constraint position")
|
||||||
|
// ok to continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
check.recordUse(e, obj)
|
check.recordUse(e, obj)
|
||||||
@ -159,15 +162,6 @@ func (check *Checker) varType(e syntax.Expr) Type {
|
|||||||
return typ
|
return typ
|
||||||
}
|
}
|
||||||
|
|
||||||
// anyType type-checks the type expression e and returns its type, or Typ[Invalid].
|
|
||||||
// The type may be generic or instantiated.
|
|
||||||
func (check *Checker) anyType(e syntax.Expr) Type {
|
|
||||||
typ := check.typInternal(e, nil)
|
|
||||||
assert(isTyped(typ))
|
|
||||||
check.recordTypeAndValue(e, typexpr, typ, nil)
|
|
||||||
return typ
|
|
||||||
}
|
|
||||||
|
|
||||||
// definedType is like typ but also accepts a type name def.
|
// definedType is like typ but also accepts a type name def.
|
||||||
// If def != nil, e is the type specification for the defined type def, declared
|
// If def != nil, e is the type specification for the defined type def, declared
|
||||||
// in a type declaration, and def.underlying will be set to the type of e before
|
// in a type declaration, and def.underlying will be set to the type of e before
|
||||||
|
@ -10,16 +10,18 @@ type R[T any] struct {
|
|||||||
F T
|
F T
|
||||||
}
|
}
|
||||||
|
|
||||||
type S = R
|
// type S = R // disallowed for now
|
||||||
|
|
||||||
type Sint = R[int]
|
type Sint = R[int]
|
||||||
|
|
||||||
type Simp = a.Rimp
|
// type Simp = a.Rimp // disallowed for now
|
||||||
|
|
||||||
type SimpString Simp[string]
|
// type SimpString Simp[string] // disallowed for now
|
||||||
|
type SimpString a.Rimp[string]
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var s S[int]
|
// var s S[int] // disallowed for now
|
||||||
|
var s R[int]
|
||||||
if s.F != 0 {
|
if s.F != 0 {
|
||||||
panic(s.F)
|
panic(s.F)
|
||||||
}
|
}
|
||||||
@ -27,7 +29,8 @@ func main() {
|
|||||||
if s2.F != 0 {
|
if s2.F != 0 {
|
||||||
panic(s2.F)
|
panic(s2.F)
|
||||||
}
|
}
|
||||||
var s3 Simp[string]
|
// var s3 Simp[string] // disallowed for now
|
||||||
|
var s3 a.Rimp[string]
|
||||||
if s3.F != "" {
|
if s3.F != "" {
|
||||||
panic(s3.F)
|
panic(s3.F)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user