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

return RewriteResult for rewrite_block and rewrite_closure #6235

Merged
merged 2 commits into from
Jul 15, 2024

Conversation

ding-young
Copy link
Contributor

Tracked by #6206

Description

modify the signature of rewrite_block and rewrite_closure to return RewriteResult

Side notes

  • I did not change the return type of rewrite_empty_block(it returns None when given block is not empty) since every caller tries rewrite_empty_block first and then applies formatting logic for non empty block if it returns None. It seems that there's no need to propagate RewriteError upward from this function currently.

@ding-young ding-young changed the title Rw result block return RewriteResult for rewrite_block and rewrite_closure Jul 10, 2024
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.

Mostly looks like a simple translation from Option<String> -> RewriteResult, but I not 100% sure about one of the calls to rewrite_block_with_visitor. Could you double check that one.

Comment on lines -606 to 624
let result = rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true);
if let Some(ref result_str) = result {
if allow_single_line && result_str.lines().count() <= 3 {
if let rw @ Some(_) =
rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
{
return rw;
}
let result_str =
rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true)?;
if allow_single_line && result_str.lines().count() <= 3 {
if let rw @ Ok(_) = rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
{
return rw;
}
}

result
Ok(result_str)
Copy link
Contributor

Choose a reason for hiding this comment

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

This doesn't look like a one-to-one translations from Option<String> -> RewriteResult. Also, doesn't rewrite_block_with_visitor now return RewriteResult? I would have expected to see something closer this:

    let result = rewrite_block_with_visitor(context, &prefix, block, attrs, label, shape, true);
    if let Ok(ref result_str) = result {
        if allow_single_line && result_str.lines().count() <= 3 {
            if let rw @ Ok(_) = rewrite_single_line_block(context, &prefix, block, attrs, label, shape)
            {
                return rw;
            }
        }
    }

Copy link
Contributor Author

@ding-young ding-young Jul 10, 2024

Choose a reason for hiding this comment

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

Yes, it isn't a one-to-one translation but I made this change intentionally.

From what I understood of the original code:

(1) If rewrite_block_with_visitor fails, return None.
(2) If rewrite_block_with_visitor succeeds, check whether we should try to format it into a single-line block.
      a. If rewrite_single_line_block succeeds, return that result.
      b. If rewrite_single_line_block fails, just return the result from rewrite_block_with_visitor.

My intention was to simplify if let Ok(ref result_str) = result {...} with using ? operator on calling rewrite_block_with_visitor. I thought it won't make a difference in control flow or formatting logic described above.
Still, if you think this change does not worth it or it actually breaks the original control flow, please tell me.

Copy link
Contributor

Choose a reason for hiding this comment

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

After taking another look at this the change seems to preserve the original behavior so we're good here!

@ytmimi ytmimi added GSoC Google Summer of Code pr-waiting-on-author and removed pr-not-reviewed labels Jul 10, 2024
@ytmimi ytmimi merged commit 071ca7d into rust-lang:master Jul 15, 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.

None yet

3 participants