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

update macro rewrite functions to return RewriteResult #6271

Merged
merged 1 commit into from
Aug 25, 2024

Conversation

ding-young
Copy link
Contributor

@ding-young ding-young commented Aug 7, 2024

Tracked by #6206

Description

  • update both rewrite_macro and rewrite_macro_def to return RewriteResult
  • add a new enum variant that represents macro rewrite failure

@ytmimi
Copy link
Contributor

ytmimi commented Aug 7, 2024

Looks like some of the self_tests are failing because of a long comment. In general its probably better to place comments directly above the code that you're trying to explain given the max_width constraints that rustfmt has.

---- test::self_tests stdout ----
error[internal]: line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option), found: 105)
    --> src/macros.rs:1043:1043:101
     |
1043 |     let parsed_args = MacroArgParser::new().parse(token_stream).macro_error()?; // TODO macro parse error
     |                                                                                                     ^^^^^
     |
     = note: set `error_on_unformatted = false` to suppress the warning against comments or string literals

@ytmimi ytmimi added the GSoC Google Summer of Code label Aug 8, 2024
@ding-young ding-young force-pushed the rewrite-macro branch 2 times, most recently from fcd6d93 to 4dc935f Compare August 22, 2024 00:19
src/macros.rs Outdated
Comment on lines 247 to 260
// Format well-known macros which cannot be parsed as a valid AST.
if macro_name == "lazy_static!" && !has_comment {
if let success @ Some(..) = format_lazy_static(context, shape, ts.clone()) {
if let success @ Ok(..) = format_lazy_static(context, shape, ts.clone(), mac.span()) {
return success;
}
}
Copy link
Contributor Author

@ding-young ding-young Aug 22, 2024

Choose a reason for hiding this comment

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

        match format_lazy_static(context, shape, ts.clone(), mac.span()) {
            Ok(rw) => return Ok(rw),
            Err(err) => match err {
                RewriteError::MacroFailure { macro_error_kind, span: _ } 
                if macro_error_kind== MacroErrorKind::ParseFailure => {},
                _ => return Err(err),
            },
        }

I think we can improve error reporting on calling format_lazy_static this way.

If we fail to parse macro with known (expected) syntax, we should still need to try formatting like following example.

lazy_static!(xxx, yyyy, zzzzz)

However, if we already succeeds to parse lazy_static with known syntax but fails to format due to other reason, it's better to tell users about it.

Since this causes change in formatting logic I want to hear your opinion about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting idea. I think we can give this a try!

src/rewrite.rs Outdated Show resolved Hide resolved
src/rewrite.rs Outdated Show resolved Hide resolved
src/macros.rs Outdated Show resolved Hide resolved
@ding-young ding-young marked this pull request as ready for review August 22, 2024 00:48
src/rewrite.rs Show resolved Hide resolved
src/macros.rs Outdated Show resolved Hide resolved
src/macros.rs Outdated Show resolved Hide resolved
src/macros.rs Outdated Show resolved Hide resolved
nested_shape.sub_width(1)?,
)?);
result.push_str(
&rewrite_assign_rhs(
Copy link
Contributor

Choose a reason for hiding this comment

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

Question: Do we have plans to make rewrite_assign_rhs return a RewriteResut?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely yes. However, when I tried it a few weeks ago, I found that we might not be able to fully avoid using unknown_error() inside rewrite_assign_rhs due to challenges in grabbing the correct span. Anyway, I’ll give it another try and update you on the outcome.

src/rewrite.rs Outdated
Comment on lines 44 to 46
MacroErrorKind::ParseFailure => write!(f, "(parse failure)"),
MacroErrorKind::ReplaceMacroVariable => write!(f, "(replacing macro variables with $)"),
MacroErrorKind::Unknown => write!(f, "(unknown reason)"),
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 replaced empty string with "(unknown reason)", and full error msg would look like

Failed to format given macro(unknown reason) at: Span {....} 

I’d appreciate any other suggestions regarding the error message, though.

Copy link
Contributor

Choose a reason for hiding this comment

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

I didn't realize that this Display impl would feed into RewriteError::MacroFailures Display impl. I think that not specifying a reason would have been fine now that I think about it more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then I'll just revert it to empty string :)

@ding-young
Copy link
Contributor Author

Thank you for the review! I’ve made the requested changes.

src/rewrite.rs Outdated
@@ -1,6 +1,7 @@
// A generic trait to abstract the rewriting of an element (of the AST).

use std::cell::{Cell, RefCell};
use std::fmt::{self};
Copy link
Contributor

@ytmimi ytmimi Aug 24, 2024

Choose a reason for hiding this comment

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

nit: Is there a specific reason we're importing this as std::fmt::{self}; and not std::fmt;? We can probably just impl std::fmt::Display for MacroErrorKind below and avoid the use altogether.

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 agree that it’s better to avoid the additional use here. Thank you for pointing it out. I updated the code.

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

Thanks for making those changes. PR looks good now!

@ytmimi
Copy link
Contributor

ytmimi commented Aug 25, 2024

Running the Diff-Check

Edit: Job passed ✅

@ytmimi ytmimi merged commit 6f5e99b into rust-lang:master Aug 25, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GSoC Google Summer of Code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants