Skip to content

Enable a mode in the parser in which it inspects alternative token choices to mutate test cases #1340

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

Merged
merged 1 commit into from
Apr 16, 2023

Conversation

ahoppen
Copy link
Member

@ahoppen ahoppen commented Feb 12, 2023

The basic idea is that the parser records which TokenSpecs it checked for at individual offsets in the source. We can then use that information to generate new, interesting test cases by replacing a token by the one of the TokenSpec we checked for. This technique has found 7 bugs in the parser and I’m expecting it to find quite a few more once we assert that tokens have one of the expected kinds. I’m fixing the bugs this technique found in #1341.

Gathering of that information is hidden behind a conditional compilation flag because just performing the check of whether we want to record alternative token choices inflicts a 6% performance regression, which doesn’t provide any value except when we are running SwiftParserTest.

@ahoppen
Copy link
Member Author

ahoppen commented Feb 12, 2023

@swift-ci Please 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.

Cool idea!

/// matches this `TokenSpec`.
///
/// IMPORTANT: Should only be used when generating tokens during the
/// modifiation of test cases. This should never be used in the parser itself.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// modifiation of test cases. This should never be used in the parser itself.
/// modification of test cases. This should never be used in the parser itself.

Comment on lines 560 to 562
// FIXME: Currently, tests are failing when we enable test case mutation.
// Once all of those issues are fixed, this should become
// let enableTestCaseMutation = ProcessInfo.processInfo.environment["SKIP_LONG_TESTS"] != "1"
Copy link
Contributor

Choose a reason for hiding this comment

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

Out of interest, how much longer does this take to run?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh, it’s quite significant. It increases test time from 39s to 250s if you are not skipping long tests (SKIP_LONG_TESTS=1)

Copy link
Contributor

Choose a reason for hiding this comment

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

How many mutations happen? Given it's mutating one at a time (concurrently, but each one just has the one change), That's maybe not as bad as I thought it would be 😅. Wonder if it's worth batching some together.

Copy link
Member Author

Choose a reason for hiding this comment

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

How many mutations happen?

I didn’t measure but I imagine there are on average 3 to 5 different choices for each token. I thought about batching them together, but you really need to be clever so one doesn’t shadow the other. And trying to be clever is usually a bad idea in my experience.

@ahoppen ahoppen force-pushed the ahoppen/source-alteration branch from 78fe006 to cf26631 Compare February 14, 2023 10:15
ahoppen added a commit to ahoppen/swift-syntax that referenced this pull request Apr 14, 2023
These parser failures were found by swiftlang#1340.

- There was one round-trip failure introduced by swiftlang#1464
- A few cases were just consume tokens as the wrong child
- The list of expected token kinds of `DeclModifierSynax` didn’t contain one modifier that was actually parsed
ahoppen added a commit to ahoppen/swift that referenced this pull request Apr 14, 2023
@ahoppen ahoppen force-pushed the ahoppen/source-alteration branch from cf26631 to f3e13ef Compare April 14, 2023 01:33
ahoppen added a commit to ahoppen/swift-syntax that referenced this pull request Apr 14, 2023
These parser failures were found by swiftlang#1340.

- There was one round-trip failure introduced by swiftlang#1464
- A few cases were just consume tokens as the wrong child
- The list of expected token kinds of `DeclModifierSynax` didn’t contain one modifier that was actually parsed
…oices to mutate test cases

The basic idea is that the parser records which `TokenSpec`s it checked for at individual offsets in the source. We can then use that information to generate new, interesting test cases by replacing a token by the one of the `TokenSpec` we checked for. This technique has found 11 bugs in the parser and I’m expecting it to find quite a few more once we assert that tokens have one of the expected kinds.

Gathering of that information is hidden behind a conditional compilation flag because just performing the check of whether we want to record alternative token choices inflicts a 6% performance regression, which doesn’t provide any value except when we are running SwiftParserTest.
@ahoppen ahoppen force-pushed the ahoppen/source-alteration branch from f3e13ef to b32317c Compare April 15, 2023 20:40
@ahoppen
Copy link
Member Author

ahoppen commented Apr 15, 2023

swiftlang/swift#65174

@swift-ci Please test

@ahoppen ahoppen merged commit 011d9c1 into swiftlang:main Apr 16, 2023
@ahoppen ahoppen deleted the ahoppen/source-alteration branch April 16, 2023 02:05
ahoppen added a commit to ahoppen/swift-syntax that referenced this pull request Apr 18, 2023
Enable a mode in the parser in which it inspects alternative token choices to mutate test cases
ahoppen added a commit to ahoppen/swift-syntax that referenced this pull request Apr 18, 2023
Enable a mode in the parser in which it inspects alternative token choices to mutate test cases
meg-gupta pushed a commit to meg-gupta/swift that referenced this pull request Apr 18, 2023
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