From f52b8fa83f00b70215e9980b2d7fc0c52c873c3b Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 18 Jan 2023 13:34:48 -0800 Subject: [PATCH] go/types: generate infer.go Also, remove pos parameter from inferB (was unused). Change-Id: I050f64d9fe916628499a55ca46f15c1f58ed4d1d Reviewed-on: https://go-review.googlesource.com/c/go/+/461691 Auto-Submit: Robert Griesemer Reviewed-by: Robert Findley Run-TryBot: Robert Griesemer TryBot-Result: Gopher Robot Reviewed-by: Robert Griesemer --- src/cmd/compile/internal/types2/infer.go | 6 ++-- src/go/types/generator.go | 44 ++++++++++++++++++++++++ src/go/types/infer.go | 34 +++++++++--------- 3 files changed, 64 insertions(+), 20 deletions(-) diff --git a/src/cmd/compile/internal/types2/infer.go b/src/cmd/compile/internal/types2/infer.go index f8cd23f068..9f57476c57 100644 --- a/src/cmd/compile/internal/types2/infer.go +++ b/src/cmd/compile/internal/types2/infer.go @@ -222,7 +222,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, // 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 } @@ -256,7 +256,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type, // --- 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 } @@ -452,7 +452,7 @@ func (w *tpWalker) isParameterizedTypeList(list []Type) bool { // 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 { diff --git a/src/go/types/generator.go b/src/go/types/generator.go index 1e5458bd82..2a0b9e5d5c 100644 --- a/src/go/types/generator.go +++ b/src/go/types/generator.go @@ -90,6 +90,7 @@ var filemap = map[string]action{ "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, @@ -184,6 +185,49 @@ func fixTokenPos(f *ast.File) { }) } +// 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 { diff --git a/src/go/types/infer.go b/src/go/types/infer.go index d77f07447a..2dc9ad17f9 100644 --- a/src/go/types/infer.go +++ b/src/go/types/infer.go @@ -1,9 +1,10 @@ +// 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 @@ -21,13 +22,12 @@ import ( // 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) { @@ -167,12 +167,12 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []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 { @@ -224,7 +224,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, // 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 } @@ -258,15 +258,15 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type, // --- 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 } @@ -454,7 +454,7 @@ func (w *tpWalker) isParameterizedTypeList(list []Type) bool { // 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 { -- 2.50.0