aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2022-11-03 11:56:43 -0700
committerMichael Knyszek <mknyszek@google.com>2022-11-09 18:44:17 +0000
commitd2a7a180d38d4a51eb5d8f86bb71bed0ecee09ce (patch)
tree82214a19928f8cc847c8713fa68e91f022c477eb
parentca571803a87968b280d74b03cbc820ecf68e069d (diff)
downloadgo-d2a7a180d38d4a51eb5d8f86bb71bed0ecee09ce.tar.gz
go-d2a7a180d38d4a51eb5d8f86bb71bed0ecee09ce.zip
[release-branch.go1.18] 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 #55889. Fixes #56556. Change-Id: Id762fff96fd13ba0f1e696929a9e276dfcba2620 Reviewed-on: https://go-review.googlesource.com/c/go/+/447755 TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Matthew Dempsky <mdempsky@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Reviewed-on: https://go-review.googlesource.com/c/go/+/447815
-rw-r--r--src/cmd/compile/internal/noder/noder.go4
-rw-r--r--test/fixedbugs/issue55889.go21
2 files changed, 24 insertions, 1 deletions
diff --git a/src/cmd/compile/internal/noder/noder.go b/src/cmd/compile/internal/noder/noder.go
index b36db67a507..17ec87788a6 100644
--- a/src/cmd/compile/internal/noder/noder.go
+++ b/src/cmd/compile/internal/noder/noder.go
@@ -323,7 +323,9 @@ func (p *noder) processPragmas() {
}
n := ir.AsNode(typecheck.Lookup(l.local).Def)
if n == nil || n.Op() != ir.ONAME {
- p.errorAt(l.pos, "//go:linkname must refer to declared function or variable")
+ if types.AllowsGoVersion(types.LocalPkg, 1, 18) {
+ p.errorAt(l.pos, "//go:linkname must refer to declared function or variable")
+ }
continue
}
if n.Sym().Linkname != "" {
diff --git a/test/fixedbugs/issue55889.go b/test/fixedbugs/issue55889.go
new file mode 100644
index 00000000000..68812c2157b
--- /dev/null
+++ b/test/fixedbugs/issue55889.go
@@ -0,0 +1,21 @@
+// errorcheck -0 -lang=go1.17
+
+// Copyright 2022 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.
+
+// Prior to Go 1.18, ineffectual //go:linkname directives were treated
+// as noops. Ensure that modules that contain these directives (e.g.,
+// x/sys prior to go.dev/cl/274573) continue to compile.
+
+package p
+
+import _ "unsafe"
+
+//go:linkname nonexistent nonexistent
+
+//go:linkname constant constant
+const constant = 42
+
+//go:linkname typename typename
+type typename int