]> git.lizzy.rs Git - rust.git/blobdiff - src/items.rs
Format exitential type
[rust.git] / src / items.rs
index 421e0d2651b4b0100e3d7b5bc8e56bde9f26d6c0..62bcfd0538d852fdb55bb9959e2f67f5b9bea5c6 100644 (file)
 use syntax::{ast, ptr, symbol};
 
 use codemap::{LineRangeUtils, SpanUtils};
-use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
-              recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
+use comment::{
+    combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
+    recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
+};
 use config::{BraceStyle, Config, Density, IndentStyle};
-use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
-           rewrite_assign_rhs_with, ExprType, RhsTactics};
+use expr::{
+    format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
+    ExprType, RhsTactics,
+};
 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
 use macros::{rewrite_macro, MacroPosition};
 use overflow;
 use rewrite::{Rewrite, RewriteContext};
 use shape::{Indent, Shape};
 use spanned::Spanned;
-use types::TraitTyParamBounds;
 use utils::*;
 use vertical::rewrite_with_alignment;
 use visitor::FmtVisitor;
@@ -121,8 +124,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
     }
 }
 
-// TODO convert to using rewrite style rather than visitor
-// TODO format modules in this style
+// FIXME convert to using rewrite style rather than visitor
+// FIXME format modules in this style
 #[allow(dead_code)]
 struct Item<'a> {
     keyword: &'static str,
@@ -138,7 +141,8 @@ fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Ite
             keyword: "",
             abi: format_abi(fm.abi, config.force_explicit_abi(), true),
             vis: None,
-            body: fm.items
+            body: fm
+                .items
                 .iter()
                 .map(|i| BodyElement::ForeignItem(i))
                 .collect(),
