diff --git a/src/types.rs b/src/types.rs index 7f96a2299a3..fd6b6e3ca56 100644 --- a/src/types.rs +++ b/src/types.rs @@ -616,7 +616,18 @@ impl Rewrite for ast::TraitRef { impl Rewrite for ast::Ty { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { match self.node { - ast::TyKind::TraitObject(ref bounds, ..) => bounds.rewrite(context, shape), + ast::TyKind::TraitObject(ref bounds, tobj_syntax) => { + // we have to consider 'dyn' keyword is used or not!!! + let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn; + // 4 is length of 'dyn ' + let shape = if is_dyn { shape.offset_left(4)? } else { shape }; + let res = bounds.rewrite(context, shape)?; + if is_dyn { + Some(format!("dyn {}", res)) + } else { + Some(res) + } + } ast::TyKind::Ptr(ref mt) => { let prefix = match mt.mutbl { Mutability::Mutable => "*mut ", diff --git a/tests/source/issue-2506.rs b/tests/source/issue-2506.rs new file mode 100644 index 00000000000..fd0ecc9c661 --- /dev/null +++ b/tests/source/issue-2506.rs @@ -0,0 +1,16 @@ +#![feature(dyn_trait)] +fn main() { + // checks rustfmt doesn't remove dyn + trait MyTrait { + fn method(&self) -> u64; + } + fn f1(a: Box) {} + + // checks if line wrap works correctly + trait Very_______________________Long__________________Name____________________Trait { + fn method(&self) -> u64; + } + + fn f2(a: Box) {} + +} diff --git a/tests/target/issue-2506.rs b/tests/target/issue-2506.rs new file mode 100644 index 00000000000..ee2debee9fb --- /dev/null +++ b/tests/target/issue-2506.rs @@ -0,0 +1,22 @@ +#![feature(dyn_trait)] +fn main() { + // checks rustfmt doesn't remove dyn + trait MyTrait { + fn method(&self) -> u64; + } + fn f1(a: Box) {} + + // checks if line wrap works correctly + trait Very_______________________Long__________________Name____________________Trait + { + fn method(&self) -> u64; + } + + fn f2( + a: Box< + dyn Very_______________________Long__________________Name____________________Trait + + 'static, + >, + ) { + } +}