Skip to content

Commit 48e8569

Browse files
committed
reflect: error if Value.Addr().Elem.CanSet() diff from Value.CanSet()
1 parent df901bc commit 48e8569

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/reflect/all_test.go

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ func TestCanSetField(t *testing.T) {
350350
}
351351

352352
type testCase struct {
353+
// -1 means Addr().Elem() of current value
353354
index []int
354355
canSet bool
355356
}
@@ -360,35 +361,65 @@ func TestCanSetField(t *testing.T) {
360361
val: ValueOf(&S1{}),
361362
cases: []testCase{
362363
{[]int{0}, false},
364+
{[]int{0, -1}, false},
363365
{[]int{0, 0}, false},
366+
{[]int{0, 0, -1}, false},
367+
{[]int{0, -1, 0}, false},
368+
{[]int{0, -1, 0, -1}, false},
364369
{[]int{0, 1}, true},
370+
{[]int{0, 1, -1}, true},
371+
{[]int{0, -1, 1}, true},
372+
{[]int{0, -1, 1, -1}, true},
365373
{[]int{1}, false},
374+
{[]int{1, -1}, false},
366375
{[]int{2}, true},
376+
{[]int{2, -1}, true},
367377
},
368378
}, {
369379
val: ValueOf(&S2{embed: &embed{}}),
370380
cases: []testCase{
371381
{[]int{0}, false},
382+
{[]int{0, -1}, false},
372383
{[]int{0, 0}, false},
384+
{[]int{0, 0, -1}, false},
385+
{[]int{0, -1, 0}, false},
386+
{[]int{0, -1, 0, -1}, false},
373387
{[]int{0, 1}, true},
388+
{[]int{0, 1, -1}, true},
389+
{[]int{0, -1, 1}, true},
390+
{[]int{0, -1, 1, -1}, true},
374391
{[]int{1}, false},
375392
{[]int{2}, true},
376393
},
377394
}, {
378395
val: ValueOf(&S3{}),
379396
cases: []testCase{
380397
{[]int{0}, true},
398+
{[]int{0, -1}, true},
381399
{[]int{0, 0}, false},
400+
{[]int{0, 0, -1}, false},
401+
{[]int{0, -1, 0}, false},
402+
{[]int{0, -1, 0, -1}, false},
382403
{[]int{0, 1}, true},
404+
{[]int{0, 1, -1}, true},
405+
{[]int{0, -1, 1}, true},
406+
{[]int{0, -1, 1, -1}, true},
383407
{[]int{1}, false},
384408
{[]int{2}, true},
385409
},
386410
}, {
387411
val: ValueOf(&S4{Embed: &Embed{}}),
388412
cases: []testCase{
389413
{[]int{0}, true},
414+
{[]int{0, -1}, true},
390415
{[]int{0, 0}, false},
416+
{[]int{0, 0, -1}, false},
417+
{[]int{0, -1, 0}, false},
418+
{[]int{0, -1, 0, -1}, false},
391419
{[]int{0, 1}, true},
420+
{[]int{0, 1, -1}, true},
421+
{[]int{0, -1, 1}, true},
422+
{[]int{0, -1, 1, -1}, true},
392423
{[]int{1}, false},
393424
{[]int{2}, true},
394425
},
@@ -402,7 +433,11 @@ func TestCanSetField(t *testing.T) {
402433
if f.Kind() == Ptr {
403434
f = f.Elem()
404435
}
405-
f = f.Field(i)
436+
if i == -1 {
437+
f = f.Addr().Elem()
438+
} else {
439+
f = f.Field(i)
440+
}
406441
}
407442
if got := f.CanSet(); got != tc.canSet {
408443
t.Errorf("CanSet() = %v, want %v", got, tc.canSet)

0 commit comments

Comments
 (0)