Skip to content
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

fix: Fix sniper printer failing to prepend whitespace to top-added CtModifiable #3856

Conversation

slarse
Copy link
Collaborator

@slarse slarse commented Mar 23, 2021

Fix #3855

This fixes a bug causing the sniper printer to not prepend whitespace to type members and local variables added at the start of a non-empty collection. For example, given this class:

class Cls {
    private int someInt = 2;
}

adding a new field private int otherInt = 3 at the top would be printed like so:

class Cls {private int otherInt = 3;
    private int someInt = 3;

The same thing applies to local variables in non-empty statement lists.

The bug is caused by the sniper printer looking the source fragment of any CtModifiable or ModifierKind only by role, which causes any source fragment belonging to a CtModifiable to match any other CtModifiable. This itself is required to avoid other corner cases, it appears. Thus, the sniper always thinks it finds a match for a newly created CtModifiable when the collection it's added in is non-empty, and then does not print the "synthetic" whitespace.

The fix is simply to require any CtModifiable to already exist (have a valid source position) to be matched by role alone, see 5a03df6.

Note: The test cases fail on Windows due to containing literal newlines. All test cases in the sniper printer tests that contain literal newlines fail on Windows. That's an issue that isn't related to this particular patch, so the new test cases currently ignore Windows. I need to debug the newline problem separately. Most likely, somewhere CRLF line endings are inserted instead of LF line endings, even though the sniper printer should respect the line endings of the original source file.

@slarse slarse changed the title fix: Fix sniper printer failing to prepend whitespace top-added CtModifiable wip: fix: Fix sniper printer failing to prepend whitespace top-added CtModifiable Mar 23, 2021
@slarse slarse changed the title wip: fix: Fix sniper printer failing to prepend whitespace top-added CtModifiable review: fix: Fix sniper printer failing to prepend whitespace top-added CtModifiable Mar 23, 2021
@slarse slarse changed the title review: fix: Fix sniper printer failing to prepend whitespace top-added CtModifiable review: fix: Fix sniper printer failing to prepend whitespace to top-added CtModifiable Mar 23, 2021
@monperrus
Copy link
Collaborator

LGTM will merge.

Note that it's a typical Nopol and Dynamoth patch, so in theory we should be able to synthesize the patch only based on the failing test cases.

@slarse
Copy link
Collaborator Author

slarse commented Mar 24, 2021

Note that it's a typical Nopol and Dynamoth patch, so in theory we should be able to synthesize the patch only based on the failing test cases.

I could try that, but I'd still need a Windows environment to do it (otherwise, no failures :). I do have a Windows system, I just don't have it set up for any kind of development. I'll get around to it eventually!

@monperrus monperrus changed the title review: fix: Fix sniper printer failing to prepend whitespace to top-added CtModifiable fix: Fix sniper printer failing to prepend whitespace to top-added CtModifiable Mar 25, 2021
@monperrus monperrus merged commit 6aee183 into INRIA:master Mar 25, 2021
@monperrus
Copy link
Collaborator

Thanks @slarse!

@slarse slarse deleted the issue/3855-fix-sniper-printer-failing-to-prepend-whitespace branch March 25, 2021 07:21
@monperrus monperrus mentioned this pull request Aug 19, 2021
woutersmeenk pushed a commit to woutersmeenk/spoon that referenced this pull request Aug 29, 2021
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.

bug: Sniper printer fails to prepend whitespace to CtModifiable added at start of non-empty collection
2 participants