]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: do not fatal when typechecking conversion expression
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 3 Oct 2023 14:54:48 +0000 (21:54 +0700)
committerGopher Robot <gobot@golang.org>
Thu, 5 Oct 2023 19:44:52 +0000 (19:44 +0000)
The types2 typechecker already reported all invalid conversions required
by the Go language spec. However, the conversion involves go pragma is
not specified in the spec, so is not checked by types2.

Fixing this by handling the error gracefully during typecheck, just like
how old typechecker did before CL 394575.

Fixes #63333

Change-Id: I04c4121971c62d96f75ded1794ab4bdf3a6cd0ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/532515
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Keith Randall <khr@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>

src/cmd/compile/internal/typecheck/expr.go
test/fixedbugs/issue63333.go [new file with mode: 0644]

index 53d0cbf96dec645d8dd2e4b3a3c2c7c89dfd9be7..83d1355fe5dbdd567a87e0b6eaf331cedf11b303 100644 (file)
@@ -8,6 +8,7 @@ import (
        "fmt"
        "go/constant"
        "go/token"
+       "internal/types/errors"
        "strings"
 
        "cmd/compile/internal/base"
@@ -353,7 +354,10 @@ func tcConv(n *ir.ConvExpr) ir.Node {
        }
        op, why := Convertop(n.X.Op() == ir.OLITERAL, t, n.Type())
        if op == ir.OXXX {
-               base.Fatalf("cannot convert %L to type %v%s", n.X, n.Type(), why)
+               // Due to //go:nointerface, we may be stricter than types2 here (#63333).
+               base.ErrorfAt(n.Pos(), errors.InvalidConversion, "cannot convert %L to type %v%s", n.X, n.Type(), why)
+               n.SetType(nil)
+               return n
        }
 
        n.SetOp(op)
diff --git a/test/fixedbugs/issue63333.go b/test/fixedbugs/issue63333.go
new file mode 100644 (file)
index 0000000..e14b367
--- /dev/null
@@ -0,0 +1,15 @@
+// errorcheck -goexperiment fieldtrack
+
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package p
+
+func f(interface{ m() }) {}
+func g()                 { f(new(T)) } // ERROR "m method is marked 'nointerface'"
+
+type T struct{}
+
+//go:nointerface
+func (*T) m() {}