mirror of
https://github.com/golang/go
synced 2024-11-23 00:40:08 -07:00
go/types: simplify Checker.funcInst
This is a port of CL 306169 to go/types, adjusted only for use of the typeparams package, and the different positioning API. Change-Id: I3095f4b0dae4473e75ec2a988ea282bac1a4bab3 Reviewed-on: https://go-review.googlesource.com/c/go/+/312189 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
parent
39785912b9
commit
614a9c2613
@ -17,42 +17,31 @@ import (
|
|||||||
// funcInst type-checks a function instantiaton inst and returns the result in x.
|
// funcInst type-checks a function instantiaton inst and returns the result in x.
|
||||||
// The operand x must be the evaluation of inst.X and its type must be a signature.
|
// The operand x must be the evaluation of inst.X and its type must be a signature.
|
||||||
func (check *Checker) funcInst(x *operand, inst *ast.IndexExpr) {
|
func (check *Checker) funcInst(x *operand, inst *ast.IndexExpr) {
|
||||||
exprs := typeparams.UnpackExpr(inst.Index)
|
xlist := typeparams.UnpackExpr(inst.Index)
|
||||||
args, ok := check.exprOrTypeList(exprs)
|
targs := check.typeList(xlist)
|
||||||
if !ok {
|
if targs == nil {
|
||||||
x.mode = invalid
|
x.mode = invalid
|
||||||
x.expr = inst
|
x.expr = inst
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if len(args) > 0 && args[0].mode != typexpr {
|
assert(len(targs) == len(xlist))
|
||||||
check.errorf(args[0], _NotAType, "%s is not a type", args[0])
|
|
||||||
ok = false
|
|
||||||
}
|
|
||||||
|
|
||||||
// check number of type arguments
|
// check number of type arguments
|
||||||
n := len(args)
|
n := len(targs)
|
||||||
sig := x.typ.(*Signature)
|
sig := x.typ.(*Signature)
|
||||||
if n > len(sig.tparams) {
|
if n > len(sig.tparams) {
|
||||||
check.errorf(args[n-1], _Todo, "got %d type arguments but want %d", n, len(sig.tparams))
|
check.errorf(xlist[n-1], _Todo, "got %d type arguments but want %d", n, len(sig.tparams))
|
||||||
x.mode = invalid
|
x.mode = invalid
|
||||||
x.expr = inst
|
x.expr = inst
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect types
|
// determine argument positions (for error reporting)
|
||||||
targs := make([]Type, n)
|
|
||||||
// TODO(rFindley) use a positioner here? instantiate would need to be
|
// TODO(rFindley) use a positioner here? instantiate would need to be
|
||||||
// updated accordingly.
|
// updated accordingly.
|
||||||
poslist := make([]token.Pos, n)
|
poslist := make([]token.Pos, n)
|
||||||
for i, a := range args {
|
for i, x := range xlist {
|
||||||
if a.mode != typexpr {
|
poslist[i] = x.Pos()
|
||||||
// error was reported earlier
|
|
||||||
x.mode = invalid
|
|
||||||
x.expr = inst
|
|
||||||
return
|
|
||||||
}
|
|
||||||
targs[i] = a.typ
|
|
||||||
poslist[i] = a.Pos()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we don't have enough type arguments, use constraint type inference
|
// if we don't have enough type arguments, use constraint type inference
|
||||||
@ -87,14 +76,6 @@ func (check *Checker) funcInst(x *operand, inst *ast.IndexExpr) {
|
|||||||
assert(n == len(sig.tparams))
|
assert(n == len(sig.tparams))
|
||||||
|
|
||||||
// instantiate function signature
|
// instantiate function signature
|
||||||
for i, typ := range targs {
|
|
||||||
// some positions may be missing if types are inferred
|
|
||||||
var pos token.Pos
|
|
||||||
if i < len(poslist) {
|
|
||||||
pos = poslist[i]
|
|
||||||
}
|
|
||||||
check.ordinaryType(atPos(pos), typ)
|
|
||||||
}
|
|
||||||
res := check.instantiate(x.Pos(), sig, targs, poslist).(*Signature)
|
res := check.instantiate(x.Pos(), sig, targs, poslist).(*Signature)
|
||||||
assert(res.tparams == nil) // signature is not generic anymore
|
assert(res.tparams == nil) // signature is not generic anymore
|
||||||
if inferred {
|
if inferred {
|
||||||
|
Loading…
Reference in New Issue
Block a user