From 9cb784ac6926a8b91c1a0dce99612e713150b30c Mon Sep 17 00:00:00 2001 From: Matthew Dempsky Date: Tue, 21 Jun 2022 06:36:28 -0700 Subject: [PATCH] [dev.unified] cmd/compile/internal/noder: add pkgWriter.typeOf helper Getting the type of a value expression is already a very common operation during writing, and it's going to become more common to handle implicit conversions. Change-Id: I5401c6b01546bbf8e85b1ed3fe4acf2835925e2f Reviewed-on: https://go-review.googlesource.com/c/go/+/413395 Reviewed-by: Keith Randall Run-TryBot: Matthew Dempsky Reviewed-by: David Chase TryBot-Result: Gopher Robot --- src/cmd/compile/internal/noder/writer.go | 30 ++++++++++++------------ 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/cmd/compile/internal/noder/writer.go b/src/cmd/compile/internal/noder/writer.go index 7bbd3abc5d3..a562bec26f0 100644 --- a/src/cmd/compile/internal/noder/writer.go +++ b/src/cmd/compile/internal/noder/writer.go @@ -120,6 +120,14 @@ func (pw *pkgWriter) unexpected(what string, p poser) { pw.fatalf(p, "unexpected %s: %v (%T)", what, p, p) } +// typeOf returns the Type of the given value expression. +func (pw *pkgWriter) typeOf(expr syntax.Expr) types2.Type { + tv, ok := pw.info.Types[expr] + assert(ok) + assert(tv.IsValue()) + return tv.Type +} + // A writer provides APIs for writing out an individual element. type writer struct { p *pkgWriter @@ -1258,9 +1266,7 @@ func (w *writer) switchStmt(stmt *syntax.SwitchStmt) { var iface types2.Type if guard, ok := stmt.Tag.(*syntax.TypeSwitchGuard); w.Bool(ok) { - tv, ok := w.p.info.Types[guard.X] - assert(ok && tv.IsValue()) - iface = tv.Type + iface = w.p.typeOf(guard.X) w.pos(guard) if tag := guard.Lhs; w.Bool(tag != nil) { @@ -1410,8 +1416,7 @@ func (w *writer) expr(expr syntax.Expr) { w.selector(sel.Obj()) case *syntax.IndexExpr: - tv, ok := w.p.info.Types[expr.Index] - assert(ok && tv.IsValue()) + _ = w.p.typeOf(expr.Index) // ensure this is an index expression, not an instantiation w.Code(exprIndex) w.expr(expr.X) @@ -1427,13 +1432,12 @@ func (w *writer) expr(expr syntax.Expr) { } case *syntax.AssertExpr: - tv, ok := w.p.info.Types[expr.X] - assert(ok && tv.IsValue()) + iface := w.p.typeOf(expr.X) w.Code(exprAssert) w.expr(expr.X) w.pos(expr) - w.exprType(tv.Type, expr.Type, false) + w.exprType(iface, expr.Type, false) case *syntax.Operation: if expr.Y == nil { @@ -1523,14 +1527,12 @@ func (w *writer) optExpr(expr syntax.Expr) { } func (w *writer) compLit(lit *syntax.CompositeLit) { - tv, ok := w.p.info.Types[lit] - assert(ok) + typ := w.p.typeOf(lit) w.Sync(pkgbits.SyncCompLit) w.pos(lit) - w.typ(tv.Type) + w.typ(typ) - typ := tv.Type if ptr, ok := types2.CoreType(typ).(*types2.Pointer); ok { typ = ptr.Elem() } @@ -1562,9 +1564,7 @@ func (w *writer) compLit(lit *syntax.CompositeLit) { } func (w *writer) funcLit(expr *syntax.FuncLit) { - tv, ok := w.p.info.Types[expr] - assert(ok) - sig := tv.Type.(*types2.Signature) + sig := w.p.typeOf(expr).(*types2.Signature) body, closureVars := w.p.bodyIdx(sig, expr.Body, w.dict)