diff --git a/src/items.rs b/src/items.rs index 4e5dc9d5765..457c6749520 100644 --- a/src/items.rs +++ b/src/items.rs @@ -1240,11 +1240,11 @@ pub fn rewrite_associated_type(ident: ast::Ident, let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt { let bounds: &[_] = ty_param_bounds; let bound_str = try_opt!(bounds.iter() - .map(|ty_bound| { - ty_bound.rewrite(context, Shape::legacy(context.config.max_width, indent)) - }) - .intersperse(Some(" + ".to_string())) - .collect::>()); + .map(|ty_bound| { + ty_bound.rewrite(context, Shape::legacy(context.config.max_width, indent)) + }) + .intersperse(Some(" + ".to_string())) + .collect::>()); if bounds.len() > 0 { format!(": {}", bound_str) } else { @@ -1267,6 +1267,22 @@ pub fn rewrite_associated_type(ident: ast::Ident, } } +pub fn rewrite_associated_impl_type(ident: ast::Ident, + defaultness: ast::Defaultness, + ty_opt: Option<&ptr::P>, + ty_param_bounds_opt: Option<&ast::TyParamBounds>, + context: &RewriteContext, + indent: Indent) + -> Option { + let result = + try_opt!(rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)); + + match defaultness { + ast::Defaultness::Default => Some(format!("default {}", result)), + _ => Some(result), + } +} + impl Rewrite for ast::FunctionRetTy { fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option { match *self { diff --git a/src/visitor.rs b/src/visitor.rs index 1344121b5a6..6cd00ffc1f4 100644 --- a/src/visitor.rs +++ b/src/visitor.rs @@ -21,7 +21,8 @@ use config::Config; use rewrite::{Rewrite, RewriteContext}; use comment::rewrite_comment; use macros::{rewrite_macro, MacroPosition}; -use items::{rewrite_static, rewrite_associated_type, rewrite_type_alias, format_impl, format_trait}; +use items::{rewrite_static, rewrite_associated_type, rewrite_associated_impl_type, + rewrite_type_alias, format_impl, format_trait}; fn is_use_item(item: &ast::Item) -> bool { match item.node { @@ -411,11 +412,12 @@ impl<'a> FmtVisitor<'a> { self.push_rewrite(ii.span, rewrite); } ast::ImplItemKind::Type(ref ty) => { - let rewrite = rewrite_associated_type(ii.ident, - Some(ty), - None, - &self.get_context(), - self.block_indent); + let rewrite = rewrite_associated_impl_type(ii.ident, + ii.defaultness, + Some(ty), + None, + &self.get_context(), + self.block_indent); self.push_rewrite(ii.span, rewrite); } ast::ImplItemKind::Macro(ref mac) => { diff --git a/tests/target/issue-1255.rs b/tests/target/issue-1255.rs new file mode 100644 index 00000000000..2d4633844a9 --- /dev/null +++ b/tests/target/issue-1255.rs @@ -0,0 +1,10 @@ +// Test for issue #1255 +// Default annotation incorrectly removed on associated types +#![feature(specialization)] + +trait Trait { + type Type; +} +impl Trait for T { + default type Type = u64; // 'default' should not be removed +}