cmd/compile: do not fatal when typechecking conversion expression

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>
This commit is contained in:
Cuong Manh Le 2023-10-03 21:54:48 +07:00 committed by Gopher Robot
parent 62bdbd2596
commit 38db0316f4
2 changed files with 20 additions and 1 deletions

View 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)

View File

@ -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() {}