Closed prattmic closed 1 month ago
cc @golang/compiler
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Thanks @gabyhelp. #10762 points out that this could be ambiguous. If b
is a type, then the error message would be accurate.
Still, as a user I know b
is not a type, so the error message is not very helpful.
This error comes from the parser, so it can't use knowledge of what kind of symbol b is. I suppose the type checker could emit a second error:
./prog.go:7:15: syntax error: mixed named and unnamed parameters
./prog.go:7:15: type error: parameter b has no type
We don't want to report two errors for a single bug; also the type checker is not run if there are syntax errors. But perhaps the error message can be improved.
Change https://go.dev/cl/615917 mentions this issue: cmd/compile/internal/syntax: improve error message for mixed named/unnamed parameters in identifier list
We don't want to report two errors for a single bug; also the type checker is not run if there are syntax errors. But perhaps the error message can be improved.
Hi @griesemer , I have created a CL to improve the error message. The error looks like this now:
./prog.go:7:15: syntax error: function named parameter declaration is incomplete
I cannot find a better name than "named function parameter declaration" here as the go spec actual doesn't provide a good naming for it as well. The EBNF ParameterDecl
mixes the unnamed/named parameter declaration together and describes the limitation in the description part.
I have changed the spec description as well so users could find the error quickly by checking the specification based on the error message.
also the type checker is not run if there are syntax errors.
If the compiler tolerates some errors in the parsing stage and checks them in the type check stage, the compiler could report more accurate error messages. For this specific case, I don't think it's worth doing as the possible huge code changes.
But in a general sense, do you think it's a correct direction to continue type check to check some errors that the parser cannot report accurately?
Change https://go.dev/cl/616036 mentions this issue: go/parser: improve error message for unnamed param decl in identifier list
Change https://go.dev/cl/615919 mentions this issue: doc/go_spec.html: clarify named/unnamed parameter declartions
Change https://go.dev/cl/615919 mentions this issue: doc/go_spec.html: clarify named/unnamed parameter declarations
Change https://go.dev/cl/617015 mentions this issue: go/parser, syntax: better error message for parameter missing type
Go version
1.23
Output of
go env
in your module/workspace:What did you do?
https://go.dev/play/p/hgKdYn08tsY
I forgot to add the type to the last argument of a function definition:
This should have some type name after
b
.What did you see happen?
What did you expect to see?
All parameters are named here, the actual problem is the missing type, so I expected something like: "syntax error: parameter 'b' missing type"