Skip to content

Commit a72622d

Browse files
Cuong Manh Lecuonglm
Cuong Manh Le
authored andcommitted
cmd/compile: skip "_" function in reflectdata.MarkUsedIfaceMethod
CL 256798 added compiler ability to retain only used interface methods, by generating a mark relocation whenever an interface method is used. To do that, the compiler needs the current function linker object. However, for unnamed function "func _()", its linker object is nil, causes the compiler crashes for code in #45258. CL 283313 fixed the code in #45258 unintentionally, since when the compiler now does not walk unnamed functions anymore. This CL fixes the root issue, by making reflectdata.MarkUsedIfaceMethod skips unnamed functions, and also adding regression test. Fixes #45258 Change-Id: I4cbefb0a89d9928f70c00dc8a271cb61cd20a49c Reviewed-on: https://go-review.googlesource.com/c/go/+/311130 Trust: Cuong Manh Le <[email protected]> Run-TryBot: Cuong Manh Le <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent c914e61 commit a72622d

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

src/cmd/compile/internal/reflectdata/reflect.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,6 +1837,10 @@ func MarkTypeUsedInInterface(t *types.Type, from *obj.LSym) {
18371837
// MarkUsedIfaceMethod marks that an interface method is used in the current
18381838
// function. n is OCALLINTER node.
18391839
func MarkUsedIfaceMethod(n *ir.CallExpr) {
1840+
// skip unnamed functions (func _())
1841+
if ir.CurFunc.LSym == nil {
1842+
return
1843+
}
18401844
dot := n.X.(*ir.SelectorExpr)
18411845
ityp := dot.X.Type()
18421846
tsym := TypeLinksym(ityp)

test/fixedbugs/issue45258.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile
2+
3+
// Copyright 2021 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+
type Fooer interface {
10+
Foo() Barer
11+
}
12+
13+
type Barer interface {
14+
Bar()
15+
}
16+
17+
type impl struct{}
18+
19+
func (r *impl) Foo() Barer {
20+
return r
21+
}
22+
23+
func (r *impl) Bar() {}
24+
25+
func _() {
26+
var r Fooer = &impl{}
27+
r.Foo().Bar()
28+
}

0 commit comments

Comments
 (0)