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

impl rewrite_result for ControlFlow, Stmt, update rewrite_index #6291

Merged
merged 1 commit into from
Sep 1, 2024

Conversation

ding-young
Copy link
Contributor

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

tracked by #6206

Description

  • impl rewrite_result for Stmt, ControlFlow OverflowableItem, Lifetime
    There are still unknown_error()s due to calling format_expr, but they will be removed after updating format_expr in later pr.
  • update rewrite_index to return RewriteResult
  • removed a few unknown_error() if possible

@@ -1703,12 +1703,11 @@ impl<'a> Iterator for CommentCodeSlices<'a> {
}

/// Checks is `new` didn't miss any comment from `span`, if it removed any, return previous text
/// (if it fits in the width/offset, else return `None`), else return `new`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Before updating recover_comment_removed, it always returned Some(String). The previous comment about this function is no longer accurate, as it was written when the function used to call wrap_str a long time ago.

src/expr.rs Outdated
Comment on lines 1617 to 1633
match (orig_index_rw, new_index_rw) {
(_, Some(ref new_index_str)) if !new_index_str.contains('\n') => Some(format!(
(_, Ok(ref new_index_str)) if !new_index_str.contains('\n') => Ok(format!(
"{}{}[{}]",
expr_str,
indent.to_string_with_newline(context.config),
new_index_str,
)),
(None, Some(ref new_index_str)) => Some(format!(
(Err(_), Ok(ref new_index_str)) => Ok(format!(
"{}{}[{}]",
expr_str,
indent.to_string_with_newline(context.config),
new_index_str,
)),
(Some(ref index_str), _) => Some(format!("{expr_str}[{index_str}]")),
_ => None,
(Ok(ref index_str), _) => Ok(format!("{expr_str}[{index_str}]")),
(Err(_), Err(new_index_rw_err)) => Err(new_index_rw_err),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

orig_index_rw is the rewrite result of trying to fit the index on a single line, while new_index_rw is the rewrite result of placing the index on the next line. Since this is kinda first attempt and a second chance approach, it’s better to choose one error to propagate up when both results are err. I decided to propagate the error from the second attempt (new_index_rw_err) since it’s more generous in terms of width constraints.

This kind of matching orig_rw and new_rw is quite common, so if you have any thoughts on which one to return in this situation, please let me know.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's an interesting dilemma, but I think returning the second error in this case is fine. It might be worth leaving a comment in the code explaining some of this context and noting that the decision was somewhat arbitrary, but we decided to return the second rewrite failure.

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 another PR. Take a look at the review when you get a chance. There are a few minor things I'd like to address before merging.

src/expr.rs Outdated Show resolved Hide resolved
src/expr.rs Outdated Show resolved Hide resolved
src/expr.rs Outdated
Comment on lines 1617 to 1633
match (orig_index_rw, new_index_rw) {
(_, Some(ref new_index_str)) if !new_index_str.contains('\n') => Some(format!(
(_, Ok(ref new_index_str)) if !new_index_str.contains('\n') => Ok(format!(
"{}{}[{}]",
expr_str,
indent.to_string_with_newline(context.config),
new_index_str,
)),
(None, Some(ref new_index_str)) => Some(format!(
(Err(_), Ok(ref new_index_str)) => Ok(format!(
"{}{}[{}]",
expr_str,
indent.to_string_with_newline(context.config),
new_index_str,
)),
(Some(ref index_str), _) => Some(format!("{expr_str}[{index_str}]")),
_ => None,
(Ok(ref index_str), _) => Ok(format!("{expr_str}[{index_str}]")),
(Err(_), Err(new_index_rw_err)) => Err(new_index_rw_err),
Copy link
Contributor

Choose a reason for hiding this comment

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

It's an interesting dilemma, but I think returning the second error in this case is fine. It might be worth leaving a comment in the code explaining some of this context and noting that the decision was somewhat arbitrary, but we decided to return the second rewrite failure.

src/items.rs Outdated Show resolved Hide resolved
Comment on lines -483 to +484
..
}) => rewrite_bounded_lifetime(lifetime, bounds, context, shape)?,
span,
}) => rewrite_bounded_lifetime(lifetime, bounds, span, context, shape).ok()?,
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to add a rewrite_result method for ast::WherePredicate? I see that this only updates the rewrite method.

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 added a rewrite_result for ast::WherePredicate in different branch. This pr does not update it.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ding-young Is there a PR open for those changes?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see you just opened #6309

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 updating the code based on the suggestions!

Running the Diff Check

Edit: Job passed ✅

@ytmimi ytmimi merged commit 35b3889 into rust-lang:master Sep 1, 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