Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
go/parser: EmptyStmt.Semicolon.End() is off by one #9979
If you parse
Then you get a LabeledStmt containing an EmptyStmt, and the EmptyStmt.Semicolon has as its value the offset of the } character, and then EmptyStmt.Semicolon.End() returns that+1, which means that the End is recorded as just beyond the closing brace. If you try to delete the statement from the program using the reported boundaries, you end up deleting the brace.
Full test case:
You can see that the EmptyStmt has the same end as the BlockStmt (and for that matter the FuncDecl and File).
I am kind of confused by the definition of EmptyStmt.Semicolon. What is the "preceding semicolon" when there's no semicolon due to semicolon insertion? It's tempting to say it is the \n, but there's not always a \n either.
Just to explore, consider changing var prog to
Then the +1 in Semicolon.End cannot be right, because there is nothing safe to +1 over. Contrast this with:
It seems like the fix requires an extra field in EmptyStmt to distinguish real semicolons from implicit semicolons.