]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/noder: refactor exprConvert code
authorMatthew Dempsky <mdempsky@google.com>
Thu, 1 Sep 2022 23:02:49 +0000 (16:02 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Fri, 2 Sep 2022 18:25:53 +0000 (18:25 +0000)
This CL deduplicates the explicit and implicit exprConvert code paths
to have a single common function, so they're easier to keep in sync.

Change-Id: I2b145d2ce6de6018ffc2db5cdb9d891f4e223381
Reviewed-on: https://go-review.googlesource.com/c/go/+/427677
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/compile/internal/noder/writer.go

index e7aa5c1c499d58d290f986ec67139ae642c003ae..c2ff639b00b0bc5fbbc4d5ce1cc4856c67b7d9b3 100644 (file)
@@ -1794,14 +1794,7 @@ func (w *writer) expr(expr syntax.Expr) {
                if tv.IsType() {
                        assert(len(expr.ArgList) == 1)
                        assert(!expr.HasDots)
-
-                       w.Code(exprConvert)
-                       w.Bool(false) // explicit
-                       w.typ(tv.Type)
-                       w.pos(expr)
-                       w.convRTTI(w.p.typeOf(expr.ArgList[0]), tv.Type)
-                       w.Bool(isTypeParam(tv.Type))
-                       w.expr(expr.ArgList[0])
+                       w.convertExpr(tv.Type, expr.ArgList[0], false)
                        break
                }
 
@@ -2069,19 +2062,29 @@ func (w *writer) multiExpr(pos poser, dstType func(int) types2.Type, exprs []syn
 // from expr's type, then an implicit conversion operation is inserted
 // at expr's position.
 func (w *writer) implicitConvExpr(dst types2.Type, expr syntax.Expr) {
+       w.convertExpr(dst, expr, true)
+}
+
+func (w *writer) convertExpr(dst types2.Type, expr syntax.Expr, implicit bool) {
        src := w.p.typeOf(expr)
-       if dst != nil && !types2.Identical(src, dst) {
-               if !types2.AssignableTo(src, dst) {
-                       w.p.fatalf(expr.Pos(), "%v is not assignable to %v", src, dst)
-               }
-               w.Code(exprConvert)
-               w.Bool(true) // implicit
-               w.typ(dst)
-               w.pos(expr)
-               w.convRTTI(src, dst)
-               w.Bool(isTypeParam(dst))
-               // fallthrough
+
+       // Omit implicit no-op conversions.
+       identical := dst == nil || types2.Identical(src, dst)
+       if implicit && identical {
+               w.expr(expr)
+               return
        }
+
+       if implicit && !types2.AssignableTo(src, dst) {
+               w.p.fatalf(expr, "%v is not assignable to %v", src, dst)
+       }
+
+       w.Code(exprConvert)
+       w.Bool(implicit)
+       w.typ(dst)
+       w.pos(expr)
+       w.convRTTI(src, dst)
+       w.Bool(isTypeParam(dst))
        w.expr(expr)
 }