Skip to content

Commit 0a6e1fa

Browse files
committed
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 <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: David Chase <[email protected]>
1 parent aa6a7fa commit 0a6e1fa

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/cmd/compile/internal/typecheck/typecheck.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1818,7 +1818,7 @@ func isTermNode(n ir.Node) bool {
18181818
}
18191819

18201820
func Conv(n ir.Node, t *types.Type) ir.Node {
1821-
if types.Identical(n.Type(), t) {
1821+
if types.IdenticalStrict(n.Type(), t) {
18221822
return n
18231823
}
18241824
n = ir.NewConvExpr(base.Pos, ir.OCONV, nil, n)
@@ -1830,7 +1830,7 @@ func Conv(n ir.Node, t *types.Type) ir.Node {
18301830
// ConvNop converts node n to type t using the OCONVNOP op
18311831
// and typechecks the result with ctxExpr.
18321832
func ConvNop(n ir.Node, t *types.Type) ir.Node {
1833-
if types.Identical(n.Type(), t) {
1833+
if types.IdenticalStrict(n.Type(), t) {
18341834
return n
18351835
}
18361836
n = ir.NewConvExpr(base.Pos, ir.OCONVNOP, nil, n)

test/typeparam/issue54537.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// run
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package main
8+
9+
func main() {
10+
_ = F[bool]
11+
12+
var x string
13+
_ = G(x == "foo")
14+
}
15+
16+
func F[T ~bool](x string) {
17+
var _ T = x == "foo"
18+
}
19+
20+
func G[T any](t T) *T {
21+
return &t
22+
}

0 commit comments

Comments
 (0)