1
0
mirror of https://github.com/golang/go synced 2024-11-17 02:14:42 -07:00

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 <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>
This commit is contained in:
Cuong Manh Le 2023-11-28 09:50:25 +07:00 committed by Gopher Robot
parent 636c6e350d
commit 27316739ac
3 changed files with 27 additions and 4 deletions

View 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

View 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

View File

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