Skip to content

Commit 2731673

Browse files
cuonglmgopherbot
authored andcommitted
types2, go/types: fix type checking of ~[]E passing to unsafe builtins
Fixes #64406 Change-Id: I58002ad722a229fe6db0be08d745fbad86048c6d Reviewed-on: https://go-review.googlesource.com/c/go/+/545395 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> Auto-Submit: Robert Griesemer <[email protected]>
1 parent 636c6e3 commit 2731673

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/cmd/compile/internal/types2/builtins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
799799
// unsafe.Slice(ptr *T, len IntegerType) []T
800800
check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
801801

802-
ptr, _ := under(x.typ).(*Pointer) // TODO(gri) should this be coreType rather than under?
802+
ptr, _ := coreType(x.typ).(*Pointer)
803803
if ptr == nil {
804804
check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
805805
return
@@ -820,7 +820,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
820820
// unsafe.SliceData(slice []T) *T
821821
check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
822822

823-
slice, _ := under(x.typ).(*Slice) // TODO(gri) should this be coreType rather than under?
823+
slice, _ := coreType(x.typ).(*Slice)
824824
if slice == nil {
825825
check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
826826
return

src/go/types/builtins.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
798798
// unsafe.Slice(ptr *T, len IntegerType) []T
799799
check.verifyVersionf(call.Fun, go1_17, "unsafe.Slice")
800800

801-
ptr, _ := under(x.typ).(*Pointer) // TODO(gri) should this be coreType rather than under?
801+
ptr, _ := coreType(x.typ).(*Pointer)
802802
if ptr == nil {
803803
check.errorf(x, InvalidUnsafeSlice, invalidArg+"%s is not a pointer", x)
804804
return
@@ -819,7 +819,7 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
819819
// unsafe.SliceData(slice []T) *T
820820
check.verifyVersionf(call.Fun, go1_20, "unsafe.SliceData")
821821

822-
slice, _ := under(x.typ).(*Slice) // TODO(gri) should this be coreType rather than under?
822+
slice, _ := coreType(x.typ).(*Slice)
823823
if slice == nil {
824824
check.errorf(x, InvalidUnsafeSliceData, invalidArg+"%s is not a slice", x)
825825
return
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2023 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package issue64406
6+
7+
import (
8+
"unsafe"
9+
)
10+
11+
func sliceData[E any, S ~[]E](s S) *E {
12+
return unsafe.SliceData(s)
13+
}
14+
15+
func slice[E any, S ~*E](s S) []E {
16+
return unsafe.Slice(s, 0)
17+
}
18+
19+
func f() {
20+
s := []uint32{0}
21+
_ = sliceData(s)
22+
_ = slice(&s)
23+
}

0 commit comments

Comments
 (0)