Skip to content

Commit 760d3b2

Browse files
committed
reflect: allow conversion from slice to array ptr
Note that this removes an invariant: v.Type().ConvertibleTo(t) might return true, yet v.Convert(t) might panic nevertheless. This is a fairly unavoidable consequence of the decision to add the first-ever conversion that can panic. ConvertibleTo describes a relationship between types, but whether the conversion panics now depends on the value, not just the type. If this turns out to be a problem, we can add v.ConvertibleTo(t), or something similar, to allow callers to avoid the panic. This is the last of the changes needed to complete the implementation. Fixes #395 Change-Id: I79b7e4dd87a67a47723e00a65d0b1ac6090371b7 Reviewed-on: https://go-review.googlesource.com/c/go/+/301652 Trust: Josh Bleecher Snyder <[email protected]> Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent c187443 commit 760d3b2

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

src/reflect/all_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -3822,6 +3822,10 @@ type MyStruct2 struct {
38223822
}
38233823
type MyString string
38243824
type MyBytes []byte
3825+
type MyBytesArrayPtr0 *[0]byte
3826+
type MyBytesArrayPtr *[4]byte
3827+
type MyBytesArray0 [0]byte
3828+
type MyBytesArray [4]byte
38253829
type MyRunes []int32
38263830
type MyFunc func()
38273831
type MyByte byte
@@ -4128,6 +4132,30 @@ var convertTests = []struct {
41284132
{V(MyString("runes♝")), V(MyRunes("runes♝"))},
41294133
{V(MyRunes("runes♕")), V(MyString("runes♕"))},
41304134

4135+
// slice to array pointer
4136+
{V([]byte(nil)), V((*[0]byte)(nil))},
4137+
{V([]byte{}), V(new([0]byte))},
4138+
{V([]byte{7}), V(&[1]byte{7})},
4139+
{V(MyBytes([]byte(nil))), V((*[0]byte)(nil))},
4140+
{V(MyBytes([]byte{})), V(new([0]byte))},
4141+
{V(MyBytes([]byte{9})), V(&[1]byte{9})},
4142+
{V([]byte(nil)), V(MyBytesArrayPtr0(nil))},
4143+
{V([]byte{}), V(MyBytesArrayPtr0(new([0]byte)))},
4144+
{V([]byte{1, 2, 3, 4}), V(MyBytesArrayPtr(&[4]byte{1, 2, 3, 4}))},
4145+
{V(MyBytes([]byte{})), V(MyBytesArrayPtr0(new([0]byte)))},
4146+
{V(MyBytes([]byte{5, 6, 7, 8})), V(MyBytesArrayPtr(&[4]byte{5, 6, 7, 8}))},
4147+
4148+
{V([]byte(nil)), V((*MyBytesArray0)(nil))},
4149+
{V([]byte{}), V((*MyBytesArray0)(new([0]byte)))},
4150+
{V([]byte{1, 2, 3, 4}), V(&MyBytesArray{1, 2, 3, 4})},
4151+
{V(MyBytes([]byte(nil))), V((*MyBytesArray0)(nil))},
4152+
{V(MyBytes([]byte{})), V((*MyBytesArray0)(new([0]byte)))},
4153+
{V(MyBytes([]byte{5, 6, 7, 8})), V(&MyBytesArray{5, 6, 7, 8})},
4154+
{V(new([0]byte)), V(new(MyBytesArray0))},
4155+
{V(new(MyBytesArray0)), V(new([0]byte))},
4156+
{V(MyBytesArrayPtr0(nil)), V((*[0]byte)(nil))},
4157+
{V((*[0]byte)(nil)), V(MyBytesArrayPtr0(nil))},
4158+
41314159
// named types and equal underlying types
41324160
{V(new(int)), V(new(integer))},
41334161
{V(new(integer)), V(new(int))},
@@ -4288,6 +4316,9 @@ func TestConvert(t *testing.T) {
42884316
if vout2.Type() != tt.out.Type() || !DeepEqual(out2, tt.out.Interface()) {
42894317
t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) = %T(%[3]v), want %T(%[4]v)", tt.in.Interface(), t2, out2, tt.out.Interface())
42904318
}
4319+
if got, want := vout2.Kind(), vout2.Type().Kind(); got != want {
4320+
t.Errorf("ValueOf(%T(%[1]v)).Convert(%s) has internal kind %v want %v", tt.in.Interface(), t1, got, want)
4321+
}
42914322

42924323
// vout3 represents a new value of the out type, set to vout2. This makes
42934324
// sure the converted value vout2 is really usable as a regular value.
@@ -4332,6 +4363,19 @@ func TestConvert(t *testing.T) {
43324363
}
43334364
}
43344365

