Skip to content

Commit 10d3b13

Browse files
committed
cmd/compile: ensure stenciled function bodies are nonempty
Our compiler gets confused between functions that were declared with no body, and those which have a body but it is empty. Ensure that when stenciling, we generate a nonempty body. The particular test that causes this problem is in cmd/compile/internal/gc/main.go:enqueueFunc. It thinks that if a function has no body, then we need to generate ABI wrappers for it, but not compile it. Fixes #49524 Change-Id: Id962666a2098f60a2421484b6a776eafdc4f4a63 Reviewed-on: https://go-review.googlesource.com/c/go/+/363395 Trust: Keith Randall <khr@golang.org> Trust: Dan Scales <danscales@google.com> Run-TryBot: Keith Randall <khr@golang.org> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Dan Scales <danscales@google.com>
1 parent c622d1d commit 10d3b13

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/cmd/compile/internal/noder/stencil.go

+6
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,12 @@ func (g *genInst) genericSubst(newsym *types.Sym, nameNode *ir.Name, shapes []*t
802802

803803
// Make sure name/type of newf is set before substituting the body.
804804
newf.Body = subst.list(gf.Body)
805+
if len(newf.Body) == 0 {
806+
// Ensure the body is nonempty, for issue 49524.
807+
// TODO: have some other way to detect the difference between
808+
// a function declared with no body, vs. one with an empty body?
809+
newf.Body = append(newf.Body, ir.NewBlockStmt(gf.Pos(), nil))
810+
}
805811

806812
if len(subst.defnMap) > 0 {
807813
base.Fatalf("defnMap is not empty")

test/typeparam/issue49524.dir/a.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package a
6+
7+
func F[T any]() {
8+
}

test/typeparam/issue49524.dir/main.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
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+
import "a"
8+
9+
func main() {
10+
a.F[int]()
11+
}

test/typeparam/issue49524.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// rundir -G=3
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 ignored

0 commit comments

Comments
 (0)