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 Result from Shape methods #6398

Merged
merged 2 commits into from
Nov 27, 2024
Merged

Conversation

camsteffen
Copy link
Contributor

@camsteffen camsteffen commented Nov 18, 2024

  • The Shape modifier methods now return Result<Shape, ExeedsMaxWidthError>
  • There are _opt variants of the methods for when an Option is still needed
    • I believe this will remain useful in the long term, since sometimes we want to fallback and never error out
  • There is a minor change in behavior for when multiple methods are chained together - the width used in the error will be the last successfully modified width. I think this is okay?

@ding-young
Copy link
Contributor

IMO, using the last successfully modified width is fine. Actually, that might be the more accurate width in some cases.

@calebcartwright
Copy link
Member

IMO, using the last successfully modified width is fine. Actually, that might be the more accurate width in some cases.

by the way @ding-young thank you for all your excellent work this year! I know you worked closely with @ytmimi and we didn't get a chance to interact much, but i really appreciate all the work you did and I'm glad that you have expressed an interest in remaining engaged with it

@calebcartwright
Copy link
Member

There is a minor change in behavior for when multiple methods are chained together - the width used in the error will be the last successfully modified width. I think this is okay?

@camsteffen - re: this item, i've not looked at the diff yet but if there's a change to the formatting output (even if it's an improvement) then we'll need to edition gate it behind style edition 2027

e.g.

let init_str = if style_edition >= StyleEdition::Edition2024 {

@camsteffen
Copy link
Contributor Author

camsteffen commented Nov 20, 2024

There is a minor change in behavior for when multiple methods are chained together - the width used in the error will be the last successfully modified width. I think this is okay?

@camsteffen - re: this item, i've not looked at the diff yet but if there's a change to the formatting output (even if it's an improvement) then we'll need to edition gate it behind style edition 2027

There should be no change in user-facing behavior, formatting. Just a change to the contents of the error object, which is currently never used IIUC.

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.

Really happy with the simplification! Left a few minor comments inline.

src/closures.rs Outdated Show resolved Hide resolved
src/shape.rs Outdated Show resolved Hide resolved
src/shape.rs Outdated
Comment on lines 250 to 260
self.width
.checked_sub(n)
.map(|width| Shape { width, ..*self })
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to define sub_width_opt, and really all the *_opt functions in terms of their Result<T, MaxWidthError> versions?

    pub(crate) fn sub_width_opt(&self, n: usize) -> Option<Shape> {
        self.sub_width(n, rustc_span::DUMMY_SP).ok()
    }

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 considered that and I opted to avoid using DUMMY_SP. But it's not a strong preference for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another possibility is to go the other way around with something like sub_width_opt(n).ok_or_else(|| self.max_width_error(span))

Copy link
Contributor

Choose a reason for hiding this comment

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

I think you're second idea would be a nice way to avoid using a DUMMY_SP.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done!

Avoid "width" since it can get confused with the width field.
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 helping simplify the code with these Shape updates!

@ytmimi ytmimi merged commit 2a64f21 into rust-lang:master Nov 27, 2024
26 checks passed
@ytmimi ytmimi added release-notes Needs an associated changelog entry and removed pr-waiting-on-author labels Nov 27, 2024
@camsteffen camsteffen deleted the shaperr branch November 27, 2024 02:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-notes Needs an associated changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants