Skip to content

Commit f403764

Browse files
committed
reflect: Error if Value.Addr().Elem.CanSet() diff from Value.CanSet()
1 parent df901bc commit f403764

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/reflect/all_test.go

Lines changed: 9 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 taking Addr() of current value
353354
index []int
354355
canSet bool
355356
}
@@ -362,6 +363,7 @@ func TestCanSetField(t *testing.T) {
362363
{[]int{0}, false},
363364
{[]int{0, 0}, false},
364365
{[]int{0, 1}, true},
366+
{[]int{0, -1, 1}, true},
365367
{[]int{1}, false},
366368
{[]int{2}, true},
367369
},
@@ -371,6 +373,7 @@ func TestCanSetField(t *testing.T) {
371373
{[]int{0}, false},
372374
{[]int{0, 0}, false},
373375
{[]int{0, 1}, true},
376+
{[]int{0, -1, 1}, true},
374377
{[]int{1}, false},
375378
{[]int{2}, true},
376379
},
@@ -380,6 +383,7 @@ func TestCanSetField(t *testing.T) {
380383
{[]int{0}, true},
381384
{[]int{0, 0}, false},
382385
{[]int{0, 1}, true},
386+
{[]int{0, -1, 1}, true},
383387
{[]int{1}, false},
384388
{[]int{2}, true},
385389
},
@@ -402,7 +406,11 @@ func TestCanSetField(t *testing.T) {
402406
if f.Kind() == Ptr {
403407
f = f.Elem()
404408
}
405-
f = f.Field(i)
409+
if i == -1 {
410+
f = f.Addr().Elem()
411+
} else {
412+
f = f.Field(i)
413+
}
406414
}
407415
if got := f.CanSet(); got != tc.canSet {
408416
t.Errorf("CanSet() = %v, want %v", got, tc.canSet)

0 commit comments

Comments
 (0)