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

rust-lang/style-team#189: rhs-should-use-indent-of-last-line-of-lhs #6305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,6 +2124,24 @@ pub(crate) fn rewrite_assign_rhs_expr<R: Rewrite>(
rhs_kind: &RhsAssignKind<'_>,
rhs_tactics: RhsTactics,
) -> RewriteResult {
let get_rhs_shape = || {
let last_line = lhs.lines().last().unwrap_or_default();
let tab_spaces = context.config.tab_spaces();
let new_shape = shape
.block_indent(tab_spaces)
.saturating_sub_width(tab_spaces);
Comment on lines +2130 to +2132
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you explain the call to .saturating_sub_width(tab_spaces). I'm not sure if that's necessary.

Copy link
Contributor Author

@johnhuichen johnhuichen Sep 4, 2024

Choose a reason for hiding this comment

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

Without saturating_sub_width,

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info =
            long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
    }
}

is formatted to

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info = long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
    }
}

and throws

line formatted, but exceeded maximum width (maximum: 100 (see `max_width` option), found: 101)

rather than

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info =
                long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
    }
}

I think the remaining width is not accounted for properly if I only use block_indent

Copy link
Contributor

Choose a reason for hiding this comment

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

Could you explain why we're using tab_spaces? What happens if we don't use .saturating_sub_width(tab_spaces), but the rhs was even longer? Does it wrap correctly then?

Part of me wonders if the shape is off to begin with.

Copy link
Contributor Author

@johnhuichen johnhuichen Sep 4, 2024

Choose a reason for hiding this comment

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

the shape that was used before adding extra block_indent is

old shape = Shape { width: 91, indent: Indent { block_indent: 8, alignment: 0 }, offset: 0 }

and the new shape with block_indent but without saturating_sub_width is

new shape = Shape { width: 91, indent: Indent { block_indent: 12, alignment: 0 }, offset: 0

Copy link
Contributor Author

@johnhuichen johnhuichen Sep 4, 2024

Choose a reason for hiding this comment

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

the new shape is also the shape of the last line of lhs

            .extra_info = long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;

has 12 spaces before .extra_info and .extra_info = long_long_long_long_long_long_long_long_long_long_long_long_long_long_long; is 89 chars. So the remaining width shouldn't be 91, but 87

i.e. when indent was 8 spaces, width = 91, max width = 100, so
width = max - indent - 1

when indent is 12, width = 100 - 12 - 1 =87

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@ytmimi I think the above should answer your last question in this PR. Can you take a look?

Copy link
Contributor

@ytmimi ytmimi Sep 9, 2024

Choose a reason for hiding this comment

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

Thanks for the detailed explanation. I didn't have time to revisit this PR over the weekend, but I plan on giving this another look later this week

let extra_indent_string = new_shape.to_string(&context.config);
if last_line.starts_with(extra_indent_string.as_ref()) {
new_shape
} else {
shape
}
};
let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
get_rhs_shape()
} else {
shape
};
let last_line_width = last_line_width(lhs).saturating_sub(if lhs.contains('\n') {
shape.indent.width()
} else {
Expand Down
7 changes: 7 additions & 0 deletions src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ impl Shape {
offset_indent.to_string_inner(config, 0)
}

/// similar to to_string_with_newline, except the result does not start with a new line
pub(crate) fn to_string(&self, config: &Config) -> Cow<'static, str> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably worth adding a doc comment so that we know that this function gives us a single line of indentation.

let mut offset_indent = self.indent;
offset_indent.alignment = self.offset;
offset_indent.to_string_inner(config, 1)
}

/// Creates a `Shape` with a virtually infinite width.
pub(crate) fn infinite_width(&self) -> Shape {
Shape {
Expand Down
24 changes: 24 additions & 0 deletions tests/source/rust-lang/style-team#189.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
}
}
24 changes: 24 additions & 0 deletions tests/target/rust-lang/style-team#189.rs
Copy link
Contributor

Choose a reason for hiding this comment

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

There are a few other test cases that I think we should add:

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info = Some(ExtraInfo {
                parent,
                count: count as u16,
                children: children.into_boxed_slice(),
            }) + Some(ExtraInfo {
                parent,
                count: count as u16,
                children: children.into_boxed_slice(),
            });
    }
}

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info =
                long_long_long_long_long_long_long_long_long_long_long_long_long_long_long
                    .as_mut()
                    .another_call()
                    .get_extra_info();
    }
}

impl SomeType {
    fn method(&mut self) {
        self.array[array_index as usize]
            .as_mut()
            .expect("thing must exist")
            .extra_info =
                long_long_long_long_long_long_long_long_long_long_long_long_long_long_long
                    .as_mut()
                    .another_call()
                    .get_extra_info() + Some(ExtraInfo {
                        parent,
                        count: count as u16,
                        children: children.into_boxed_slice(),
                    });
    }
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// rustfmt-style_edition: 2024

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info = Some(ExtraInfo {
parent,
count: count as u16,
children: children.into_boxed_slice(),
});
}
}

impl SomeType {
fn method(&mut self) {
self.array[array_index as usize]
.as_mut()
.expect("thing must exist")
.extra_info =
long_long_long_long_long_long_long_long_long_long_long_long_long_long_long;
}
}
Loading