Skip to content

Commit 99092f2

Browse files
committed
reflect: use TestConvert from all_test.go and make it pass
This PR removes convert_test.go which was a minimal version of this test.
1 parent 3939dce commit 99092f2

5 files changed

Lines changed: 130 additions & 410 deletions

File tree

src/internal/reflectlite/type.go

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,11 +760,6 @@ func (r *RawType) ConvertibleTo(u *RawType) bool {
760760

761761
// This logic is mostly copied from Value.CanConvert
762762

763-
// Don't need to do anything
764-
if r.underlying() == u.underlying() {
765-
return true
766-
}
767-
768763
switch r.Kind() {
769764
case Int, Int8, Int16, Int32, Int64:
770765
switch u.Kind() {
@@ -812,30 +807,41 @@ func (r *RawType) ConvertibleTo(u *RawType) bool {
812807

813808
case Pointer:
814809
// This may fail at runtime if there isn't room
815-
if u.elem().Kind() == Array {
810+
if u.elem().Kind() == Array && r.elem() == u.elem().elem() {
816811
return true
817812
}
818813

819814
case String:
820815
// bytes or runes
821-
if r.elem().Kind() == Uint8 || r.elem().Kind() == Int32 {
816+
if !r.elem().isNamed() && r.elem().Kind() == Uint8 || r.elem().Kind() == Int32 {
822817
return true
823818
}
824819

825820
}
826821

827822
case String:
828823
// bytes or runes
829-
if u.elem().Kind() == Uint8 || u.elem().Kind() == Int32 {
824+
if u.Kind() == Slice && (!u.elem().isNamed() && u.elem().Kind() == Uint8 || u.elem().Kind() == Int32) {
825+
return true
826+
}
827+
828+
case Pointer:
829+
if !r.isNamed() && u.Kind() == Pointer && !u.isNamed() && r.elem().underlying() == u.elem().underlying() {
830830
return true
831831
}
832+
}
832833

834+
if r.underlying() == u.underlying() {
835+
return true
836+
}
837+
838+
if u.Kind() == Interface && u.NumMethod() == 0 {
839+
return true
833840
}
834841

835842
// TODO(dgryski): Unimplemented
836843
// struct types
837844
// channels
838-
//
839845

840846
return false
841847

src/internal/reflectlite/value.go

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,20 @@ func (v Value) isExported() bool {
4747
return v.flags&valueFlagExported != 0
4848
}
4949

50-
func (v Value) isRO() bool {
50+
func (v Value) IsRO() bool {
5151
return v.flags&(valueFlagRO) != 0
5252
}
5353

54+
func (v *Value) MakeRO(ro bool) {
55+
if ro {
56+
v.flags |= valueFlagRO
57+
} else {
58+
v.flags &^= valueFlagRO
59+
}
60+
}
61+
5462
func (v Value) checkRO() {
55-
if v.isRO() {
63+
if v.IsRO() {
5664
panic("reflect: value is not settable")
5765
}
5866
}
@@ -297,7 +305,7 @@ func (v Value) IsValid() bool {
297305
}
298306

299307
func (v Value) CanInterface() bool {
300-
return v.isExported() && !v.isRO()
308+
return v.isExported() && !v.IsRO()
301309
}
302310

303311
func (v Value) CanAddr() bool {
@@ -1532,6 +1540,12 @@ func convertOp(src Value, typ Type) (Value, bool) {
15321540
return cvtStringRunes(src, rtype), true
15331541
}
15341542
}
1543+
1544+
case Pointer:
1545+
rtype := typ.(*RawType)
1546+
if rtype.Kind() == Pointer && !rtype.isNamed() {
1547+
return cvtDirect(src, rtype), true
1548+
}
15351549
}
15361550

15371551
// TODO(dgryski): Unimplemented:
@@ -1576,6 +1590,14 @@ func cvtFloat(v Value, t *RawType) Value {
15761590
return makeFloat(v.flags, v.Float(), t)
15771591
}
15781592

1593+
func cvtDirect(v Value, t *RawType) Value {
1594+
return Value{
1595+
typecode: t,
1596+
value: v.value,
1597+
flags: v.flags,
1598+
}
1599+
}
1600+
15791601
func cvtComplex(v Value, t *RawType) Value {
15801602
return makeComplex(v.flags, v.Complex(), t)
15811603
}
@@ -1697,7 +1719,7 @@ func cvtIntString(v Value, t *RawType) Value {
16971719
return Value{
16981720
typecode: t,
16991721
value: unsafe.Pointer(&s),
1700-
flags: v.flags | valueFlagRO,
1722+
flags: v.flags,
17011723
}
17021724
}
17031725

@@ -1710,7 +1732,7 @@ func cvtUintString(v Value, t *RawType) Value {
17101732
return Value{
17111733
typecode: t,
17121734
value: unsafe.Pointer(&s),
1713-
flags: v.flags | valueFlagRO,
1735+
flags: v.flags,
17141736
}
17151737
}
17161738

src/reflect/all_test.go

Lines changed: 87 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -4249,8 +4249,6 @@ type BytesChan chan []byte
42494249
type BytesChanRecv <-chan []byte
42504250
type BytesChanSend chan<- []byte
42514251

4252-
/*
4253-
42544252
var convertTests = []struct {
42554253
in Value
42564254
out Value
@@ -4286,7 +4284,7 @@ var convertTests = []struct {
42864284
}
42874285
}
42884286
}
4289-
*/ /*
4287+
*/
42904288
{V(int8(1)), V(int8(1))},
42914289
{V(int8(2)), V(uint8(2))},
42924290
{V(uint8(3)), V(int8(3))},
@@ -4602,37 +4600,41 @@ var convertTests = []struct {
46024600
{V((func())(nil)), V(MyFunc(nil))},
46034601
{V((MyFunc)(nil)), V((func())(nil))},
46044602

4605-
// structs with different tags
4606-
{V(struct {
4607-
x int `some:"foo"`
4608-
}{}), V(struct {
4609-
x int `some:"bar"`
4610-
}{})},
4603+
/*
4604+
4605+
// structs with different tags
4606+
{V(struct {
4607+
x int `some:"foo"`
4608+
}{}), V(struct {
4609+
x int `some:"bar"`
4610+
}{})},
46114611
4612-
{V(struct {
4613-
x int `some:"bar"`
4614-
}{}), V(struct {
4615-
x int `some:"foo"`
4616-
}{})},
4612+
{V(struct {
4613+
x int `some:"bar"`
4614+
}{}), V(struct {
4615+
x int `some:"foo"`
4616+
}{})},
46174617
4618-
{V(MyStruct{}), V(struct {
4619-
x int `some:"foo"`
4620-
}{})},
4618+
{V(MyStruct{}), V(struct {
4619+
x int `some:"foo"`
4620+
}{})},
46214621
4622-
{V(struct {
4623-
x int `some:"foo"`
4624-
}{}), V(MyStruct{})},
4622+
{V(struct {
4623+
x int `some:"foo"`
4624+
}{}), V(MyStruct{})},
46254625
4626-
{V(MyStruct{}), V(struct {
4627-
x int `some:"bar"`
4628-
}{})},
4626+
{V(MyStruct{}), V(struct {
4627+
x int `some:"bar"`
4628+
}{})},
46294629
4630-
{V(struct {
4631-
x int `some:"bar"`
4632-
}{}), V(MyStruct{})},
4630+
{V(struct {
4631+
x int `some:"bar"`
4632+
}{}), V(MyStruct{})},
46334633
4634-
{V(MyStruct1{}), V(MyStruct2{})},
4635-
{V(MyStruct2{}), V(MyStruct1{})},
4634+
4635+
{V(MyStruct1{}), V(MyStruct2{})},
4636+
{V(MyStruct2{}), V(MyStruct1{})},
4637+
*/
46364638

46374639
// can convert *byte and *MyByte
46384640
{V((*byte)(nil)), V((*MyByte)(nil))},
@@ -4674,46 +4676,59 @@ var convertTests = []struct {
46744676
{V(new(io.Reader)), V(new(io.Reader))},
46754677
{V(new(io.Writer)), V(new(io.Writer))},
46764678

4677-
// channels
4678-
{V(IntChan(nil)), V((chan<- int)(nil))},
4679-
{V(IntChan(nil)), V((<-chan int)(nil))},
4680-
{V((chan int)(nil)), V(IntChanRecv(nil))},
4681-
{V((chan int)(nil)), V(IntChanSend(nil))},
4682-
{V(IntChanRecv(nil)), V((<-chan int)(nil))},
4683-
{V((<-chan int)(nil)), V(IntChanRecv(nil))},
4684-
{V(IntChanSend(nil)), V((chan<- int)(nil))},
4685-
{V((chan<- int)(nil)), V(IntChanSend(nil))},
4686-
{V(IntChan(nil)), V((chan int)(nil))},
4687-
{V((chan int)(nil)), V(IntChan(nil))},
4688-
{V((chan int)(nil)), V((<-chan int)(nil))},
4689-
{V((chan int)(nil)), V((chan<- int)(nil))},
4690-
{V(BytesChan(nil)), V((chan<- []byte)(nil))},
4691-
{V(BytesChan(nil)), V((<-chan []byte)(nil))},
4692-
{V((chan []byte)(nil)), V(BytesChanRecv(nil))},
4693-
{V((chan []byte)(nil)), V(BytesChanSend(nil))},
4694-
{V(BytesChanRecv(nil)), V((<-chan []byte)(nil))},
4695-
{V((<-chan []byte)(nil)), V(BytesChanRecv(nil))},
4696-
{V(BytesChanSend(nil)), V((chan<- []byte)(nil))},
4697-
{V((chan<- []byte)(nil)), V(BytesChanSend(nil))},
4698-
{V(BytesChan(nil)), V((chan []byte)(nil))},
4699-
{V((chan []byte)(nil)), V(BytesChan(nil))},
4700-
{V((chan []byte)(nil)), V((<-chan []byte)(nil))},
4701-
{V((chan []byte)(nil)), V((chan<- []byte)(nil))},
4702-
4703-
// cannot convert other instances (channels)
4704-
{V(IntChan(nil)), V(IntChan(nil))},
4705-
{V(IntChanRecv(nil)), V(IntChanRecv(nil))},
4706-
{V(IntChanSend(nil)), V(IntChanSend(nil))},
4707-
{V(BytesChan(nil)), V(BytesChan(nil))},
4708-
{V(BytesChanRecv(nil)), V(BytesChanRecv(nil))},
4709-
{V(BytesChanSend(nil)), V(BytesChanSend(nil))},
4710-
4711-
// interfaces
4712-
{V(int(1)), EmptyInterfaceV(int(1))},
4713-
{V(string("hello")), EmptyInterfaceV(string("hello"))},
4714-
{V(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
4715-
{ReadWriterV(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
4716-
{V(new(bytes.Buffer)), ReadWriterV(new(bytes.Buffer))},
4679+
/*
4680+
4681+
// channels
4682+
{V(IntChan(nil)), V((chan<- int)(nil))},
4683+
{V(IntChan(nil)), V((<-chan int)(nil))},
4684+
{V((chan int)(nil)), V(IntChanRecv(nil))},
4685+
{V((chan int)(nil)), V(IntChanSend(nil))},
4686+
{V(IntChanRecv(nil)), V((<-chan int)(nil))},
4687+
{V((<-chan int)(nil)), V(IntChanRecv(nil))},
4688+
{V(IntChanSend(nil)), V((chan<- int)(nil))},
4689+
{V((chan<- int)(nil)), V(IntChanSend(nil))},
4690+
{V(IntChan(nil)), V((chan int)(nil))},
4691+
{V((chan int)(nil)), V(IntChan(nil))},
4692+
{V((chan int)(nil)), V((<-chan int)(nil))},
4693+
{V((chan int)(nil)), V((chan<- int)(nil))},
4694+
{V(BytesChan(nil)), V((chan<- []byte)(nil))},
4695+
{V(BytesChan(nil)), V((<-chan []byte)(nil))},
4696+
{V((chan []byte)(nil)), V(BytesChanRecv(nil))},
4697+
{V((chan []byte)(nil)), V(BytesChanSend(nil))},
4698+
{V(BytesChanRecv(nil)), V((<-chan []byte)(nil))},
4699+
{V((<-chan []byte)(nil)), V(BytesChanRecv(nil))},
4700+
{V(BytesChanSend(nil)), V((chan<- []byte)(nil))},
4701+
{V((chan<- []byte)(nil)), V(BytesChanSend(nil))},
4702+
{V(BytesChan(nil)), V((chan []byte)(nil))},
4703+
{V((chan []byte)(nil)), V(BytesChan(nil))},
4704+
{V((chan []byte)(nil)), V((<-chan []byte)(nil))},
4705+
{V((chan []byte)(nil)), V((chan<- []byte)(nil))},
4706+
4707+
// cannot convert other instances (channels)
4708+
{V(IntChan(nil)), V(IntChan(nil))},
4709+
{V(IntChanRecv(nil)), V(IntChanRecv(nil))},
4710+
{V(IntChanSend(nil)), V(IntChanSend(nil))},
4711+
{V(BytesChan(nil)), V(BytesChan(nil))},
4712+
{V(BytesChanRecv(nil)), V(BytesChanRecv(nil))},
4713+
{V(BytesChanSend(nil)), V(BytesChanSend(nil))},
4714+
4715+
// interfaces
4716+
{V(int(1)), EmptyInterfaceV(int(1))},
4717+
{V(string("hello")), EmptyInterfaceV(string("hello"))},
4718+
{V(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
4719+
{ReadWriterV(new(bytes.Buffer)), ReaderV(new(bytes.Buffer))},
4720+
{V(new(bytes.Buffer)), ReadWriterV(new(bytes.Buffer))},
4721+
*/
4722+
4723+
}
4724+
4725+
func IsRO(v Value) bool {
4726+
return v.IsRO()
4727+
}
4728+
4729+
func MakeRO(v Value) Value {
4730+
v.MakeRO(true)
4731+
return v
47174732
}
47184733

47194734
func TestConvert(t *testing.T) {
@@ -4804,6 +4819,8 @@ func TestConvert(t *testing.T) {
48044819
}
48054820
}
48064821

4822+
/*
4823+
48074824
func TestConvertPanic(t *testing.T) {
48084825
s := make([]byte, 4)
48094826
p := new([8]byte)
@@ -4848,6 +4865,8 @@ func TestConvertSlice2Array(t *testing.T) {
48484865
}
48494866
}
48504867
4868+
*/
4869+
48514870
var gFloat32 float32
48524871

48534872
const snan uint32 = 0x7f800001
@@ -4870,8 +4889,6 @@ func TestConvertNaNs(t *testing.T) {
48704889
}
48714890
}
48724891

4873-
*/
4874-
48754892
type ComparableStruct struct {
48764893
X int
48774894
}

0 commit comments

Comments
 (0)