]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: generate infer.go
authorRobert Griesemer <gri@golang.org>
Wed, 18 Jan 2023 21:34:48 +0000 (13:34 -0800)
committerGopher Robot <gobot@golang.org>
Fri, 20 Jan 2023 01:31:26 +0000 (01:31 +0000)
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 <gri@google.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@google.com>
src/cmd/compile/internal/types2/infer.go
src/go/types/generator.go
src/go/types/infer.go

index f8cd23f0688b84c298ca9b8cf0c0f74229b2f8ec..9f57476c573114c71a771486a7bcf2927d8a0e9e 100644 (file)
@@ -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 {
index 1e5458bd8237fa8182032352d0771445633994db..2a0b9e5d5c15deb5295161c5a15dccd5d17cc59e 100644 (file)
@@ -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 {
index d77f07447adf65b818de7fe392546b0e68d3925c..2dc9ad17f9f392c3a1522eff0eef9e16f49ff67c 100644 (file)
@@ -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 {