From: Keith Randall Date: Thu, 11 Nov 2021 16:45:02 +0000 (-0800) Subject: cmd/compile: ensure stenciled function bodies are nonempty X-Git-Tag: go1.18beta1~369 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=10d3b1355184320f6d9623cb35e848e5af7c29ed;p=gostls13.git 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 Trust: Dan Scales Run-TryBot: Keith Randall TryBot-Result: Go Bot Reviewed-by: Dan Scales --- diff --git a/src/cmd/compile/internal/noder/stencil.go b/src/cmd/compile/internal/noder/stencil.go index c8c5d80cfc..20197565f5 100644 --- a/src/cmd/compile/internal/noder/stencil.go +++ b/src/cmd/compile/internal/noder/stencil.go @@ -802,6 +802,12 @@ func (g *genInst) genericSubst(newsym *types.Sym, nameNode *ir.Name, shapes []*t // Make sure name/type of newf is set before substituting the body. newf.Body = subst.list(gf.Body) + if len(newf.Body) == 0 { + // Ensure the body is nonempty, for issue 49524. + // TODO: have some other way to detect the difference between + // a function declared with no body, vs. one with an empty body? + newf.Body = append(newf.Body, ir.NewBlockStmt(gf.Pos(), nil)) + } if len(subst.defnMap) > 0 { base.Fatalf("defnMap is not empty") diff --git a/test/typeparam/issue49524.dir/a.go b/test/typeparam/issue49524.dir/a.go new file mode 100644 index 0000000000..f40075e953 --- /dev/null +++ b/test/typeparam/issue49524.dir/a.go @@ -0,0 +1,8 @@ +// Copyright 2021 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 a + +func F[T any]() { +} diff --git a/test/typeparam/issue49524.dir/main.go b/test/typeparam/issue49524.dir/main.go new file mode 100644 index 0000000000..ef00c8a81c --- /dev/null +++ b/test/typeparam/issue49524.dir/main.go @@ -0,0 +1,11 @@ +package main + +// Copyright 2021 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. + +import "a" + +func main() { + a.F[int]() +} diff --git a/test/typeparam/issue49524.go b/test/typeparam/issue49524.go new file mode 100644 index 0000000000..76930e5e4f --- /dev/null +++ b/test/typeparam/issue49524.go @@ -0,0 +1,7 @@ +// rundir -G=3 + +// Copyright 2021 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 ignored