Skip to content
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
2 changes: 1 addition & 1 deletion src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,7 @@ where
pre_comment,
pre_comment_style,
// leave_last is set to true only for rewrite_items
item: if self.inner.peek().is_none() && self.leave_last {
item: if is_last && self.leave_last {
Err(RewriteError::SkipFormatting)
} else {
(self.get_item_string)(&item)
Expand Down
2 changes: 1 addition & 1 deletion src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,7 @@ impl<'a> Context<'a> {
|item| item.rewrite_result(self.context, self.nested_shape),
span.lo(),
span.hi(),
true,
self.items.len() > 1,
);
let mut list_items: Vec<_> = items.collect();

Expand Down
45 changes: 39 additions & 6 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,13 +725,46 @@ impl Rewrite for ast::GenericParam {
TypeDensity::Compressed => "=",
TypeDensity::Wide => " = ",
};
param.push_str(eq_str);
let budget = shape
// Need to allow the space required for the rewrite instead of bailing
// regardless of if the line overflows. If it doesn't overflow, it's split.
// If it still overflows, then the identifier is too long,
// which the user will have to deal with.
let rewrite = def.rewrite_result(
context,
Shape::legacy(usize::MAX, shape.indent + param.len()),
)?;
let can_fit_in_line = shape
.width
.checked_sub(param.len())
.max_width_error(shape.width, self.span())?;
let rewrite =
def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
.saturating_sub(param.len())
.saturating_sub(eq_str.len())
.checked_sub(rewrite.len())
.is_some();
if can_fit_in_line {
param.push_str(eq_str);
} else {
// Begin a new line
param.push('\n');
for _ in 0..shape.indent.block_indent {
param.push(' ');
}
let can_fit_in_line_with_eq_str = shape
.width
.saturating_sub(eq_str.len().min(2))
.checked_sub(rewrite.len())
.is_some();
if can_fit_in_line_with_eq_str {
// Fits together with eq, remove leading whitespace
param.push_str(eq_str.trim_start());
} else {
// Split into three lines, might fit, might not, remove all whitespace,
// eq goes on its own line
param.push_str(eq_str.trim());
param.push('\n');
for _ in 0..shape.indent.block_indent {
param.push(' ');
}
}
}
param.push_str(&rewrite);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/source/issue-6052/repro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub enum ManagementFrameBody<
'a,
RateIterator = SupportedRatesTLVReadRateIterator<'a>,
ExtendedRateIterator = ExtendedSupportedRatesTLVReadRateIterator<'a>,
TLVIterator: IntoIterator<Item = IEEE80211TLV<'a, RateIterator, ExtendedRateIterator>> = TLVReadIterator<'a>,
ActionFramePayload = &'a [u8],
>
{
Action(ActionFrameBody<ActionFramePayload>),
ActionNoAck(ActionFrameBody<ActionFramePayload>),
Beacon(BeaconFrameBody<'a, RateIterator, ExtendedRateIterator, TLVIterator>),
ATIM,
Unknown {
sub_type: ManagementFrameSubtype,
body: &'a [u8],
},
}
21 changes: 21 additions & 0 deletions tests/target/issue-6052/overflows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Passing the max-line boundary seems to be, combined with the other stuff, causing the panic
pub enum Dummy<
SomeVeryLongStructDeclarationAsItemMakingTheLineOverflowTheRightHandSideAssignmentIsImportant
= MyDefault,
> {}

pub enum Dummy2<
SomeVeryLongStructDeclarationAsItemMakingTheLineOverflowTheRightHandSideAssignmentIsImport
= MyDefault,
> {}

pub enum Dummy3<
SomeVeryLongStructDeclarationAsItemMakingTheLineOverflowTheRightHandSideAssignmentIsImport
=
MyDefaultThatIsAlsoTooLongToBeFitIntoTheNextLineCausingATripleSplitThisMayBeOverdoingItOrNotIdk,
> {}

pub enum Dummy4<
SomeVeryLongStructDeclarationAsItemMakingTheLineOverflowTheRightHandSideAssignmentIsImport
= MyDefaultThatIsAlsoTooLongToBeFitIntoTheNextLineCausingATripleSplitThisMayBeOverdoingItOrNot,
> {}
17 changes: 17 additions & 0 deletions tests/target/issue-6052/repro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
pub enum ManagementFrameBody<
'a,
RateIterator = SupportedRatesTLVReadRateIterator<'a>,
ExtendedRateIterator = ExtendedSupportedRatesTLVReadRateIterator<'a>,
TLVIterator: IntoIterator<Item = IEEE80211TLV<'a, RateIterator, ExtendedRateIterator>>
= TLVReadIterator<'a>,
ActionFramePayload = &'a [u8],
> {
Action(ActionFrameBody<ActionFramePayload>),
ActionNoAck(ActionFrameBody<ActionFramePayload>),
Beacon(BeaconFrameBody<'a, RateIterator, ExtendedRateIterator, TLVIterator>),
ATIM,
Unknown {
sub_type: ManagementFrameSubtype,
body: &'a [u8],
},
}
Loading