golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.27k stars 17.57k forks source link

cmd/compile: struct literal error prints struct definition instead of struct name #68184

Closed rsc closed 2 months ago

rsc commented 3 months ago
% cat /tmp/x.go
package main

type VeryLongStruct struct {
    A1  int
    A2  int
    A3  int
    A4  int
    A5  int
    A6  int
    A7  int
    A8  int
    A9  int
    A10 int
    A11 int
    A12 int
    A13 int
    A14 int
    A15 int
    A16 int
    A17 int
    A18 int
    A19 int
    A20 int
}

func main() {
    var x VeryLongStruct
    x.B2 = false

    xx := []VeryLongStruct{{B2: false}}
    _ = xx
}
% go build /tmp/x.go
# command-line-arguments
/tmp/x.go:28:4: x.B2 undefined (type VeryLongStruct has no field or method B2)
/tmp/x.go:30:26: unknown field B2 in struct literal of type struct{A1 int; A2 int; A3 int; A4 int; A5 int; A6 int; A7 int; A8 int; A9 int; A10 int; A11 int; A12 int; A13 int; A14 int; A15 int; A16 int; A17 int; A18 int; A19 int; A20 int}
% 

Note that the plain x.B2 assignment prints a nice error mentioning VeryLongStruct by name.

In contrast, the same assignment in the struct literal prints the actual struct definition, which is too long to be useful. Saying

/tmp/x.go:30:26: unknown field B2 in struct literal of type VeryLongStruct

would be better.

/cc @griesemer

gabyhelp commented 3 months ago

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

gopherbot commented 3 months ago

Change https://go.dev/cl/595075 mentions this issue: go/types, types2: report type name in comp. literal error, if possible