You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
go/parser: poor error recovery after missing function call argument #58833
This program contains a syntax error in Abs(,) that causes the parser's recovery to generate a BadExpr for math.Lde.
func g() {
math.Abs(,)
//
math.Lde()
}
This causes go/types not to recognize math as a reference to a package, and causes completions in gopls to misbehave.
I think the parser could do a better job of recovery. @griesemer gives reasons why this is difficult in #24327 (comment):
go/parser is likely falling behind the compiler due to the compiler's parser (package syntax) being actively improved whenever we run into unsatisfying error messages. It's fairly time-consuming to back-port those fixes because the parsers don't have the same structure, and small local changes sometimes have unexpected consequences with respect to error handling (hence those changes, even if small, are time-consuming to get right).
Ideally, in the long run we should migrate to package syntax. Failing that, one option might be to replace go/parser with the syntax package parser while keeping the same API. That might be not too hard since the syntax trees are reasonably close.
Related issue of gopls completion after a syntax error:
This specific error is perhaps not too hard to fix individually.
Re-using the compiler's syntax package's parser code, but producing go/ast may be a good solution in the long run. Perhaps parts of it can be copied in pieces. E.g., the parts the parse parameter lists could be copied and adjusted to produce go/ast, and as a "side-effect" this bug might get fixed.
package main
import "fmt"
func nop(...any){}
func main() {
nop(1, 2, 3 fmt.Println) // notice missing comma after 3
nop()
nop()
nop()
var a, b, c int
println(a, b, c)
}
Errors:
play.go:8:21: missing ',' in argument list
play.go:8:24: expected operand, found '.'
play.go:14:9: missing ',' in argument list
play.go:14:21: missing ',' in argument list
play.go:14:24: expected operand, found newline
play.go:16:3: expected ')', found 'EOF'
play.go:16:3: expected ';', found 'EOF'
play.go:16:3: expected ';', found 'EOF'
play.go:16:3: expected '}', found 'EOF'
play.go:16:3: missing ',' in argument list
play.go:14:13: undeclared name: a
play.go:14:16: undeclared name: b
play.go:14:19: undeclared name: c
play.go:3:8: "fmt" imported but not used
FWIW, pretty printing the damaged tree produces only a small part of it:
Observe that recovery consumes the if of various statements of the form if x == y {, changing the parser's disposition towards the { so that it becomes a composite literal y{...}. There are two examples above: nil{...} and token.DEFINE{...}. It also sucks in and discards a potentially long function in the middle.
Activity
griesemer commentedon Mar 2, 2023
This specific error is perhaps not too hard to fix individually.
Re-using the compiler's syntax package's parser code, but producing go/ast may be a good solution in the long run. Perhaps parts of it can be copied in pieces. E.g., the parts the parse parameter lists could be copied and adjusted to produce go/ast, and as a "side-effect" this bug might get fixed.
adonovan commentedon May 3, 2023
Another version of the same recovery cascade:
FWIW, pretty printing the damaged tree produces only a small part of it:
griesemer commentedon May 3, 2023
For reference, for the above program, the compiler produces simply
gopherbot commentedon May 8, 2023
Change https://go.dev/cl/493616 mentions this issue:
gopls/internal/lsp/cache: skip type errors after parse errorsgopls/internal/lsp/cache: skip type errors after parse errors
adonovan commentedon Aug 27, 2024
Here's a variant distilled from #68918 in which the parser swallowed (originally) thousands of lines during recovery:
Using the types playground, this parse tree is pretty printed as:
Observe that recovery consumes the
ifof various statements of the formif x == y {, changing the parser's disposition towards the{so that it becomes a composite literaly{...}. There are two examples above:nil{...}andtoken.DEFINE{...}. It also sucks in and discards a potentially long function in the middle.gopherbot commentedon Apr 27, 2026
Change https://go.dev/cl/770861 mentions this issue:
gopls/internal/golang/completion: fix replacement range in invalid codegopls/internal/golang/completion: fix replacement range in invalid code