Skip to content

fix adds a trailing comma to struct-like macro #2490

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 7 commits into from
Mar 8, 2018

Conversation

csmoe
Copy link
Member

@csmoe csmoe commented Feb 25, 2018

should resolve #2445
Input

test!(RunPassPretty {
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true
});

Output(before)

test!(RunPassPretty {
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true,
});

After

test!(RunPassPretty {
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true
});

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you for your PR! And sorry about the late review.

The added test is wrong, it could be that span_ends_with_comma does not work with comments (I added it, sorry!). Would you please update this PR to fix it?

});

test!(Test {
field: i32 // comment
Copy link
Contributor

Choose a reason for hiding this comment

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

This test seems wrong: we need to preserve a trailing comma if the macro originally has it.

/// trailing comma. This function is used when rewriting macro, as adding or removing a trailing
/// comma from macro can potentially break the code.
fn span_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
let mut encountered_closing_paren = false;
let mut encountered_closing_braces = false;
for c in context.snippet(span).chars().rev() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please try using CharClasses here?

@topecongiro
Copy link
Contributor

And this PR needs rebase. Sorry!

@nrc
Copy link
Member

nrc commented Mar 5, 2018

@csmoe I think the rebase went wrong. Within the branch struct_macro_trailing_comma you should be able to run git rebase master to rebase your commits on top of master without pulling in the intermediate commits. To get back to a state where you can do that, you probably want to use git reflog and checkout an earlier version of your branch. Let me know if you need more help with this.

@csmoe csmoe changed the title fix adds a trailing comma to struct-like macro [WIP]fix adds a trailing comma to struct-like macro Mar 5, 2018
@csmoe csmoe changed the title [WIP]fix adds a trailing comma to struct-like macro [WIP] fix adds a trailing comma to struct-like macro Mar 5, 2018
@csmoe csmoe force-pushed the struct_macro_trailing_comma branch from 21916b9 to 5df80f2 Compare March 5, 2018 13:34
@csmoe csmoe force-pushed the struct_macro_trailing_comma branch from 5df80f2 to 73b06bb Compare March 5, 2018 14:08
src/expr.rs Outdated
#[allow(dead_code)]
/// Check if a struct representation ends with comma by match on the chars of struct
/// snippet reversely, once a FullCodeCharKind::Normal comma matched, returns true
fn struct_lit_ends_with_comma(context: &RewriteContext, span: Span) -> bool {
Copy link
Member Author

Choose a reason for hiding this comment

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

I added a function to check if a struct lit ends with comma here, but I need to match the comma reversely because normally match might never handle the last field before it returns.

I had tried to impl DoubleEndedIterator for CharClasses {...}, the basic next_back method is easy to learn from std-docs, but it's a bit difficult for me to tackle the method on CharClasses, could you please give me some tips the deal with it? @topecongiro

@csmoe
Copy link
Member Author

csmoe commented Mar 7, 2018

Input

test!(RunPassPretty {
            // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true  // should, force no trailing comma here
});

test!(RunPassPretty {
            // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true,         // should, preserve the trailing comma
});

test!(Test{
    field: i32, // comment
});

Output(before)

test!(RunPassPretty {
    // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true, // should, force no trailing comma here
});

test!(RunPassPretty {
    // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true, // should, preserve the trailing comma
});

test!(Test {
    field: i32, // comment
});

After

test!(RunPassPretty {
    // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true // should, force no trailing comma here
});

test!(RunPassPretty {
    // comment
    path: "src/test/run-pass/pretty",
    mode: "pretty",
    suite: "run-pass",
    default: false,
    host: true, // should, preserve the trailing comma
});

test!(Test {
    field: i32, // comment
});

@csmoe csmoe changed the title [WIP] fix adds a trailing comma to struct-like macro fix adds a trailing comma to struct-like macro Mar 7, 2018
@csmoe csmoe force-pushed the struct_macro_trailing_comma branch from 10e808b to a4cbcb9 Compare March 7, 2018 13:23
@topecongiro
Copy link
Contributor

@csmoe Thank you for your update! And my apologies that I could not give you a feedback in a timely manner.

I am afraid that using retain does not work well when there is a comma inside comment. Would you please add a test where there is a comma inside comment?

I think that you do not need to implement DoubleEndedIterator for CharClasses, just check that the last normal code is ,) or ,}. Rather than returning early, we could keep processing so that the last field will be checked. For example, you could write like this:

    let mut result;
    let mut prev_char;

    for (kind, c) in CharClasses::new(context.snippet(span).chars()) {
        match c {
            _ if kind.is_comment() || c.is_whitespace() => continue,
            ')' | '}' => result = result && prev_char != c,
            ',' => result = true,
            _ => result = false,
        }
        prev_char = c;
    }

    result

@csmoe
Copy link
Member Author

csmoe commented Mar 8, 2018

@topecongiro Comma in comment fixed.

Thank for your patience on guiding me here and sorry for bothering you with the annoying errors in those undeliberate commits. I have learned some tips to write a proper test case and skills to tackle string scanning, Thank you.

Copy link
Contributor

@topecongiro topecongiro left a comment

Choose a reason for hiding this comment

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

Thank you for the update! I left two inline comments about minor updates, otherwise LGTM.

src/expr.rs Outdated
@@ -7,7 +7,6 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please bring back an empty line here?

src/lib.rs Outdated
@@ -12,6 +12,7 @@
#![feature(decl_macro)]
#![feature(match_default_bindings)]
#![feature(type_ascription)]
#![feature(string_retain)]
Copy link
Contributor

Choose a reason for hiding this comment

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

Would you please remove this unused feature?

@topecongiro topecongiro merged commit a2f8617 into rust-lang:master Mar 8, 2018
@topecongiro
Copy link
Contributor

Thank you!

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.

rustfmt adds a trailing comma to struct-like macro
3 participants