diff --git a/src/types.rs b/src/types.rs index a67ba876591..5d57b99d32d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -521,7 +521,24 @@ impl Rewrite for ast::GenericBound { impl Rewrite for ast::GenericBounds { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { - join_bounds(context, shape, self, true) + if self.is_empty() { + return Some(String::new()); + } + + let span = mk_sp(self.get(0)?.span().lo(), self.last()?.span().hi()); + let has_paren = context.snippet(span).starts_with("("); + let bounds_shape = if has_paren { + shape.offset_left(1)?.sub_width(1)? + } else { + shape + }; + join_bounds(context, bounds_shape, self, true).map(|s| { + if has_paren { + format!("({})", s) + } else { + s + } + }) } } diff --git a/tests/source/issue-2506.rs b/tests/source/dyn_trait.rs similarity index 87% rename from tests/source/issue-2506.rs rename to tests/source/dyn_trait.rs index 94a927580f6..012643be92c 100644 --- a/tests/source/issue-2506.rs +++ b/tests/source/dyn_trait.rs @@ -1,5 +1,7 @@ #![feature(dyn_trait)] + fn main() { + // #2506 // checks rustfmt doesn't remove dyn trait MyTrait { fn method(&self) -> u64; @@ -13,4 +15,6 @@ fn main() { fn f2(a: Box) {} + // #2582 + let _: &dyn (::std::any::Any) = &msg; } diff --git a/tests/target/issue-2506.rs b/tests/target/dyn_trait.rs similarity index 88% rename from tests/target/issue-2506.rs rename to tests/target/dyn_trait.rs index 1e1971db85f..b6e2810a57c 100644 --- a/tests/target/issue-2506.rs +++ b/tests/target/dyn_trait.rs @@ -1,5 +1,7 @@ #![feature(dyn_trait)] + fn main() { + // #2506 // checks rustfmt doesn't remove dyn trait MyTrait { fn method(&self) -> u64; @@ -19,4 +21,7 @@ fn main() { >, ) { } + + // #2582 + let _: &dyn (::std::any::Any) = &msg; } diff --git a/tests/target/fn-simple.rs b/tests/target/fn-simple.rs index 6be9ad69b93..60b18a978ed 100644 --- a/tests/target/fn-simple.rs +++ b/tests/target/fn-simple.rs @@ -107,4 +107,4 @@ pub(crate) fn init() {} crate fn init() {} // #2630 -fn make_map String>(records: &Vec, key_fn: F) -> HashMap {} +fn make_map String)>(records: &Vec, key_fn: F) -> HashMap {}