Skip to content

Commit ec2ea40

Browse files
committed
cmd/compile: restrict //go:notinheap to runtime/internal/sys
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 <[email protected]> Reviewed-by: Joedian Reid <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent 134cd34 commit ec2ea40

File tree

7 files changed

+24
-46
lines changed

7 files changed

+24
-46
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ func (p *noder) pragma(pos syntax.Pos, blankLine bool, text string, old syntax.P
344344
if flag == 0 && !allowedStdPragmas[verb] && base.Flag.Std {
345345
p.error(syntax.Error{Pos: pos, Msg: fmt.Sprintf("//%s is not allowed in the standard library", verb)})
346346
}
347+
if flag == ir.NotInHeap && *base.Flag.LowerP != "runtime/internal/sys" {
348+
p.error(syntax.Error{Pos: pos, Msg: "//go:notinheap only allowed in runtime/internal/sys"})
349+
}
347350
pragma.Flag |= flag
348351
pragma.Pos = append(pragma.Pos, pragmaPos{flag, pos})
349352
}

test/directive.go

+1-15
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,9 @@ const c = 1
2929
//go:noinline // ERROR "misplaced compiler directive"
3030
type T int
3131

32-
// ok
33-
//go:notinheap
34-
type T1 int
35-
3632
type (
37-
//go:notinheap
3833
//go:noinline // ERROR "misplaced compiler directive"
39-
T2 int
40-
T2b int
41-
//go:notinheap
42-
T2c int
34+
T2 int
4335
//go:noinline // ERROR "misplaced compiler directive"
4436
T3 int
4537
)
@@ -61,11 +53,5 @@ func f() {
6153
_ = func() {}
6254

6355
//go:noinline // ERROR "misplaced compiler directive"
64-
// ok:
65-
//go:notinheap
6656
type T int
6757
}
68-
69-
// someday there might be a directive that can apply to type aliases, but go:notinheap doesn't.
70-
//go:notinheap // ERROR "misplaced compiler directive"
71-
type T6 = int

test/directive2.go

+4-9
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,20 @@ package main
1313

1414
//go:build bad // ERROR "misplaced compiler directive"
1515

16-
//go:notinheap // ERROR "misplaced compiler directive"
16+
//go:noinline // ERROR "misplaced compiler directive"
1717
type (
18-
T2 int //go:notinheap // ERROR "misplaced compiler directive"
18+
T2 int //go:noinline // ERROR "misplaced compiler directive"
1919
T2b int
2020
T2c int
2121
T3 int
2222
)
2323

24-
//go:notinheap // ERROR "misplaced compiler directive"
24+
//go:noinline // ERROR "misplaced compiler directive"
2525
type (
26-
//go:notinheap
2726
T4 int
2827
)
2928

30-
//go:notinheap // ERROR "misplaced compiler directive"
29+
//go:noinline // ERROR "misplaced compiler directive"
3130
type ()
3231

3332
type T5 int
@@ -53,10 +52,6 @@ func f() {
5352
const c = 1
5453

5554
_ = func() {}
56-
57-
// ok:
58-
//go:notinheap
59-
type T int
6055
}
6156

6257
// EOF

test/fixedbugs/bug515.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66

77
// Caused a gofrontend crash.
88

9+
//go:build gccgo
10+
911
package p
1012

1113
//go:notinheap
1214
type S1 struct{}
1315

1416
type S2 struct {
15-
r interface { Read([]byte) (int, error) }
17+
r interface{ Read([]byte) (int, error) }
1618
s1, s2 []byte
17-
p *S1
18-
n uintptr
19+
p *S1
20+
n uintptr
1921
}
2022

2123
var V any = S2{}

test/notinheap2.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44
// Use of this source code is governed by a BSD-style
55
// license that can be found in the LICENSE file.
66

7-
// Test walk errors for go:notinheap.
7+
// Test walk errors for not-in-heap.
8+
9+
//go:build cgo
810

911
package p
1012

11-
//go:notinheap
13+
import "runtime/cgo"
14+
1215
type nih struct {
16+
_ cgo.Incomplete
1317
next *nih
1418
}
1519

test/notinheap3.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66

77
// Test write barrier elimination for notinheap.
88

9+
//go:build cgo
10+
911
package p
1012

13+
import "runtime/cgo"
14+
1115
type t1 struct {
1216
x *nih
1317
s []nih
@@ -20,8 +24,8 @@ type t2 struct {
2024
y [1024]byte
2125
}
2226

23-
//go:notinheap
2427
type nih struct {
28+
_ cgo.Incomplete
2529
x uintptr
2630
}
2731

test/typeparam/mdempsky/11.go

-16
This file was deleted.

0 commit comments

Comments
 (0)