A struct type with a missing close brace has an StructType.End pos of zero. That means the enclosing nodes such as ReturnStmt may also have a computed End that is zero, or perhaps slightly larger:

Screenshot 2024-04-04 at 2 43 08 PM

I believe this is the root cause of at least some of the many gopls crashes recorded in https://github.com/golang/go/issues/64547.

Ultimately this is a problem of parser error recovery. We need to establish the invariants that: - all tree nodes have valid Pos/End positions; and - all tree nodes have nested Pos/End subranges (with a possible exception for the func keyword in FuncDecl/FuncType).

See also: - https://github.com/golang/go/issues/48300 ;-)

Comment From: gopherbot

Change https://go.dev/cl/576655 mentions this issue: gopls/internal/cache: analysis: repair start/end and refine bug report

Comment From: findleyr

Let's try to fix this for Go 1.23. Adding to the milestone.

Comment From: gopherbot

Change https://go.dev/cl/577595 mentions this issue: [gopls-release-branch.0.15] gopls/internal/cache: analysis: repair start/end and refine bug report

Comment From: adonovan

@findleyr suggests that a fuzzer connected to an invariant checker would be a good source of regression test cases.

Comment From: findleyr

Shall we expand this issue to introducing and enforcing our stated invariant?

Comment From: adonovan

Shall we expand this issue to introducing and enforcing our stated invariant?

Perhaps we need an umbrella issue. See also https://github.com/golang/go/issues/66790#issuecomment-2050667924, which is the beginning of a theory of pos/end in ill-formed trees.

Comment From: gopherbot

Change https://go.dev/cl/630675 mentions this issue: gopls/internal/crash: don't crash in xrefs on out of bound nodes

Comment From: gopherbot

Change https://go.dev/cl/668238 mentions this issue: go/ast: enforce FileStart <= Node.Pos <= Node.End <= FileEnd

Comment From: gopherbot

Change https://go.dev/cl/668677 mentions this issue: x/tools: prepare for go1.25 parser changes

Comment From: gopherbot

Change https://go.dev/cl/672055 mentions this issue: x/tools: various cleanups related to planned parser changes