mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] cmd/compile/internal/types2: remove code for implicit type arguments
The design draft doesn't support this anymore.
Also: Fixed a potential bug in the receiver unpack code
(found by rfindley@).
Change-Id: Ic52eedc686adcb4d5a98884ad0134679c3685c13
Reviewed-on: https://go-review.googlesource.com/c/go/+/278853
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
068dd0470b
commit
c4f0da5750
2 changed files with 4 additions and 39 deletions
|
|
@ -686,33 +686,24 @@ func (check *Checker) collectTypeParams(list []*syntax.Field) (tparams []*TypeNa
|
||||||
var bound Type
|
var bound Type
|
||||||
for i, j := 0, 0; i < len(list); i = j {
|
for i, j := 0, 0; i < len(list); i = j {
|
||||||
f := list[i]
|
f := list[i]
|
||||||
ftype := f.Type
|
|
||||||
|
|
||||||
// determine the range of type parameters list[i:j] with identical type bound
|
// determine the range of type parameters list[i:j] with identical type bound
|
||||||
// (declared as in (type a, b, c B))
|
// (declared as in (type a, b, c B))
|
||||||
j = i + 1
|
j = i + 1
|
||||||
for j < len(list) && list[j].Type == ftype {
|
for j < len(list) && list[j].Type == f.Type {
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
|
|
||||||
// this should never be the case, but be careful
|
// this should never be the case, but be careful
|
||||||
if ftype == nil {
|
if f.Type == nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the type bound expects exactly one type argument, permit leaving
|
|
||||||
// it away and use the corresponding type parameter as implicit argument.
|
|
||||||
// This allows us to write (type p b(p), q b(q), r b(r)) as (type p, q, r b).
|
|
||||||
// Enabled if enableImplicitTParam is set.
|
|
||||||
const enableImplicitTParam = false
|
|
||||||
|
|
||||||
// The predeclared identifier "any" is visible only as a constraint
|
// The predeclared identifier "any" is visible only as a constraint
|
||||||
// in a type parameter list. Look for it before general constraint
|
// in a type parameter list. Look for it before general constraint
|
||||||
// resolution.
|
// resolution.
|
||||||
if tident, _ := f.Type.(*syntax.Name); tident != nil && tident.Value == "any" && check.lookup("any") == nil {
|
if tident, _ := f.Type.(*syntax.Name); tident != nil && tident.Value == "any" && check.lookup("any") == nil {
|
||||||
bound = universeAny
|
bound = universeAny
|
||||||
} else if enableImplicitTParam {
|
|
||||||
bound = check.anyType(f.Type)
|
|
||||||
} else {
|
} else {
|
||||||
bound = check.typ(f.Type)
|
bound = check.typ(f.Type)
|
||||||
}
|
}
|
||||||
|
|
@ -723,34 +714,6 @@ func (check *Checker) collectTypeParams(list []*syntax.Field) (tparams []*TypeNa
|
||||||
// type C(type T C) interface {}
|
// type C(type T C) interface {}
|
||||||
// (issue #39724).
|
// (issue #39724).
|
||||||
if _, ok := bound.Under().(*Interface); ok {
|
if _, ok := bound.Under().(*Interface); ok {
|
||||||
if enableImplicitTParam && isGeneric(bound) {
|
|
||||||
base := bound.(*Named) // only a *Named type can be generic
|
|
||||||
if j-i != 1 || len(base.tparams) != 1 {
|
|
||||||
// TODO(gri) make this error message better
|
|
||||||
check.errorf(ftype, "cannot use generic type %s without instantiation (more than one type parameter)", bound)
|
|
||||||
bound = Typ[Invalid]
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// We have exactly one type parameter.
|
|
||||||
// "Manually" instantiate the bound with each type
|
|
||||||
// parameter the bound applies to.
|
|
||||||
// TODO(gri) this code (in more general form) is also in
|
|
||||||
// checker.typInternal for the *ast.CallExpr case. Factor?
|
|
||||||
typ := new(instance)
|
|
||||||
typ.check = check
|
|
||||||
typ.pos = ftype.Pos()
|
|
||||||
typ.base = base
|
|
||||||
typ.targs = []Type{tparams[i].typ}
|
|
||||||
typ.poslist = []syntax.Pos{f.Name.Pos()}
|
|
||||||
// Make sure we check instantiation works at least once
|
|
||||||
// and that the resulting type is valid.
|
|
||||||
check.atEnd(func() {
|
|
||||||
check.validType(typ.expand(), nil)
|
|
||||||
})
|
|
||||||
// update bound and recorded type
|
|
||||||
bound = typ
|
|
||||||
check.recordTypeAndValue(ftype, typexpr, typ, nil)
|
|
||||||
}
|
|
||||||
// set the type bounds
|
// set the type bounds
|
||||||
for i < j {
|
for i < j {
|
||||||
tparams[i].typ.(*TypeParam).bound = bound
|
tparams[i].typ.(*TypeParam).bound = bound
|
||||||
|
|
|
||||||
|
|
@ -495,11 +495,13 @@ L: // unpack receiver type
|
||||||
case *syntax.ParenExpr:
|
case *syntax.ParenExpr:
|
||||||
rtyp = t.X
|
rtyp = t.X
|
||||||
// case *ast.StarExpr:
|
// case *ast.StarExpr:
|
||||||
|
// ptr = true
|
||||||
// rtyp = t.X
|
// rtyp = t.X
|
||||||
case *syntax.Operation:
|
case *syntax.Operation:
|
||||||
if t.Op != syntax.Mul || t.Y != nil {
|
if t.Op != syntax.Mul || t.Y != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
ptr = true
|
||||||
rtyp = t.X
|
rtyp = t.X
|
||||||
default:
|
default:
|
||||||
break L
|
break L
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue