From: Matthew Dempsky Date: Wed, 16 Mar 2022 05:25:36 +0000 (-0700) Subject: cmd/compile: detect invalid NIH conversions within unified IR X-Git-Tag: go1.19beta1~1049 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=a1bf50eefe35087ac7151116558f4c19095b8473;p=gostls13.git cmd/compile: detect invalid NIH conversions within unified IR 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 Run-TryBot: Matthew Dempsky TryBot-Result: Gopher Robot Reviewed-by: Cuong Manh Le --- diff --git a/src/cmd/compile/internal/noder/reader.go b/src/cmd/compile/internal/noder/reader.go index 73e4ddbbed..dd3bb1523e 100644 --- a/src/cmd/compile/internal/noder/reader.go +++ b/src/cmd/compile/internal/noder/reader.go @@ -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)) } }