Skip to content

Commit 6191902

Browse files
authored
Merge pull request rust-lang#2507 from kngwyu/issue-2506
Issue 2506
2 parents 5025a53 + 8ea79aa commit 6191902

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

src/types.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,18 @@ impl Rewrite for ast::TraitRef {
616616
impl Rewrite for ast::Ty {
617617
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
618618
match self.node {
619-
ast::TyKind::TraitObject(ref bounds, ..) => bounds.rewrite(context, shape),
619+
ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
620+
// we have to consider 'dyn' keyword is used or not!!!
621+
let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn;
622+
// 4 is length of 'dyn '
623+
let shape = if is_dyn { shape.offset_left(4)? } else { shape };
624+
let res = bounds.rewrite(context, shape)?;
625+
if is_dyn {
626+
Some(format!("dyn {}", res))
627+
} else {
628+
Some(res)
629+
}
630+
}
620631
ast::TyKind::Ptr(ref mt) => {
621632
let prefix = match mt.mutbl {
622633
Mutability::Mutable => "*mut ",

tests/source/issue-2506.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(dyn_trait)]
2+
fn main() {
3+
// checks rustfmt doesn't remove dyn
4+
trait MyTrait {
5+
fn method(&self) -> u64;
6+
}
7+
fn f1(a: Box<dyn MyTrait>) {}
8+
9+
// checks if line wrap works correctly
10+
trait Very_______________________Long__________________Name____________________Trait {
11+
fn method(&self) -> u64;
12+
}
13+
14+
fn f2(a: Box<dyn Very_______________________Long__________________Name____________________Trait+ 'static,>) {}
15+
16+
}

tests/target/issue-2506.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(dyn_trait)]
2+
fn main() {
3+
// checks rustfmt doesn't remove dyn
4+
trait MyTrait {
5+
fn method(&self) -> u64;
6+
}
7+
fn f1(a: Box<dyn MyTrait>) {}
8+
9+
// checks if line wrap works correctly
10+
trait Very_______________________Long__________________Name____________________Trait
11+
{
12+
fn method(&self) -> u64;
13+
}
14+
15+
fn f2(
16+
a: Box<
17+
dyn Very_______________________Long__________________Name____________________Trait
18+
+ 'static,
19+
>,
20+
) {
21+
}
22+
}

0 commit comments

Comments
 (0)