diff options
| author | Dan Scales <danscales@google.com> | 2021-03-29 16:07:18 -0700 |
|---|---|---|
| committer | Dan Scales <danscales@google.com> | 2021-03-30 19:44:38 +0000 |
| commit | 1318fb4a32371311688c6b868c3041f0501b6aeb (patch) | |
| tree | c109d6aceab1e1373324cfd98e983febd4981262 /src/cmd/compile/internal/noder/expr.go | |
| parent | 3300390ec70c6b8e2392e4ab32342d426e2d3532 (diff) | |
| download | go-1318fb4a32371311688c6b868c3041f0501b6aeb.tar.xz | |
cmd/compile: handle partial type inference that doesn't require function args
Handle the case where types can be partially inferred for an
instantiated function that is not immediately called. The key for the
Inferred map is the CallExpr (if inferring types required the function
arguments) or the IndexExpr (if types could be inferred without the
function arguments).
Added new tests for the case where the function isn't immediately called
to typelist.go.
Change-Id: I60f503ad67cd192da2f2002060229efd4930dc39
Reviewed-on: https://go-review.googlesource.com/c/go/+/305909
Trust: Dan Scales <danscales@google.com>
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Dan Scales <danscales@google.com>
Reviewed-by: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Diffstat (limited to 'src/cmd/compile/internal/noder/expr.go')
| -rw-r--r-- | src/cmd/compile/internal/noder/expr.go | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/src/cmd/compile/internal/noder/expr.go b/src/cmd/compile/internal/noder/expr.go index ecdc7c74b1..eee39ecadb 100644 --- a/src/cmd/compile/internal/noder/expr.go +++ b/src/cmd/compile/internal/noder/expr.go @@ -94,16 +94,12 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node { case *syntax.CallExpr: fun := g.expr(expr.Fun) - // The key for the Inferred map is usually the expr. - key := syntax.Expr(expr) - if _, ok := expr.Fun.(*syntax.IndexExpr); ok { - // If the Fun is an IndexExpr, then this may be a - // partial type inference case. In this case, we look up - // the IndexExpr in the Inferred map. - // TODO(gri): should types2 always record the callExpr as the key? - key = syntax.Expr(expr.Fun) - } - if inferred, ok := g.info.Inferred[key]; ok && len(inferred.Targs) > 0 { + // The key for the Inferred map is the CallExpr (if inferring + // types required the function arguments) or the IndexExpr below + // (if types could be inferred without the function arguments). + if inferred, ok := g.info.Inferred[expr]; ok && len(inferred.Targs) > 0 { + // This is the case where inferring types required the + // types of the function arguments. targs := make([]ir.Node, len(inferred.Targs)) for i, targ := range inferred.Targs { targs[i] = ir.TypeNode(g.typ(targ)) @@ -126,7 +122,16 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node { case *syntax.IndexExpr: var targs []ir.Node - if _, ok := expr.Index.(*syntax.ListExpr); ok { + + if inferred, ok := g.info.Inferred[expr]; ok && len(inferred.Targs) > 0 { + // This is the partial type inference case where the types + // can be inferred from other type arguments without using + // the types of the function arguments. + targs = make([]ir.Node, len(inferred.Targs)) + for i, targ := range inferred.Targs { + targs[i] = ir.TypeNode(g.typ(targ)) + } + } else if _, ok := expr.Index.(*syntax.ListExpr); ok { targs = g.exprList(expr.Index) } else { index := g.expr(expr.Index) @@ -137,12 +142,13 @@ func (g *irgen) expr0(typ types2.Type, expr syntax.Expr) ir.Node { // This is generic function instantiation with a single type targs = []ir.Node{index} } - // This is a generic function instantiation (e.g. min[int]) + // This is a generic function instantiation (e.g. min[int]). + // Generic type instantiation is handled in the type + // section of expr() above (using g.typ). x := g.expr(expr.X) if x.Op() != ir.ONAME || x.Type().Kind() != types.TFUNC { panic("Incorrect argument for generic func instantiation") } - // This could also be an OTYPEINST once we can handle those examples. n := ir.NewInstExpr(pos, ir.OFUNCINST, x, targs) typed(g.typ(typ), n) return n |
