Skip to content
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

cmd/compile: return LITERAL on INDEXMAP when map and key are LITERALs #35763

Open
Segflow opened this issue Nov 21, 2019 · 1 comment · May be fixed by #35764
Milestone

Comments

@Segflow
Copy link

@Segflow Segflow commented Nov 21, 2019

Given

package main
const (
	key = "literalKey"
)
func main() {
	v := map[string]string{
		"literalKey": "v1",
		"anotherkey": "v2",
	}[key]
	_ = v
}

When compiled, the compiler will add the different map initialisations methods and assign the different values, then call mapaccess1.

I believe this should not be the case when the map is an OMAPLIT and the key is a LITERAL defined in the map.

The compiler at compile-time can detect this and change the INDEXMAP operation to LITERAL.

The above code should be similar to this one:

package main
const (
	key = "literalKey"
)
func main() {
	v := "v1"
	_ = v
}

What version of Go are you using (go version)?

$ go version
go version go1.13.4 darwin/amd64
Segflow added a commit to Segflow/go that referenced this issue Nov 21, 2019
Currently if a MAPINDEX is done on a MAPLIT where the key is also a LITERAL
defined in the map we still build the map and assign all the keys,
where we can at compile-time change the INDEXMAP node by the LITERAL.

Fixes golang#35763
@gopherbot

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Nov 21, 2019

Change https://golang.org/cl/208323 mentions this issue: cmd/compile: return LITERAL on INDEXMAP when map and key are LITERALs

@randall77 randall77 added this to the Go1.15 milestone Nov 22, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.