Skip to content

Commit 934c210

Browse files
committed
Handle dyn* syntax when rewriting ast::TyKind::TraitObject
Resolves 5542 Prior to rust-lang/rust#101212 the `ast::TraitObjectSyntax` enum only had two variants `Dyn` and `None`. The PR that introduced the `dyn*` syntax added a new variant `DynStar`, but did not update the formatting rules to account for the new variant. Now the new `DynStar` variant is properly handled and is no longer removed by rustfmt.
1 parent ef91154 commit 934c210

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/types.rs

+9-7
Original file line numberDiff line numberDiff line change
@@ -664,20 +664,22 @@ impl Rewrite for ast::Ty {
664664
match self.kind {
665665
ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
666666
// we have to consider 'dyn' keyword is used or not!!!
667-
let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn;
668-
// 4 is length of 'dyn '
669-
let shape = if is_dyn { shape.offset_left(4)? } else { shape };
667+
let shape = match tobj_syntax {
668+
ast::TraitObjectSyntax::Dyn => shape.offset_left(4)?, // 4 is offset 'dyn '
669+
ast::TraitObjectSyntax::DynStar => shape.offset_left(5)?, // 5 is offset 'dyn* '
670+
ast::TraitObjectSyntax::None => shape,
671+
};
670672
let mut res = bounds.rewrite(context, shape)?;
671673
// We may have falsely removed a trailing `+` inside macro call.
672674
if context.inside_macro() && bounds.len() == 1 {
673675
if context.snippet(self.span).ends_with('+') && !res.ends_with('+') {
674676
res.push('+');
675677
}
676678
}
677-
if is_dyn {
678-
Some(format!("dyn {}", res))
679-
} else {
680-
Some(res)
679+
match tobj_syntax {
680+
ast::TraitObjectSyntax::Dyn => Some(format!("dyn {}", res)),
681+
ast::TraitObjectSyntax::DynStar => Some(format!("dyn* {}", res)),
682+
ast::TraitObjectSyntax::None => Some(res),
681683
}
682684
}
683685
ast::TyKind::Ptr(ref mt) => {

tests/target/issue_5542.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(dyn_star)]
2+
#![allow(incomplete_features)]
3+
4+
use core::fmt::Debug;
5+
6+
fn main() {
7+
let i = 42;
8+
let dyn_i = i as dyn* Debug;
9+
dbg!(dyn_i);
10+
}

0 commit comments

Comments
 (0)