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: provide better error message when directly assigning to struct field in map #13779

Closed
dsnet opened this issue Dec 30, 2015 · 2 comments
Milestone

Comments

@dsnet
Copy link
Member

dsnet commented Dec 30, 2015

Using go1.5

Compile the following code:

package main

func main() {
    type person struct{ age, weight, height int }
    students := map[string]person{"sally": person{12, 50, 32}}
    students["sally"].age = 3
}

Currently, I see the error:
prog.go:6: cannot assign to students["sally"].age
While the message tells you what went wrong, it doesn't tell you why it is wrong. It causes a programmer to scratch his/her head wondering why this is happening. "Did I misspell the field name?" "Is the map using the wrong type?" etc...

I expect to see something more informative:
prog.go:6: cannot directly assign to struct field students["sally"].age in map

This is related to #3117

@ianlancetaylor ianlancetaylor added this to the Go1.7 milestone Dec 31, 2015
@dr2chase dr2chase self-assigned this Jan 2, 2016
@gopherbot
Copy link

CL https://golang.org/cl/21462 mentions this issue.

@gopherbot
Copy link

CL https://golang.org/cl/21463 mentions this issue.

gopherbot pushed a commit that referenced this issue Apr 4, 2016
Identify this assignment case and instead of the more general error

    prog.go:6: cannot assign to students["sally"].age (value of type int)

produce

    prog.go:6: cannot directly assign to struct field students["sally"].age in map

that explains why the assignment is not possible. Used ExprString
instead of String of operand since the type of the field is not relevant
to the error.

Updates #13779.

Change-Id: I581251145ae6336ddd181b9ddd77f657c51b5aff
Reviewed-on: https://go-review.googlesource.com/21463
Reviewed-by: David Chase <drchase@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
@golang golang locked and limited conversation to collaborators Apr 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants