Skip to content

Commit cdfa2f8

Browse files
ytmimicalebcartwright
authored andcommitted
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 a9ae746 commit cdfa2f8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

src/types.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -683,21 +683,19 @@ impl Rewrite for ast::Ty {
683683
match self.kind {
684684
ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
685685
// we have to consider 'dyn' keyword is used or not!!!
686-
let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn;
687-
// 4 is length of 'dyn '
688-
let shape = if is_dyn { shape.offset_left(4)? } else { shape };
686+
let (shape, prefix) = match tobj_syntax {
687+
ast::TraitObjectSyntax::Dyn => (shape.offset_left(4)?, "dyn "),
688+
ast::TraitObjectSyntax::DynStar => (shape.offset_left(5)?, "dyn* "),
689+
ast::TraitObjectSyntax::None => (shape, ""),
690+
};
689691
let mut res = bounds.rewrite(context, shape)?;
690692
// We may have falsely removed a trailing `+` inside macro call.
691693
if context.inside_macro() && bounds.len() == 1 {
692694
if context.snippet(self.span).ends_with('+') && !res.ends_with('+') {
693695
res.push('+');
694696
}
695697
}
696-
if is_dyn {
697-
Some(format!("dyn {}", res))
698-
} else {
699-
Some(res)
700-
}
698+
Some(format!("{}{}", prefix, res))
701699
}
702700
ast::TyKind::Ptr(ref mt) => {
703701
let prefix = match mt.mutbl {

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)