Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 945fb50

Browse files
authored
Merge pull request rust-lang#3091 from topecongiro/issue-3060
Do not add parens around lifetimes
2 parents c4a8cdc + 0ac68c9 commit 945fb50

File tree

3 files changed

+62
-10
lines changed

3 files changed

+62
-10
lines changed

src/types.rs

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ fn rewrite_bounded_lifetime(
473473
"{}{}{}",
474474
result,
475475
colon,
476-
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
476+
join_bounds(context, shape.sub_width(overhead)?, bounds, true, false)?
477477
);
478478
Some(result)
479479
}
@@ -515,13 +515,7 @@ impl Rewrite for ast::GenericBounds {
515515
} else {
516516
shape
517517
};
518-
join_bounds(context, bounds_shape, self, true).map(|s| {
519-
if has_paren {
520-
format!("({})", s)
521-
} else {
522-
s
523-
}
524-
})
518+
join_bounds(context, bounds_shape, self, true, has_paren)
525519
}
526520
}
527521

@@ -757,17 +751,47 @@ fn join_bounds(
757751
shape: Shape,
758752
items: &[ast::GenericBound],
759753
need_indent: bool,
754+
has_paren: bool,
760755
) -> Option<String> {
756+
debug_assert!(!items.is_empty());
757+
761758
// Try to join types in a single line
762759
let joiner = match context.config.type_punctuation_density() {
763760
TypeDensity::Compressed => "+",
764761
TypeDensity::Wide => " + ",
765762
};
766763
let type_strs = items
767764
.iter()
768-
.map(|item| item.rewrite(context, shape))
765+
.map(|item| {
766+
item.rewrite(
767+
context,
768+
if has_paren {
769+
shape.sub_width(1)?.offset_left(1)?
770+
} else {
771+
shape
772+
},
773+
)
774+
})
769775
.collect::<Option<Vec<_>>>()?;
770-
let result = type_strs.join(joiner);
776+
let mut result = String::with_capacity(128);
777+
let mut closing_paren = has_paren;
778+
if has_paren {
779+
result.push('(');
780+
}
781+
result.push_str(&type_strs[0]);
782+
if has_paren && type_strs.len() == 1 {
783+
result.push(')');
784+
}
785+
for (i, type_str) in type_strs[1..].iter().enumerate() {
786+
if closing_paren {
787+
if let ast::GenericBound::Outlives(..) = items[i + 1] {
788+
result.push(')');
789+
closing_paren = false;
790+
}
791+
}
792+
result.push_str(joiner);
793+
result.push_str(type_str);
794+
}
771795
if items.len() <= 1 || (!result.contains('\n') && result.len() <= shape.width) {
772796
return Some(result);
773797
}
@@ -790,10 +814,20 @@ fn join_bounds(
790814
ast::GenericBound::Trait(..) => last_line_extendable(s),
791815
};
792816
let mut result = String::with_capacity(128);
817+
let mut closing_paren = has_paren;
818+
if has_paren {
819+
result.push('(');
820+
}
793821
result.push_str(&type_strs[0]);
794822
let mut can_be_put_on_the_same_line = is_bound_extendable(&result, &items[0]);
795823
let generic_bounds_in_order = is_generic_bounds_in_order(items);
796824
for (bound, bound_str) in items[1..].iter().zip(type_strs[1..].iter()) {
825+
if closing_paren {
826+
if let ast::GenericBound::Outlives(..) = bound {
827+
closing_paren = false;
828+
result.push(')');
829+
}
830+
}
797831
if generic_bounds_in_order && can_be_put_on_the_same_line {
798832
result.push_str(joiner);
799833
} else {

tests/source/type.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,12 @@ impl Future<Item = (), Error = SomeError> + 'a,
8282
'a + 'b +
8383
'c {
8484
}
85+
86+
// #3060
87+
macro_rules! foo {
88+
($foo_api: ty) => {
89+
type Target = ( $foo_api ) + 'static;
90+
}
91+
}
92+
93+
type Target = ( FooAPI ) + 'static;

tests/target/type.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,12 @@ pub fn do_something<'a, T: Trait1 + Trait2 + 'a>(
8181
Error = SomeError,
8282
> + 'a + 'b + 'c {
8383
}
84+
85+
// #3060
86+
macro_rules! foo {
87+
($foo_api: ty) => {
88+
type Target = ($foo_api) + 'static;
89+
};
90+
}
91+
92+
type Target = (FooAPI) + 'static;

0 commit comments

Comments
 (0)