]> Cypherpunks repositories - gostls13.git/commitdiff
types2, go/types: fix type checking of ~[]E passing to unsafe builtins
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Tue, 28 Nov 2023 02:50:25 +0000 (09:50 +0700)
committerGopher Robot <gobot@golang.org>
Wed, 29 Nov 2023 20:34:33 +0000 (20:34 +0000)
Fixes #64406

Change-Id: I58002ad722a229fe6db0be08d745fbad86048c6d
Reviewed-on: https://go-review.googlesource.com/c/go/+/545395
Reviewed-by: Robert Griesemer <gri@google.com>
Run-TryBot: Robert Griesemer <gri@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Robert Griesemer <gri@google.com>

src/cmd/compile/internal/types2/builtins.go
src/go/types/builtins.go
src/internal/types/testdata/fixedbugs/issue64406.go [new file with mode: 0644]

index 575a1daed208eabbff24fa06a50bf78c1f4e3021..60f6d7f4152dd2ba7cf8a92d5f3a6550bf9f6576 100644 (file)
@@ -799,7 +799,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
                // unsafe.Slice(ptr *T, len IntegerType) []T
                check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
 
-               ptr, _ := under(x.typ).(*Pointer) // TODO(gri) should this be coreType rather than under?
+               ptr, _ := coreType(x.typ).(*Pointer)
                if ptr == nil {
                        check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
                        return
@@ -820,7 +820,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
                // unsafe.SliceData(slice []T) *T
                check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
 
-               slice, _ := under(x.typ).(*Slice) // TODO(gri) should this be coreType rather than under?
+               slice, _ := coreType(x.typ).(*Slice)
                if slice == nil {
                        check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
                        return
index 3a3cee1726369654039077cdb9a6129a2d61ddad..901573661b934ea44554397da9379c1dab22475d 100644 (file)
@@ -798,7 +798,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                // unsafe.Slice(ptr *T, len IntegerType) []T
                check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
 
-               ptr, _ := under(x.typ).(*Pointer) // TODO(gri) should this be coreType rather than under?
+               ptr, _ := coreType(x.typ).(*Pointer)
                if ptr == nil {
                        check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
                        return
@@ -819,7 +819,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
                // unsafe.SliceData(slice []T) *T
                check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
 
-               slice, _ := under(x.typ).(*Slice) // TODO(gri) should this be coreType rather than under?
+               slice, _ := coreType(x.typ).(*Slice)
                if slice == nil {
                        check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
                        return
diff --git a/src/internal/types/testdata/fixedbugs/issue64406.go b/src/internal/types/testdata/fixedbugs/issue64406.go
new file mode 100644 (file)
index 0000000..54b959d
--- /dev/null
@@ -0,0 +1,23 @@
+// Copyright 2023 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 issue64406
+
+import (
+       "unsafe"
+)
+
+func sliceData[E any, S ~[]E](s S) *E {
+       return unsafe.SliceData(s)
+}
+
+func slice[E any, S ~*E](s S) []E {
+       return unsafe.Slice(s, 0)
+}
+
+func f() {
+       s := []uint32{0}
+       _ = sliceData(s)
+       _ = slice(&s)
+}