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.)

Fixes #55889.

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>
This commit is contained in:
Matthew Dempsky 2022-11-03 11:56:43 -07:00
parent 48ff5c1042
commit aa6240a445
3 changed files with 28 additions and 2 deletions

View file

@ -132,7 +132,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(1, 18) {
p.errorAt(l.pos, "//go:linkname must refer to declared function or variable")
}
continue
}
if n.Sym().Linkname != "" {

View file

@ -11,6 +11,7 @@ import (
"cmd/compile/internal/base"
"cmd/compile/internal/ir"
"cmd/compile/internal/syntax"
"cmd/compile/internal/types"
"cmd/compile/internal/types2"
)
@ -2437,7 +2438,9 @@ func (pw *pkgWriter) collectDecls(noders []*noder) {
}
default:
pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
if types.AllowsGoVersion(1, 18) {
pw.errorf(l.pos, "//go:linkname must refer to declared function or variable")
}
}
}
}

View file

@ -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