// See how far we get with constraint type inference.
// Note that even if we don't have any type arguments, constraint type inference
// may produce results for constraints that explicitly specify a type.
- targs, index = check.inferB(pos, tparams, targs)
+ targs, index = check.inferB(tparams, targs)
if targs == nil || index < 0 {
return targs
}
// --- 4 ---
// Again, follow up with constraint type inference.
- targs, index = check.inferB(pos, tparams, targs)
+ targs, index = check.inferB(tparams, targs)
if targs == nil || index < 0 {
return targs
}
// first type argument in that list that couldn't be inferred (and thus is nil). If all
// type arguments were inferred successfully, index is < 0. The number of type arguments
// provided may be less than the number of type parameters, but there must be at least one.
-func (check *Checker) inferB(pos syntax.Pos, tparams []*TypeParam, targs []Type) (types []Type, index int) {
+func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type, index int) {
assert(len(tparams) >= len(targs) && len(targs) > 0)
if traceInference {
"context_test.go": nil,
"gccgosizes.go": nil,
"hilbert_test.go": nil,
+ "infer.go": func(f *ast.File) { fixTokenPos(f); fixInferSig(f) },
"instantiate_test.go": func(f *ast.File) { renameImportPath(f, `"cmd/compile/internal/types2"`, `"go/types"`) },
"lookup.go": nil,
"main_test.go": nil,
})
}
+// fixInferSig updates the Checker.infer signature to use a positioner instead of a token.Position
+// as first argument, renames the argument from "pos" to "posn", and updates a few internal uses of
+// "pos" to "posn" and "posn.Pos()" respectively.
+func fixInferSig(f *ast.File) {
+ ast.Inspect(f, func(n ast.Node) bool {
+ switch n := n.(type) {
+ case *ast.FuncDecl:
+ if n.Name.Name == "infer" {
+ // rewrite (pos token.Pos, ...) to (posn positioner, ...)
+ par := n.Type.Params.List[0]
+ if len(par.Names) == 1 && par.Names[0].Name == "pos" {
+ par.Names[0] = newIdent(par.Names[0].Pos(), "posn")
+ par.Type = newIdent(par.Type.Pos(), "positioner")
+ return true
+ }
+ }
+ case *ast.CallExpr:
+ if selx, _ := n.Fun.(*ast.SelectorExpr); selx != nil {
+ switch selx.Sel.Name {
+ case "renameTParams":
+ // rewrite check.renameTParams(pos, ... ) to check.renameTParams(posn.Pos(), ... )
+ if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
+ pos := n.Args[0].Pos()
+ fun := &ast.SelectorExpr{X: newIdent(pos, "posn"), Sel: newIdent(pos, "Pos")}
+ arg := &ast.CallExpr{Fun: fun, Lparen: pos, Args: nil, Ellipsis: token.NoPos, Rparen: pos}
+ n.Args[0] = arg
+ return false
+ }
+ case "errorf":
+ // rewrite check.errorf(pos, ...) to check.errorf(posn, ...)
+ if ident, _ := n.Args[0].(*ast.Ident); ident != nil && ident.Name == "pos" {
+ pos := n.Args[0].Pos()
+ arg := newIdent(pos, "posn")
+ n.Args[0] = arg
+ return false
+ }
+ }
+ }
+ }
+ return true
+ })
+}
+
// fixTraceSel renames uses of x.Trace to x.trace, where x for any x with a Trace field.
func fixTraceSel(f *ast.File) {
ast.Inspect(f, func(n ast.Node) bool {
+// Code generated by "go run generator.go"; DO NOT EDIT.
+
// Copyright 2018 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.
-// This file implements type parameter inference given
-// a list of concrete arguments and a parameter list.
+// This file implements type parameter inference.
package types
// If successful, infer returns the complete list of type arguments, one for each type parameter.
// Otherwise the result is nil and appropriate errors will be reported.
//
-// Inference proceeds as follows:
+// Inference proceeds as follows. Starting with given type arguments:
//
-// Starting with given type arguments
-// 1) apply FTI (function type inference) with typed arguments,
-// 2) apply CTI (constraint type inference),
-// 3) apply FTI with untyped function arguments,
-// 4) apply CTI.
+// 1. apply FTI (function type inference) with typed arguments,
+// 2. apply CTI (constraint type inference),
+// 3. apply FTI with untyped function arguments,
+// 4. apply CTI.
//
// The process stops as soon as all type arguments are known or an error occurs.
func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, params *Tuple, args []*operand) (result []Type) {
}
}
smap := makeSubstMap(tparams, targs)
- // TODO(rFindley): pass a positioner here, rather than arg.Pos().
+ // TODO(gri): pass a poser here, rather than arg.Pos().
inferred := check.subst(arg.Pos(), tpar, smap, nil, check.context())
- // _CannotInferTypeArgs indicates a failure of inference, though the actual
+ // CannotInferTypeArgs indicates a failure of inference, though the actual
// error may be better attributed to a user-provided type argument (hence
- // _InvalidTypeArg). We can't differentiate these cases, so fall back on
- // the more general _CannotInferTypeArgs.
+ // InvalidTypeArg). We can't differentiate these cases, so fall back on
+ // the more general CannotInferTypeArgs.
if inferred != tpar {
check.errorf(arg, CannotInferTypeArgs, "%s %s of %s does not match inferred type %s for %s", kind, targ, arg.expr, inferred, tpar)
} else {
// See how far we get with constraint type inference.
// Note that even if we don't have any type arguments, constraint type inference
// may produce results for constraints that explicitly specify a type.
- targs, index = check.inferB(posn, tparams, targs)
+ targs, index = check.inferB(tparams, targs)
if targs == nil || index < 0 {
return targs
}
// --- 4 ---
// Again, follow up with constraint type inference.
- targs, index = check.inferB(posn, tparams, targs)
+ targs, index = check.inferB(tparams, targs)
if targs == nil || index < 0 {
return targs
}
// At least one type argument couldn't be inferred.
- assert(index >= 0 && targs[index] == nil)
+ assert(targs != nil && index >= 0 && targs[index] == nil)
tpar := tparams[index]
- check.errorf(posn, CannotInferTypeArgs, "cannot infer %s (%v)", tpar.obj.name, tpar.obj.pos)
+ check.errorf(posn, CannotInferTypeArgs, "cannot infer %s (%s)", tpar.obj.name, tpar.obj.pos)
return nil
}
// first type argument in that list that couldn't be inferred (and thus is nil). If all
// type arguments were inferred successfully, index is < 0. The number of type arguments
// provided may be less than the number of type parameters, but there must be at least one.
-func (check *Checker) inferB(posn positioner, tparams []*TypeParam, targs []Type) (types []Type, index int) {
+func (check *Checker) inferB(tparams []*TypeParam, targs []Type) (types []Type, index int) {
assert(len(tparams) >= len(targs) && len(targs) > 0)
if traceInference {