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

Commit 593ac52

Browse files
authored
Merge pull request rust-lang#3138 from topecongiro/issue-3137
Simplify handling of parens around generic bound trait
2 parents 284583f + 7093bbe commit 593ac52

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

src/types.rs

Lines changed: 12 additions & 55 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, false)?
476+
join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
477477
);
478478
Some(result)
479479
}
@@ -489,13 +489,15 @@ impl Rewrite for ast::GenericBound {
489489
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
490490
match *self {
491491
ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => {
492-
match trait_bound_modifier {
492+
let snippet = context.snippet(self.span());
493+
let has_paren = snippet.starts_with("(") && snippet.ends_with(")");
494+
let rewrite = match trait_bound_modifier {
493495
ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape),
494-
ast::TraitBoundModifier::Maybe => {
495-
let rw = poly_trait_ref.rewrite(context, shape.offset_left(1)?)?;
496-
Some(format!("?{}", rw))
497-
}
498-
}
496+
ast::TraitBoundModifier::Maybe => poly_trait_ref
497+
.rewrite(context, shape.offset_left(1)?)
498+
.map(|s| format!("?{}", s)),
499+
};
500+
rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
499501
}
500502
ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
501503
}
@@ -508,14 +510,7 @@ impl Rewrite for ast::GenericBounds {
508510
return Some(String::new());
509511
}
510512

511-
let span = mk_sp(self.get(0)?.span().lo(), self.last()?.span().hi());
512-
let has_paren = context.snippet(span).starts_with('(');
513-
let bounds_shape = if has_paren {
514-
shape.offset_left(1)?.sub_width(1)?
515-
} else {
516-
shape
517-
};
518-
join_bounds(context, bounds_shape, self, true, has_paren)
513+
join_bounds(context, shape, self, true)
519514
}
520515
}
521516

@@ -751,7 +746,6 @@ fn join_bounds(
751746
shape: Shape,
752747
items: &[ast::GenericBound],
753748
need_indent: bool,
754-
has_paren: bool,
755749
) -> Option<String> {
756750
debug_assert!(!items.is_empty());
757751

@@ -762,36 +756,9 @@ fn join_bounds(
762756
};
763757
let type_strs = items
764758
.iter()
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-
})
759+
.map(|item| item.rewrite(context, shape))
775760
.collect::<Option<Vec<_>>>()?;
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-
}
761+
let result = type_strs.join(joiner);
795762
if items.len() <= 1 || (!result.contains('\n') && result.len() <= shape.width) {
796763
return Some(result);
797764
}
@@ -814,20 +781,10 @@ fn join_bounds(
814781
ast::GenericBound::Trait(..) => last_line_extendable(s),
815782
};
816783
let mut result = String::with_capacity(128);
817-
let mut closing_paren = has_paren;
818-
if has_paren {
819-
result.push('(');
820-
}
821784
result.push_str(&type_strs[0]);
822785
let mut can_be_put_on_the_same_line = is_bound_extendable(&result, &items[0]);
823786
let generic_bounds_in_order = is_generic_bounds_in_order(items);
824787
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-
}
831788
if generic_bounds_in_order && can_be_put_on_the_same_line {
832789
result.push_str(joiner);
833790
} else {

tests/source/type.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ macro_rules! foo {
9292

9393
type Target = ( FooAPI ) + 'static;
9494

95+
// #3137
96+
fn foo<T>(t: T)
97+
where
98+
T: ( FnOnce() -> () ) + Clone,
99+
U: ( FnOnce() -> () ) + 'static,
100+
{
101+
}
102+
95103
// #3117
96104
fn issue3117() {
97105
{

tests/target/type.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ macro_rules! foo {
9191

9292
type Target = (FooAPI) + 'static;
9393

94+
// #3137
95+
fn foo<T>(t: T)
96+
where
97+
T: (FnOnce() -> ()) + Clone,
98+
U: (FnOnce() -> ()) + 'static,
99+
{
100+
}
101+
94102
// #3117
95103
fn issue3117() {
96104
{

0 commit comments

Comments
 (0)