cmd/compile: fix irgen mis-handling invalid function declaration

In -G=3 mode, irgen use its own generated IR, which is mis-handling of
bodyless function and declared function with //go:noescape pragma.

Fix this by adopting the same logic in noder.funcDecl, which minor
change in linkname detection.

Fixes #48097

Change-Id: Ibef921c1f75e071ca61685e0cb4543f2ee5efc7f
Reviewed-on: https://go-review.googlesource.com/c/go/+/346470
Trust: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Dan Scales <danscales@google.com>
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Dan Scales <danscales@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Cuong Manh Le 2021-09-01 10:13:41 +07:00
parent 592ee433f5
commit 711e1c8224
5 changed files with 35 additions and 6 deletions

View File

@ -113,6 +113,10 @@ func (g *irgen) funcDecl(out *ir.Nodes, decl *syntax.FuncDecl) {
} }
} }
if decl.Body != nil && fn.Pragma&ir.Noescape != 0 {
base.ErrorfAt(fn.Pos(), "can only use //go:noescape with external func implementations")
}
if decl.Name.Value == "init" && decl.Recv == nil { if decl.Name.Value == "init" && decl.Recv == nil {
g.target.Inits = append(g.target.Inits, fn) g.target.Inits = append(g.target.Inits, fn)
} }

View File

@ -272,12 +272,6 @@ Outer:
} }
} }
// Check for unusual case where noder2 encounters a type error that types2
// doesn't check for (e.g. notinheap incompatibility).
base.ExitIfErrors()
typecheck.DeclareUniverse()
for _, p := range noders { for _, p := range noders {
// Process linkname and cgo pragmas. // Process linkname and cgo pragmas.
p.processPragmas() p.processPragmas()
@ -290,6 +284,22 @@ Outer:
}) })
} }
if base.Flag.Complete {
for _, n := range g.target.Decls {
if fn, ok := n.(*ir.Func); ok {
if fn.Body == nil && fn.Nname.Sym().Linkname == "" {
base.ErrorfAt(fn.Pos(), "missing function body")
}
}
}
}
// Check for unusual case where noder2 encounters a type error that types2
// doesn't check for (e.g. notinheap incompatibility).
base.ExitIfErrors()
typecheck.DeclareUniverse()
// Create any needed stencils of generic functions // Create any needed stencils of generic functions
g.stencil() g.stencil()

View File

@ -192,6 +192,8 @@ func TestStdFixed(t *testing.T) {
"issue20780.go", // types2 does not have constraints on stack size "issue20780.go", // types2 does not have constraints on stack size
"issue42058a.go", // types2 does not have constraints on channel element size "issue42058a.go", // types2 does not have constraints on channel element size
"issue42058b.go", // types2 does not have constraints on channel element size "issue42058b.go", // types2 does not have constraints on channel element size
"issue48097.go", // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
) )
} }

View File

@ -194,6 +194,7 @@ func TestStdFixed(t *testing.T) {
"bug251.go", // issue #34333 which was exposed with fix for #34151 "bug251.go", // issue #34333 which was exposed with fix for #34151
"issue42058a.go", // go/types does not have constraints on channel element size "issue42058a.go", // go/types does not have constraints on channel element size
"issue42058b.go", // go/types does not have constraints on channel element size "issue42058b.go", // go/types does not have constraints on channel element size
"issue48097.go", // go/types doesn't check validity of //go:xxx directives, and non-init bodyless function
) )
} }

View File

@ -0,0 +1,12 @@
// errorcheck -complete
// 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 p
func F() // ERROR "missing function body"
//go:noescape
func f() {} // ERROR "can only use //go:noescape with external func implementations"