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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

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.

Comment on lines 294 to +299
let nested_shape = shape
.shrink_left(binder.len() + const_.len() + immovable.len() + coro.len() + mover.len())
.and_then(|shape| shape.sub_width(4))
.max_width_error(shape.width, span)?;
.shrink_left(
binder.len() + const_.len() + immovable.len() + coro.len() + mover.len(),
span,
)?
.sub_width(4, span)?;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could consider defining the offset outside of the call to shrink_left:

let offset = binder.len() + const_.len() + immovable.len() + coro.len() + mover.len();
let nested_shape = shape.shrink_left(offset, span)?.sub_width(4, span)?;

Comment on lines -216 to +220
pub(crate) fn block_left(&self, width: usize) -> Option<Shape> {
self.block_indent(width).sub_width(width)
pub(crate) fn block_left(&self, n: usize, span: Span) -> Result<Shape, ExceedsMaxWidthError> {
self.block_indent(n).sub_width(n, span)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: lets keep the variable named width. I'd like to keep this consistent for all the functions below as well.

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 had done that initially and it tripped me up because .map(|width| Shape { width, ... shadows the argument. I wanted to change it because the field width and the argument width are totally different things. Of course I can work around that though.

Copy link
Contributor

@ytmimi ytmimi Nov 20, 2024

Choose a reason for hiding this comment

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

I'd be fine naming it something other than width, but I think it would be nice if it were more descriptive than n. Maybe offset? I'm open to suggestions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Offset is good. Or amount, distance, delta.

Copy link
Contributor

Choose a reason for hiding this comment

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

Any of those work for me. I'll leave it up to you on which one you think would work best.

Comment on lines +250 to +252
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants