Skip to content

Commit b65eeb8

Browse files
mdempskyandrew-d
authored andcommitted
[release-branch.go1.19] cmd/compile: allow ineffectual //go:linkname in -lang=go1.17 and older
Prior to Go 1.18, ineffectual //go:linkname directives (i.e., directives referring to an undeclared name, or to a declared type or constant) were treated as noops. In Go 1.18, we changed this into a compiler error to mitigate accidental misuse. However, the x/sys repo contained ineffectual //go:linkname directives up until go.dev/cl/274573, which has caused a lot of user confusion. It seems a bit late to worry about now, but to at least prevent further user pain, this CL changes the error message to only apply to modules using "go 1.18" or newer. (The x/sys repo declared "go 1.12" at the time go.dev/cl/274573 was submitted.) For golang#55889. Fixes golang#56557. Change-Id: Id762fff96fd13ba0f1e696929a9e276dfcba2620 Reviewed-on: https://go-review.googlesource.com/c/go/+/447755 TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Matthew Dempsky <[email protected]> Reviewed-by: Bryan Mills <[email protected]> Reviewed-on: https://go-review.googlesource.com/c/go/+/447816
1 parent 39fbccc commit b65eeb8

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ func (p *noder) processPragmas() {
132132
}
133133
n := ir.AsNode(typecheck.Lookup(l.local).Def)
134134
if n == nil || n.Op() != ir.ONAME {
135-
p.errorAt(l.pos, "//go:linkname must refer to declared function or variable")
135+
if types.AllowsGoVersion(1, 18) {
136+
p.errorAt(l.pos, "//go:linkname must refer to declared function or variable")
137+
}
136138
continue
137139
}
138140
if n.Sym().Linkname != "" {

test/fixedbugs/issue55889.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// errorcheck -0 -lang=go1.17
2+
3+
// Copyright 2022 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// Prior to Go 1.18, ineffectual //go:linkname directives were treated
8+
// as noops. Ensure that modules that contain these directives (e.g.,
9+
// x/sys prior to go.dev/cl/274573) continue to compile.
10+
11+
package p
12+
13+
import _ "unsafe"
14+
15+
//go:linkname nonexistent nonexistent
16+
17+
//go:linkname constant constant
18+
const constant = 42
19+
20+
//go:linkname typename typename
21+
type typename int

0 commit comments

Comments
 (0)