mirror of
https://github.com/golang/go
synced 2024-11-24 11:20:07 -07:00
cmd/compile/internal/types2: use an identifier map rather than isubst for recv type params
This is a port of CL 354643 from go/types to types2 with adjustments: - use of syntax rather than go/ast package as needed - adjustments due to the different code for type parameter declarations - rename of Checker.rparamMap to Checker.recvTParamMap, which seems clearer Change-Id: I5311a0c05a13c6b87ea1422b250b90c3d05c5dce Reviewed-on: https://go-review.googlesource.com/c/go/+/354693 Trust: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
parent
0d838ea5a2
commit
a7d3a0e971
@ -103,6 +103,7 @@ type Checker struct {
|
|||||||
files []*syntax.File // list of package files
|
files []*syntax.File // list of package files
|
||||||
imports []*PkgName // list of imported packages
|
imports []*PkgName // list of imported packages
|
||||||
dotImportMap map[dotImportKey]*PkgName // maps dot-imported objects to the package they were dot-imported through
|
dotImportMap map[dotImportKey]*PkgName // maps dot-imported objects to the package they were dot-imported through
|
||||||
|
recvTParamMap map[*syntax.Name]*TypeParam // maps blank receiver type parameters to their type
|
||||||
|
|
||||||
firstErr error // first error encountered
|
firstErr error // first error encountered
|
||||||
methods map[*TypeName][]*Func // maps package scope type names to associated non-blank (non-interface) methods
|
methods map[*TypeName][]*Func // maps package scope type names to associated non-blank (non-interface) methods
|
||||||
@ -292,6 +293,7 @@ func (check *Checker) checkFiles(files []*syntax.File) (err error) {
|
|||||||
check.dotImportMap = nil
|
check.dotImportMap = nil
|
||||||
check.pkgPathMap = nil
|
check.pkgPathMap = nil
|
||||||
check.seenPkgMap = nil
|
check.seenPkgMap = nil
|
||||||
|
check.recvTParamMap = nil
|
||||||
|
|
||||||
// TODO(gri) There's more memory we should release at this point.
|
// TODO(gri) There's more memory we should release at this point.
|
||||||
|
|
||||||
|
@ -4,10 +4,7 @@
|
|||||||
|
|
||||||
package types2
|
package types2
|
||||||
|
|
||||||
import (
|
import "cmd/compile/internal/syntax"
|
||||||
"cmd/compile/internal/syntax"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
// API
|
// API
|
||||||
@ -105,39 +102,33 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
|
|||||||
sig.scope = check.scope
|
sig.scope = check.scope
|
||||||
defer check.closeScope()
|
defer check.closeScope()
|
||||||
|
|
||||||
var recvTyp syntax.Expr // rewritten receiver type; valid if != nil
|
|
||||||
if recvPar != nil {
|
if recvPar != nil {
|
||||||
// collect generic receiver type parameters, if any
|
// collect generic receiver type parameters, if any
|
||||||
// - a receiver type parameter is like any other type parameter, except that it is declared implicitly
|
// - a receiver type parameter is like any other type parameter, except that it is declared implicitly
|
||||||
// - the receiver specification acts as local declaration for its type parameters, which may be blank
|
// - the receiver specification acts as local declaration for its type parameters, which may be blank
|
||||||
_, rname, rparams := check.unpackRecv(recvPar.Type, true)
|
_, rname, rparams := check.unpackRecv(recvPar.Type, true)
|
||||||
if len(rparams) > 0 {
|
if len(rparams) > 0 {
|
||||||
// Blank identifiers don't get declared and regular type-checking of the instantiated
|
tparams := make([]*TypeParam, len(rparams))
|
||||||
// parameterized receiver type expression fails in Checker.collectParams of receiver.
|
for i, rparam := range rparams {
|
||||||
// Identify blank type parameters and substitute each with a unique new identifier named
|
tparams[i] = check.declareTypeParam(rparam)
|
||||||
// "n_" (where n is the parameter index) and which cannot conflict with any user-defined
|
}
|
||||||
// name.
|
sig.rparams = bindTParams(tparams)
|
||||||
var smap map[*syntax.Name]*syntax.Name // substitution map from "_" to "!n" identifiers
|
// Blank identifiers don't get declared, so naive type-checking of the
|
||||||
|
// receiver type expression would fail in Checker.collectParams below,
|
||||||
|
// when Checker.ident cannot resolve the _ to a type.
|
||||||
|
//
|
||||||
|
// Checker.recvTParamMap maps these blank identifiers to their type parameter
|
||||||
|
// types, so that they may be resolved in Checker.ident when they fail
|
||||||
|
// lookup in the scope.
|
||||||
for i, p := range rparams {
|
for i, p := range rparams {
|
||||||
if p.Value == "_" {
|
if p.Value == "_" {
|
||||||
new := *p
|
tpar := sig.rparams.At(i)
|
||||||
new.Value = fmt.Sprintf("%d_", i)
|
if check.recvTParamMap == nil {
|
||||||
rparams[i] = &new // use n_ identifier instead of _ so it can be looked up
|
check.recvTParamMap = make(map[*syntax.Name]*TypeParam)
|
||||||
if smap == nil {
|
|
||||||
smap = make(map[*syntax.Name]*syntax.Name)
|
|
||||||
}
|
}
|
||||||
smap[p] = &new
|
check.recvTParamMap[p] = tpar
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if smap != nil {
|
|
||||||
// blank identifiers were found => use rewritten receiver type
|
|
||||||
recvTyp = isubst(recvPar.Type, smap)
|
|
||||||
}
|
|
||||||
rlist := make([]*TypeParam, len(rparams))
|
|
||||||
for i, rparam := range rparams {
|
|
||||||
rlist[i] = check.declareTypeParam(rparam)
|
|
||||||
}
|
|
||||||
sig.rparams = bindTParams(rlist)
|
|
||||||
// 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
|
||||||
var recvTParams []*TypeParam
|
var recvTParams []*TypeParam
|
||||||
@ -186,10 +177,10 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
|
|||||||
scope := NewScope(check.scope, nopos, nopos, "function body (temp. scope)")
|
scope := NewScope(check.scope, nopos, nopos, "function body (temp. scope)")
|
||||||
var recvList []*Var // TODO(gri) remove the need for making a list here
|
var recvList []*Var // TODO(gri) remove the need for making a list here
|
||||||
if recvPar != nil {
|
if recvPar != nil {
|
||||||
recvList, _ = check.collectParams(scope, []*syntax.Field{recvPar}, recvTyp, false) // use rewritten receiver type, if any
|
recvList, _ = check.collectParams(scope, []*syntax.Field{recvPar}, false) // use rewritten receiver type, if any
|
||||||
}
|
}
|
||||||
params, variadic := check.collectParams(scope, ftyp.ParamList, nil, true)
|
params, variadic := check.collectParams(scope, ftyp.ParamList, true)
|
||||||
results, _ := check.collectParams(scope, ftyp.ResultList, nil, false)
|
results, _ := check.collectParams(scope, ftyp.ResultList, false)
|
||||||
scope.Squash(func(obj, alt Object) {
|
scope.Squash(func(obj, alt Object) {
|
||||||
var err error_
|
var err error_
|
||||||
err.errorf(obj, "%s redeclared in this block", obj.Name())
|
err.errorf(obj, "%s redeclared in this block", obj.Name())
|
||||||
@ -281,8 +272,8 @@ func (check *Checker) funcType(sig *Signature, recvPar *syntax.Field, tparams []
|
|||||||
}
|
}
|
||||||
|
|
||||||
// collectParams declares the parameters of list in scope and returns the corresponding
|
// collectParams declares the parameters of list in scope and returns the corresponding
|
||||||
// variable list. If type0 != nil, it is used instead of the first type in list.
|
// variable list.
|
||||||
func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, type0 syntax.Expr, variadicOk bool) (params []*Var, variadic bool) {
|
func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, variadicOk bool) (params []*Var, variadic bool) {
|
||||||
if list == nil {
|
if list == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -296,9 +287,6 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, type0 sy
|
|||||||
// type-check type of grouped fields only once
|
// type-check type of grouped fields only once
|
||||||
if ftype != prev {
|
if ftype != prev {
|
||||||
prev = ftype
|
prev = ftype
|
||||||
if i == 0 && type0 != nil {
|
|
||||||
ftype = type0
|
|
||||||
}
|
|
||||||
if t, _ := ftype.(*syntax.DotsType); t != nil {
|
if t, _ := ftype.(*syntax.DotsType); t != nil {
|
||||||
ftype = t.Elem
|
ftype = t.Elem
|
||||||
if variadicOk && i == len(list)-1 {
|
if variadicOk && i == len(list)-1 {
|
||||||
@ -348,61 +336,3 @@ func (check *Checker) collectParams(scope *Scope, list []*syntax.Field, type0 sy
|
|||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// isubst returns an x with identifiers substituted per the substitution map smap.
|
|
||||||
// isubst only handles the case of (valid) method receiver type expressions correctly.
|
|
||||||
func isubst(x syntax.Expr, smap map[*syntax.Name]*syntax.Name) syntax.Expr {
|
|
||||||
switch n := x.(type) {
|
|
||||||
case *syntax.Name:
|
|
||||||
if alt := smap[n]; alt != nil {
|
|
||||||
return alt
|
|
||||||
}
|
|
||||||
// case *syntax.StarExpr:
|
|
||||||
// X := isubst(n.X, smap)
|
|
||||||
// if X != n.X {
|
|
||||||
// new := *n
|
|
||||||
// new.X = X
|
|
||||||
// return &new
|
|
||||||
// }
|
|
||||||
case *syntax.Operation:
|
|
||||||
if n.Op == syntax.Mul && n.Y == nil {
|
|
||||||
X := isubst(n.X, smap)
|
|
||||||
if X != n.X {
|
|
||||||
new := *n
|
|
||||||
new.X = X
|
|
||||||
return &new
|
|
||||||
}
|
|
||||||
}
|
|
||||||
case *syntax.IndexExpr:
|
|
||||||
Index := isubst(n.Index, smap)
|
|
||||||
if Index != n.Index {
|
|
||||||
new := *n
|
|
||||||
new.Index = Index
|
|
||||||
return &new
|
|
||||||
}
|
|
||||||
case *syntax.ListExpr:
|
|
||||||
var elems []syntax.Expr
|
|
||||||
for i, elem := range n.ElemList {
|
|
||||||
new := isubst(elem, smap)
|
|
||||||
if new != elem {
|
|
||||||
if elems == nil {
|
|
||||||
elems = make([]syntax.Expr, len(n.ElemList))
|
|
||||||
copy(elems, n.ElemList)
|
|
||||||
}
|
|
||||||
elems[i] = new
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if elems != nil {
|
|
||||||
new := *n
|
|
||||||
new.ElemList = elems
|
|
||||||
return &new
|
|
||||||
}
|
|
||||||
case *syntax.ParenExpr:
|
|
||||||
return isubst(n.X, smap) // no need to keep parentheses
|
|
||||||
default:
|
|
||||||
// Other receiver type expressions are invalid.
|
|
||||||
// It's fine to ignore those here as they will
|
|
||||||
// be checked elsewhere.
|
|
||||||
}
|
|
||||||
return x
|
|
||||||
}
|
|
||||||
|
@ -28,7 +28,15 @@ func (check *Checker) ident(x *operand, e *syntax.Name, def *Named, wantType boo
|
|||||||
switch obj {
|
switch obj {
|
||||||
case nil:
|
case nil:
|
||||||
if e.Value == "_" {
|
if e.Value == "_" {
|
||||||
|
// Blank identifiers are never declared, but the current identifier may
|
||||||
|
// be a placeholder for a receiver type parameter. In this case we can
|
||||||
|
// resolve its type and object from Checker.recvTParamMap.
|
||||||
|
if tpar := check.recvTParamMap[e]; tpar != nil {
|
||||||
|
x.mode = typexpr
|
||||||
|
x.typ = tpar
|
||||||
|
} else {
|
||||||
check.error(e, "cannot use _ as value or type")
|
check.error(e, "cannot use _ as value or type")
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if check.conf.CompilerErrorMessages {
|
if check.conf.CompilerErrorMessages {
|
||||||
check.errorf(e, "undefined: %s", e.Value)
|
check.errorf(e, "undefined: %s", e.Value)
|
||||||
|
Loading…
Reference in New Issue
Block a user