Skip to content

Commit

Permalink
Handle dyn* syntax when rewriting ast::TyKind::TraitObject
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ytmimi committed Sep 30, 2022
1 parent ef91154 commit 934c210
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -664,20 +664,22 @@ impl Rewrite for ast::Ty {
match self.kind {
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 shape = match tobj_syntax {
ast::TraitObjectSyntax::Dyn => shape.offset_left(4)?, // 4 is offset 'dyn '
ast::TraitObjectSyntax::DynStar => shape.offset_left(5)?, // 5 is offset 'dyn* '
ast::TraitObjectSyntax::None => shape,
};
let mut res = bounds.rewrite(context, shape)?;
// We may have falsely removed a trailing `+` inside macro call.
if context.inside_macro() && bounds.len() == 1 {
if context.snippet(self.span).ends_with('+') && !res.ends_with('+') {
res.push('+');
}
}
if is_dyn {
Some(format!("dyn {}", res))
} else {
Some(res)
match tobj_syntax {
ast::TraitObjectSyntax::Dyn => Some(format!("dyn {}", res)),
ast::TraitObjectSyntax::DynStar => Some(format!("dyn* {}", res)),
ast::TraitObjectSyntax::None => Some(res),
}
}
ast::TyKind::Ptr(ref mt) => {
Expand Down
10 changes: 10 additions & 0 deletions tests/target/issue_5542.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#![feature(dyn_star)]
#![allow(incomplete_features)]

use core::fmt::Debug;

fn main() {
let i = 42;
let dyn_i = i as dyn* Debug;
dbg!(dyn_i);
}

0 comments on commit 934c210

Please sign in to comment.