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/go: unnecessary parens in error message #60599

Closed
rsc opened this issue Jun 5, 2023 · 3 comments
Closed

cmd/go: unnecessary parens in error message #60599

rsc opened this issue Jun 5, 2023 · 3 comments
Assignees
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Jun 5, 2023

$ go build
toolchain.go:131:24: syntax error: cannot use assignment (gotoolchain) = ("path") as value

The code only says if gotoolchain = "path" {, a typo of = for ==.

I am not sure what the parens mean in this error message.
Every time I see them (apparently I do this with some frequency)
it is a bit jarring, almost like they are single-element tuples,
but then I remember Go has no tuples, at least not parenthesized ones like Python.
It would be better not to show them:

toolchain.go:131:24: syntax error: cannot use assignment gotoolchain = "path" as value

Or maybe:

toolchain.go:131:24: syntax error: cannot use assignment as value: gotoolchain = "path"

/cc @griesemer

@rsc rsc added the NeedsFix The path to resolution is known, but the work has not been done. label Jun 5, 2023
@rsc rsc added this to the Go1.22 milestone Jun 5, 2023
@griesemer
Copy link
Contributor

Trivial repro: https://go.dev/play/p/nFjETHH2pFk?v=gotip

@griesemer griesemer self-assigned this Jun 5, 2023
@griesemer
Copy link
Contributor

griesemer commented Jun 5, 2023

There's explicit code in the parser to emphasize the LHS and RHS for this kind of error:

// A common syntax error is to write '=' instead of '==',
// which turns an expression into an assignment. Provide
// a more explicit error message in that case to prevent
// further confusion.
var str string
if as, ok := s.(*AssignStmt); ok && as.Op == 0 {
	// Emphasize Lhs and Rhs of assignment with parentheses to highlight '='.
	// Do it always - it's not worth going through the trouble of doing it
	// only for "complex" left and right sides.
	str = "assignment (" + String(as.Lhs) + ") = (" + String(as.Rhs) + ")"
} else {
	str = String(s)
}

I added this in response to #23385.

This is trivially changed (or even removed), but we should be conscious of the issue. Without the special casing, the error for

package p

func _(x, y int) {
	if x = y {}
}

will be:

syntax error: cannot use x = y as value

This is problematic when the LHS and RHS are more complex as in x || y = z where we want to emphasize x || y as the LHS. But perhaps the parentheses logic should just apply of a side is complex.

@gopherbot
Copy link

Change https://go.dev/cl/500975 mentions this issue: cmd/compile/internal/syntax: better error message when using = instead of ==

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

3 participants