-
Notifications
You must be signed in to change notification settings - Fork 898
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
Impl rewrite_result for ast nodes in items.rs #6212
Conversation
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.
Looks like we're on the right track with this. Left a few suggestions for way that we could improve things.
src/items.rs
Outdated
let inner_width = shape.width.checked_sub(3).ok_or_else(|| { | ||
RewriteError::ExceedsMaxWidth { | ||
configured_width: shape.width, | ||
span: self.span(), | ||
} | ||
})?; |
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.
This isn't something we need to address right now, but I've noticed a lot of .ok_or_else(|| { RewriteError::ExceedsMaxWidth {..} })
calls throughout the code.
I wonder if we can make this a little more ergonomic. Here are three ideas I have:
-
Define new helper methods on
Shape
-
Add a macro that will handle some of this boilerplate
-
Define an extension trait so we can more easily convert from
Option<T>
->Result<T, RewriteError>
.
Here's one idea that I had:/// Extension trait used to conveniently convert to RewriteError pub(crate) trait RewriteErrorExtension<T> { fn max_with_error(self, width: usize, span: Span) -> Result<T, RewriteError>; fn unknown_error(self) -> Result<T, RewriteError>; } impl<T> RewriteErrorExtension<T> for Option<T> { fn max_with_error(self, width: usize, span: Span) -> Result<T, RewriteError> { self.ok_or_else(|| RewriteError::ExceedsMaxWidth { configured_width: width, span: span, }) } fn unknown_error(self) -> Result<T, RewriteError> { self.ok_or_else(|| RewriteError::Unknown) } }
@ytmimi Thank you for your review. It seems like I missed some details. I'll take a more careful look at the next PR. |
I'm fairly certain that If you feel like it would be a lot to extend the scope of the current PR then we can always revisit modifying all of these functions in future PRs, but if it's not a heavy lift then we might want to consider doing some of that work in this PR. |
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 working through all the feedback! Here's my follow up review
let pat_shape = shape.offset_left(4)?; | ||
let pat_shape = shape | ||
.offset_left(4) | ||
.max_width_error(shape.width, self.span())?; |
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.
On thing I've been wondering is whether we should use shap.width
or context.config.max_width()
for the max_width_error. Would love to get your thoughts on this.
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 believe shape.width
provides more specific information, considering that rewrite tries to fit the formatted code into the given shape, while context.config.max_width()
is set by the global configuration. Even if we want to expose only max_width
to users in error messages (since shape.width
might be confusing), in most cases, callers can retrieve context.config.max_width()
from its rewrite context so converting RewriteError
to ErrorKind
would be possible.
However, if there isn't much opportunity to retry formatting based on the returned RewriteError
with shape.width
and span
, it might be pointless to include shape.width, especially if this information is unused.
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 think it's fine to leave this as shape.width
for now. We can always revisit this decision in the future if it turns out there's something better we should use instead of the width.
let orig_ty = shape | ||
.offset_left(overhead + spacing.len()) | ||
.and_then(|ty_shape| field.ty.rewrite(context, ty_shape)); | ||
.and_then(|ty_shape| field.ty.rewrite_result(context, ty_shape).ok()); | ||
|
||
if let Some(ref ty) = orig_ty { | ||
if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) { | ||
return Some(attr_prefix + &spacing + ty); | ||
return Ok(attr_prefix + &spacing + ty); | ||
} | ||
} |
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.
Since we are gonna continue formatting though offset_left
returns None, I replaced ty.rewrite
with ty.rewrite_result.ok()
instead of converting Option into RwResult like below.
let orig_ty = shape
.offset_left(overhead + spacing.len())
.max_width(shape.width, field.span())
.and_then(|ty_shape| field.ty.rewrite_result(context, ty_shape));
if let Ok(ref ty) = orig_ty {
if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
return Ok(attr_prefix + &spacing + ty);
}
}
Although it seems somewhat pointless to calling rewrite_result.ok() instead of rewrite, I did so since rewrite will return Result after all.
It's pretty minor issue, but I want to hear your suggestion if you have one.
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 think calling rewrite_result.ok()
is fine. We could also follow your suggestion, but I don't think it makes much of a difference right now.
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.
Great work. I took another look and I think we're good to go on this one
@ding-young when you have a chance can you squash the commits and rebase this PR to bring it up to date |
adf8c0a
to
4b32322
Compare
impl rewrite_result for ast::Local, ast::FieldDef, ast::Param, ast::FnRetTy
4b32322
to
8675289
Compare
Tracked by #6206
Description
RewriteError
and blanket implementation forrewrite_result
rewrite_result
for 4 nodes;ast::Local, ast::FieldDef, ast::Param, ast::FnRetTy