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: make "struct initializer" errors more specific #23732

Closed
kschon opened this issue Feb 7, 2018 · 2 comments

Comments

Projects
None yet
5 participants
@kschon
Copy link

commented Feb 7, 2018

Please answer these questions before submitting your issue. Thanks!

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

go version go1.9.3 linux/amd64

Does this issue reproduce with the latest release?

1.9.3 is the latest version.

What operating system and processor architecture are you using (go env)?

amd64
linux

What did you do?

Try building this program:

1 package foo
2
3 import (
4 )
5
6 type Foo struct{
7 A int
8 B int
9 C interface{}
10 }
11
12 type Bar struct{
13 A string
14 }
15
16 func main() {
17 f := Foo{
18 1,
19 2,
20 3,
21 Bar{"hello"},
22 }
23 }

What did you expect to see?

An error message that leads me straight to the problem (i.e. that the Foo initialization has the wrong number of args).

What did you see instead?

./test.go:21:6: too many values in struct initializer

This is ambiguous, as it could apply to Foo or Bar. I had a real conundrum with a much larger struct whose definition was in another file, and two of us spent a long time trying to figure out what was wrong with Bar before realizing that it was embedded in a larger struct, and coincidentally on the last line. Could the error message be changed to say the following?
./test.go:21:6: too many values in initializer of struct Foo

Similarly, if you delete lines 19-20, you get:
./test.go:19:6: too few values in struct initializer

which has the same problem.

@mvdan mvdan changed the title Could build error specify which struct has the wrong number of args? cmd/compile: make "struct initializer" errors more specific Feb 7, 2018

@mvdan

This comment has been minimized.

Copy link
Member

commented Feb 7, 2018

@ianlancetaylor ianlancetaylor added this to the Go1.11 milestone Feb 7, 2018

@odeke-em odeke-em self-assigned this Feb 11, 2018

@gopherbot

This comment has been minimized.

Copy link

commented Feb 11, 2018

Change https://golang.org/cl/93277 mentions this issue: cmd/compile: report the struct type in invalid number of initializer values

@gopherbot gopherbot closed this in 25d22d9 Feb 13, 2018

@golang golang locked and limited conversation to collaborators Feb 13, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.