]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix irgen mis-handling invalid function declaration
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 1 Sep 2021 03:13:41 +0000 (10:13 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Wed, 1 Sep 2021 19:51:25 +0000 (19:51 +0000)
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>
src/cmd/compile/internal/noder/decl.go
src/cmd/compile/internal/noder/irgen.go
src/cmd/compile/internal/types2/stdlib_test.go
src/go/types/stdlib_test.go
test/fixedbugs/issue48097.go [new file with mode: 0644]

index 87a8667003f27c8d941b3060af5c2dd596912d22..de481fb5fcaffb55e4ab74c9d78537b0b4e6d124 100644 (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 {
                g.target.Inits = append(g.target.Inits, fn)
        }
index d53c254001c26853f686920df9bb6b738aab2759..70f7991a8e005c9446939b1adffdffedd3e48e3f 100644 (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 {
                // Process linkname and cgo pragmas.
                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
        g.stencil()
 
index cde35c17b6f717f97dd3781f914ee0fcfe0afc99..5bf29824184ce19d5d61452cd944ccc4e04739db 100644 (file)
@@ -192,6 +192,8 @@ func TestStdFixed(t *testing.T) {
                "issue20780.go",  // types2 does not have constraints on stack size
                "issue42058a.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
+
        )
 }
 
index 3eb7519a91af06d2d27f8c19fdb1b4bb358e0ea9..12ed9a54f22fdce672b29a0071cb3539184f71d7 100644 (file)
@@ -194,6 +194,7 @@ func TestStdFixed(t *testing.T) {
                "bug251.go",      // issue #34333 which was exposed with fix for #34151
                "issue42058a.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
        )
 }
 
diff --git a/test/fixedbugs/issue48097.go b/test/fixedbugs/issue48097.go
new file mode 100644 (file)
index 0000000..b08c2a2
--- /dev/null
@@ -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"