1
0
mirror of https://github.com/golang/go synced 2024-11-22 08:34:40 -07:00

cmd/compile: fix "expression has untyped type" ICE in generic code

During walk, we sometimes desugar OEQ nodes into multiple "untyped
bool" expressions, and then use typecheck.Conv to convert back to the
original OEQ node's type.

However, typecheck.Conv had a short-circuit path that if the type is
already identical to the target type according to types.Identical,
then we skipped the conversion. This short-circuit is normally fine;
but with generic code and shape types, it considers "untyped bool" and
"go.shape.bool" to be identical types. And we could end up leaving an
expression of "untyped bool", which then fails an internal consistency
check later.

The simple fix is to change Conv to use types.IdenticalStrict, so that
we ensure "untyped bool" gets converted to "go.shape.bool". And for
good measure, make the same change to ConvNop.

This issue was discovered and reported against unified IR, but the
issue was latent within the non-unified frontend too.

Fixes #54537.

Change-Id: I7559a346b063349b35749e8a2da704be18e51654
Reviewed-on: https://go-review.googlesource.com/c/go/+/424937
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
This commit is contained in:
Matthew Dempsky 2022-08-18 21:38:08 -07:00
parent aa6a7fa775
commit 0a6e1fa986
2 changed files with 24 additions and 2 deletions

View File

@ -1818,7 +1818,7 @@ func isTermNode(n ir.Node) bool {
}
func Conv(n ir.Node, t *types.Type) ir.Node {
if types.Identical(n.Type(), t) {
if types.IdenticalStrict(n.Type(), t) {
return n
}
n = ir.NewConvExpr(base.Pos, ir.OCONV, nil, n)
@ -1830,7 +1830,7 @@ func Conv(n ir.Node, t *types.Type) ir.Node {
// ConvNop converts node n to type t using the OCONVNOP op
// and typechecks the result with ctxExpr.
func ConvNop(n ir.Node, t *types.Type) ir.Node {
if types.Identical(n.Type(), t) {
if types.IdenticalStrict(n.Type(), t) {
return n
}
n = ir.NewConvExpr(base.Pos, ir.OCONVNOP, nil, n)

View File

@ -0,0 +1,22 @@
// run
// Copyright 2022 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 main
func main() {
_ = F[bool]
var x string
_ = G(x == "foo")
}
func F[T ~bool](x string) {
var _ T = x == "foo"
}
func G[T any](t T) *T {
return &t
}