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

1.x fix for missing comments extra macro body indentation #4697

Conversation

davidBar-On
Copy link
Contributor

@davidBar-On davidBar-On commented Feb 13, 2021

1.x fix for issue #4603 - infinite macro body indentation because of "missing comment". The fix is per the approach discussed in #4629.

As discussed, the fix includes handling both LostComment and TrailingWhitespace errors, although I am not sure if TrailingWhitespace is needed.

UPDATE: the tests fail because an issue that i cannot understand. When running cargo make test in my local machine it fails on the same errors. However, when running manually the created ./target/debug/rustfmt the output is o.k. I hoped that this is a problem in my local machine, but unfortunately it seems it isn't. I will be glad to get help with this issue.

@calebcartwright
Copy link
Member

The cargo make piece was a bit of a red herring (you'll see the same failures running cargo +nightly test with the requisite env vars set). The real problem is that we need error_on_unformatted to be set to true for the session that's formatting the macro def body, otherwise the errors we are looking for get tossed.

When running the rustfmt bin directly, it will pick up on the rustfmt.toml config file in the root of the repository which does set this, whereas our test suites will, by design, override/set explicit config values which is why the tests are failing.

Think this can be resolved by simply setting that on the config:

config.set().emit_mode(config::EmitMode::Stdout);
config.set().verbose(Verbosity::Quiet);
config.set().hide_parse_errors(true);
if is_macro_def {
    config.set().error_on_unformatted(true);
}

@davidBar-On davidBar-On force-pushed the 1.x-issue-4603-macro-with-comment-intentation branch from a7c6c3f to 4eb31fb Compare February 13, 2021 18:44
@davidBar-On
Copy link
Contributor Author

Thanks! Setting the config solved the issue.

Note that the changes include a fix of a minor rustfmt 1.x issue: in the following code, moved the closing ) after has_diff() to be after has_macro_format_failure:

rustfmt/src/lib.rs

Lines 485 to 486 in 7de6968

|| self.has_diff())
|| self.errors.has_macro_format_failure

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

Thanks! Few minor things but otherwise LGTM

@@ -331,6 +331,9 @@ pub(crate) struct ReportedErrors {

/// Formatted code differs from existing code (--check only).
pub(crate) has_diff: bool,

/// Formatted code missed something, like lost comments or extra trailing space
pub(crate) has_missing_errors: bool,
Copy link
Member

Choose a reason for hiding this comment

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

I think we should bikeshed on the name here, as the way the name currently reads is confusing. This flag is indicator that there was something that rustfmt failed to be able to format successfully (almost always caused by the presence of a comment in a place rustfmt cannot support comments).

It's really more indicative that there were parts of the input code that rustfmt failed to format and reverted to the original formatting, so there are unformatted portions of the code in the output.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I forgot to mention the name in the discussion, as I couldn't find a good name for both lost-comments and trailing-white-spaces errors. What about has_lost_comments_error? has_unformatted_original_code? Another option is to split it to has_lost_comments_error and has_trailing_whitespaces_error.

Copy link
Member

Choose a reason for hiding this comment

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

i'd prefer something that references "failed" or "unformatted"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Renamed has_missing_errors to has_unformatted_original_code. Hope this is o.k.

Copy link
Member

Choose a reason for hiding this comment

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

I don't care too much about the internal name, but do have some reservations about the public facing function given that the rustfmt lib is available for outside consumers (and we can't change it once we put it out there).

I think it's important to keep errors in the name, so would prefer either:

(a) has_unformatted_code_errors
(b) has_unformatted_errors
(c) skip an aggregate helper, and just track/expose whitespace vs. lost comments separately (has_lost_comment_errors and has_trailing_whitespace_errors).

Please be sure to update the function name(s) accordingly too

src/lib.rs Outdated Show resolved Hide resolved
@davidBar-On davidBar-On force-pushed the 1.x-issue-4603-macro-with-comment-intentation branch from 54a5666 to a3c6e73 Compare February 16, 2021 14:40
@davidBar-On
Copy link
Contributor Author

Changed the name to has_unformatted_code_errors.

@calebcartwright
Copy link
Member

Awesome, 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.

2 participants