Skip to content

Commit 81c9b1d

Browse files
cuonglmgopherbot
authored andcommitted
cmd/compile: fix broken IR for iface -> eface
For implementing interface to empty interface conversion, the compiler generate code like: var res *uint8 res = itab if res != nil { res = res.type } However, itab has type *uintptr, so the assignment is broken. The problem is not shown up, until CL 450215, which call typecheck on this broken assignment. To fix this, just cast itab to *uint8 when doing the conversion. Fixes #56768 Change-Id: Id42792d18e7f382578b40854d46eecd49673792c Reviewed-on: https://go-review.googlesource.com/c/go/+/451256 Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Auto-Submit: Cuong Manh Le <[email protected]>
1 parent 0bd4710 commit 81c9b1d

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/cmd/compile/internal/walk/convert.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,15 @@ func walkConvInterface(n *ir.ConvExpr, init *ir.Nodes) ir.Node {
8080

8181
var typeWord ir.Node
8282
if toType.IsEmptyInterface() {
83-
// Implement interface to empty interface conversion.
84-
// res = itab
83+
// Implement interface to empty interface conversion:
84+
//
85+
// var res *uint8
86+
// res = (*uint8)(unsafe.Pointer(itab))
8587
// if res != nil {
8688
// res = res.type
8789
// }
8890
typeWord = typecheck.Temp(types.NewPtr(types.Types[types.TUINT8]))
89-
init.Append(ir.NewAssignStmt(base.Pos, typeWord, itab))
91+
init.Append(ir.NewAssignStmt(base.Pos, typeWord, typecheck.Conv(typecheck.Conv(itab, types.Types[types.TUNSAFEPTR]), typeWord.Type())))
9092
nif := ir.NewIfStmt(base.Pos, typecheck.Expr(ir.NewBinaryExpr(base.Pos, ir.ONE, typeWord, typecheck.NodNil())), nil, nil)
9193
nif.Body = []ir.Node{ir.NewAssignStmt(base.Pos, typeWord, itabType(typeWord))}
9294
init.Append(nif)

test/fixedbugs/issue56768.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile
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 p
8+
9+
type I interface {
10+
M()
11+
}
12+
13+
type slice []any
14+
15+
func f() {
16+
ss := struct{ i I }{}
17+
18+
_ = [...]struct {
19+
s slice
20+
}{
21+
{
22+
s: slice{ss.i},
23+
},
24+
{
25+
s: slice{ss.i},
26+
},
27+
{
28+
s: slice{ss.i},
29+
},
30+
{
31+
s: slice{ss.i},
32+
},
33+
{
34+
s: slice{ss.i},
35+
},
36+
}
37+
}

0 commit comments

Comments
 (0)