mirror of
https://github.com/golang/go
synced 2024-11-26 17:07:09 -07:00
[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>
This commit is contained in:
parent
1cd505c353
commit
9cb1b0f50b
@ -674,77 +674,54 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (check *Checker) collectTypeParams(list []*syntax.Field) (tparams []*TypeName) {
|
func (check *Checker) collectTypeParams(list []*syntax.Field) []*TypeName {
|
||||||
// Type parameter lists should not be empty. The parser will
|
tparams := make([]*TypeName, len(list))
|
||||||
// complain but we still may get an incorrect AST: ignore it.
|
|
||||||
if len(list) == 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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
|
// The scope of type parameters starts at the beginning of the type parameter
|
||||||
// list (so we can have mutually recursive parameterized interfaces).
|
// list (so we can have mutually recursive parameterized type bounds).
|
||||||
for _, f := range list {
|
for i, f := range list {
|
||||||
tparams = check.declareTypeParam(tparams, f.Name)
|
tparams[i] = check.declareTypeParam(i, f.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
var bound Type
|
var bound Type
|
||||||
for i, j := 0, 0; i < len(list); i = j {
|
for i, f := range list {
|
||||||
f := list[i]
|
// Optimization: Re-use the previous type bound if it hasn't changed.
|
||||||
|
// This also preserves the grouped output of type parameter lists
|
||||||
// determine the range of type parameters list[i:j] with identical type bound
|
// when printing type strings.
|
||||||
// (declared as in (type a, b, c B))
|
if i == 0 || f.Type != list[i-1].Type {
|
||||||
j = i + 1
|
bound = check.boundType(f.Type)
|
||||||
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
|
tparams[i].typ.(*TypeParam).bound = bound
|
||||||
i++
|
|
||||||
}
|
|
||||||
} else if bound != Typ[Invalid] {
|
|
||||||
check.errorf(f.Type, "%s is not an interface", bound)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (check *Checker) declareTypeParam(tparams []*TypeName, 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.declare(check.scope, name, tpar, check.scope.pos) // TODO(gri) check scope position
|
|
||||||
tparams = append(tparams, tpar)
|
|
||||||
|
|
||||||
if check.conf.Trace {
|
|
||||||
check.trace(name.Pos(), "type param = %v", tparams[len(tparams)-1])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return tparams
|
return tparams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (check *Checker) declareTypeParam(index int, name *syntax.Name) *TypeName {
|
||||||
|
tpar := NewTypeName(name.Pos(), check.pkg, name.Value, nil)
|
||||||
|
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
|
||||||
|
return tpar
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
func (check *Checker) collectMethods(obj *TypeName) {
|
||||||
// get associated methods
|
// get associated methods
|
||||||
// (Checker.collectObjects only collects methods with non-blank names;
|
// (Checker.collectObjects only collects methods with non-blank names;
|
||||||
|
@ -48,10 +48,9 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
|
|||||||
// blank identifiers were found => use rewritten receiver type
|
// blank identifiers were found => use rewritten receiver type
|
||||||
recvTyp = isubst(recvPar.Type, smap)
|
recvTyp = isubst(recvPar.Type, smap)
|
||||||
}
|
}
|
||||||
// TODO(gri) rework declareTypeParams
|
sig.rparams = make([]*TypeName, len(rparams))
|
||||||
sig.rparams = nil
|
for i, rparam := range rparams {
|
||||||
for _, rparam := range rparams {
|
sig.rparams[i] = check.declareTypeParam(i, rparam)
|
||||||
sig.rparams = check.declareTypeParam(sig.rparams, rparam)
|
|
||||||
}
|
}
|
||||||
// determine receiver type to get its type parameters
|
// determine receiver type to get its type parameters
|
||||||
// and the respective type parameter bounds
|
// and the respective type parameter bounds
|
||||||
|
37
src/cmd/compile/internal/types2/testdata/fixedbugs/issue40789.go2
vendored
Normal file
37
src/cmd/compile/internal/types2/testdata/fixedbugs/issue40789.go2
vendored
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
// Copyright 2021 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 main
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
m := map[string]int{
|
||||||
|
"a": 6,
|
||||||
|
"b": 7,
|
||||||
|
}
|
||||||
|
fmt.Println(copyMap[map[string]int, string, int](m))
|
||||||
|
}
|
||||||
|
|
||||||
|
type Map[K comparable, V any] interface {
|
||||||
|
map[K] V
|
||||||
|
}
|
||||||
|
|
||||||
|
func copyMap[M Map[K, V], K comparable, V any](m M) M {
|
||||||
|
m1 := make(M)
|
||||||
|
for k, v := range m {
|
||||||
|
m1[k] = v
|
||||||
|
}
|
||||||
|
return m1
|
||||||
|
}
|
||||||
|
|
||||||
|
// simpler test case from the same issue
|
||||||
|
|
||||||
|
type A[X comparable] interface {
|
||||||
|
[]X
|
||||||
|
}
|
||||||
|
|
||||||
|
func f[B A[X], X comparable]() B {
|
||||||
|
return nil
|
||||||
|
}
|
@ -626,7 +626,11 @@ func (t *TypeParam) SetId(id uint64) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TypeParam) Bound() *Interface {
|
func (t *TypeParam) Bound() *Interface {
|
||||||
iface := asInterface(t.bound)
|
// we may not have an interface (error reported elsewhere)
|
||||||
|
iface, _ := under(t.bound).(*Interface)
|
||||||
|
if iface == nil {
|
||||||
|
return &emptyInterface
|
||||||
|
}
|
||||||
// use the type bound position if we have one
|
// use the type bound position if we have one
|
||||||
pos := nopos
|
pos := nopos
|
||||||
if n, _ := t.bound.(*Named); n != nil {
|
if n, _ := t.bound.(*Named); n != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user