]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: detect invalid NIH conversions within unified IR
authorMatthew Dempsky <mdempsky@google.com>
Wed, 16 Mar 2022 05:25:36 +0000 (22:25 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 16 Mar 2022 18:30:48 +0000 (18:30 +0000)
Unified IR currently relies on typecheck to diagnose invalid
//go:notinheap conversions, which prevents removing all of
its (otherwise) dead error-reporting code.

This CL updates the unified IR reader to instead proactively diagnose
these invalid conversions. This logic can be removed again once #46731
is implemented, but in the mean time it allows progress on #51691.

Updates #46731.
Updates #51691.

Change-Id: Ifae81aaad770209ec7a67bc10b55660f291e403e
Reviewed-on: https://go-review.googlesource.com/c/go/+/392917
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/noder/reader.go

index 73e4ddbbedc1229e4bf95e944e0eedbd4406210e..dd3bb1523e124e6af0cbaa975af8ce00e1287fde 100644 (file)
@@ -1676,6 +1676,20 @@ func (r *reader) expr() (res ir.Node) {
                typ := r.typ()
                pos := r.pos()
                x := r.expr()
+
+               // TODO(mdempsky): Stop constructing expressions of untyped type.
+               x = typecheck.DefaultLit(x, typ)
+
+               if op, why := typecheck.Convertop(x.Op() == ir.OLITERAL, x.Type(), typ); op == ir.OXXX {
+                       // types2 ensured that x is convertable to typ under standard Go
+                       // semantics, but cmd/compile also disallows some conversions
+                       // involving //go:notinheap.
+                       //
+                       // TODO(mdempsky): This can be removed after #46731 is implemented.
+                       base.ErrorfAt(pos, "cannot convert %L to type %v%v", x, typ, why)
+                       base.ErrorExit() // harsh, but prevents constructing invalid IR
+               }
+
                return typecheck.Expr(ir.NewConvExpr(pos, ir.OCONV, typ, x))
        }
 }