Skip to content

reflect: keep RO flags unchanged in Value.Addr #32787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion src/reflect/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ func TestCanSetField(t *testing.T) {
}

type testCase struct {
// -1 means Addr().Elem() of current value
index []int
canSet bool
}
Expand All @@ -360,35 +361,65 @@ func TestCanSetField(t *testing.T) {
val: ValueOf(&S1{}),
cases: []testCase{
{[]int{0}, false},
{[]int{0, -1}, false},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{1, -1}, false},
{[]int{2}, true},
{[]int{2, -1}, true},
},
}, {
val: ValueOf(&S2{embed: &embed{}}),
cases: []testCase{
{[]int{0}, false},
{[]int{0, -1}, false},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
}, {
val: ValueOf(&S3{}),
cases: []testCase{
{[]int{0}, true},
{[]int{0, -1}, true},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
}, {
val: ValueOf(&S4{Embed: &Embed{}}),
cases: []testCase{
{[]int{0}, true},
{[]int{0, -1}, true},
{[]int{0, 0}, false},
{[]int{0, 0, -1}, false},
{[]int{0, -1, 0}, false},
{[]int{0, -1, 0, -1}, false},
{[]int{0, 1}, true},
{[]int{0, 1, -1}, true},
{[]int{0, -1, 1}, true},
{[]int{0, -1, 1, -1}, true},
{[]int{1}, false},
{[]int{2}, true},
},
Expand All @@ -402,7 +433,11 @@ func TestCanSetField(t *testing.T) {
if f.Kind() == Ptr {
f = f.Elem()
}
f = f.Field(i)
if i == -1 {
f = f.Addr().Elem()
} else {
f = f.Field(i)
}
}
if got := f.CanSet(); got != tc.canSet {
t.Errorf("CanSet() = %v, want %v", got, tc.canSet)
Expand Down
5 changes: 4 additions & 1 deletion src/reflect/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ func (v Value) Addr() Value {
if v.flag&flagAddr == 0 {
panic("reflect.Value.Addr of unaddressable value")
}
return Value{v.typ.ptrTo(), v.ptr, v.flag.ro() | flag(Ptr)}
// Preserve flagRO instead of using v.flag.ro() so that
// v.Addr().Elem() is equivalent to v (#32772)
fl := v.flag & flagRO
return Value{v.typ.ptrTo(), v.ptr, fl | flag(Ptr)}
}

// Bool returns v's underlying value.
Expand Down