diff options
author | Josh Bleecher Snyder <josharian@gmail.com> | 2021-03-14 14:24:47 -0700 |
---|---|---|
committer | Josh Bleecher Snyder <josharian@gmail.com> | 2021-04-21 00:53:48 +0000 |
commit | faa4fa1a6e94fce4f6fa22524a2bece5125213b6 (patch) | |
tree | ecd5c4b860564b5b1926725f6ffafcebfac3b3d9 /test | |
parent | 1c268431f49ee2fc843eac52a0854aea3d02a6e0 (diff) | |
download | go-faa4fa1a6e94fce4f6fa22524a2bece5125213b6.tar.gz go-faa4fa1a6e94fce4f6fa22524a2bece5125213b6.zip |
cmd/compile: allow conversion from slice to array ptr
Panic if the slice is too short.
Updates #395
Change-Id: I90f4bff2da5d8f3148ba06d2482084f32b25c29a
Reviewed-on: https://go-review.googlesource.com/c/go/+/301650
Trust: Josh Bleecher Snyder <josharian@gmail.com>
Run-TryBot: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/convert2.go | 14 | ||||
-rw-r--r-- | test/convert4.go | 86 | ||||
-rw-r--r-- | test/escape_slice.go | 4 |
3 files changed, 104 insertions, 0 deletions
diff --git a/test/convert2.go b/test/convert2.go index e7044b2453..8e43967aaa 100644 --- a/test/convert2.go +++ b/test/convert2.go @@ -313,3 +313,17 @@ func _() { t = u // ERROR "cannot use .* in assignment|incompatible type" t = (*T)(u) // ERROR "cannot convert" } + +func _() { + var s []byte + _ = ([4]byte)(s) // ERROR "cannot convert" + _ = (*[4]byte)(s) + + type A [4]byte + _ = (A)(s) // ERROR "cannot convert" + _ = (*A)(s) + + type P *[4]byte + _ = (P)(s) + _ = (*P)(s) // ERROR "cannot convert" +} diff --git a/test/convert4.go b/test/convert4.go new file mode 100644 index 0000000000..2bc9c96a52 --- /dev/null +++ b/test/convert4.go @@ -0,0 +1,86 @@ +// run + +// Copyright 2020 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. + +// Test conversion from slice to array pointer. + +package main + +func wantPanic(fn func(), s string) { + defer func() { + err := recover() + if err == nil { + panic("expected panic") + } + if got := err.(error).Error(); got != s { + panic("expected panic " + s + " got " + got) + } + }() + fn() +} + +func main() { + s := make([]byte, 8, 10) + if p := (*[8]byte)(s); &p[0] != &s[0] { + panic("*[8]byte conversion failed") + } + wantPanic( + func() { + _ = (*[9]byte)(s) + }, + "runtime error: cannot convert slice with length 8 to pointer to array with length 9", + ) + + var n []byte + if p := (*[0]byte)(n); p != nil { + panic("nil slice converted to *[0]byte should be nil") + } + + z := make([]byte, 0) + if p := (*[0]byte)(z); p == nil { + panic("empty slice converted to *[0]byte should be non-nil") + } + + // Test with named types + type Slice []int + type Int4 [4]int + type PInt4 *[4]int + ii := make(Slice, 4) + if p := (*Int4)(ii); &p[0] != &ii[0] { + panic("*Int4 conversion failed") + } + if p := PInt4(ii); &p[0] != &ii[0] { + panic("PInt4 conversion failed") + } +} + +// test static variable conversion + +var ( + ss = make([]string, 10) + s5 = (*[5]string)(ss) + s10 = (*[10]string)(ss) + + ns []string + ns0 = (*[0]string)(ns) + + zs = make([]string, 0) + zs0 = (*[0]string)(zs) +) + +func init() { + if &ss[0] != &s5[0] { + panic("s5 conversion failed") + } + if &ss[0] != &s10[0] { + panic("s5 conversion failed") + } + if ns0 != nil { + panic("ns0 should be nil") + } + if zs0 == nil { + panic("zs0 should not be nil") + } +} diff --git a/test/escape_slice.go b/test/escape_slice.go index 6ce852e9c5..d60414736c 100644 --- a/test/escape_slice.go +++ b/test/escape_slice.go @@ -101,6 +101,10 @@ func slice11() { _ = s } +func slice12(x []int) *[1]int { // ERROR "leaking param: x to result ~r1 level=0$" + return (*[1]int)(x) +} + func envForDir(dir string) []string { // ERROR "dir does not escape" env := os.Environ() return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string{...} does not escape" |