]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: restrict //go:notinheap to runtime/internal/sys
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 7 Aug 2022 18:10:18 +0000 (01:10 +0700)
committerCuong Manh Le <cuong.manhle.vn@gmail.com>
Fri, 2 Sep 2022 06:22:23 +0000 (06:22 +0000)
So it won't be visible outside of runtime package. There are changes to
make tests happy:

 - For test/directive*.go files, using "go:noinline" for testing misplaced
 directives instead.
 - Restrict test/fixedbugs/bug515.go for gccgo only.
 - For test/notinheap{2,3}.go, using runtime/cgo.Incomplete for marking
 the type as not-in-heap. Though it's somewhat clumsy, it's the easiest
 way to keep the test errors for not-in-heap types until we can cleanup
 further.
 - test/typeparam/mdempsky/11.go is about defined type in user code marked
 as go:notinheap, which can't happen after this CL, though.

Fixes #46731

Change-Id: I869f5b2230c8a2a363feeec042e7723bbc416e8e
Reviewed-on: https://go-review.googlesource.com/c/go/+/421882
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Joedian Reid <joedian@golang.org>
Reviewed-by: David Chase <drchase@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/compile/internal/noder/noder.go
test/directive.go
test/directive2.go
test/fixedbugs/bug515.go
test/notinheap2.go
test/notinheap3.go
test/typeparam/mdempsky/11.go [deleted file]

index 15b1bf7b9fe51ab21f0eec58168bca99b49019b1..b68d7b7702475465bf9baa2bd2c235fbec27f9a8 100644 (file)
@@ -344,6 +344,9 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
                if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
                        p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
                }
+               if flag == ir.NotInHeap && *base.Flag.LowerP != "runtime/internal/sys" {
+                       p.error(syntax.Error{Pos: pos, Msg: "//go:notinheap only allowed in runtime/internal/sys"})
+               }
                pragma.Flag |= flag
                pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
        }
index 147e81db2c2377238d0d1b5e842de9f1a22553e0..8da15e243798366ea829fab87aec61f24a4979d2 100644 (file)
@@ -29,17 +29,9 @@ const c = 1
 //go:noinline // ERROR "misplaced compiler directive"
 type T int
 
-// ok
-//go:notinheap
-type T1 int
-
 type (
-       //go:notinheap
        //go:noinline // ERROR "misplaced compiler directive"
-       T2  int
-       T2b int
-       //go:notinheap
-       T2c int
+       T2 int
        //go:noinline // ERROR "misplaced compiler directive"
        T3 int
 )
@@ -61,11 +53,5 @@ func f() {
        _ = func() {}
 
        //go:noinline // ERROR "misplaced compiler directive"
-       // ok:
-       //go:notinheap
        type T int
 }
-
-// someday there might be a directive that can apply to type aliases, but go:notinheap doesn't.
-//go:notinheap // ERROR "misplaced compiler directive"
-type T6 = int
index e73e11235debfa23013a30f57b2a8c3293ae9920..2bb9ca9f0a44040e1b5d587c654d4391e609f74d 100644 (file)
@@ -13,21 +13,20 @@ package main
 
 //go:build bad // ERROR "misplaced compiler directive"
 
-//go:notinheap // ERROR "misplaced compiler directive"
+//go:noinline // ERROR "misplaced compiler directive"
 type (
-       T2  int //go:notinheap // ERROR "misplaced compiler directive"
+       T2  int //go:noinline // ERROR "misplaced compiler directive"
        T2b int
        T2c int
        T3  int
 )
 
-//go:notinheap // ERROR "misplaced compiler directive"
+//go:noinline // ERROR "misplaced compiler directive"
 type (
-       //go:notinheap
        T4 int
 )
 
-//go:notinheap // ERROR "misplaced compiler directive"
+//go:noinline // ERROR "misplaced compiler directive"
 type ()
 
 type T5 int
@@ -53,10 +52,6 @@ func f() {
        const c = 1
 
        _ = func() {}
-
-       // ok:
-       //go:notinheap
-       type T int
 }
 
 // EOF
index 186f46609ac62a56e72cee6899b379a25eb3934f..80d426fd6761569454429a4a08014d5f9530e5f8 100644 (file)
@@ -6,16 +6,18 @@
 
 // Caused a gofrontend crash.
 
+//go:build gccgo
+
 package p
 
 //go:notinheap
 type S1 struct{}
 
 type S2 struct {
-       r interface { Read([]byte) (int, error) }
+       r      interface{ Read([]byte) (int, error) }
        s1, s2 []byte
-       p *S1
-       n uintptr
+       p      *S1
+       n      uintptr
 }
 
 var V any = S2{}
index 100ed37b7218ad2ce8604fa13675db3bf9fa2ee2..cc1024ad5a5f920410c2834f255725cb8a8a1b3c 100644 (file)
@@ -4,12 +4,16 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-// Test walk errors for go:notinheap.
+// Test walk errors for not-in-heap.
+
+//go:build cgo
 
 package p
 
-//go:notinheap
+import "runtime/cgo"
+
 type nih struct {
+       _    cgo.Incomplete
        next *nih
 }
 
index 5ace8d6793f21bdb87252f7425143fd31f9b454c..b442ed42cd6e36771c0894abd55b36691365f20d 100644 (file)
@@ -6,8 +6,12 @@
 
 // Test write barrier elimination for notinheap.
 
+//go:build cgo
+
 package p
 
+import "runtime/cgo"
+
 type t1 struct {
        x *nih
        s []nih
@@ -20,8 +24,8 @@ type t2 struct {
        y [1024]byte
 }
 
-//go:notinheap
 type nih struct {
+       _ cgo.Incomplete
        x uintptr
 }
 
diff --git a/test/typeparam/mdempsky/11.go b/test/typeparam/mdempsky/11.go
deleted file mode 100644 (file)
index e86c038..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-// errorcheck
-
-// 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.
-
-// Reported by Cuong Manh Le.
-
-package p
-
-type a struct{}
-
-//go:notinheap
-type b a
-
-var _ = (*b)(new(a)) // ERROR "cannot convert"