Skip to content

Conversation

@ahoppen
Copy link
Member

@ahoppen ahoppen commented Nov 5, 2021

Instead, let the lexer know that it should interpret the next token to be lexed as an EOF token. This makes parsing cutoff a lot more stable because the parser is not expecting the current token kind to change in a lot of places.

Technically, this causes two more tokens to be parsed than in the previous implementation (namely Parser.Tok and Lexer.NextToken) but since we bail out at a fairly low nesting level, this shouldn't make a big difference.

@ahoppen ahoppen requested a review from bnbarham November 5, 2021 09:10
@ahoppen
Copy link
Member Author

ahoppen commented Nov 5, 2021

@swift-ci Please smoke test

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this was from me, obviously doesn't matter but could remove if you wanted it to be a little neater :P

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever. The file hit the issue before and now doesn’t hit it any longer so I don’t want to change it anymore.

@ahoppen
Copy link
Member Author

ahoppen commented Nov 8, 2021

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Nov 8, 2021

@swift-ci Please smoke test

@ahoppen
Copy link
Member Author

ahoppen commented Nov 9, 2021

I refactored the PR a bit, reverting a fair bit of #19631 because the checks for StructureMarkerRAII::isFailed are no longer necessary with the new cut-off model.

@bnbarham Could you take another look at the changes?

@ahoppen
Copy link
Member Author

ahoppen commented Nov 9, 2021

@swift-ci Please smoke test

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, much cleaner!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this ever be a nullptr? BufferID is set from BaseLexer->getBufferID() which would suggest not, in which case maybe &BaseLexer makes more sense?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. Changed it to a reference.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The crash here was the assertion failure right? Would it be worth adding // REQUIRES: asserts or a comment to that effect in here? The test does work either way so maybe doesn't matter, but IMO it'd be nice to differentiate it from the existing structure_overflow*.swift ones.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment.

Previously, when we reached the maximum nesting level, we changed the current token’s kind to an EOF token. A lot of places in the parser are not set up to expect this token change. The intended workaround was to check whether pushing a structure marker failed (which would change the token kind) and bail out parsing if this happened. This was fragile and caused assertion failures in assert builds.

Instead of changing the current token’s kind, and failing to push the structure marker, let the lexer know that it should cut off lexing, essentially making the input buffer stop at the current position. The parser will continue to consume its current token (`Parser.Tok`) and the next token that’s already lexed in the lexer (`Lexer.NextToken`) before reaching the emulated EOF token. Thus two more tokens are parsed than before, but that shouldn’t make much of a difference.
@ahoppen
Copy link
Member Author

ahoppen commented Nov 9, 2021

@swift-ci Please smoke test

@ahoppen ahoppen merged commit 60c78af into swiftlang:main Nov 9, 2021
@ahoppen ahoppen deleted the pr/lexing-cutoff branch November 9, 2021 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants