Matthew Dempsky has uploaded this change for review.
cmd/compile: restrict use of unsafe.{Add,Slice} to go1.17 or newer
This CL only fixes the legacy typechecker, because it's the only one
applicable to Go 1.17: go/types doesn't have a knob for controlling
supported language version, and types2 integration isn't complete in
Go 1.17 anyway. Instead, it's added to types2's known-failures list to
be addressed for Go 1.18.
Fixes #46525.
Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
A test/fixedbugs/issue46525.go
M test/run.go
3 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/src/cmd/compile/internal/typecheck/func.go b/src/cmd/compile/internal/typecheck/func.go
index f381e1d..9fa9537 100644
--- a/src/cmd/compile/internal/typecheck/func.go
+++ b/src/cmd/compile/internal/typecheck/func.go
@@ -981,6 +981,9 @@
// tcUnsafeAdd typechecks an OUNSAFEADD node.
func tcUnsafeAdd(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Add")
+ }
n.X = AssignConv(Expr(n.X), types.Types[types.TUNSAFEPTR], "argument to unsafe.Add")
n.Y = DefaultLit(Expr(n.Y), types.Types[types.TINT])
if n.X.Type() == nil || n.Y.Type() == nil {
@@ -997,6 +1000,9 @@
// tcUnsafeSlice typechecks an OUNSAFESLICE node.
func tcUnsafeSlice(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Slice")
+ }
n.X = Expr(n.X)
n.Y = Expr(n.Y)
if n.X.Type() == nil || n.Y.Type() == nil {
diff --git a/test/fixedbugs/issue46525.go b/test/fixedbugs/issue46525.go
new file mode 100644
index 0000000..164e147
--- /dev/null
+++ b/test/fixedbugs/issue46525.go
@@ -0,0 +1,14 @@
+// errorcheck -lang=go1.16
+
+// 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.
+
+package p
+
+import "unsafe"
+
+func main() {
+ _ = unsafe.Add(unsafe.Pointer(nil), 0) // ERROR "unsafe.Add requires go1.17 or later"
+ _ = unsafe.Slice(new(byte), 1) // ERROR "unsafe.Slice requires go1.17 or later"
+}
diff --git a/test/run.go b/test/run.go
index 5e60de7..cc82bd7 100644
--- a/test/run.go
+++ b/test/run.go
@@ -1995,6 +1995,7 @@
"fixedbugs/issue42058b.go": true, // types2 doesn't report "channel element type too large"
"fixedbugs/issue4232.go": true, // types2 reports (correct) extra errors
"fixedbugs/issue4452.go": true, // types2 reports (correct) extra errors
+ "fixedbugs/issue46525.go": true, // types2 doesn't check language version for unsafe.{Add,Slice}
"fixedbugs/issue5609.go": true, // types2 needs a better error message
"fixedbugs/issue6889.go": true, // types2 can handle this without constant overflow
"fixedbugs/issue7525.go": true, // types2 reports init cycle error on different line - ok otherwise
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor.
1 comment:
Patchset:
Evidently go/types has language version support in Go 1.17, just not exposed. I'll fix that and update the CL shortly.
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Matthew Dempsky.
Matthew Dempsky uploaded patch set #2 to this change.
cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer
This CL updates cmd/compile (including types2) and go/types to report
errors about using unsafe.Add and unsafe.Slice when language
compatibility is set to Go 1.16 or older.
Fixes #46525.
Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
M src/cmd/compile/internal/types2/builtins.go
M src/go/types/builtins.go
A test/fixedbugs/issue46525.go
4 files changed, 40 insertions(+), 0 deletions(-)
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Robert Findley, Robert Griesemer, Ian Lance Taylor.
Matthew Dempsky uploaded patch set #3 to this change.
cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer
This CL updates cmd/compile (including types2) and go/types to report
errors about using unsafe.Add and unsafe.Slice when language
compatibility is set to Go 1.16 or older.
Fixes #46525.
Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
---
M src/cmd/compile/internal/typecheck/func.go
M src/cmd/compile/internal/types2/builtins.go
M src/go/types/builtins.go
A test/fixedbugs/issue46525.go
4 files changed, 46 insertions(+), 0 deletions(-)
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Robert Griesemer, Ian Lance Taylor, Matthew Dempsky.
Patch set 3:Code-Review +1
1 comment:
Patchset:
LGTM for the go/types change. I'll let Ian and Robert review for the compiler, and for the behavior with respect to -lang.
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.
Attention is currently required from: Ian Lance Taylor, Matthew Dempsky.
Patch set 3:Code-Review +2
Matthew Dempsky submitted this change.
cmd/compile,go/types: restrict use of unsafe.{Add,Slice} to go1.17 or newer
This CL updates cmd/compile (including types2) and go/types to report
errors about using unsafe.Add and unsafe.Slice when language
compatibility is set to Go 1.16 or older.
Fixes #46525.
Change-Id: I1bfe025a672d9f4b929f443064ad1effd38d0363
Reviewed-on: https://go-review.googlesource.com/c/go/+/324369
Run-TryBot: Matthew Dempsky <mdem...@google.com>
Reviewed-by: Robert Findley <rfin...@google.com>
Reviewed-by: Robert Griesemer <g...@golang.org>
TryBot-Result: Go Bot <go...@golang.org>
Trust: Matthew Dempsky <mdem...@google.com>
---
M src/cmd/compile/internal/typecheck/func.go
M src/cmd/compile/internal/types2/builtins.go
M src/go/types/builtins.go
A test/fixedbugs/issue46525.go
4 files changed, 46 insertions(+), 0 deletions(-)
diff --git a/src/cmd/compile/internal/typecheck/func.go b/src/cmd/compile/internal/typecheck/func.go
index f381e1d..a6dfbbf 100644
--- a/src/cmd/compile/internal/typecheck/func.go
+++ b/src/cmd/compile/internal/typecheck/func.go
@@ -981,6 +981,12 @@
// tcUnsafeAdd typechecks an OUNSAFEADD node.
func tcUnsafeAdd(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Add")
+ n.SetType(nil)
+ return n
+ }
+
n.X = AssignConv(Expr(n.X), types.Types[types.TUNSAFEPTR], "argument to unsafe.Add")
n.Y = DefaultLit(Expr(n.Y), types.Types[types.TINT])
if n.X.Type() == nil || n.Y.Type() == nil {
@@ -997,6 +1003,12 @@
// tcUnsafeSlice typechecks an OUNSAFESLICE node.
func tcUnsafeSlice(n *ir.BinaryExpr) *ir.BinaryExpr {
+ if !types.AllowsGoVersion(curpkg(), 1, 17) {
+ base.ErrorfVers("go1.17", "unsafe.Slice")
+ n.SetType(nil)
+ return n
+ }
+
n.X = Expr(n.X)
n.Y = Expr(n.Y)
if n.X.Type() == nil || n.Y.Type() == nil {
diff --git a/src/cmd/compile/internal/types2/builtins.go b/src/cmd/compile/internal/types2/builtins.go
index b9e178d..f90e06f 100644
--- a/src/cmd/compile/internal/types2/builtins.go
+++ b/src/cmd/compile/internal/types2/builtins.go
@@ -579,6 +579,11 @@
case _Add:
// unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
+ if !check.allowVersion(check.pkg, 1, 17) {
+ check.error(call.Fun, "unsafe.Add requires go1.17 or later")
+ return
+ }
+
check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
if x.mode == invalid {
return
@@ -675,6 +680,11 @@
case _Slice:
// unsafe.Slice(ptr *T, len IntegerType) []T
+ if !check.allowVersion(check.pkg, 1, 17) {
+ check.error(call.Fun, "unsafe.Slice requires go1.17 or later")
+ return
+ }
+
typ := asPointer(x.typ)
if typ == nil {
check.errorf(x, invalidArg+"%s is not a pointer", x)
diff --git a/src/go/types/builtins.go b/src/go/types/builtins.go
index 739051c..2a2d54d 100644
--- a/src/go/types/builtins.go
+++ b/src/go/types/builtins.go
@@ -588,6 +588,11 @@
case _Add:
// unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
+ if !check.allowVersion(check.pkg, 1, 17) {
+ check.errorf(call.Fun, _InvalidUnsafeAdd, "unsafe.Add requires go1.17 or later")
+ return
+ }
+
check.assignment(x, Typ[UnsafePointer], "argument to unsafe.Add")
if x.mode == invalid {
return
@@ -684,6 +689,11 @@
case _Slice:
// unsafe.Slice(ptr *T, len IntegerType) []T
+ if !check.allowVersion(check.pkg, 1, 17) {
+ check.errorf(call.Fun, _InvalidUnsafeSlice, "unsafe.Slice requires go1.17 or later")
+ return
+ }
+
typ := asPointer(x.typ)
if typ == nil {
check.invalidArg(x, _InvalidUnsafeSlice, "%s is not a pointer", x)
diff --git a/test/fixedbugs/issue46525.go b/test/fixedbugs/issue46525.go
new file mode 100644
index 0000000..164e147
--- /dev/null
+++ b/test/fixedbugs/issue46525.go
@@ -0,0 +1,14 @@
+// errorcheck -lang=go1.16
+
+// 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.
+
+package p
+
+import "unsafe"
+
+func main() {
+ _ = unsafe.Add(unsafe.Pointer(nil), 0) // ERROR "unsafe.Add requires go1.17 or later"
+ _ = unsafe.Slice(new(byte), 1) // ERROR "unsafe.Slice requires go1.17 or later"
+}
To view, visit change 324369. To unsubscribe, or for help writing mail filters, visit settings.