diff options
author | Robert Griesemer <gri@golang.org> | 2021-06-29 12:22:21 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2021-07-01 16:42:34 +0000 |
commit | 9cb1b0f50b5852b24e1a7b66f09faa1a521ae108 (patch) | |
tree | 72da1e367ffd794a681c2b5e08ed80cc28000fd3 /src/cmd/compile/internal/types2/decl.go | |
parent | 1cd505c353e4656ba28fd8de8708e8c8e6c21cbf (diff) | |
download | go-9cb1b0f50b5852b24e1a7b66f09faa1a521ae108.tar.gz go-9cb1b0f50b5852b24e1a7b66f09faa1a521ae108.zip |
[dev.typeparams] cmd/compile/internal/types2: delay interface check for type bounds
While at it, clean up code for collecting/declaring type parameters.
For #40789.
Change-Id: I0855137d5ee85c0ae2fa60d33b28c24a33132fbc
Reviewed-on: https://go-review.googlesource.com/c/go/+/331690
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
Diffstat (limited to 'src/cmd/compile/internal/types2/decl.go')
-rw-r--r-- | src/cmd/compile/internal/types2/decl.go | 85 |
1 files changed, 31 insertions, 54 deletions
diff --git a/src/cmd/compile/internal/types2/decl.go b/src/cmd/compile/internal/types2/decl.go index 00b4ef7010..d36da06f42 100644 --- a/src/cmd/compile/internal/types2/decl.go +++ b/src/cmd/compile/internal/types2/decl.go @@ -674,75 +674,52 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named } -func (check *Checker) collectTypeParams(list []*syntax.Field) (tparams []*TypeName) { - // Type parameter lists should not be empty. The parser will - // complain but we still may get an incorrect AST: ignore it. - if len(list) == 0 { - return - } +func (check *Checker) collectTypeParams(list []*syntax.Field) []*TypeName { + tparams := make([]*TypeName, len(list)) - // Declare type parameters up-front, with empty interface as type bound. + // Declare type parameters up-front. // The scope of type parameters starts at the beginning of the type parameter - // list (so we can have mutually recursive parameterized interfaces). - for _, f := range list { - tparams = check.declareTypeParam(tparams, f.Name) + // list (so we can have mutually recursive parameterized type bounds). + for i, f := range list { + tparams[i] = check.declareTypeParam(i, f.Name) } var bound Type - for i, j := 0, 0; i < len(list); i = j { - f := list[i] - - // determine the range of type parameters list[i:j] with identical type bound - // (declared as in (type a, b, c B)) - j = i + 1 - for j < len(list) && list[j].Type == f.Type { - j++ - } - - // this should never be the case, but be careful - if f.Type == nil { - continue - } - - // The predeclared identifier "any" is visible only as a constraint - // in a type parameter list. Look for it before general constraint - // resolution. - if tident, _ := unparen(f.Type).(*syntax.Name); tident != nil && tident.Value == "any" && check.lookup("any") == nil { - bound = universeAny - } else { - bound = check.typ(f.Type) - } - - // type bound must be an interface - // TODO(gri) We should delay the interface check because - // we may not have a complete interface yet: - // type C(type T C) interface {} - // (issue #39724). - if _, ok := under(bound).(*Interface); ok { - // set the type bounds - for i < j { - tparams[i].typ.(*TypeParam).bound = bound - i++ - } - } else if bound != Typ[Invalid] { - check.errorf(f.Type, "%s is not an interface", bound) + for i, f := range list { + // Optimization: Re-use the previous type bound if it hasn't changed. + // This also preserves the grouped output of type parameter lists + // when printing type strings. + if i == 0 || f.Type != list[i-1].Type { + bound = check.boundType(f.Type) } + tparams[i].typ.(*TypeParam).bound = bound } - return + return tparams } -func (check *Checker) declareTypeParam(tparams []*TypeName, name *syntax.Name) []*TypeName { +func (check *Checker) declareTypeParam(index int, name *syntax.Name) *TypeName { tpar := NewTypeName(name.Pos(), check.pkg, name.Value, nil) - check.NewTypeParam(tpar, len(tparams), &emptyInterface) // assigns type to tpar as a side-effect + check.NewTypeParam(tpar, index, nil) // assigns type to tpar as a side-effect check.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position - tparams = append(tparams, tpar) + return tpar +} - if check.conf.Trace { - check.trace(name.Pos(), "type param = %v", tparams[len(tparams)-1]) +// boundType type-checks the type expression e and returns its type, or Typ[Invalid]. +// The type must be an interface, including the predeclared type "any". +func (check *Checker) boundType(e syntax.Expr) Type { + // The predeclared identifier "any" is visible only as a type bound in a type parameter list. + if name, _ := unparen(e).(*syntax.Name); name != nil && name.Value == "any" && check.lookup("any") == nil { + return universeAny } - return tparams + bound := check.typ(e) + check.later(func() { + if _, ok := under(bound).(*Interface); !ok && bound != Typ[Invalid] { + check.errorf(e, "%s is not an interface", bound) + } + }) + return bound } func (check *Checker) collectMethods(obj *TypeName) { |