Skip to content

reflect: Calling reflect.SetMapIndex(k, v) causes mutations to k if k is removed in the map #7896

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
gopherbot opened this issue Apr 29, 2014 · 4 comments
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Milestone

Comments

@gopherbot
Copy link
Contributor

by gmadrid:

See code at http://play.golang.org/p/MIkFP_Zrxb

What does 'go version' print?
go version go1.2.1 darwin/amd64

What steps reproduce the problem?
Calling reflect.SetMapIndex() causes changes to the key that prevent future calls to
SetMapIndex from working.

If possible, include a link to a program on play.golang.org.
http://play.golang.org/p/MIkFP_Zrxb

What happened?
1. Run the linked code.
2. Notice that the key after the call to SetMapIndex is different
3. Notice that the second call to SetMapIndex doesn't work.

What should have happened instead?
The key should be unchanged and both calls to SetMapIndex should work.


Code copied below: 

package main

import (
    "fmt"
    "reflect"
)

func main() {
    m1 := map[string]bool{"a": true, "b": true}
    m2 := map[string]bool{"a": true, "b": true}

    fmt.Println(m1)

    v1 := reflect.ValueOf(m1)
    k := v1.MapKeys()[0]

    fmt.Println("KEY BEFORE", k)
    v1.SetMapIndex(k, reflect.Value{})  // COMMENT THIS OUT
    fmt.Println("m1:", m1)
    fmt.Println("KEY AFTER", k)

    v2 := reflect.ValueOf(m2)
    v2.SetMapIndex(k, reflect.Value{})
    fmt.Println("KEY AFTER SECOND CALL", k)
    fmt.Println("m2:", m2)
}
@minux
Copy link
Member

minux commented Apr 29, 2014

Comment 1:

http://play.golang.org/p/EtZmoIyYPz
Because you removed key "a" from the map, it makes sense that the prior key value isn't
valid anymore.
I think this is a documentation issue.

Labels changed: added release-go1.3maybe, repo-main, documentation.

Status changed to Accepted.

@ianlancetaylor
Copy link
Contributor

Comment 2:

I do think this is a bug, but it seems to be fixed on tip.

Status changed to Fixed.

@gopherbot
Copy link
Contributor Author

Comment 3 by gmadrid:

If this is WAI, it's a pretty awkward API. The behavior of SetMapIndex depends on 1) the
origin of the key value, and 2) the order of the SetMapIndex calls. Both of these could
lead to some very difficult to find bugs.
http://play.golang.org/p/8HY4i0N8_z

@gopherbot
Copy link
Contributor Author

Comment 4 by gmadrid:

Sure enough. Looks like it was fixed here:
https://code.google.com/p/go/source/detail?r=ecccf07e7f9d341ca577433b794ab8061fbdc092&path=/src/pkg/reflect/value.go
I can work around this by deleting from the maps in a different order. 
Thanks, y'all.

@gopherbot gopherbot added fixed Documentation Issues describing a change to documentation. labels Apr 30, 2014
@rsc rsc added this to the Go1.3 milestone Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Documentation Issues describing a change to documentation. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants