golang / go

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

cmd/compile: bad error message for argument missing type #69506

Closed prattmic closed 1 month ago

prattmic commented 1 month ago

Go version

1.23

Output of go env in your module/workspace:

N/A

What did you do?

https://go.dev/play/p/hgKdYn08tsY

I forgot to add the type to the last argument of a function definition:

func f(a int, b) {
}

This should have some type name after b.

What did you see happen?

./prog.go:7:15: syntax error: mixed named and unnamed parameters

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"

prattmic commented 1 month ago

cc @golang/compiler

gabyhelp commented 1 month ago

Related Issues and Documentation

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

prattmic commented 1 month ago

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.

adonovan commented 1 month ago

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
griesemer commented 1 month ago

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.

gopherbot commented 1 month ago

Change https://go.dev/cl/615917 mentions this issue: cmd/compile/internal/syntax: improve error message for mixed named/unnamed parameters in identifier list

xieyuschen commented 1 month ago

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?

gopherbot commented 1 month ago

Change https://go.dev/cl/616036 mentions this issue: go/parser: improve error message for unnamed param decl in identifier list

gopherbot commented 1 month ago

Change https://go.dev/cl/615919 mentions this issue: doc/go_spec.html: clarify named/unnamed parameter declartions

gopherbot commented 1 month ago

Change https://go.dev/cl/615919 mentions this issue: doc/go_spec.html: clarify named/unnamed parameter declarations

gopherbot commented 1 month ago

Change https://go.dev/cl/617015 mentions this issue: go/parser, syntax: better error message for parameter missing type