diff --git a/src/items.rs b/src/items.rs index 7771344f3db..0c152d98767 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1087,6 +1087,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) } } +pub fn format_trait_alias( + context: &RewriteContext, + ident: ast::Ident, + generics: &ast::Generics, + ty_param_bounds: &ast::TyParamBounds, + shape: Shape, +) -> Option { + let alias = ident.name.as_str(); + // 6 = "trait ", 2 = " =" + let g_shape = shape.offset_left(6 + alias.len())?.sub_width(2)?; + let generics_str = rewrite_generics(context, generics, g_shape, generics.span)?; + let lhs = format!("trait {}{} =", alias, generics_str); + // 1 = ";" + rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";") +} + fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option { let header_str = format_header(p.prefix, p.ident, p.vis); let generics_str = if let Some(generics) = p.generics { diff --git a/src/visitor.rs b/src/visitor.rs index a02baaf14da..ae9f7a091f9 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -22,8 +22,8 @@ use comment::{combine_strs_with_missing_comments, contains_comment, remove_trail CodeCharKind, CommentCodeSlices, FindUncommented}; use comment::rewrite_comment; use config::{BraceStyle, Config}; -use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type, - rewrite_type_alias, FnSig, StaticParts, StructParts}; +use items::{format_impl, format_trait, format_trait_alias, rewrite_associated_impl_type, + rewrite_associated_type, rewrite_type_alias, FnSig, StaticParts, StructParts}; use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorPlace, SeparatorTactic}; use macros::{rewrite_macro, MacroPosition}; @@ -373,9 +373,16 @@ impl<'b, 'a: 'b> FmtVisitor<'a> { let rw = format_trait(&self.get_context(), item, self.block_indent); self.push_rewrite(item.span, rw); } - ast::ItemKind::TraitAlias(..) => { - // FIXME: #2283. - self.push_rewrite(item.span, None); + ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => { + let shape = Shape::indented(self.block_indent, self.config); + let rw = format_trait_alias( + &self.get_context(), + item.ident, + generics, + ty_param_bounds, + shape, + ); + self.push_rewrite(item.span, rw); } ast::ItemKind::ExternCrate(_) => { let rw = rewrite_extern_crate(&self.get_context(), item); diff --git a/tests/source/trait.rs b/tests/source/trait.rs index e625c4c9703..823b1e44a1e 100644 --- a/tests/source/trait.rs +++ b/tests/source/trait.rs @@ -71,3 +71,19 @@ trait Foo { type ItRev = > as UntypedTimeSeries>::IterRev; type IteRev = > as UntypedTimeSeries>::IterRev; } + +// Trait aliases +trait FooBar = + Foo + + Bar; +trait FooBar = + Foo + + Bar; +trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA = FooBar; +trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA = FooBar; +#[rustfmt_skip] +trait FooBar = Foo + + Bar; diff --git a/tests/target/trait.rs b/tests/target/trait.rs index 632107d8938..a570cb4d540 100644 --- a/tests/target/trait.rs +++ b/tests/target/trait.rs @@ -99,3 +99,24 @@ trait Foo { type IteRev = > as UntypedTimeSeries>::IterRev; } + +// Trait aliases +trait FooBar = Foo + Bar; +trait FooBar = Foo + Bar; +trait AAAAAAAAAAAAAAAAAA = BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAAA = + BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAA = + BBBBBBBBBBBBBBBBBBB + CCCCCCCCCCCCCCCCCCCCCCCCCCCCC + DDDDDDDDDDDDDDDDDDD; +trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA = + FooBar; +trait AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA< + A, + B, + C, + D, + E, +> = FooBar; +#[rustfmt_skip] +trait FooBar = Foo + + Bar;