-
Notifications
You must be signed in to change notification settings - Fork 905
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
Conversation
Looks like some of the
|
fcd6d93
to
4dc935f
Compare
src/macros.rs
Outdated
// 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; | ||
} | ||
} |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
4dc935f
to
b97b946
Compare
nested_shape.sub_width(1)?, | ||
)?); | ||
result.push_str( | ||
&rewrite_assign_rhs( |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
b97b946
to
cae6a79
Compare
src/rewrite.rs
Outdated
MacroErrorKind::ParseFailure => write!(f, "(parse failure)"), | ||
MacroErrorKind::ReplaceMacroVariable => write!(f, "(replacing macro variables with $)"), | ||
MacroErrorKind::Unknown => write!(f, "(unknown reason)"), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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::MacroFailure
s Display
impl. I think that not specifying a reason would have been fine now that I think about it more.
There was a problem hiding this comment.
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 :)
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}; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
cae6a79
to
5da18ea
Compare
There was a problem hiding this 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!
Running the Diff-Check Edit: Job passed ✅ |
Tracked by #6206
Description
rewrite_macro
andrewrite_macro_def
to return RewriteResult