4366+
func TestConvertPanic(t *testing.T) {
4367+
s := make([]byte, 4)
4368+
p := new([8]byte)
4369+
v := ValueOf(s)
4370+
pt := TypeOf(p)
4371+
if !v.Type().ConvertibleTo(pt) {
4372+
t.Errorf("[]byte should be convertible to *[8]byte")
4373+
}
4374+
shouldPanic("reflect: cannot convert slice with length 4 to array pointer with length 8", func() {
4375+
_ = v.Convert(pt)
4376+
})
4377+
}
4378+
43354379
var gFloat32 float32
43364380

43374381
func TestConvertNaNs(t *testing.T) {

src/reflect/type.go

+2
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,11 @@ type Type interface {
106106
AssignableTo(u Type) bool
107107

108108
// ConvertibleTo reports whether a value of the type is convertible to type u.
109+
// Even if ConvertibleTo returns true, the conversion may still panic.
109110
ConvertibleTo(u Type) bool
110111

111112
// Comparable reports whether values of this type are comparable.
113+
// Even if Comparable returns true, the comparison may still panic.
112114
Comparable() bool
113115

114116
// Methods applicable only to some types, depending on Kind.

src/reflect/value.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package reflect
66

77
import (
88
"internal/abi"
9+
"internal/itoa"
910
"internal/unsafeheader"
1011
"math"
1112
"runtime"
@@ -2770,7 +2771,7 @@ func (v Value) assignTo(context string, dst *rtype, target unsafe.Pointer) Value
27702771

27712772
// Convert returns the value v converted to type t.
27722773
// If the usual Go conversion rules do not allow conversion
2773-
// of the value v to type t, Convert panics.
2774+
// of the value v to type t, or if converting v to type t panics, Convert panics.
27742775
func (v Value) Convert(t Type) Value {
27752776
if v.flag&flagMethod != 0 {
27762777
v = makeMethodValue("Convert", v)
@@ -2841,6 +2842,11 @@ func convertOp(dst, src *rtype) func(Value, Type) Value {
28412842
return cvtRunesString
28422843
}
28432844
}
2845+
// "x is a slice, T is a pointer-to-array type,
2846+
// and the slice and array types have identical element types."
2847+
if dst.Kind() == Ptr && dst.Elem().Kind() == Array && src.Elem() == dst.Elem().Elem() {
2848+
return cvtSliceArrayPtr
2849+
}
28442850

28452851
case Chan:
28462852
if dst.Kind() == Chan && specialChannelAssignability(dst, src) {
@@ -3034,6 +3040,16 @@ func cvtStringRunes(v Value, t Type) Value {
30343040
return makeRunes(v.flag.ro(), []rune(v.String()), t)
30353041
}
30363042

3043+
// convertOp: []T -> *[N]T
3044+
func cvtSliceArrayPtr(v Value, t Type) Value {
3045+
n := t.Elem().Len()
3046+
h := (*unsafeheader.Slice)(v.ptr)
3047+
if n > h.Len {
3048+
panic("reflect: cannot convert slice with length " + itoa.Itoa(h.Len) + " to array pointer with length " + itoa.Itoa(n))
3049+
}
3050+
return Value{t.common(), h.Data, v.flag&^(flagIndir|flagAddr|flagKindMask) | flag(Ptr)}
3051+
}
3052+
30373053
// convertOp: direct copy
30383054
func cvtDirect(v Value, typ Type) Value {
30393055
f := v.flag

0 commit comments

Comments
 (0)