Skip to content

Commit

Permalink
Fix for issue 4689 - indentation on multiline single generic bound (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
davidBar-On authored Oct 23, 2021
1 parent 1546005 commit cc5521d
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 4 deletions.
49 changes: 45 additions & 4 deletions src/formatting/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,24 @@ fn join_bounds_inner(
ast::GenericBound::Trait(..) => last_line_extendable(s),
};

// Whether a PathSegment segment includes internal array containing more than one item
let is_segment_with_multi_items_array = |seg: &ast::PathSegment| {
if let Some(args_in) = &seg.args {
match &**args_in {
ast::AngleBracketed(args) => {
if args.args.len() > 1 {
true
} else {
false
}
}
_ => false,
}
} else {
false
}
};

let result = items.iter().enumerate().try_fold(
(String::new(), None, false),
|(strs, prev_trailing_span, prev_extendable), (i, item)| {
Expand Down Expand Up @@ -1046,10 +1064,33 @@ fn join_bounds_inner(
},
)?;

if !force_newline
&& items.len() > 1
&& (result.0.contains('\n') || result.0.len() > shape.width)
{
// Whether retry the function with forced newline is needed:
// Only if result is not already multiline and did not exceed line width,
// and either there is more than one item;
// or the single item is of type `Trait`,
// and any of the internal arrays contains more than one item;
let retry_with_force_newline =
if force_newline || (!result.0.contains('\n') && result.0.len() <= shape.width) {
false
} else {
if items.len() > 1 {
true
} else {
match items[0] {
ast::GenericBound::Trait(ref poly_trait_ref, ..) => {
let segments = &poly_trait_ref.trait_ref.path.segments;
if segments.len() > 1 {
true
} else {
is_segment_with_multi_items_array(&segments[0])
}
}
_ => false,
}
}
};

if retry_with_force_newline {
join_bounds_inner(context, shape, items, need_indent, true)
} else {
Some(result.0)
Expand Down
147 changes: 147 additions & 0 deletions tests/source/issue-4689.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Based on the issue description
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + fmt::Write
{
//
}
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + fmt::Write1 + fmt::Write2
{
//
}
pub trait PrettyPrinter<'tcx>:
fmt::Write + Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}
pub trait PrettyPrinter<'tcx>:
fmt::Write + Printer1<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + Printer2<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}

// Some test cases to ensure other cases formatting were not changed
fn f() -> Box<
FnMut() -> Thing<
WithType = LongItemName,
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
>,
> {
}
fn f() -> Box<
FnMut() -> Thing<
WithType = LongItemName,
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
> + fmt::Write1
+ fmt::Write2,
> {
}

fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
)
{
}
fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
) + fmt::Write
{
}

fn elaborate_bounds<F>(mut mk_cand: F)
where
F: for<> FnMut(
&mut ProbeContext<>,
ty::PolyTraitRefffffffffffffffffffffffffffffffff<>,
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
),
{
}
fn elaborate_bounds<F>(mut mk_cand: F)
where
F: for<> FnMut(
&mut ProbeContext<>,
ty::PolyTraitRefffffffffffffffffffffffffffffffff<>,
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
) + fmt::Write,
{
}

fn build_sorted_static_get_entry_names(
mut entries: entryyyyyyyy,
) -> (
impl Fn(
AlphabeticalTraversal,
Seconddddddddddddddddddddddddddddddddddd
) -> Parammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+ Sendddddddddddddddddddddddddddddddddddddddddddd
) {
}

pub trait SomeTrait:
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
{
}

trait B = where
for<'b> &'b Self: Send
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;
150 changes: 150 additions & 0 deletions tests/target/issue-4689.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Based on the issue description
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + fmt::Write
{
//
}
pub trait PrettyPrinter<'tcx>:
Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + fmt::Write1
+ fmt::Write2
{
//
}
pub trait PrettyPrinter<'tcx>:
fmt::Write
+ Printer<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}
pub trait PrettyPrinter<'tcx>:
fmt::Write
+ Printer1<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
> + Printer2<
'tcx,
Error = fmt::Error,
Path = Self,
Region = Self,
Type = Self,
DynExistential = Self,
Const = Self,
>
{
//
}

// Some test cases to ensure other cases formatting were not changed
fn f() -> Box<
FnMut() -> Thing<
WithType = LongItemName,
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
>,
> {
}
fn f() -> Box<
FnMut() -> Thing<
WithType = LongItemName,
Error = LONGLONGLONGLONGLONGONGEvenLongerErrorNameLongerLonger,
> + fmt::Write1
+ fmt::Write2,
> {
}

fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
),
{
}
fn foo<F>(foo2: F)
where
F: Fn(
// this comment is deleted
) + fmt::Write,
{
}

fn elaborate_bounds<F>(mut mk_cand: F)
where
F: FnMut(
&mut ProbeContext,
ty::PolyTraitRefffffffffffffffffffffffffffffffff,
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
),
{
}
fn elaborate_bounds<F>(mut mk_cand: F)
where
F: FnMut(
&mut ProbeContext,
ty::PolyTraitRefffffffffffffffffffffffffffffffff,
tyyyyyyyyyyyyyyyyyyyyy::AssociatedItem,
) + fmt::Write,
{
}

fn build_sorted_static_get_entry_names(
mut entries: entryyyyyyyy,
) -> (
impl Fn(
AlphabeticalTraversal,
Seconddddddddddddddddddddddddddddddddddd,
) -> Parammmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm
+ Sendddddddddddddddddddddddddddddddddddddddddddd
) {
}

pub trait SomeTrait:
Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ Eqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
{
}

trait B = where
for<'b> &'b Self: Send
+ Cloneeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
+ Copyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy;

0 comments on commit cc5521d

Please sign in to comment.