@@ -188,10 +192,10 @@ pub fn from_method_sig(
         generics: &'a ast::Generics,
     ) -> FnSig<'a> {
         FnSig {
-            unsafety: method_sig.unsafety,
-            constness: method_sig.constness.node,
+            unsafety: method_sig.header.unsafety,
+            constness: method_sig.header.constness.node,
             defaultness: ast::Defaultness::Final,
-            abi: method_sig.abi,
+            abi: method_sig.header.abi,
             decl: &*method_sig.decl,
             generics,
             visibility: DEFAULT_VISIBILITY,
@@ -205,13 +209,13 @@ pub fn from_fn_kind(
         defaultness: ast::Defaultness,
     ) -> FnSig<'a> {
         match *fn_kind {
-            visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig {
+            visit::FnKind::ItemFn(_, fn_header, visibility, _) => FnSig {
                 decl,
                 generics,
-                abi,
-                constness: constness.node,
+                abi: fn_header.abi,
+                constness: fn_header.constness.node,
                 defaultness,
-                unsafety,
+                unsafety: fn_header.unsafety,
                 visibility: visibility.clone(),
             },
             visit::FnKind::Method(_, method_sig, vis, _) => {
@@ -229,7 +233,7 @@ pub fn from_fn_kind(
     fn to_str(&self, context: &RewriteContext) -> String {
         let mut result = String::with_capacity(128);
         // Vis defaultness constness unsafety abi.
-        result.push_str(&*format_visibility(&self.visibility));
+        result.push_str(&*format_visibility(context, &self.visibility));
         result.push_str(format_defaultness(self.defaultness));
         result.push_str(format_constness(self.constness));
         result.push_str(format_unsafety(self.unsafety));
@@ -309,7 +313,8 @@ pub fn rewrite_fn(
             rewrite_fn_base(&context, indent, ident, fn_sig, span, newline_brace, true)?;
 
         // 2 = ` {`
-        if self.config.brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace
+        if self.config.brace_style() == BraceStyle::AlwaysNextLine
+            || force_newline_brace
             || last_line_width(&result) + 2 > self.shape().width
         {
             newline_brace = true;
@@ -372,7 +377,8 @@ fn single_line_fn(
 
         let codemap = self.get_context().codemap;
 
-        if self.config.empty_item_single_line() && is_empty_block(block, None, codemap)
+        if self.config.empty_item_single_line()
+            && is_empty_block(block, None, codemap)
             && self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
         {
             return Some(format!("{}{{}}", fn_str));
@@ -431,7 +437,7 @@ pub fn visit_enum(
         generics: &ast::Generics,
         span: Span,
     ) {
-        let enum_header = format_header("enum ", ident, vis);
+        let enum_header = format_header(&self.get_context(), "enum ", ident, vis);
         self.push_str(&enum_header);
 
         let enum_snippet = self.snippet(span);
@@ -526,6 +532,7 @@ fn format_variant_list(
             shape,
             ends_with_newline: true,
             preserve_newline: true,
+            nested: false,
             config: self.config,
         };
 
@@ -564,16 +571,65 @@ fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option<
             )?,
             ast::VariantData::Unit(..) => {
                 if let Some(ref expr) = field.node.disr_expr {
-                    let lhs = format!("{} =", field.node.ident.name);
-                    rewrite_assign_rhs(&context, lhs, &**expr, shape)?
+                    let lhs = format!("{} =", rewrite_ident(&context, field.node.ident));
+                    rewrite_assign_rhs(&context, lhs, &*expr.value, shape)?
                 } else {
-                    field.node.ident.name.to_string()
+                    rewrite_ident(&context, field.node.ident).to_owned()
                 }
             }
         };
 
         combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
     }
+
+    fn visit_impl_items(&mut self, items: &[ast::ImplItem]) {
+        if self.get_context().config.reorder_impl_items() {
+            // Create visitor for each items, then reorder them.
+            let mut buffer = vec![];
+            for item in items {
+                self.visit_impl_item(item);
+                buffer.push((self.buffer.clone(), item.clone()));
+                self.buffer.clear();
+            }
+            // type -> const -> macro -> method
+            use ast::ImplItemKind::*;
+            fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
+                match (a, b) {
+                    (Type(..), Type(..)) | (Const(..), Const(..)) => false,
+                    _ => true,
+                }
+            }
+
+            buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
+                (Type(..), _) => Ordering::Less,
+                (_, Type(..)) => Ordering::Greater,
+                (Const(..), _) => Ordering::Less,
+                (_, Const(..)) => Ordering::Greater,
+                (Macro(..), _) => Ordering::Less,
+                (_, Macro(..)) => Ordering::Greater,
+                _ => a.span.lo().cmp(&b.span.lo()),
+            });
+            let mut prev_kind = None;
+            for (buf, item) in buffer {
+                // Make sure that there are at least a single empty line between
+                // different impl items.
+                if prev_kind
+                    .as_ref()
+                    .map_or(false, |prev_kind| need_empty_line(prev_kind, &item.node))
+                {
+                    self.push_str("\n");
+                }
+                let indent_str = self.block_indent.to_string_with_newline(self.config);
+                self.push_str(&indent_str);
+                self.push_str(buf.trim());
+                prev_kind = Some(item.node.clone());
+            }
+        } else {
+            for item in items {
+                self.visit_impl_item(item);
+            }
+        }
+    }
 }
 
 pub fn format_impl(
@@ -593,7 +649,20 @@ pub fn format_impl(
         } else {
             context.budget(last_line_width(&result))
         };
-        let option = WhereClauseOption::snuggled(&ref_and_type);
+
+        let mut option = WhereClauseOption::snuggled(&ref_and_type);
+        let snippet = context.snippet(item.span);
+        let open_pos = snippet.find_uncommented("{")? + 1;
+        if !contains_comment(&snippet[open_pos..])
+            && items.is_empty()
+            && generics.where_clause.predicates.len() == 1
+            && !result.contains('\n')
+        {
+            option.suppress_comma();
+            option.snuggle();
+            option.compress_where();
+        }
+
         let where_clause_str = rewrite_where_clause(
             context,
             &generics.where_clause,
@@ -628,6 +697,12 @@ pub fn format_impl(
         if is_impl_single_line(context, items, &result, &where_clause_str, item)? {
             result.push_str(&where_clause_str);
             if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
+                // if the where_clause contains extra comments AND
+                // there is only one where clause predicate
+                // recover the suppressed comma in single line where_clause formatting
+                if generics.where_clause.predicates.len() == 1 {
+                    result.push_str(",");
+                }
                 result.push_str(&format!("{}{{{}}}", &sep, &sep));
             } else {
                 result.push_str(" {}");
@@ -635,11 +710,6 @@ pub fn format_impl(
             return Some(result);
         }
 
-        if !where_clause_str.is_empty() && !where_clause_str.contains('\n') {
-            let width = offset.block_indent + context.config.tab_spaces() - 1;
-            let where_indent = Indent::new(0, width);
-            result.push_str(&where_indent.to_string_with_newline(context.config));
-        }
         result.push_str(&where_clause_str);
 
         let need_newline = last_line_contains_single_line_comment(&result) || result.contains('\n');
@@ -668,51 +738,7 @@ pub fn format_impl(
             visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
 
             visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
-            if context.config.reorder_impl_items() {
-                // Create visitor for each items, then reorder them.
-                let mut buffer = vec![];
-                for item in items {
-                    visitor.visit_impl_item(item);
-                    buffer.push((visitor.buffer.clone(), item.clone()));
-                    visitor.buffer.clear();
-                }
-                // type -> const -> macro -> method
-                use ast::ImplItemKind::*;
-                fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
-                    match (a, b) {
-                        (Type(..), Type(..)) | (Const(..), Const(..)) => false,
-                        _ => true,
-                    }
-                }
-
-                buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
-                    (Type(..), _) => Ordering::Less,
-                    (_, Type(..)) => Ordering::Greater,
-                    (Const(..), _) => Ordering::Less,
-                    (_, Const(..)) => Ordering::Greater,
-                    (Macro(..), _) => Ordering::Less,
-                    (_, Macro(..)) => Ordering::Greater,
-                    _ => Ordering::Less,
-                });
-                let mut prev_kind = None;
-                for (buf, item) in buffer {
-                    // Make sure that there are at least a single empty line between
-                    // different impl items.
-                    if prev_kind
-                        .as_ref()
-                        .map_or(false, |prev_kind| need_empty_line(prev_kind, &item.node))
-                    {
-                        visitor.push_str("\n");
-                    }
-                    visitor.push_str(&item_indent.to_string_with_newline(context.config));
-                    visitor.push_str(buf.trim());
-                    prev_kind = Some(item.node.clone());
-                }
-            } else {
-                for item in items {
-                    visitor.visit_impl_item(item);
-                }
-            }
+            visitor.visit_impl_items(items);
 
             visitor.format_missing(item.span.hi() - BytePos(1));
 
@@ -724,7 +750,7 @@ fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
             result.push_str(&outer_indent_str);
         }
 
-        if result.ends_with('{') {
+        if result.ends_with('{') && !context.config.empty_item_single_line() {
             result.push_str(&sep);
         }
         result.push('}');
@@ -746,7 +772,9 @@ fn is_impl_single_line(
     let open_pos = snippet.find_uncommented("{")? + 1;
 
     Some(
-        context.config.empty_item_single_line() && items.is_empty() && !result.contains('\n')
+        context.config.empty_item_single_line()
+            && items.is_empty()
+            && !result.contains('\n')
             && result.len() + where_clause_str.len() <= context.config.max_width()
             && !contains_comment(&snippet[open_pos..]),
     )
@@ -769,7 +797,7 @@ fn format_impl_ref_and_type(
     {
         let mut result = String::with_capacity(128);
 
-        result.push_str(&format_visibility(&item.vis));
+        result.push_str(&format_visibility(context, &item.vis));
         result.push_str(format_defaultness(defaultness));
         result.push_str(format_unsafety(unsafety));
 
@@ -888,8 +916,8 @@ pub struct StructParts<'a> {
 }
 
 impl<'a> StructParts<'a> {
-    fn format_header(&self) -> String {
-        format_header(self.prefix, self.ident, self.vis)
+    fn format_header(&self, context: &RewriteContext) -> String {
+        format_header(context, self.prefix, self.ident, self.vis)
     }
 
     fn from_variant(variant: &'a ast::Variant) -> Self {
@@ -942,16 +970,16 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         is_auto,
         unsafety,
         ref generics,
-        ref type_param_bounds,
+        ref generic_bounds,
         ref trait_items,
     ) = item.node
     {
         let mut result = String::with_capacity(128);
         let header = format!(
             "{}{}{}trait ",
-            format_auto(is_auto),
-            format_visibility(&item.vis),
+            format_visibility(context, &item.vis),
             format_unsafety(unsafety),
+            format_auto(is_auto),
         );
         result.push_str(&header);
 
@@ -960,7 +988,7 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
         let generics_str = rewrite_generics(
             context,
-            &item.ident.to_string(),
+            rewrite_ident(context, item.ident),
             generics,
             shape,
             mk_sp(item.span.lo(), body_lo),
@@ -968,23 +996,22 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
         result.push_str(&generics_str);
 
         // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
-        if !type_param_bounds.is_empty() {
+        if !generic_bounds.is_empty() {
             let ident_hi = context
                 .snippet_provider
-                .span_after(item.span, &format!("{}", item.ident));
-            let bound_hi = type_param_bounds.last().unwrap().span().hi();
+                .span_after(item.span, &item.ident.as_str());
+            let bound_hi = generic_bounds.last().unwrap().span().hi();
             let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
             if contains_comment(snippet) {
                 return None;
             }
-        }
-        if !type_param_bounds.is_empty() {
+
             result = rewrite_assign_rhs_with(
                 context,
                 result + ":",
-                &TraitTyParamBounds::new(type_param_bounds),
+                generic_bounds,
                 shape,
-                RhsTactics::ForceNextLine,
+                RhsTactics::ForceNextLineWithoutIndent,
             )?;
         }
 
@@ -997,10 +1024,10 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
             };
 
             let where_budget = context.budget(last_line_width(&result));
-            let pos_before_where = if type_param_bounds.is_empty() {
+            let pos_before_where = if generic_bounds.is_empty() {
                 generics.where_clause.span.lo()
             } else {
-                type_param_bounds[type_param_bounds.len() - 1].span().hi()
+                generic_bounds[generic_bounds.len() - 1].span().hi()
             };
             let option = WhereClauseOption::snuggled(&generics_str);
             let where_clause_str = rewrite_where_clause(
@@ -1105,20 +1132,20 @@ pub fn format_trait_alias(
     context: &RewriteContext,
     ident: ast::Ident,
     generics: &ast::Generics,
-    ty_param_bounds: &ast::TyParamBounds,
+    generic_bounds: &ast::GenericBounds,
     shape: Shape,
 ) -> Option<String> {
-    let alias = ident.name.as_str();
+    let alias = rewrite_ident(context, ident);
     // 6 = "trait ", 2 = " ="
     let g_shape = shape.offset_left(6)?.sub_width(2)?;
     let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?;
     let lhs = format!("trait {} =", generics_str);
     // 1 = ";"
-    rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";")
+    rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";")
 }
 
 fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
-    let header_str = format_header(p.prefix, p.ident, p.vis);
+    let header_str = format_header(context, p.prefix, p.ident, p.vis);
     let generics_str = if let Some(generics) = p.generics {
         let hi = if generics.where_clause.predicates.is_empty() {
             generics.span.hi()
@@ -1150,7 +1177,7 @@ pub fn format_struct_struct(
     let mut result = String::with_capacity(1024);
     let span = struct_parts.span;
 
-    let header_str = struct_parts.format_header();
+    let header_str = struct_parts.format_header(context);
     result.push_str(&header_str);
 
     let header_hi = span.lo() + BytePos(header_str.len() as u32);
@@ -1185,7 +1212,8 @@ pub fn format_struct_struct(
     // 1 = `}`
     let overhead = if fields.is_empty() { 1 } else { 0 };
     let total_width = result.len() + generics_str.len() + overhead;
-    if !generics_str.is_empty() && !generics_str.contains('\n')
+    if !generics_str.is_empty()
+        && !generics_str.contains('\n')
         && total_width > context.config.max_width()
     {
         result.push('\n');
@@ -1214,7 +1242,9 @@ pub fn format_struct_struct(
         one_line_budget,
     )?;
 
-    if !items_str.contains('\n') && !result.contains('\n') && items_str.len() <= one_line_budget
+    if !items_str.contains('\n')
+        && !result.contains('\n')
+        && items_str.len() <= one_line_budget
         && !last_line_contains_single_line_comment(&items_str)
     {
         Some(format!("{} {} }}", result, items_str))
@@ -1282,7 +1312,7 @@ fn format_tuple_struct(
     let mut result = String::with_capacity(1024);
     let span = struct_parts.span;
 
-    let header_str = struct_parts.format_header();
+    let header_str = struct_parts.format_header(context);
     result.push_str(&header_str);
 
     let body_lo = if fields.is_empty() {
@@ -1300,13 +1330,10 @@ fn format_tuple_struct(
     } else {
         // This is a dirty hack to work around a missing `)` from the span of the last field.
         let last_arg_span = fields[fields.len() - 1].span;
-        if context.snippet(last_arg_span).ends_with(')') {
-            last_arg_span.hi()
-        } else {
-            context
-                .snippet_provider
-                .span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
-        }
+        context
+            .snippet_provider
+            .opt_span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
+            .unwrap_or(last_arg_span.hi())
     };
 
     let where_clause_str = match struct_parts.generics {
@@ -1355,7 +1382,8 @@ fn format_tuple_struct(
         )?;
     }
 
-    if !where_clause_str.is_empty() && !where_clause_str.contains('\n')
+    if !where_clause_str.is_empty()
+        && !where_clause_str.contains('\n')
         && (result.contains('\n')
             || offset.block_indent + result.len() + where_clause_str.len() + 1
                 > context.config.max_width())
@@ -1363,39 +1391,36 @@ fn format_tuple_struct(
         // We need to put the where clause on a new line, but we didn't
         // know that earlier, so the where clause will not be indented properly.
         result.push('\n');
-        result
-            .push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
-                .to_string(context.config));
+        result.push_str(
+            &(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config),
+        );
     }
     result.push_str(&where_clause_str);
 
     Some(result)
 }
 
-pub fn rewrite_type_alias(
+fn rewrite_type_prefix(
     context: &RewriteContext,
     indent: Indent,
+    prefix: &str,
     ident: ast::Ident,
-    ty: &ast::Ty,
     generics: &ast::Generics,
-    vis: &ast::Visibility,
-    span: Span,
 ) -> Option<String> {
     let mut result = String::with_capacity(128);
-
-    result.push_str(&format_visibility(vis));
-    result.push_str("type ");
+    result.push_str(prefix);
+    let ident_str = rewrite_ident(context, ident);
 
     // 2 = `= `
-    let g_shape = Shape::indented(indent, context.config)
-        .offset_left(result.len())?
-        .sub_width(2)?;
-    let g_span = mk_sp(
-        context.snippet_provider.span_after(span, "type"),
-        ty.span.lo(),
-    );
-    let generics_str = rewrite_generics(context, &ident.to_string(), generics, g_shape, g_span)?;
-    result.push_str(&generics_str);
+    if generics.params.is_empty() {
+        result.push_str(ident_str)
+    } else {
+        let g_shape = Shape::indented(indent, context.config)
+            .offset_left(result.len())?
+            .sub_width(2)?;
+        let generics_str = rewrite_generics(context, ident_str, generics, g_shape, generics.span)?;
+        result.push_str(&generics_str);
+    }
 
     let where_budget = context.budget(last_line_width(&result));
     let option = WhereClauseOption::snuggled(&result);
@@ -1406,24 +1431,76 @@ pub fn rewrite_type_alias(
         Shape::legacy(where_budget, indent),
         Density::Vertical,
         "=",
-        Some(span.hi()),
+        None,
         generics.span.hi(),
         option,
         false,
     )?;
     result.push_str(&where_clause_str);
-    if where_clause_str.is_empty() {
-        result.push_str(" =");
+
+    Some(result)
+}
+
+fn rewrite_type_item<R: Rewrite>(
+    context: &RewriteContext,
+    indent: Indent,
+    prefix: &str,
+    suffix: &str,
+    ident: ast::Ident,
+    rhs: &R,
+    generics: &ast::Generics,
+    vis: &ast::Visibility,
+) -> Option<String> {
+    let mut result = String::with_capacity(128);
+    result.push_str(&rewrite_type_prefix(
+        context,
+        indent,
+        &format!("{}{} ", format_visibility(context, vis), prefix),
+        ident,
+        generics,
+    )?);
+
+    if generics.where_clause.predicates.is_empty() {
+        result.push_str(suffix);
     } else {
-        result.push_str(&format!(
-            "{}=",
-            indent.to_string_with_newline(context.config)
-        ));
+        result.push_str(&indent.to_string_with_newline(context.config));
+        result.push_str(suffix.trim_left());
     }
 
     // 1 = ";"
-    let ty_shape = Shape::indented(indent, context.config).sub_width(1)?;
-    rewrite_assign_rhs(context, result, ty, ty_shape).map(|s| s + ";")
+    let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
+    rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
+}
+
+pub fn rewrite_type_alias(
+    context: &RewriteContext,
+    indent: Indent,
+    ident: ast::Ident,
+    ty: &ast::Ty,
+    generics: &ast::Generics,
+    vis: &ast::Visibility,
+) -> Option<String> {
+    rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
+}
+
+pub fn rewrite_existential_type(
+    context: &RewriteContext,
+    indent: Indent,
+    ident: ast::Ident,
+    generic_bounds: &ast::GenericBounds,
+    generics: &ast::Generics,
+    vis: &ast::Visibility,
+) -> Option<String> {
+    rewrite_type_item(
+        context,
+        indent,
+        "existential type",
+        ":",
+        ident,
+        generic_bounds,
+        generics,
+        vis,
+    )
 }
 
 fn type_annotation_spacing(config: &Config) -> (&str, &str) {
@@ -1437,10 +1514,15 @@ pub fn rewrite_struct_field_prefix(
     context: &RewriteContext,
     field: &ast::StructField,
 ) -> Option<String> {
-    let vis = format_visibility(&field.vis);
+    let vis = format_visibility(context, &field.vis);
     let type_annotation_spacing = type_annotation_spacing(context.config);
     Some(match field.ident {
-        Some(name) => format!("{}{}{}:", vis, name, type_annotation_spacing.0),
+        Some(name) => format!(
+            "{}{}{}:",
+            vis,
+            rewrite_ident(context, name),
+            type_annotation_spacing.0
+        ),
         None => format!("{}", vis),
     })
 }
@@ -1486,7 +1568,7 @@ pub fn rewrite_struct_field(
         attrs_extendable,
     )?;
     let overhead = last_line_width(&attr_prefix);
-    let lhs_offset = lhs_max_width.checked_sub(overhead).unwrap_or(0);
+    let lhs_offset = lhs_max_width.saturating_sub(overhead);
     for _ in 0..lhs_offset {
         spacing.push(' ');
     }
@@ -1593,7 +1675,7 @@ fn rewrite_static(
     );
     let mut prefix = format!(
         "{}{}{} {}{}{}",
-        format_visibility(static_parts.vis),
+        format_visibility(context, static_parts.vis),
         static_parts.defaultness.map_or("", format_defaultness),
         static_parts.prefix,
         format_mutability(static_parts.mutability),
@@ -1630,7 +1712,7 @@ fn rewrite_static(
             &**expr,
             Shape::legacy(remaining_width, offset.block_only()),
         ).and_then(|res| recover_comment_removed(res, static_parts.span, context))
-            .map(|s| if s.ends_with(';') { s } else { s + ";" })
+        .map(|s| if s.ends_with(';') { s } else { s + ";" })
     } else {
         Some(format!("{}{};", prefix, ty_str))
     }
@@ -1639,13 +1721,13 @@ fn rewrite_static(
 pub fn rewrite_associated_type(
     ident: ast::Ident,
     ty_opt: Option<&ptr::P<ast::Ty>>,
-    ty_param_bounds_opt: Option<&ast::TyParamBounds>,
+    generic_bounds_opt: Option<&ast::GenericBounds>,
     context: &RewriteContext,
     indent: Indent,
 ) -> Option<String> {
-    let prefix = format!("type {}", ident);
+    let prefix = format!("type {}", rewrite_ident(context, ident));
 
-    let type_bounds_str = if let Some(bounds) = ty_param_bounds_opt {
+    let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
         if bounds.is_empty() {
             String::new()
         } else {
@@ -1667,15 +1749,25 @@ pub fn rewrite_associated_type(
     }
 }
 
+pub fn rewrite_existential_impl_type(
+    context: &RewriteContext,
+    ident: ast::Ident,
+    generic_bounds: &ast::GenericBounds,
+    indent: Indent,
+) -> Option<String> {
+    rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
+        .map(|s| format!("existential {}", s))
+}
+
 pub fn rewrite_associated_impl_type(
     ident: ast::Ident,
     defaultness: ast::Defaultness,
     ty_opt: Option<&ptr::P<ast::Ty>>,
-    ty_param_bounds_opt: Option<&ast::TyParamBounds>,
+    generic_bounds_opt: Option<&ast::GenericBounds>,
     context: &RewriteContext,
     indent: Indent,
 ) -> Option<String> {
-    let result = rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)?;
+    let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?;
 
     match defaultness {
         ast::Defaultness::Default => Some(format!("default {}", result)),
@@ -1709,7 +1801,8 @@ fn is_empty_infer(context: &RewriteContext, ty: &ast::Ty) -> bool {
 impl Rewrite for ast::Arg {
     fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         if is_named_arg(self) {
-            let mut result = self.pat
+            let mut result = self
+                .pat
                 .rewrite(context, Shape::legacy(shape.width, shape.indent))?;
 
             if !is_empty_infer(context, &*self.ty) {
@@ -1722,7 +1815,8 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
                 }
                 let overhead = last_line_width(&result);
                 let max_width = shape.width.checked_sub(overhead)?;
-                let ty_str = self.ty
+                let ty_str = self
+                    .ty
                     .rewrite(context, Shape::legacy(max_width, shape.indent))?;
                 result.push_str(&ty_str);
             }
@@ -1849,8 +1943,13 @@ fn rewrite_fn_base(
     };
     let fd = fn_sig.decl;
     let g_span = mk_sp(span.lo(), fd.output.span().lo());
-    let generics_str =
-        rewrite_generics(context, &ident.to_string(), fn_sig.generics, shape, g_span)?;
+    let generics_str = rewrite_generics(
+        context,
+        rewrite_ident(context, ident),
+        fn_sig.generics,
+        shape,
+        g_span,
+    )?;
     result.push_str(&generics_str);
 
     let snuggle_angle_bracket = generics_str
@@ -1860,7 +1959,8 @@ fn rewrite_fn_base(
 
     // Note that the width and indent don't really matter, we'll re-layout the
     // return type later anyway.
-    let ret_str = fd.output
+    let ret_str = fd
+        .output
         .rewrite(context, Shape::indented(indent, context.config))?;
 
     let multi_line_ret_str = ret_str.contains('\n');
@@ -1895,11 +1995,6 @@ fn rewrite_fn_base(
     } else {
         result.push('(');
     }
-    if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty()
-        && result.ends_with('(')
-    {
-        result.push(' ')
-    }
 
     // Skip `pub(crate)`.
     let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
@@ -1957,9 +2052,6 @@ fn rewrite_fn_base(
         if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
             result.push('\n');
         }
-        if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty() {
-            result.push(' ')
-        }
         // If the last line of args contains comment, we cannot put the closing paren
         // on the same line.
         if arg_str
@@ -2017,7 +2109,8 @@ fn rewrite_fn_base(
         if multi_line_ret_str || ret_should_indent {
             // Now that we know the proper indent and width, we need to
             // re-layout the return type.
-            let ret_str = fd.output
+            let ret_str = fd
+                .output
                 .rewrite(context, Shape::indented(ret_indent, context.config))?;
             result.push_str(&ret_str);
         } else {
@@ -2116,10 +2209,22 @@ pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
     pub fn snuggled(current: &str) -> WhereClauseOption {
         WhereClauseOption {
             suppress_comma: false,
-            snuggle: trimmed_last_line_width(current) == 1,
+            snuggle: last_line_width(current) == 1,
             compress_where: false,
         }
     }
+
+    pub fn suppress_comma(&mut self) {
+        self.suppress_comma = true
+    }
+
+    pub fn compress_where(&mut self) {
+        self.compress_where = true
+    }
+
+    pub fn snuggle(&mut self) {
+        self.snuggle = true
+    }
 }
 
 fn rewrite_args(
@@ -2134,7 +2239,8 @@ fn rewrite_args(
     variadic: bool,
     generics_str_contains_newline: bool,
 ) -> Option<String> {
-    let mut arg_item_strs = args.iter()
+    let mut arg_item_strs = args
+        .iter()
         .map(|arg| arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent)))
         .collect::<Option<Vec<_>>>()?;
 
@@ -2267,6 +2373,7 @@ enum ArgumentKind<'a> {
         shape: Shape::legacy(budget, indent),
         ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
         preserve_newline: true,
+        nested: false,
         config: context.config,
     };
 
@@ -2454,6 +2561,7 @@ fn rewrite_where_clause_rfc_style(
         shape: clause_shape,
         ends_with_newline: true,
         preserve_newline: true,
+        nested: false,
         config: context.config,
     };
     let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
@@ -2469,9 +2577,12 @@ fn rewrite_where_clause_rfc_style(
     let newline_after_where = comment_separator(&comment_after, clause_shape);
 
     // 6 = `where `
-    let clause_sep = if where_clause_option.compress_where && comment_before.is_empty()
-        && comment_after.is_empty() && !preds_str.contains('\n')
-        && 6 + preds_str.len() <= shape.width || where_single_line
+    let clause_sep = if where_clause_option.compress_where
+        && comment_before.is_empty()
+        && comment_after.is_empty()
+        && !preds_str.contains('\n')
+        && 6 + preds_str.len() <= shape.width
+        || where_single_line
     {
         Cow::from(" ")
     } else {
@@ -2565,6 +2676,7 @@ fn rewrite_where_clause(
         shape: Shape::legacy(budget, offset),
         ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
         preserve_newline: true,
+        nested: false,
         config: context.config,
     };
     let preds_str = write_list(&item_vec, &fmt)?;
@@ -2581,7 +2693,8 @@ fn rewrite_where_clause(
     } else {
         terminator.len()
     };
-    if density == Density::Tall || preds_str.contains('\n')
+    if density == Density::Tall
+        || preds_str.contains('\n')
         || shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
     {
         Some(format!(
@@ -2620,8 +2733,18 @@ fn rewrite_comments_before_after_where(
     Some((before_comment, after_comment))
 }
 
-fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
-    format!("{}{}{}", format_visibility(vis), item_name, ident)
+fn format_header(
+    context: &RewriteContext,
+    item_name: &str,
+    ident: ast::Ident,
+    vis: &ast::Visibility,
+) -> String {
+    format!(
+        "{}{}{}",
+        format_visibility(context, vis),
+        item_name,
+        rewrite_ident(context, ident)
+    )
 }
 
 #[derive(PartialEq, Eq, Clone, Copy)]
@@ -2651,7 +2774,7 @@ fn format_generics(
         }
         // If the generics are not parameterized then generics.span.hi() == 0,
         // so we use span.lo(), which is the position after `struct Foo`.
-        let span_end_before_where = if generics.is_parameterized() {
+        let span_end_before_where = if !generics.params.is_empty() {
             generics.span.hi()
         } else {
             span.lo()
@@ -2669,11 +2792,13 @@ fn format_generics(
             false,
         )?;
         result.push_str(&where_clause_str);
-        brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine
+        brace_pos == BracePos::ForceSameLine
+            || brace_style == BraceStyle::PreferSameLine
             || (generics.where_clause.predicates.is_empty()
                 && trimmed_last_line_width(&result) == 1)
     } else {
-        brace_pos == BracePos::ForceSameLine || trimmed_last_line_width(&result) == 1
+        brace_pos == BracePos::ForceSameLine
+            || trimmed_last_line_width(&result) == 1
             || brace_style != BraceStyle::AlwaysNextLine
     };
     if brace_pos == BracePos::None {
@@ -2711,29 +2836,36 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
         let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
 
         let item_str = match self.node {
-            ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
-                rewrite_fn_base(
-                    context,
-                    shape.indent,
-                    self.ident,
-                    &FnSig::new(fn_decl, generics, self.vis.clone()),
-                    span,
-                    false,
-                    false,
-                ).map(|(s, _)| format!("{};", s))
-            }
+            ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => rewrite_fn_base(
+                context,
+                shape.indent,
+                self.ident,
+                &FnSig::new(fn_decl, generics, self.vis.clone()),
+                span,
+                false,
+                false,
+            ).map(|(s, _)| format!("{};", s)),
             ast::ForeignItemKind::Static(ref ty, is_mutable) => {
                 // FIXME(#21): we're dropping potential comments in between the
                 // function keywords here.
-                let vis = format_visibility(&self.vis);
+                let vis = format_visibility(context, &self.vis);
                 let mut_str = if is_mutable { "mut " } else { "" };
-                let prefix = format!("{}static {}{}:", vis, mut_str, self.ident);
+                let prefix = format!(
+                    "{}static {}{}:",
+                    vis,
+                    mut_str,
+                    rewrite_ident(context, self.ident)
+                );
                 // 1 = ;
                 rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
             }
             ast::ForeignItemKind::Ty => {
-                let vis = format_visibility(&self.vis);
-                Some(format!("{}type {};", vis, self.ident))
+                let vis = format_visibility(context, &self.vis);
+                Some(format!(
+                    "{}type {};",
+                    vis,
+                    rewrite_ident(context, self.ident)
+                ))
             }
             ast::ForeignItemKind::Macro(ref mac) => {
                 rewrite_macro(mac, None, context, shape, MacroPosition::Item)
@@ -2756,21 +2888,12 @@ fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
     }
 }
 
-impl Rewrite for ast::GenericParam {
-    fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
-        match *self {
-            ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape),
-            ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape),
-        }
-    }
-}
-
 /// Rewrite an inline mod.
-pub fn rewrite_mod(item: &ast::Item) -> String {
+pub fn rewrite_mod(context: &RewriteContext, item: &ast::Item) -> String {
     let mut result = String::with_capacity(32);
-    result.push_str(&*format_visibility(&item.vis));
+    result.push_str(&*format_visibility(context, &item.vis));
     result.push_str("mod ");
-    result.push_str(&item.ident.to_string());
+    result.push_str(rewrite_ident(context, item.ident));
     result.push(';');
     result
 }