]> git.lizzy.rs Git - rust.git/blob - src/tools/rustfmt/src/items.rs
Added docs to internal_macro const
[rust.git] / src / tools / rustfmt / src / items.rs
1 // Formatting top-level items - functions, structs, enums, traits, impls.
2
3 use std::borrow::Cow;
4 use std::cmp::{max, min, Ordering};
5
6 use regex::Regex;
7 use rustc_ast::visit;
8 use rustc_ast::{ast, ptr};
9 use rustc_span::{symbol, BytePos, Span, DUMMY_SP};
10
11 use crate::attr::filter_inline_attrs;
12 use crate::comment::{
13     combine_strs_with_missing_comments, contains_comment, is_last_comment_block,
14     recover_comment_removed, recover_missing_comment_in_span, rewrite_missing_comment,
15     FindUncommented,
16 };
17 use crate::config::lists::*;
18 use crate::config::{BraceStyle, Config, IndentStyle, Version};
19 use crate::expr::{
20     is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
21     rewrite_assign_rhs_with_comments, RhsTactics,
22 };
23 use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
24 use crate::macros::{rewrite_macro, MacroPosition};
25 use crate::overflow;
26 use crate::rewrite::{Rewrite, RewriteContext};
27 use crate::shape::{Indent, Shape};
28 use crate::source_map::{LineRangeUtils, SpanUtils};
29 use crate::spanned::Spanned;
30 use crate::stmt::Stmt;
31 use crate::utils::*;
32 use crate::vertical::rewrite_with_alignment;
33 use crate::visitor::FmtVisitor;
34
35 const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
36     kind: ast::VisibilityKind::Inherited,
37     span: DUMMY_SP,
38     tokens: None,
39 };
40
41 fn type_annotation_separator(config: &Config) -> &str {
42     colon_spaces(config)
43 }
44
45 // Statements of the form
46 // let pat: ty = init;
47 impl Rewrite for ast::Local {
48     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
49         debug!(
50             "Local::rewrite {:?} {} {:?}",
51             self, shape.width, shape.indent
52         );
53
54         skip_out_of_file_lines_range!(context, self.span);
55
56         if contains_skip(&self.attrs) || matches!(self.kind, ast::LocalKind::InitElse(..)) {
57             return None;
58         }
59
60         let attrs_str = self.attrs.rewrite(context, shape)?;
61         let mut result = if attrs_str.is_empty() {
62             "let ".to_owned()
63         } else {
64             combine_strs_with_missing_comments(
65                 context,
66                 &attrs_str,
67                 "let ",
68                 mk_sp(
69                     self.attrs.last().map(|a| a.span.hi()).unwrap(),
70                     self.span.lo(),
71                 ),
72                 shape,
73                 false,
74             )?
75         };
76
77         // 4 = "let ".len()
78         let pat_shape = shape.offset_left(4)?;
79         // 1 = ;
80         let pat_shape = pat_shape.sub_width(1)?;
81         let pat_str = self.pat.rewrite(context, pat_shape)?;
82         result.push_str(&pat_str);
83
84         // String that is placed within the assignment pattern and expression.
85         let infix = {
86             let mut infix = String::with_capacity(32);
87
88             if let Some(ref ty) = self.ty {
89                 let separator = type_annotation_separator(context.config);
90                 let ty_shape = if pat_str.contains('\n') {
91                     shape.with_max_width(context.config)
92                 } else {
93                     shape
94                 }
95                 .offset_left(last_line_width(&result) + separator.len())?
96                 // 2 = ` =`
97                 .sub_width(2)?;
98
99                 let rewrite = ty.rewrite(context, ty_shape)?;
100
101                 infix.push_str(separator);
102                 infix.push_str(&rewrite);
103             }
104
105             if self.kind.init().is_some() {
106                 infix.push_str(" =");
107             }
108
109             infix
110         };
111
112         result.push_str(&infix);
113
114         if let Some((init, _els)) = self.kind.init_else_opt() {
115             // 1 = trailing semicolon;
116             let nested_shape = shape.sub_width(1)?;
117
118             result = rewrite_assign_rhs(context, result, init, nested_shape)?;
119             // todo else
120         }
121
122         result.push(';');
123         Some(result)
124     }
125 }
126
127 // FIXME convert to using rewrite style rather than visitor
128 // FIXME format modules in this style
129 #[allow(dead_code)]
130 #[derive(Debug)]
131 struct Item<'a> {
132     unsafety: ast::Unsafe,
133     abi: Cow<'static, str>,
134     vis: Option<&'a ast::Visibility>,
135     body: Vec<BodyElement<'a>>,
136     span: Span,
137 }
138
139 impl<'a> Item<'a> {
140     fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
141         Item {
142             unsafety: fm.unsafety,
143             abi: format_extern(
144                 ast::Extern::from_abi(fm.abi),
145                 config.force_explicit_abi(),
146                 true,
147             ),
148             vis: None,
149             body: fm
150                 .items
151                 .iter()
152                 .map(|i| BodyElement::ForeignItem(i))
153                 .collect(),
154             span,
155         }
156     }
157 }
158
159 #[derive(Debug)]
160 enum BodyElement<'a> {
161     // Stmt(&'a ast::Stmt),
162     // Field(&'a ast::ExprField),
163     // Variant(&'a ast::Variant),
164     // Item(&'a ast::Item),
165     ForeignItem(&'a ast::ForeignItem),
166 }
167
168 /// Represents a fn's signature.
169 pub(crate) struct FnSig<'a> {
170     decl: &'a ast::FnDecl,
171     generics: &'a ast::Generics,
172     ext: ast::Extern,
173     is_async: Cow<'a, ast::Async>,
174     constness: ast::Const,
175     defaultness: ast::Defaultness,
176     unsafety: ast::Unsafe,
177     visibility: ast::Visibility,
178 }
179
180 impl<'a> FnSig<'a> {
181     pub(crate) fn from_method_sig(
182         method_sig: &'a ast::FnSig,
183         generics: &'a ast::Generics,
184         visibility: ast::Visibility,
185     ) -> FnSig<'a> {
186         FnSig {
187             unsafety: method_sig.header.unsafety,
188             is_async: Cow::Borrowed(&method_sig.header.asyncness),
189             constness: method_sig.header.constness,
190             defaultness: ast::Defaultness::Final,
191             ext: method_sig.header.ext,
192             decl: &*method_sig.decl,
193             generics,
194             visibility,
195         }
196     }
197
198     pub(crate) fn from_fn_kind(
199         fn_kind: &'a visit::FnKind<'_>,
200         generics: &'a ast::Generics,
201         decl: &'a ast::FnDecl,
202         defaultness: ast::Defaultness,
203     ) -> FnSig<'a> {
204         match *fn_kind {
205             visit::FnKind::Fn(fn_ctxt, _, fn_sig, vis, _) => match fn_ctxt {
206                 visit::FnCtxt::Assoc(..) => {
207                     let mut fn_sig = FnSig::from_method_sig(fn_sig, generics, vis.clone());
208                     fn_sig.defaultness = defaultness;
209                     fn_sig
210                 }
211                 _ => FnSig {
212                     decl,
213                     generics,
214                     ext: fn_sig.header.ext,
215                     constness: fn_sig.header.constness,
216                     is_async: Cow::Borrowed(&fn_sig.header.asyncness),
217                     defaultness,
218                     unsafety: fn_sig.header.unsafety,
219                     visibility: vis.clone(),
220                 },
221             },
222             _ => unreachable!(),
223         }
224     }
225
226     fn to_str(&self, context: &RewriteContext<'_>) -> String {
227         let mut result = String::with_capacity(128);
228         // Vis defaultness constness unsafety abi.
229         result.push_str(&*format_visibility(context, &self.visibility));
230         result.push_str(format_defaultness(self.defaultness));
231         result.push_str(format_constness(self.constness));
232         result.push_str(format_async(&self.is_async));
233         result.push_str(format_unsafety(self.unsafety));
234         result.push_str(&format_extern(
235             self.ext,
236             context.config.force_explicit_abi(),
237             false,
238         ));
239         result
240     }
241 }
242
243 impl<'a> FmtVisitor<'a> {
244     fn format_item(&mut self, item: &Item<'_>) {
245         self.buffer.push_str(format_unsafety(item.unsafety));
246         self.buffer.push_str(&item.abi);
247
248         let snippet = self.snippet(item.span);
249         let brace_pos = snippet.find_uncommented("{").unwrap();
250
251         self.push_str("{");
252         if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
253             // FIXME: this skips comments between the extern keyword and the opening
254             // brace.
255             self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
256             self.block_indent = self.block_indent.block_indent(self.config);
257
258             if !item.body.is_empty() {
259                 for item in &item.body {
260                     self.format_body_element(item);
261                 }
262             }
263
264             self.format_missing_no_indent(item.span.hi() - BytePos(1));
265             self.block_indent = self.block_indent.block_unindent(self.config);
266             let indent_str = self.block_indent.to_string(self.config);
267             self.push_str(&indent_str);
268         }
269
270         self.push_str("}");
271         self.last_pos = item.span.hi();
272     }
273
274     fn format_body_element(&mut self, element: &BodyElement<'_>) {
275         match *element {
276             BodyElement::ForeignItem(item) => self.format_foreign_item(item),
277         }
278     }
279
280     pub(crate) fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
281         let item = Item::from_foreign_mod(fm, span, self.config);
282         self.format_item(&item);
283     }
284
285     fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
286         let rewrite = item.rewrite(&self.get_context(), self.shape());
287         let hi = item.span.hi();
288         let span = if item.attrs.is_empty() {
289             item.span
290         } else {
291             mk_sp(item.attrs[0].span.lo(), hi)
292         };
293         self.push_rewrite(span, rewrite);
294         self.last_pos = hi;
295     }
296
297     pub(crate) fn rewrite_fn_before_block(
298         &mut self,
299         indent: Indent,
300         ident: symbol::Ident,
301         fn_sig: &FnSig<'_>,
302         span: Span,
303     ) -> Option<(String, FnBraceStyle)> {
304         let context = self.get_context();
305
306         let mut fn_brace_style = newline_for_brace(self.config, &fn_sig.generics.where_clause);
307         let (result, _, force_newline_brace) =
308             rewrite_fn_base(&context, indent, ident, fn_sig, span, fn_brace_style)?;
309
310         // 2 = ` {`
311         if self.config.brace_style() == BraceStyle::AlwaysNextLine
312             || force_newline_brace
313             || last_line_width(&result) + 2 > self.shape().width
314         {
315             fn_brace_style = FnBraceStyle::NextLine
316         }
317
318         Some((result, fn_brace_style))
319     }
320
321     pub(crate) fn rewrite_required_fn(
322         &mut self,
323         indent: Indent,
324         ident: symbol::Ident,
325         sig: &ast::FnSig,
326         generics: &ast::Generics,
327         span: Span,
328     ) -> Option<String> {
329         // Drop semicolon or it will be interpreted as comment.
330         let span = mk_sp(span.lo(), span.hi() - BytePos(1));
331         let context = self.get_context();
332
333         let (mut result, ends_with_comment, _) = rewrite_fn_base(
334             &context,
335             indent,
336             ident,
337             &FnSig::from_method_sig(sig, generics, DEFAULT_VISIBILITY),
338             span,
339             FnBraceStyle::None,
340         )?;
341
342         // If `result` ends with a comment, then remember to add a newline
343         if ends_with_comment {
344             result.push_str(&indent.to_string_with_newline(context.config));
345         }
346
347         // Re-attach semicolon
348         result.push(';');
349
350         Some(result)
351     }
352
353     pub(crate) fn single_line_fn(
354         &self,
355         fn_str: &str,
356         block: &ast::Block,
357         inner_attrs: Option<&[ast::Attribute]>,
358     ) -> Option<String> {
359         if fn_str.contains('\n') || inner_attrs.map_or(false, |a| !a.is_empty()) {
360             return None;
361         }
362
363         let context = self.get_context();
364
365         if self.config.empty_item_single_line()
366             && is_empty_block(&context, block, None)
367             && self.block_indent.width() + fn_str.len() + 3 <= self.config.max_width()
368             && !last_line_contains_single_line_comment(fn_str)
369         {
370             return Some(format!("{} {{}}", fn_str));
371         }
372
373         if !self.config.fn_single_line() || !is_simple_block_stmt(&context, block, None) {
374             return None;
375         }
376
377         let res = Stmt::from_ast_node(block.stmts.first()?, true)
378             .rewrite(&self.get_context(), self.shape())?;
379
380         let width = self.block_indent.width() + fn_str.len() + res.len() + 5;
381         if !res.contains('\n') && width <= self.config.max_width() {
382             Some(format!("{} {{ {} }}", fn_str, res))
383         } else {
384             None
385         }
386     }
387
388     pub(crate) fn visit_static(&mut self, static_parts: &StaticParts<'_>) {
389         let rewrite = rewrite_static(&self.get_context(), static_parts, self.block_indent);
390         self.push_rewrite(static_parts.span, rewrite);
391     }
392
393     pub(crate) fn visit_struct(&mut self, struct_parts: &StructParts<'_>) {
394         let is_tuple = match struct_parts.def {
395             ast::VariantData::Tuple(..) => true,
396             _ => false,
397         };
398         let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
399             .map(|s| if is_tuple { s + ";" } else { s });
400         self.push_rewrite(struct_parts.span, rewrite);
401     }
402
403     pub(crate) fn visit_enum(
404         &mut self,
405         ident: symbol::Ident,
406         vis: &ast::Visibility,
407         enum_def: &ast::EnumDef,
408         generics: &ast::Generics,
409         span: Span,
410     ) {
411         let enum_header =
412             format_header(&self.get_context(), "enum ", ident, vis, self.block_indent);
413         self.push_str(&enum_header);
414
415         let enum_snippet = self.snippet(span);
416         let brace_pos = enum_snippet.find_uncommented("{").unwrap();
417         let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
418         let generics_str = format_generics(
419             &self.get_context(),
420             generics,
421             self.config.brace_style(),
422             if enum_def.variants.is_empty() {
423                 BracePos::ForceSameLine
424             } else {
425                 BracePos::Auto
426             },
427             self.block_indent,
428             // make a span that starts right after `enum Foo`
429             mk_sp(ident.span.hi(), body_start),
430             last_line_width(&enum_header),
431         )
432         .unwrap();
433         self.push_str(&generics_str);
434
435         self.last_pos = body_start;
436
437         match self.format_variant_list(enum_def, body_start, span.hi()) {
438             Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
439             rw => {
440                 self.push_rewrite(mk_sp(body_start, span.hi()), rw);
441                 self.block_indent = self.block_indent.block_unindent(self.config);
442             }
443         }
444     }
445
446     // Format the body of an enum definition
447     fn format_variant_list(
448         &mut self,
449         enum_def: &ast::EnumDef,
450         body_lo: BytePos,
451         body_hi: BytePos,
452     ) -> Option<String> {
453         if enum_def.variants.is_empty() {
454             let mut buffer = String::with_capacity(128);
455             // 1 = "}"
456             let span = mk_sp(body_lo, body_hi - BytePos(1));
457             format_empty_struct_or_tuple(
458                 &self.get_context(),
459                 span,
460                 self.block_indent,
461                 &mut buffer,
462                 "",
463                 "}",
464             );
465             return Some(buffer);
466         }
467         let mut result = String::with_capacity(1024);
468         let original_offset = self.block_indent;
469         self.block_indent = self.block_indent.block_indent(self.config);
470
471         // If enum variants have discriminants, try to vertically align those,
472         // provided the discrims are not shifted too much  to the right
473         let align_threshold: usize = self.config.enum_discrim_align_threshold();
474         let discr_ident_lens: Vec<usize> = enum_def
475             .variants
476             .iter()
477             .filter(|var| var.disr_expr.is_some())
478             .map(|var| rewrite_ident(&self.get_context(), var.ident).len())
479             .collect();
480         // cut the list at the point of longest discrim shorter than the threshold
481         // All of the discrims under the threshold will get padded, and all above - left as is.
482         let pad_discrim_ident_to = *discr_ident_lens
483             .iter()
484             .filter(|&l| *l <= align_threshold)
485             .max()
486             .unwrap_or(&0);
487
488         let itemize_list_with = |one_line_width: usize| {
489             itemize_list(
490                 self.snippet_provider,
491                 enum_def.variants.iter(),
492                 "}",
493                 ",",
494                 |f| {
495                     if !f.attrs.is_empty() {
496                         f.attrs[0].span.lo()
497                     } else {
498                         f.span.lo()
499                     }
500                 },
501                 |f| f.span.hi(),
502                 |f| self.format_variant(f, one_line_width, pad_discrim_ident_to),
503                 body_lo,
504                 body_hi,
505                 false,
506             )
507             .collect()
508         };
509         let mut items: Vec<_> = itemize_list_with(self.config.struct_variant_width());
510
511         // If one of the variants use multiple lines, use multi-lined formatting for all variants.
512         let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
513         let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
514         if has_multiline_variant && has_single_line_variant {
515             items = itemize_list_with(0);
516         }
517
518         let shape = self.shape().sub_width(2)?;
519         let fmt = ListFormatting::new(shape, self.config)
520             .trailing_separator(self.config.trailing_comma())
521             .preserve_newline(true);
522
523         let list = write_list(&items, &fmt)?;
524         result.push_str(&list);
525         result.push_str(&original_offset.to_string_with_newline(self.config));
526         result.push('}');
527         Some(result)
528     }
529
530     // Variant of an enum.
531     fn format_variant(
532         &self,
533         field: &ast::Variant,
534         one_line_width: usize,
535         pad_discrim_ident_to: usize,
536     ) -> Option<String> {
537         if contains_skip(&field.attrs) {
538             let lo = field.attrs[0].span.lo();
539             let span = mk_sp(lo, field.span.hi());
540             return Some(self.snippet(span).to_owned());
541         }
542
543         let context = self.get_context();
544         // 1 = ','
545         let shape = self.shape().sub_width(1)?;
546         let attrs_str = field.attrs.rewrite(&context, shape)?;
547         let lo = field
548             .attrs
549             .last()
550             .map_or(field.span.lo(), |attr| attr.span.hi());
551         let span = mk_sp(lo, field.span.lo());
552
553         let variant_body = match field.data {
554             ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
555                 &context,
556                 &StructParts::from_variant(field),
557                 self.block_indent,
558                 Some(one_line_width),
559             )?,
560             ast::VariantData::Unit(..) => rewrite_ident(&context, field.ident).to_owned(),
561         };
562
563         let variant_body = if let Some(ref expr) = field.disr_expr {
564             let lhs = format!("{:1$} =", variant_body, pad_discrim_ident_to);
565             rewrite_assign_rhs_with(
566                 &context,
567                 lhs,
568                 &*expr.value,
569                 shape,
570                 RhsTactics::AllowOverflow,
571             )?
572         } else {
573             variant_body
574         };
575
576         combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
577     }
578
579     fn visit_impl_items(&mut self, items: &[ptr::P<ast::AssocItem>]) {
580         if self.get_context().config.reorder_impl_items() {
581             // Create visitor for each items, then reorder them.
582             let mut buffer = vec![];
583             for item in items {
584                 self.visit_impl_item(item);
585                 buffer.push((self.buffer.clone(), item.clone()));
586                 self.buffer.clear();
587             }
588
589             fn is_type(ty: &Option<rustc_ast::ptr::P<ast::Ty>>) -> bool {
590                 if let Some(lty) = ty {
591                     if let ast::TyKind::ImplTrait(..) = lty.kind {
592                         return false;
593                     }
594                 }
595                 true
596             }
597
598             fn is_opaque(ty: &Option<rustc_ast::ptr::P<ast::Ty>>) -> bool {
599                 !is_type(ty)
600             }
601
602             fn both_type(
603                 a: &Option<rustc_ast::ptr::P<ast::Ty>>,
604                 b: &Option<rustc_ast::ptr::P<ast::Ty>>,
605             ) -> bool {
606                 is_type(a) && is_type(b)
607             }
608
609             fn both_opaque(
610                 a: &Option<rustc_ast::ptr::P<ast::Ty>>,
611                 b: &Option<rustc_ast::ptr::P<ast::Ty>>,
612             ) -> bool {
613                 is_opaque(a) && is_opaque(b)
614             }
615
616             // In rustc-ap-v638 the `OpaqueTy` AssocItemKind variant was removed but
617             // we still need to differentiate to maintain sorting order.
618
619             // type -> opaque -> const -> macro -> method
620             use crate::ast::AssocItemKind::*;
621             fn need_empty_line(a: &ast::AssocItemKind, b: &ast::AssocItemKind) -> bool {
622                 match (a, b) {
623                     (TyAlias(lty), TyAlias(rty))
624                         if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
625                     {
626                         false
627                     }
628                     (Const(..), Const(..)) => false,
629                     _ => true,
630                 }
631             }
632
633             buffer.sort_by(|(_, a), (_, b)| match (&a.kind, &b.kind) {
634                 (TyAlias(lty), TyAlias(rty))
635                     if both_type(&lty.3, &rty.3) || both_opaque(&lty.3, &rty.3) =>
636                 {
637                     a.ident.as_str().cmp(&b.ident.as_str())
638                 }
639                 (Const(..), Const(..)) | (MacCall(..), MacCall(..)) => {
640                     a.ident.as_str().cmp(&b.ident.as_str())
641                 }
642                 (Fn(..), Fn(..)) => a.span.lo().cmp(&b.span.lo()),
643                 (TyAlias(ty), _) if is_type(&ty.3) => Ordering::Less,
644                 (_, TyAlias(ty)) if is_type(&ty.3) => Ordering::Greater,
645                 (TyAlias(..), _) => Ordering::Less,
646                 (_, TyAlias(..)) => Ordering::Greater,
647                 (Const(..), _) => Ordering::Less,
648                 (_, Const(..)) => Ordering::Greater,
649                 (MacCall(..), _) => Ordering::Less,
650                 (_, MacCall(..)) => Ordering::Greater,
651             });
652             let mut prev_kind = None;
653             for (buf, item) in buffer {
654                 // Make sure that there are at least a single empty line between
655                 // different impl items.
656                 if prev_kind
657                     .as_ref()
658                     .map_or(false, |prev_kind| need_empty_line(prev_kind, &item.kind))
659                 {
660                     self.push_str("\n");
661                 }
662                 let indent_str = self.block_indent.to_string_with_newline(self.config);
663                 self.push_str(&indent_str);
664                 self.push_str(buf.trim());
665                 prev_kind = Some(item.kind.clone());
666             }
667         } else {
668             for item in items {
669                 self.visit_impl_item(item);
670             }
671         }
672     }
673 }
674
675 pub(crate) fn format_impl(
676     context: &RewriteContext<'_>,
677     item: &ast::Item,
678     offset: Indent,
679 ) -> Option<String> {
680     if let ast::ItemKind::Impl(impl_kind) = &item.kind {
681         let ast::ImplKind {
682             ref generics,
683             ref self_ty,
684             ref items,
685             ..
686         } = **impl_kind;
687         let mut result = String::with_capacity(128);
688         let ref_and_type = format_impl_ref_and_type(context, item, offset)?;
689         let sep = offset.to_string_with_newline(context.config);
690         result.push_str(&ref_and_type);
691
692         let where_budget = if result.contains('\n') {
693             context.config.max_width()
694         } else {
695             context.budget(last_line_width(&result))
696         };
697
698         let mut option = WhereClauseOption::snuggled(&ref_and_type);
699         let snippet = context.snippet(item.span);
700         let open_pos = snippet.find_uncommented("{")? + 1;
701         if !contains_comment(&snippet[open_pos..])
702             && items.is_empty()
703             && generics.where_clause.predicates.len() == 1
704             && !result.contains('\n')
705         {
706             option.suppress_comma();
707             option.snuggle();
708             option.allow_single_line();
709         }
710
711         let missing_span = mk_sp(self_ty.span.hi(), item.span.hi());
712         let where_span_end = context.snippet_provider.opt_span_before(missing_span, "{");
713         let where_clause_str = rewrite_where_clause(
714             context,
715             &generics.where_clause,
716             context.config.brace_style(),
717             Shape::legacy(where_budget, offset.block_only()),
718             false,
719             "{",
720             where_span_end,
721             self_ty.span.hi(),
722             option,
723         )?;
724
725         // If there is no where-clause, we may have missing comments between the trait name and
726         // the opening brace.
727         if generics.where_clause.predicates.is_empty() {
728             if let Some(hi) = where_span_end {
729                 match recover_missing_comment_in_span(
730                     mk_sp(self_ty.span.hi(), hi),
731                     Shape::indented(offset, context.config),
732                     context,
733                     last_line_width(&result),
734                 ) {
735                     Some(ref missing_comment) if !missing_comment.is_empty() => {
736                         result.push_str(missing_comment);
737                     }
738                     _ => (),
739                 }
740             }
741         }
742
743         if is_impl_single_line(context, items.as_slice(), &result, &where_clause_str, item)? {
744             result.push_str(&where_clause_str);
745             if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
746                 // if the where_clause contains extra comments AND
747                 // there is only one where-clause predicate
748                 // recover the suppressed comma in single line where_clause formatting
749                 if generics.where_clause.predicates.len() == 1 {
750                     result.push(',');
751                 }
752                 result.push_str(&format!("{}{{{}}}", sep, sep));
753             } else {
754                 result.push_str(" {}");
755             }
756             return Some(result);
757         }
758
759         result.push_str(&where_clause_str);
760
761         let need_newline = last_line_contains_single_line_comment(&result) || result.contains('\n');
762         match context.config.brace_style() {
763             _ if need_newline => result.push_str(&sep),
764             BraceStyle::AlwaysNextLine => result.push_str(&sep),
765             BraceStyle::PreferSameLine => result.push(' '),
766             BraceStyle::SameLineWhere => {
767                 if !where_clause_str.is_empty() {
768                     result.push_str(&sep);
769                 } else {
770                     result.push(' ');
771                 }
772             }
773         }
774
775         result.push('{');
776         // this is an impl body snippet(impl SampleImpl { /* here */ })
777         let lo = max(self_ty.span.hi(), generics.where_clause.span.hi());
778         let snippet = context.snippet(mk_sp(lo, item.span.hi()));
779         let open_pos = snippet.find_uncommented("{")? + 1;
780
781         if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
782             let mut visitor = FmtVisitor::from_context(context);
783             let item_indent = offset.block_only().block_indent(context.config);
784             visitor.block_indent = item_indent;
785             visitor.last_pos = lo + BytePos(open_pos as u32);
786
787             visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
788             visitor.visit_impl_items(items);
789
790             visitor.format_missing(item.span.hi() - BytePos(1));
791
792             let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
793             let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
794
795             result.push_str(&inner_indent_str);
796             result.push_str(visitor.buffer.trim());
797             result.push_str(&outer_indent_str);
798         } else if need_newline || !context.config.empty_item_single_line() {
799             result.push_str(&sep);
800         }
801
802         result.push('}');
803
804         Some(result)
805     } else {
806         unreachable!();
807     }
808 }
809
810 fn is_impl_single_line(
811     context: &RewriteContext<'_>,
812     items: &[ptr::P<ast::AssocItem>],
813     result: &str,
814     where_clause_str: &str,
815     item: &ast::Item,
816 ) -> Option<bool> {
817     let snippet = context.snippet(item.span);
818     let open_pos = snippet.find_uncommented("{")? + 1;
819
820     Some(
821         context.config.empty_item_single_line()
822             && items.is_empty()
823             && !result.contains('\n')
824             && result.len() + where_clause_str.len() <= context.config.max_width()
825             && !contains_comment(&snippet[open_pos..]),
826     )
827 }
828
829 fn format_impl_ref_and_type(
830     context: &RewriteContext<'_>,
831     item: &ast::Item,
832     offset: Indent,
833 ) -> Option<String> {
834     if let ast::ItemKind::Impl(impl_kind) = &item.kind {
835         let ast::ImplKind {
836             unsafety,
837             polarity,
838             defaultness,
839             constness,
840             ref generics,
841             of_trait: ref trait_ref,
842             ref self_ty,
843             ..
844         } = **impl_kind;
845         let mut result = String::with_capacity(128);
846
847         result.push_str(&format_visibility(context, &item.vis));
848         result.push_str(format_defaultness(defaultness));
849         result.push_str(format_unsafety(unsafety));
850
851         let shape = if context.config.version() == Version::Two {
852             Shape::indented(offset + last_line_width(&result), context.config)
853         } else {
854             generics_shape_from_config(
855                 context.config,
856                 Shape::indented(offset + last_line_width(&result), context.config),
857                 0,
858             )?
859         };
860         let generics_str = rewrite_generics(context, "impl", generics, shape)?;
861         result.push_str(&generics_str);
862         result.push_str(format_constness_right(constness));
863
864         let polarity_str = match polarity {
865             ast::ImplPolarity::Negative(_) => "!",
866             ast::ImplPolarity::Positive => "",
867         };
868
869         let polarity_overhead;
870         let trait_ref_overhead;
871         if let Some(ref trait_ref) = *trait_ref {
872             let result_len = last_line_width(&result);
873             result.push_str(&rewrite_trait_ref(
874                 context,
875                 trait_ref,
876                 offset,
877                 polarity_str,
878                 result_len,
879             )?);
880             polarity_overhead = 0; // already written
881             trait_ref_overhead = " for".len();
882         } else {
883             polarity_overhead = polarity_str.len();
884             trait_ref_overhead = 0;
885         }
886
887         // Try to put the self type in a single line.
888         let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
889             // If there is no where-clause adapt budget for type formatting to take space and curly
890             // brace into account.
891             match context.config.brace_style() {
892                 BraceStyle::AlwaysNextLine => 0,
893                 _ => 2,
894             }
895         } else {
896             0
897         };
898         let used_space = last_line_width(&result)
899             + polarity_overhead
900             + trait_ref_overhead
901             + curly_brace_overhead;
902         // 1 = space before the type.
903         let budget = context.budget(used_space + 1);
904         if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
905             if !self_ty_str.contains('\n') {
906                 if trait_ref.is_some() {
907                     result.push_str(" for ");
908                 } else {
909                     result.push(' ');
910                     result.push_str(polarity_str);
911                 }
912                 result.push_str(&self_ty_str);
913                 return Some(result);
914             }
915         }
916
917         // Couldn't fit the self type on a single line, put it on a new line.
918         result.push('\n');
919         // Add indentation of one additional tab.
920         let new_line_offset = offset.block_indent(context.config);
921         result.push_str(&new_line_offset.to_string(context.config));
922         if trait_ref.is_some() {
923             result.push_str("for ");
924         } else {
925             result.push_str(polarity_str);
926         }
927         let budget = context.budget(last_line_width(&result) + polarity_overhead);
928         let type_offset = match context.config.indent_style() {
929             IndentStyle::Visual => new_line_offset + trait_ref_overhead,
930             IndentStyle::Block => new_line_offset,
931         };
932         result.push_str(&*self_ty.rewrite(context, Shape::legacy(budget, type_offset))?);
933         Some(result)
934     } else {
935         unreachable!();
936     }
937 }
938
939 fn rewrite_trait_ref(
940     context: &RewriteContext<'_>,
941     trait_ref: &ast::TraitRef,
942     offset: Indent,
943     polarity_str: &str,
944     result_len: usize,
945 ) -> Option<String> {
946     // 1 = space between generics and trait_ref
947     let used_space = 1 + polarity_str.len() + result_len;
948     let shape = Shape::indented(offset + used_space, context.config);
949     if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) {
950         if !trait_ref_str.contains('\n') {
951             return Some(format!(" {}{}", polarity_str, trait_ref_str));
952         }
953     }
954     // We could not make enough space for trait_ref, so put it on new line.
955     let offset = offset.block_indent(context.config);
956     let shape = Shape::indented(offset, context.config);
957     let trait_ref_str = trait_ref.rewrite(context, shape)?;
958     Some(format!(
959         "{}{}{}",
960         offset.to_string_with_newline(context.config),
961         polarity_str,
962         trait_ref_str
963     ))
964 }
965
966 pub(crate) struct StructParts<'a> {
967     prefix: &'a str,
968     ident: symbol::Ident,
969     vis: &'a ast::Visibility,
970     def: &'a ast::VariantData,
971     generics: Option<&'a ast::Generics>,
972     span: Span,
973 }
974
975 impl<'a> StructParts<'a> {
976     fn format_header(&self, context: &RewriteContext<'_>, offset: Indent) -> String {
977         format_header(context, self.prefix, self.ident, self.vis, offset)
978     }
979
980     fn from_variant(variant: &'a ast::Variant) -> Self {
981         StructParts {
982             prefix: "",
983             ident: variant.ident,
984             vis: &DEFAULT_VISIBILITY,
985             def: &variant.data,
986             generics: None,
987             span: variant.span,
988         }
989     }
990
991     pub(crate) fn from_item(item: &'a ast::Item) -> Self {
992         let (prefix, def, generics) = match item.kind {
993             ast::ItemKind::Struct(ref def, ref generics) => ("struct ", def, generics),
994             ast::ItemKind::Union(ref def, ref generics) => ("union ", def, generics),
995             _ => unreachable!(),
996         };
997         StructParts {
998             prefix,
999             ident: item.ident,
1000             vis: &item.vis,
1001             def,
1002             generics: Some(generics),
1003             span: item.span,
1004         }
1005     }
1006 }
1007
1008 fn format_struct(
1009     context: &RewriteContext<'_>,
1010     struct_parts: &StructParts<'_>,
1011     offset: Indent,
1012     one_line_width: Option<usize>,
1013 ) -> Option<String> {
1014     match *struct_parts.def {
1015         ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
1016         ast::VariantData::Tuple(ref fields, _) => {
1017             format_tuple_struct(context, struct_parts, fields, offset)
1018         }
1019         ast::VariantData::Struct(ref fields, _) => {
1020             format_struct_struct(context, struct_parts, fields, offset, one_line_width)
1021         }
1022     }
1023 }
1024
1025 pub(crate) fn format_trait(
1026     context: &RewriteContext<'_>,
1027     item: &ast::Item,
1028     offset: Indent,
1029 ) -> Option<String> {
1030     if let ast::ItemKind::Trait(trait_kind) = &item.kind {
1031         let ast::TraitKind(is_auto, unsafety, ref generics, ref generic_bounds, ref trait_items) =
1032             **trait_kind;
1033         let mut result = String::with_capacity(128);
1034         let header = format!(
1035             "{}{}{}trait ",
1036             format_visibility(context, &item.vis),
1037             format_unsafety(unsafety),
1038             format_auto(is_auto),
1039         );
1040         result.push_str(&header);
1041
1042         let body_lo = context.snippet_provider.span_after(item.span, "{");
1043
1044         let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
1045         let generics_str =
1046             rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
1047         result.push_str(&generics_str);
1048
1049         // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1050         if !generic_bounds.is_empty() {
1051             let ident_hi = context
1052                 .snippet_provider
1053                 .span_after(item.span, &item.ident.as_str());
1054             let bound_hi = generic_bounds.last().unwrap().span().hi();
1055             let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
1056             if contains_comment(snippet) {
1057                 return None;
1058             }
1059
1060             result = rewrite_assign_rhs_with(
1061                 context,
1062                 result + ":",
1063                 generic_bounds,
1064                 shape,
1065                 RhsTactics::ForceNextLineWithoutIndent,
1066             )?;
1067         }
1068
1069         // Rewrite where-clause.
1070         if !generics.where_clause.predicates.is_empty() {
1071             let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
1072
1073             let where_budget = context.budget(last_line_width(&result));
1074             let pos_before_where = if generic_bounds.is_empty() {
1075                 generics.where_clause.span.lo()
1076             } else {
1077                 generic_bounds[generic_bounds.len() - 1].span().hi()
1078             };
1079             let option = WhereClauseOption::snuggled(&generics_str);
1080             let where_clause_str = rewrite_where_clause(
1081                 context,
1082                 &generics.where_clause,
1083                 context.config.brace_style(),
1084                 Shape::legacy(where_budget, offset.block_only()),
1085                 where_on_new_line,
1086                 "{",
1087                 None,
1088                 pos_before_where,
1089                 option,
1090             )?;
1091             // If the where-clause cannot fit on the same line,
1092             // put the where-clause on a new line
1093             if !where_clause_str.contains('\n')
1094                 && last_line_width(&result) + where_clause_str.len() + offset.width()
1095                     > context.config.comment_width()
1096             {
1097                 let width = offset.block_indent + context.config.tab_spaces() - 1;
1098                 let where_indent = Indent::new(0, width);
1099                 result.push_str(&where_indent.to_string_with_newline(context.config));
1100             }
1101             result.push_str(&where_clause_str);
1102         } else {
1103             let item_snippet = context.snippet(item.span);
1104             if let Some(lo) = item_snippet.find('/') {
1105                 // 1 = `{`
1106                 let comment_hi = body_lo - BytePos(1);
1107                 let comment_lo = item.span.lo() + BytePos(lo as u32);
1108                 if comment_lo < comment_hi {
1109                     match recover_missing_comment_in_span(
1110                         mk_sp(comment_lo, comment_hi),
1111                         Shape::indented(offset, context.config),
1112                         context,
1113                         last_line_width(&result),
1114                     ) {
1115                         Some(ref missing_comment) if !missing_comment.is_empty() => {
1116                             result.push_str(missing_comment);
1117                         }
1118                         _ => (),
1119                     }
1120                 }
1121             }
1122         }
1123
1124         match context.config.brace_style() {
1125             _ if last_line_contains_single_line_comment(&result)
1126                 || last_line_width(&result) + 2 > context.budget(offset.width()) =>
1127             {
1128                 result.push_str(&offset.to_string_with_newline(context.config));
1129             }
1130             BraceStyle::AlwaysNextLine => {
1131                 result.push_str(&offset.to_string_with_newline(context.config));
1132             }
1133             BraceStyle::PreferSameLine => result.push(' '),
1134             BraceStyle::SameLineWhere => {
1135                 if result.contains('\n')
1136                     || (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
1137                 {
1138                     result.push_str(&offset.to_string_with_newline(context.config));
1139                 } else {
1140                     result.push(' ');
1141                 }
1142             }
1143         }
1144         result.push('{');
1145
1146         let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi());
1147         let snippet = context.snippet(block_span);
1148         let open_pos = snippet.find_uncommented("{")? + 1;
1149         let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
1150
1151         if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
1152             let mut visitor = FmtVisitor::from_context(context);
1153             visitor.block_indent = offset.block_only().block_indent(context.config);
1154             visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
1155
1156             for item in trait_items {
1157                 visitor.visit_trait_item(item);
1158             }
1159
1160             visitor.format_missing(item.span.hi() - BytePos(1));
1161
1162             let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
1163
1164             result.push_str(&inner_indent_str);
1165             result.push_str(visitor.buffer.trim());
1166             result.push_str(&outer_indent_str);
1167         } else if result.contains('\n') {
1168             result.push_str(&outer_indent_str);
1169         }
1170
1171         result.push('}');
1172         Some(result)
1173     } else {
1174         unreachable!();
1175     }
1176 }
1177
1178 struct OpaqueTypeBounds<'a> {
1179     generic_bounds: &'a ast::GenericBounds,
1180 }
1181
1182 impl<'a> Rewrite for OpaqueTypeBounds<'a> {
1183     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1184         self.generic_bounds
1185             .rewrite(context, shape)
1186             .map(|s| format!("impl {}", s))
1187     }
1188 }
1189
1190 pub(crate) struct TraitAliasBounds<'a> {
1191     generic_bounds: &'a ast::GenericBounds,
1192     generics: &'a ast::Generics,
1193 }
1194
1195 impl<'a> Rewrite for TraitAliasBounds<'a> {
1196     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1197         let generic_bounds_str = self.generic_bounds.rewrite(context, shape)?;
1198
1199         let mut option = WhereClauseOption::new(true, WhereClauseSpace::None);
1200         option.allow_single_line();
1201
1202         let where_str = rewrite_where_clause(
1203             context,
1204             &self.generics.where_clause,
1205             context.config.brace_style(),
1206             shape,
1207             false,
1208             ";",
1209             None,
1210             self.generics.where_clause.span.lo(),
1211             option,
1212         )?;
1213
1214         let fits_single_line = !generic_bounds_str.contains('\n')
1215             && !where_str.contains('\n')
1216             && generic_bounds_str.len() + where_str.len() < shape.width;
1217         let space = if generic_bounds_str.is_empty() || where_str.is_empty() {
1218             Cow::from("")
1219         } else if fits_single_line {
1220             Cow::from(" ")
1221         } else {
1222             shape.indent.to_string_with_newline(&context.config)
1223         };
1224
1225         Some(format!("{}{}{}", generic_bounds_str, space, where_str))
1226     }
1227 }
1228
1229 pub(crate) fn format_trait_alias(
1230     context: &RewriteContext<'_>,
1231     ident: symbol::Ident,
1232     vis: &ast::Visibility,
1233     generics: &ast::Generics,
1234     generic_bounds: &ast::GenericBounds,
1235     shape: Shape,
1236 ) -> Option<String> {
1237     let alias = rewrite_ident(context, ident);
1238     // 6 = "trait ", 2 = " ="
1239     let g_shape = shape.offset_left(6)?.sub_width(2)?;
1240     let generics_str = rewrite_generics(context, &alias, generics, g_shape)?;
1241     let vis_str = format_visibility(context, vis);
1242     let lhs = format!("{}trait {} =", vis_str, generics_str);
1243     // 1 = ";"
1244     let trait_alias_bounds = TraitAliasBounds {
1245         generic_bounds,
1246         generics,
1247     };
1248     rewrite_assign_rhs(context, lhs, &trait_alias_bounds, shape.sub_width(1)?).map(|s| s + ";")
1249 }
1250
1251 fn format_unit_struct(
1252     context: &RewriteContext<'_>,
1253     p: &StructParts<'_>,
1254     offset: Indent,
1255 ) -> Option<String> {
1256     let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
1257     let generics_str = if let Some(generics) = p.generics {
1258         let hi = context.snippet_provider.span_before(p.span, ";");
1259         format_generics(
1260             context,
1261             generics,
1262             context.config.brace_style(),
1263             BracePos::None,
1264             offset,
1265             // make a span that starts right after `struct Foo`
1266             mk_sp(p.ident.span.hi(), hi),
1267             last_line_width(&header_str),
1268         )?
1269     } else {
1270         String::new()
1271     };
1272     Some(format!("{}{};", header_str, generics_str))
1273 }
1274
1275 pub(crate) fn format_struct_struct(
1276     context: &RewriteContext<'_>,
1277     struct_parts: &StructParts<'_>,
1278     fields: &[ast::FieldDef],
1279     offset: Indent,
1280     one_line_width: Option<usize>,
1281 ) -> Option<String> {
1282     let mut result = String::with_capacity(1024);
1283     let span = struct_parts.span;
1284
1285     let header_str = struct_parts.format_header(context, offset);
1286     result.push_str(&header_str);
1287
1288     let header_hi = struct_parts.ident.span.hi();
1289     let body_lo = context.snippet_provider.span_after(span, "{");
1290
1291     let generics_str = match struct_parts.generics {
1292         Some(g) => format_generics(
1293             context,
1294             g,
1295             context.config.brace_style(),
1296             if fields.is_empty() {
1297                 BracePos::ForceSameLine
1298             } else {
1299                 BracePos::Auto
1300             },
1301             offset,
1302             // make a span that starts right after `struct Foo`
1303             mk_sp(header_hi, body_lo),
1304             last_line_width(&result),
1305         )?,
1306         None => {
1307             // 3 = ` {}`, 2 = ` {`.
1308             let overhead = if fields.is_empty() { 3 } else { 2 };
1309             if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty())
1310                 || context.config.max_width() < overhead + result.len()
1311             {
1312                 format!("\n{}{{", offset.block_only().to_string(context.config))
1313             } else {
1314                 " {".to_owned()
1315             }
1316         }
1317     };
1318     // 1 = `}`
1319     let overhead = if fields.is_empty() { 1 } else { 0 };
1320     let total_width = result.len() + generics_str.len() + overhead;
1321     if !generics_str.is_empty()
1322         && !generics_str.contains('\n')
1323         && total_width > context.config.max_width()
1324     {
1325         result.push('\n');
1326         result.push_str(&offset.to_string(context.config));
1327         result.push_str(generics_str.trim_start());
1328     } else {
1329         result.push_str(&generics_str);
1330     }
1331
1332     if fields.is_empty() {
1333         let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
1334         format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
1335         return Some(result);
1336     }
1337
1338     // 3 = ` ` and ` }`
1339     let one_line_budget = context.budget(result.len() + 3 + offset.width());
1340     let one_line_budget =
1341         one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
1342
1343     let items_str = rewrite_with_alignment(
1344         fields,
1345         context,
1346         Shape::indented(offset.block_indent(context.config), context.config).sub_width(1)?,
1347         mk_sp(body_lo, span.hi()),
1348         one_line_budget,
1349     )?;
1350
1351     if !items_str.contains('\n')
1352         && !result.contains('\n')
1353         && items_str.len() <= one_line_budget
1354         && !last_line_contains_single_line_comment(&items_str)
1355     {
1356         Some(format!("{} {} }}", result, items_str))
1357     } else {
1358         Some(format!(
1359             "{}\n{}{}\n{}}}",
1360             result,
1361             offset
1362                 .block_indent(context.config)
1363                 .to_string(context.config),
1364             items_str,
1365             offset.to_string(context.config)
1366         ))
1367     }
1368 }
1369
1370 fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
1371     match vis.kind {
1372         ast::VisibilityKind::Crate(..) | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
1373         _ => default_span.lo(),
1374     }
1375 }
1376
1377 // Format tuple or struct without any fields. We need to make sure that the comments
1378 // inside the delimiters are preserved.
1379 fn format_empty_struct_or_tuple(
1380     context: &RewriteContext<'_>,
1381     span: Span,
1382     offset: Indent,
1383     result: &mut String,
1384     opener: &str,
1385     closer: &str,
1386 ) {
1387     // 3 = " {}" or "();"
1388     let used_width = last_line_used_width(&result, offset.width()) + 3;
1389     if used_width > context.config.max_width() {
1390         result.push_str(&offset.to_string_with_newline(context.config))
1391     }
1392     result.push_str(opener);
1393     match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
1394         Some(ref s) if s.is_empty() => (),
1395         Some(ref s) => {
1396             if !is_single_line(s) || first_line_contains_single_line_comment(s) {
1397                 let nested_indent_str = offset
1398                     .block_indent(context.config)
1399                     .to_string_with_newline(context.config);
1400                 result.push_str(&nested_indent_str);
1401             }
1402             result.push_str(s);
1403             if last_line_contains_single_line_comment(s) {
1404                 result.push_str(&offset.to_string_with_newline(context.config));
1405             }
1406         }
1407         None => result.push_str(context.snippet(span)),
1408     }
1409     result.push_str(closer);
1410 }
1411
1412 fn format_tuple_struct(
1413     context: &RewriteContext<'_>,
1414     struct_parts: &StructParts<'_>,
1415     fields: &[ast::FieldDef],
1416     offset: Indent,
1417 ) -> Option<String> {
1418     let mut result = String::with_capacity(1024);
1419     let span = struct_parts.span;
1420
1421     let header_str = struct_parts.format_header(context, offset);
1422     result.push_str(&header_str);
1423
1424     let body_lo = if fields.is_empty() {
1425         let lo = get_bytepos_after_visibility(struct_parts.vis, span);
1426         context
1427             .snippet_provider
1428             .span_after(mk_sp(lo, span.hi()), "(")
1429     } else {
1430         fields[0].span.lo()
1431     };
1432     let body_hi = if fields.is_empty() {
1433         context
1434             .snippet_provider
1435             .span_after(mk_sp(body_lo, span.hi()), ")")
1436     } else {
1437         // This is a dirty hack to work around a missing `)` from the span of the last field.
1438         let last_arg_span = fields[fields.len() - 1].span;
1439         context
1440             .snippet_provider
1441             .opt_span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
1442             .unwrap_or_else(|| last_arg_span.hi())
1443     };
1444
1445     let where_clause_str = match struct_parts.generics {
1446         Some(generics) => {
1447             let budget = context.budget(last_line_width(&header_str));
1448             let shape = Shape::legacy(budget, offset);
1449             let generics_str = rewrite_generics(context, "", generics, shape)?;
1450             result.push_str(&generics_str);
1451
1452             let where_budget = context.budget(last_line_width(&result));
1453             let option = WhereClauseOption::new(true, WhereClauseSpace::Newline);
1454             rewrite_where_clause(
1455                 context,
1456                 &generics.where_clause,
1457                 context.config.brace_style(),
1458                 Shape::legacy(where_budget, offset.block_only()),
1459                 false,
1460                 ";",
1461                 None,
1462                 body_hi,
1463                 option,
1464             )?
1465         }
1466         None => "".to_owned(),
1467     };
1468
1469     if fields.is_empty() {
1470         let body_hi = context
1471             .snippet_provider
1472             .span_before(mk_sp(body_lo, span.hi()), ")");
1473         let inner_span = mk_sp(body_lo, body_hi);
1474         format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
1475     } else {
1476         let shape = Shape::indented(offset, context.config).sub_width(1)?;
1477         result = overflow::rewrite_with_parens(
1478             context,
1479             &result,
1480             fields.iter(),
1481             shape,
1482             span,
1483             context.config.fn_call_width(),
1484             None,
1485         )?;
1486     }
1487
1488     if !where_clause_str.is_empty()
1489         && !where_clause_str.contains('\n')
1490         && (result.contains('\n')
1491             || offset.block_indent + result.len() + where_clause_str.len() + 1
1492                 > context.config.max_width())
1493     {
1494         // We need to put the where-clause on a new line, but we didn't
1495         // know that earlier, so the where-clause will not be indented properly.
1496         result.push('\n');
1497         result.push_str(
1498             &(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config),
1499         );
1500     }
1501     result.push_str(&where_clause_str);
1502
1503     Some(result)
1504 }
1505
1506 fn rewrite_type<R: Rewrite>(
1507     context: &RewriteContext<'_>,
1508     indent: Indent,
1509     ident: symbol::Ident,
1510     vis: &ast::Visibility,
1511     generics: &ast::Generics,
1512     generic_bounds_opt: Option<&ast::GenericBounds>,
1513     rhs: Option<&R>,
1514     span: Span,
1515 ) -> Option<String> {
1516     let mut result = String::with_capacity(128);
1517     result.push_str(&format!("{}type ", format_visibility(context, vis)));
1518     let ident_str = rewrite_ident(context, ident);
1519
1520     if generics.params.is_empty() {
1521         result.push_str(ident_str)
1522     } else {
1523         // 2 = `= `
1524         let g_shape = Shape::indented(indent, context.config)
1525             .offset_left(result.len())?
1526             .sub_width(2)?;
1527         let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
1528         result.push_str(&generics_str);
1529     }
1530
1531     if let Some(bounds) = generic_bounds_opt {
1532         if !bounds.is_empty() {
1533             // 2 = `: `
1534             let shape = Shape::indented(indent, context.config).offset_left(result.len() + 2)?;
1535             let type_bounds = bounds.rewrite(context, shape).map(|s| format!(": {}", s))?;
1536             result.push_str(&type_bounds);
1537         }
1538     }
1539
1540     let where_budget = context.budget(last_line_width(&result));
1541     let mut option = WhereClauseOption::snuggled(&result);
1542     if rhs.is_none() {
1543         option.suppress_comma();
1544     }
1545     let where_clause_str = rewrite_where_clause(
1546         context,
1547         &generics.where_clause,
1548         context.config.brace_style(),
1549         Shape::legacy(where_budget, indent),
1550         false,
1551         "=",
1552         None,
1553         generics.span.hi(),
1554         option,
1555     )?;
1556     result.push_str(&where_clause_str);
1557
1558     if let Some(ty) = rhs {
1559         // If there's a where clause, add a newline before the assignment. Otherwise just add a
1560         // space.
1561         let has_where = !generics.where_clause.predicates.is_empty();
1562         if has_where {
1563             result.push_str(&indent.to_string_with_newline(context.config));
1564         } else {
1565             result.push(' ');
1566         }
1567
1568         let comment_span = context
1569             .snippet_provider
1570             .opt_span_before(span, "=")
1571             .map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo));
1572
1573         let lhs = match comment_span {
1574             Some(comment_span)
1575                 if contains_comment(context.snippet_provider.span_to_snippet(comment_span)?) =>
1576             {
1577                 let comment_shape = if has_where {
1578                     Shape::indented(indent, context.config)
1579                 } else {
1580                     Shape::indented(indent, context.config)
1581                         .block_left(context.config.tab_spaces())?
1582                 };
1583
1584                 combine_strs_with_missing_comments(
1585                     context,
1586                     result.trim_end(),
1587                     "=",
1588                     comment_span,
1589                     comment_shape,
1590                     true,
1591                 )?
1592             }
1593             _ => format!("{}=", result),
1594         };
1595
1596         // 1 = `;`
1597         let shape = Shape::indented(indent, context.config).sub_width(1)?;
1598         rewrite_assign_rhs(context, lhs, &*ty, shape).map(|s| s + ";")
1599     } else {
1600         Some(format!("{};", result))
1601     }
1602 }
1603
1604 pub(crate) fn rewrite_opaque_type(
1605     context: &RewriteContext<'_>,
1606     indent: Indent,
1607     ident: symbol::Ident,
1608     generic_bounds: &ast::GenericBounds,
1609     generics: &ast::Generics,
1610     vis: &ast::Visibility,
1611     span: Span,
1612 ) -> Option<String> {
1613     let opaque_type_bounds = OpaqueTypeBounds { generic_bounds };
1614     rewrite_type(
1615         context,
1616         indent,
1617         ident,
1618         vis,
1619         generics,
1620         Some(generic_bounds),
1621         Some(&opaque_type_bounds),
1622         span,
1623     )
1624 }
1625
1626 fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1627     (
1628         if config.space_before_colon() { " " } else { "" },
1629         if config.space_after_colon() { " " } else { "" },
1630     )
1631 }
1632
1633 pub(crate) fn rewrite_struct_field_prefix(
1634     context: &RewriteContext<'_>,
1635     field: &ast::FieldDef,
1636 ) -> Option<String> {
1637     let vis = format_visibility(context, &field.vis);
1638     let type_annotation_spacing = type_annotation_spacing(context.config);
1639     Some(match field.ident {
1640         Some(name) => format!(
1641             "{}{}{}:",
1642             vis,
1643             rewrite_ident(context, name),
1644             type_annotation_spacing.0
1645         ),
1646         None => vis.to_string(),
1647     })
1648 }
1649
1650 impl Rewrite for ast::FieldDef {
1651     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1652         rewrite_struct_field(context, self, shape, 0)
1653     }
1654 }
1655
1656 pub(crate) fn rewrite_struct_field(
1657     context: &RewriteContext<'_>,
1658     field: &ast::FieldDef,
1659     shape: Shape,
1660     lhs_max_width: usize,
1661 ) -> Option<String> {
1662     if contains_skip(&field.attrs) {
1663         return Some(context.snippet(field.span()).to_owned());
1664     }
1665
1666     let type_annotation_spacing = type_annotation_spacing(context.config);
1667     let prefix = rewrite_struct_field_prefix(context, field)?;
1668
1669     let attrs_str = field.attrs.rewrite(context, shape)?;
1670     let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
1671     let missing_span = if field.attrs.is_empty() {
1672         mk_sp(field.span.lo(), field.span.lo())
1673     } else {
1674         mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
1675     };
1676     let mut spacing = String::from(if field.ident.is_some() {
1677         type_annotation_spacing.1
1678     } else {
1679         ""
1680     });
1681     // Try to put everything on a single line.
1682     let attr_prefix = combine_strs_with_missing_comments(
1683         context,
1684         &attrs_str,
1685         &prefix,
1686         missing_span,
1687         shape,
1688         attrs_extendable,
1689     )?;
1690     let overhead = trimmed_last_line_width(&attr_prefix);
1691     let lhs_offset = lhs_max_width.saturating_sub(overhead);
1692     for _ in 0..lhs_offset {
1693         spacing.push(' ');
1694     }
1695     // In this extreme case we will be missing a space between an attribute and a field.
1696     if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
1697         spacing.push(' ');
1698     }
1699     let orig_ty = shape
1700         .offset_left(overhead + spacing.len())
1701         .and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1702     if let Some(ref ty) = orig_ty {
1703         if !ty.contains('\n') {
1704             return Some(attr_prefix + &spacing + ty);
1705         }
1706     }
1707
1708     let is_prefix_empty = prefix.is_empty();
1709     // We must use multiline. We are going to put attributes and a field on different lines.
1710     let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
1711     // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1712     let field_str = if is_prefix_empty {
1713         field_str.trim_start()
1714     } else {
1715         &field_str
1716     };
1717     combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
1718 }
1719
1720 pub(crate) struct StaticParts<'a> {
1721     prefix: &'a str,
1722     vis: &'a ast::Visibility,
1723     ident: symbol::Ident,
1724     ty: &'a ast::Ty,
1725     mutability: ast::Mutability,
1726     expr_opt: Option<&'a ptr::P<ast::Expr>>,
1727     defaultness: Option<ast::Defaultness>,
1728     span: Span,
1729 }
1730
1731 impl<'a> StaticParts<'a> {
1732     pub(crate) fn from_item(item: &'a ast::Item) -> Self {
1733         let (defaultness, prefix, ty, mutability, expr) = match item.kind {
1734             ast::ItemKind::Static(ref ty, mutability, ref expr) => {
1735                 (None, "static", ty, mutability, expr)
1736             }
1737             ast::ItemKind::Const(defaultness, ref ty, ref expr) => {
1738                 (Some(defaultness), "const", ty, ast::Mutability::Not, expr)
1739             }
1740             _ => unreachable!(),
1741         };
1742         StaticParts {
1743             prefix,
1744             vis: &item.vis,
1745             ident: item.ident,
1746             ty,
1747             mutability,
1748             expr_opt: expr.as_ref(),
1749             defaultness,
1750             span: item.span,
1751         }
1752     }
1753
1754     pub(crate) fn from_trait_item(ti: &'a ast::AssocItem) -> Self {
1755         let (defaultness, ty, expr_opt) = match ti.kind {
1756             ast::AssocItemKind::Const(defaultness, ref ty, ref expr_opt) => {
1757                 (defaultness, ty, expr_opt)
1758             }
1759             _ => unreachable!(),
1760         };
1761         StaticParts {
1762             prefix: "const",
1763             vis: &DEFAULT_VISIBILITY,
1764             ident: ti.ident,
1765             ty,
1766             mutability: ast::Mutability::Not,
1767             expr_opt: expr_opt.as_ref(),
1768             defaultness: Some(defaultness),
1769             span: ti.span,
1770         }
1771     }
1772
1773     pub(crate) fn from_impl_item(ii: &'a ast::AssocItem) -> Self {
1774         let (defaultness, ty, expr) = match ii.kind {
1775             ast::AssocItemKind::Const(defaultness, ref ty, ref expr) => (defaultness, ty, expr),
1776             _ => unreachable!(),
1777         };
1778         StaticParts {
1779             prefix: "const",
1780             vis: &ii.vis,
1781             ident: ii.ident,
1782             ty,
1783             mutability: ast::Mutability::Not,
1784             expr_opt: expr.as_ref(),
1785             defaultness: Some(defaultness),
1786             span: ii.span,
1787         }
1788     }
1789 }
1790
1791 fn rewrite_static(
1792     context: &RewriteContext<'_>,
1793     static_parts: &StaticParts<'_>,
1794     offset: Indent,
1795 ) -> Option<String> {
1796     let colon = colon_spaces(context.config);
1797     let mut prefix = format!(
1798         "{}{}{} {}{}{}",
1799         format_visibility(context, static_parts.vis),
1800         static_parts.defaultness.map_or("", format_defaultness),
1801         static_parts.prefix,
1802         format_mutability(static_parts.mutability),
1803         rewrite_ident(context, static_parts.ident),
1804         colon,
1805     );
1806     // 2 = " =".len()
1807     let ty_shape =
1808         Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)?;
1809     let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
1810         Some(ty_str) => ty_str,
1811         None => {
1812             if prefix.ends_with(' ') {
1813                 prefix.pop();
1814             }
1815             let nested_indent = offset.block_indent(context.config);
1816             let nested_shape = Shape::indented(nested_indent, context.config);
1817             let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
1818             format!(
1819                 "{}{}",
1820                 nested_indent.to_string_with_newline(context.config),
1821                 ty_str
1822             )
1823         }
1824     };
1825
1826     if let Some(expr) = static_parts.expr_opt {
1827         let comments_lo = context.snippet_provider.span_after(static_parts.span, "=");
1828         let expr_lo = expr.span.lo();
1829         let comments_span = mk_sp(comments_lo, expr_lo);
1830
1831         let lhs = format!("{}{} =", prefix, ty_str);
1832
1833         // 1 = ;
1834         let remaining_width = context.budget(offset.block_indent + 1);
1835         rewrite_assign_rhs_with_comments(
1836             context,
1837             &lhs,
1838             &**expr,
1839             Shape::legacy(remaining_width, offset.block_only()),
1840             RhsTactics::Default,
1841             comments_span,
1842             true,
1843         )
1844         .and_then(|res| recover_comment_removed(res, static_parts.span, context))
1845         .map(|s| if s.ends_with(';') { s } else { s + ";" })
1846     } else {
1847         Some(format!("{}{};", prefix, ty_str))
1848     }
1849 }
1850
1851 pub(crate) fn rewrite_type_alias(
1852     ident: symbol::Ident,
1853     ty_opt: Option<&ptr::P<ast::Ty>>,
1854     generics: &ast::Generics,
1855     generic_bounds_opt: Option<&ast::GenericBounds>,
1856     context: &RewriteContext<'_>,
1857     indent: Indent,
1858     vis: &ast::Visibility,
1859     span: Span,
1860 ) -> Option<String> {
1861     rewrite_type(
1862         context,
1863         indent,
1864         ident,
1865         vis,
1866         generics,
1867         generic_bounds_opt,
1868         ty_opt,
1869         span,
1870     )
1871 }
1872
1873 struct OpaqueType<'a> {
1874     bounds: &'a ast::GenericBounds,
1875 }
1876
1877 impl<'a> Rewrite for OpaqueType<'a> {
1878     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1879         let shape = shape.offset_left(5)?; // `impl `
1880         self.bounds
1881             .rewrite(context, shape)
1882             .map(|s| format!("impl {}", s))
1883     }
1884 }
1885
1886 pub(crate) fn rewrite_opaque_impl_type(
1887     context: &RewriteContext<'_>,
1888     ident: symbol::Ident,
1889     generics: &ast::Generics,
1890     generic_bounds: &ast::GenericBounds,
1891     indent: Indent,
1892 ) -> Option<String> {
1893     let ident_str = rewrite_ident(context, ident);
1894     // 5 = "type "
1895     let generics_shape = Shape::indented(indent, context.config).offset_left(5)?;
1896     let generics_str = rewrite_generics(context, ident_str, generics, generics_shape)?;
1897     let prefix = format!("type {} =", generics_str);
1898     let rhs = OpaqueType {
1899         bounds: generic_bounds,
1900     };
1901
1902     rewrite_assign_rhs(
1903         context,
1904         &prefix,
1905         &rhs,
1906         Shape::indented(indent, context.config).sub_width(1)?,
1907     )
1908     .map(|s| s + ";")
1909 }
1910
1911 pub(crate) fn rewrite_associated_impl_type(
1912     ident: symbol::Ident,
1913     vis: &ast::Visibility,
1914     defaultness: ast::Defaultness,
1915     ty_opt: Option<&ptr::P<ast::Ty>>,
1916     generics: &ast::Generics,
1917     context: &RewriteContext<'_>,
1918     indent: Indent,
1919     span: Span,
1920 ) -> Option<String> {
1921     let result = rewrite_type_alias(ident, ty_opt, generics, None, context, indent, vis, span)?;
1922
1923     match defaultness {
1924         ast::Defaultness::Default(..) => Some(format!("default {}", result)),
1925         _ => Some(result),
1926     }
1927 }
1928
1929 impl Rewrite for ast::FnRetTy {
1930     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1931         match *self {
1932             ast::FnRetTy::Default(_) => Some(String::new()),
1933             ast::FnRetTy::Ty(ref ty) => {
1934                 if context.config.version() == Version::One
1935                     || context.config.indent_style() == IndentStyle::Visual
1936                 {
1937                     let inner_width = shape.width.checked_sub(3)?;
1938                     return ty
1939                         .rewrite(context, Shape::legacy(inner_width, shape.indent + 3))
1940                         .map(|r| format!("-> {}", r));
1941                 }
1942
1943                 ty.rewrite(context, shape.offset_left(3)?)
1944                     .map(|s| format!("-> {}", s))
1945             }
1946         }
1947     }
1948 }
1949
1950 fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
1951     match ty.kind {
1952         ast::TyKind::Infer => ty.span.hi() == pat_span.hi(),
1953         _ => false,
1954     }
1955 }
1956
1957 /// Recover any missing comments between the param and the type.
1958 ///
1959 /// # Returns
1960 ///
1961 /// A 2-len tuple with the comment before the colon in first position, and the comment after the
1962 /// colon in second position.
1963 fn get_missing_param_comments(
1964     context: &RewriteContext<'_>,
1965     pat_span: Span,
1966     ty_span: Span,
1967     shape: Shape,
1968 ) -> (String, String) {
1969     let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());
1970
1971     let span_before_colon = {
1972         let missing_comment_span_hi = context
1973             .snippet_provider
1974             .span_before(missing_comment_span, ":");
1975         mk_sp(pat_span.hi(), missing_comment_span_hi)
1976     };
1977     let span_after_colon = {
1978         let missing_comment_span_lo = context
1979             .snippet_provider
1980             .span_after(missing_comment_span, ":");
1981         mk_sp(missing_comment_span_lo, ty_span.lo())
1982     };
1983
1984     let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
1985         .filter(|comment| !comment.is_empty())
1986         .map_or(String::new(), |comment| format!(" {}", comment));
1987     let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
1988         .filter(|comment| !comment.is_empty())
1989         .map_or(String::new(), |comment| format!("{} ", comment));
1990     (comment_before_colon, comment_after_colon)
1991 }
1992
1993 impl Rewrite for ast::Param {
1994     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1995         let param_attrs_result = self
1996             .attrs
1997             .rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1998         let (span, has_multiple_attr_lines) = if !self.attrs.is_empty() {
1999             let num_attrs = self.attrs.len();
2000             (
2001                 mk_sp(self.attrs[num_attrs - 1].span.hi(), self.pat.span.lo()),
2002                 param_attrs_result.contains('\n'),
2003             )
2004         } else {
2005             (mk_sp(self.span.lo(), self.span.lo()), false)
2006         };
2007
2008         if let Some(ref explicit_self) = self.to_self() {
2009             rewrite_explicit_self(
2010                 context,
2011                 explicit_self,
2012                 &param_attrs_result,
2013                 span,
2014                 shape,
2015                 has_multiple_attr_lines,
2016             )
2017         } else if is_named_param(self) {
2018             let mut result = combine_strs_with_missing_comments(
2019                 context,
2020                 &param_attrs_result,
2021                 &self
2022                     .pat
2023                     .rewrite(context, Shape::legacy(shape.width, shape.indent))?,
2024                 span,
2025                 shape,
2026                 !has_multiple_attr_lines,
2027             )?;
2028
2029             if !is_empty_infer(&*self.ty, self.pat.span) {
2030                 let (before_comment, after_comment) =
2031                     get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
2032                 result.push_str(&before_comment);
2033                 result.push_str(colon_spaces(context.config));
2034                 result.push_str(&after_comment);
2035                 let overhead = last_line_width(&result);
2036                 let max_width = shape.width.checked_sub(overhead)?;
2037                 let ty_str = self
2038                     .ty
2039                     .rewrite(context, Shape::legacy(max_width, shape.indent))?;
2040                 result.push_str(&ty_str);
2041             }
2042
2043             Some(result)
2044         } else {
2045             self.ty.rewrite(context, shape)
2046         }
2047     }
2048 }
2049
2050 fn rewrite_explicit_self(
2051     context: &RewriteContext<'_>,
2052     explicit_self: &ast::ExplicitSelf,
2053     param_attrs: &str,
2054     span: Span,
2055     shape: Shape,
2056     has_multiple_attr_lines: bool,
2057 ) -> Option<String> {
2058     match explicit_self.node {
2059         ast::SelfKind::Region(lt, m) => {
2060             let mut_str = format_mutability(m);
2061             match lt {
2062                 Some(ref l) => {
2063                     let lifetime_str = l.rewrite(
2064                         context,
2065                         Shape::legacy(context.config.max_width(), Indent::empty()),
2066                     )?;
2067                     Some(combine_strs_with_missing_comments(
2068                         context,
2069                         &param_attrs,
2070                         &format!("&{} {}self", lifetime_str, mut_str),
2071                         span,
2072                         shape,
2073                         !has_multiple_attr_lines,
2074                     )?)
2075                 }
2076                 None => Some(combine_strs_with_missing_comments(
2077                     context,
2078                     &param_attrs,
2079                     &format!("&{}self", mut_str),
2080                     span,
2081                     shape,
2082                     !has_multiple_attr_lines,
2083                 )?),
2084             }
2085         }
2086         ast::SelfKind::Explicit(ref ty, mutability) => {
2087             let type_str = ty.rewrite(
2088                 context,
2089                 Shape::legacy(context.config.max_width(), Indent::empty()),
2090             )?;
2091
2092             Some(combine_strs_with_missing_comments(
2093                 context,
2094                 &param_attrs,
2095                 &format!("{}self: {}", format_mutability(mutability), type_str),
2096                 span,
2097                 shape,
2098                 !has_multiple_attr_lines,
2099             )?)
2100         }
2101         ast::SelfKind::Value(mutability) => Some(combine_strs_with_missing_comments(
2102             context,
2103             &param_attrs,
2104             &format!("{}self", format_mutability(mutability)),
2105             span,
2106             shape,
2107             !has_multiple_attr_lines,
2108         )?),
2109     }
2110 }
2111
2112 pub(crate) fn span_lo_for_param(param: &ast::Param) -> BytePos {
2113     if param.attrs.is_empty() {
2114         if is_named_param(param) {
2115             param.pat.span.lo()
2116         } else {
2117             param.ty.span.lo()
2118         }
2119     } else {
2120         param.attrs[0].span.lo()
2121     }
2122 }
2123
2124 pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param) -> BytePos {
2125     match param.ty.kind {
2126         ast::TyKind::Infer if context.snippet(param.ty.span) == "_" => param.ty.span.hi(),
2127         ast::TyKind::Infer if is_named_param(param) => param.pat.span.hi(),
2128         _ => param.ty.span.hi(),
2129     }
2130 }
2131
2132 pub(crate) fn is_named_param(param: &ast::Param) -> bool {
2133     if let ast::PatKind::Ident(_, ident, _) = param.pat.kind {
2134         ident.name != symbol::kw::Empty
2135     } else {
2136         true
2137     }
2138 }
2139
2140 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
2141 pub(crate) enum FnBraceStyle {
2142     SameLine,
2143     NextLine,
2144     None,
2145 }
2146
2147 // Return type is (result, force_new_line_for_brace)
2148 fn rewrite_fn_base(
2149     context: &RewriteContext<'_>,
2150     indent: Indent,
2151     ident: symbol::Ident,
2152     fn_sig: &FnSig<'_>,
2153     span: Span,
2154     fn_brace_style: FnBraceStyle,
2155 ) -> Option<(String, bool, bool)> {
2156     let mut force_new_line_for_brace = false;
2157
2158     let where_clause = &fn_sig.generics.where_clause;
2159
2160     let mut result = String::with_capacity(1024);
2161     result.push_str(&fn_sig.to_str(context));
2162
2163     // fn foo
2164     result.push_str("fn ");
2165
2166     // Generics.
2167     let overhead = if let FnBraceStyle::SameLine = fn_brace_style {
2168         // 4 = `() {`
2169         4
2170     } else {
2171         // 2 = `()`
2172         2
2173     };
2174     let used_width = last_line_used_width(&result, indent.width());
2175     let one_line_budget = context.budget(used_width + overhead);
2176     let shape = Shape {
2177         width: one_line_budget,
2178         indent,
2179         offset: used_width,
2180     };
2181     let fd = fn_sig.decl;
2182     let generics_str = rewrite_generics(
2183         context,
2184         rewrite_ident(context, ident),
2185         fn_sig.generics,
2186         shape,
2187     )?;
2188     result.push_str(&generics_str);
2189
2190     let snuggle_angle_bracket = generics_str
2191         .lines()
2192         .last()
2193         .map_or(false, |l| l.trim_start().len() == 1);
2194
2195     // Note that the width and indent don't really matter, we'll re-layout the
2196     // return type later anyway.
2197     let ret_str = fd
2198         .output
2199         .rewrite(context, Shape::indented(indent, context.config))?;
2200
2201     let multi_line_ret_str = ret_str.contains('\n');
2202     let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
2203
2204     // Params.
2205     let (one_line_budget, multi_line_budget, mut param_indent) = compute_budgets_for_params(
2206         context,
2207         &result,
2208         indent,
2209         ret_str_len,
2210         fn_brace_style,
2211         multi_line_ret_str,
2212     )?;
2213
2214     debug!(
2215         "rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
2216         one_line_budget, multi_line_budget, param_indent
2217     );
2218
2219     result.push('(');
2220     // Check if vertical layout was forced.
2221     if one_line_budget == 0
2222         && !snuggle_angle_bracket
2223         && context.config.indent_style() == IndentStyle::Visual
2224     {
2225         result.push_str(&param_indent.to_string_with_newline(context.config));
2226     }
2227
2228     // Skip `pub(crate)`.
2229     let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
2230     // A conservative estimation, the goal is to be over all parens in generics
2231     let params_start = fn_sig
2232         .generics
2233         .params
2234         .last()
2235         .map_or(lo_after_visibility, |param| param.span().hi());
2236     let params_end = if fd.inputs.is_empty() {
2237         context
2238             .snippet_provider
2239             .span_after(mk_sp(params_start, span.hi()), ")")
2240     } else {
2241         let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
2242         context.snippet_provider.span_after(last_span, ")")
2243     };
2244     let params_span = mk_sp(
2245         context
2246             .snippet_provider
2247             .span_after(mk_sp(params_start, span.hi()), "("),
2248         params_end,
2249     );
2250     let param_str = rewrite_params(
2251         context,
2252         &fd.inputs,
2253         one_line_budget,
2254         multi_line_budget,
2255         indent,
2256         param_indent,
2257         params_span,
2258         fd.c_variadic(),
2259     )?;
2260
2261     let put_params_in_block = match context.config.indent_style() {
2262         IndentStyle::Block => param_str.contains('\n') || param_str.len() > one_line_budget,
2263         _ => false,
2264     } && !fd.inputs.is_empty();
2265
2266     let mut params_last_line_contains_comment = false;
2267     let mut no_params_and_over_max_width = false;
2268
2269     if put_params_in_block {
2270         param_indent = indent.block_indent(context.config);
2271         result.push_str(&param_indent.to_string_with_newline(context.config));
2272         result.push_str(&param_str);
2273         result.push_str(&indent.to_string_with_newline(context.config));
2274         result.push(')');
2275     } else {
2276         result.push_str(&param_str);
2277         let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
2278         // Put the closing brace on the next line if it overflows the max width.
2279         // 1 = `)`
2280         let closing_paren_overflow_max_width =
2281             fd.inputs.is_empty() && used_width + 1 > context.config.max_width();
2282         // If the last line of params contains comment, we cannot put the closing paren
2283         // on the same line.
2284         params_last_line_contains_comment = param_str
2285             .lines()
2286             .last()
2287             .map_or(false, |last_line| last_line.contains("//"));
2288
2289         if context.config.version() == Version::Two {
2290             if closing_paren_overflow_max_width {
2291                 result.push(')');
2292                 result.push_str(&indent.to_string_with_newline(context.config));
2293                 no_params_and_over_max_width = true;
2294             } else if params_last_line_contains_comment {
2295                 result.push_str(&indent.to_string_with_newline(context.config));
2296                 result.push(')');
2297                 no_params_and_over_max_width = true;
2298             } else {
2299                 result.push(')');
2300             }
2301         } else {
2302             if closing_paren_overflow_max_width || params_last_line_contains_comment {
2303                 result.push_str(&indent.to_string_with_newline(context.config));
2304             }
2305             result.push(')');
2306         }
2307     }
2308
2309     // Return type.
2310     if let ast::FnRetTy::Ty(..) = fd.output {
2311         let ret_should_indent = match context.config.indent_style() {
2312             // If our params are block layout then we surely must have space.
2313             IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false,
2314             _ if params_last_line_contains_comment => false,
2315             _ if result.contains('\n') || multi_line_ret_str => true,
2316             _ => {
2317                 // If the return type would push over the max width, then put the return type on
2318                 // a new line. With the +1 for the signature length an additional space between
2319                 // the closing parenthesis of the param and the arrow '->' is considered.
2320                 let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
2321
2322                 // If there is no where-clause, take into account the space after the return type
2323                 // and the brace.
2324                 if where_clause.predicates.is_empty() {
2325                     sig_length += 2;
2326                 }
2327
2328                 sig_length > context.config.max_width()
2329             }
2330         };
2331         let ret_shape = if ret_should_indent {
2332             if context.config.version() == Version::One
2333                 || context.config.indent_style() == IndentStyle::Visual
2334             {
2335                 let indent = if param_str.is_empty() {
2336                     // Aligning with non-existent params looks silly.
2337                     force_new_line_for_brace = true;
2338                     indent + 4
2339                 } else {
2340                     // FIXME: we might want to check that using the param indent
2341                     // doesn't blow our budget, and if it does, then fallback to
2342                     // the where-clause indent.
2343                     param_indent
2344                 };
2345
2346                 result.push_str(&indent.to_string_with_newline(context.config));
2347                 Shape::indented(indent, context.config)
2348             } else {
2349                 let mut ret_shape = Shape::indented(indent, context.config);
2350                 if param_str.is_empty() {
2351                     // Aligning with non-existent params looks silly.
2352                     force_new_line_for_brace = true;
2353                     ret_shape = if context.use_block_indent() {
2354                         ret_shape.offset_left(4).unwrap_or(ret_shape)
2355                     } else {
2356                         ret_shape.indent = ret_shape.indent + 4;
2357                         ret_shape
2358                     };
2359                 }
2360
2361                 result.push_str(&ret_shape.indent.to_string_with_newline(context.config));
2362                 ret_shape
2363             }
2364         } else {
2365             if context.config.version() == Version::Two {
2366                 if !param_str.is_empty() || !no_params_and_over_max_width {
2367                     result.push(' ');
2368                 }
2369             } else {
2370                 result.push(' ');
2371             }
2372
2373             let ret_shape = Shape::indented(indent, context.config);
2374             ret_shape
2375                 .offset_left(last_line_width(&result))
2376                 .unwrap_or(ret_shape)
2377         };
2378
2379         if multi_line_ret_str || ret_should_indent {
2380             // Now that we know the proper indent and width, we need to
2381             // re-layout the return type.
2382             let ret_str = fd.output.rewrite(context, ret_shape)?;
2383             result.push_str(&ret_str);
2384         } else {
2385             result.push_str(&ret_str);
2386         }
2387
2388         // Comment between return type and the end of the decl.
2389         let snippet_lo = fd.output.span().hi();
2390         if where_clause.predicates.is_empty() {
2391             let snippet_hi = span.hi();
2392             let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2393             // Try to preserve the layout of the original snippet.
2394             let original_starts_with_newline = snippet
2395                 .find(|c| c != ' ')
2396                 .map_or(false, |i| starts_with_newline(&snippet[i..]));
2397             let original_ends_with_newline = snippet
2398                 .rfind(|c| c != ' ')
2399                 .map_or(false, |i| snippet[i..].ends_with('\n'));
2400             let snippet = snippet.trim();
2401             if !snippet.is_empty() {
2402                 result.push(if original_starts_with_newline {
2403                     '\n'
2404                 } else {
2405                     ' '
2406                 });
2407                 result.push_str(snippet);
2408                 if original_ends_with_newline {
2409                     force_new_line_for_brace = true;
2410                 }
2411             }
2412         }
2413     }
2414
2415     let pos_before_where = match fd.output {
2416         ast::FnRetTy::Default(..) => params_span.hi(),
2417         ast::FnRetTy::Ty(ref ty) => ty.span.hi(),
2418     };
2419
2420     let is_params_multi_lined = param_str.contains('\n');
2421
2422     let space = if put_params_in_block && ret_str.is_empty() {
2423         WhereClauseSpace::Space
2424     } else {
2425         WhereClauseSpace::Newline
2426     };
2427     let mut option = WhereClauseOption::new(fn_brace_style == FnBraceStyle::None, space);
2428     if is_params_multi_lined {
2429         option.veto_single_line();
2430     }
2431     let where_clause_str = rewrite_where_clause(
2432         context,
2433         where_clause,
2434         context.config.brace_style(),
2435         Shape::indented(indent, context.config),
2436         true,
2437         "{",
2438         Some(span.hi()),
2439         pos_before_where,
2440         option,
2441     )?;
2442     // If there are neither where-clause nor return type, we may be missing comments between
2443     // params and `{`.
2444     if where_clause_str.is_empty() {
2445         if let ast::FnRetTy::Default(ret_span) = fd.output {
2446             match recover_missing_comment_in_span(
2447                 mk_sp(params_span.hi(), ret_span.hi()),
2448                 shape,
2449                 context,
2450                 last_line_width(&result),
2451             ) {
2452                 Some(ref missing_comment) if !missing_comment.is_empty() => {
2453                     result.push_str(missing_comment);
2454                     force_new_line_for_brace = true;
2455                 }
2456                 _ => (),
2457             }
2458         }
2459     }
2460
2461     result.push_str(&where_clause_str);
2462
2463     let ends_with_comment = last_line_contains_single_line_comment(&result);
2464     force_new_line_for_brace |= ends_with_comment;
2465     force_new_line_for_brace |=
2466         is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2467     Some((result, ends_with_comment, force_new_line_for_brace))
2468 }
2469
2470 /// Kind of spaces to put before `where`.
2471 #[derive(Copy, Clone)]
2472 enum WhereClauseSpace {
2473     /// A single space.
2474     Space,
2475     /// A new line.
2476     Newline,
2477     /// Nothing.
2478     None,
2479 }
2480
2481 #[derive(Copy, Clone)]
2482 struct WhereClauseOption {
2483     suppress_comma: bool, // Force no trailing comma
2484     snuggle: WhereClauseSpace,
2485     allow_single_line: bool, // Try single line where-clause instead of vertical layout
2486     veto_single_line: bool,  // Disallow a single-line where-clause.
2487 }
2488
2489 impl WhereClauseOption {
2490     fn new(suppress_comma: bool, snuggle: WhereClauseSpace) -> WhereClauseOption {
2491         WhereClauseOption {
2492             suppress_comma,
2493             snuggle,
2494             allow_single_line: false,
2495             veto_single_line: false,
2496         }
2497     }
2498
2499     fn snuggled(current: &str) -> WhereClauseOption {
2500         WhereClauseOption {
2501             suppress_comma: false,
2502             snuggle: if last_line_width(current) == 1 {
2503                 WhereClauseSpace::Space
2504             } else {
2505                 WhereClauseSpace::Newline
2506             },
2507             allow_single_line: false,
2508             veto_single_line: false,
2509         }
2510     }
2511
2512     fn suppress_comma(&mut self) {
2513         self.suppress_comma = true
2514     }
2515
2516     fn allow_single_line(&mut self) {
2517         self.allow_single_line = true
2518     }
2519
2520     fn snuggle(&mut self) {
2521         self.snuggle = WhereClauseSpace::Space
2522     }
2523
2524     fn veto_single_line(&mut self) {
2525         self.veto_single_line = true;
2526     }
2527 }
2528
2529 fn rewrite_params(
2530     context: &RewriteContext<'_>,
2531     params: &[ast::Param],
2532     one_line_budget: usize,
2533     multi_line_budget: usize,
2534     indent: Indent,
2535     param_indent: Indent,
2536     span: Span,
2537     variadic: bool,
2538 ) -> Option<String> {
2539     if params.is_empty() {
2540         let comment = context
2541             .snippet(mk_sp(
2542                 span.lo(),
2543                 // to remove ')'
2544                 span.hi() - BytePos(1),
2545             ))
2546             .trim();
2547         return Some(comment.to_owned());
2548     }
2549     let param_items: Vec<_> = itemize_list(
2550         context.snippet_provider,
2551         params.iter(),
2552         ")",
2553         ",",
2554         |param| span_lo_for_param(param),
2555         |param| param.ty.span.hi(),
2556         |param| {
2557             param
2558                 .rewrite(context, Shape::legacy(multi_line_budget, param_indent))
2559                 .or_else(|| Some(context.snippet(param.span()).to_owned()))
2560         },
2561         span.lo(),
2562         span.hi(),
2563         false,
2564     )
2565     .collect();
2566
2567     let tactic = definitive_tactic(
2568         &param_items,
2569         context
2570             .config
2571             .fn_args_layout()
2572             .to_list_tactic(param_items.len()),
2573         Separator::Comma,
2574         one_line_budget,
2575     );
2576     let budget = match tactic {
2577         DefinitiveListTactic::Horizontal => one_line_budget,
2578         _ => multi_line_budget,
2579     };
2580     let indent = match context.config.indent_style() {
2581         IndentStyle::Block => indent.block_indent(context.config),
2582         IndentStyle::Visual => param_indent,
2583     };
2584     let trailing_separator = if variadic {
2585         SeparatorTactic::Never
2586     } else {
2587         match context.config.indent_style() {
2588             IndentStyle::Block => context.config.trailing_comma(),
2589             IndentStyle::Visual => SeparatorTactic::Never,
2590         }
2591     };
2592     let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
2593         .tactic(tactic)
2594         .trailing_separator(trailing_separator)
2595         .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
2596         .preserve_newline(true);
2597     write_list(&param_items, &fmt)
2598 }
2599
2600 fn compute_budgets_for_params(
2601     context: &RewriteContext<'_>,
2602     result: &str,
2603     indent: Indent,
2604     ret_str_len: usize,
2605     fn_brace_style: FnBraceStyle,
2606     force_vertical_layout: bool,
2607 ) -> Option<(usize, usize, Indent)> {
2608     debug!(
2609         "compute_budgets_for_params {} {:?}, {}, {:?}",
2610         result.len(),
2611         indent,
2612         ret_str_len,
2613         fn_brace_style,
2614     );
2615     // Try keeping everything on the same line.
2616     if !result.contains('\n') && !force_vertical_layout {
2617         // 2 = `()`, 3 = `() `, space is before ret_string.
2618         let overhead = if ret_str_len == 0 { 2 } else { 3 };
2619         let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
2620         match fn_brace_style {
2621             FnBraceStyle::None => used_space += 1,     // 1 = `;`
2622             FnBraceStyle::SameLine => used_space += 2, // 2 = `{}`
2623             FnBraceStyle::NextLine => (),
2624         }
2625         let one_line_budget = context.budget(used_space);
2626
2627         if one_line_budget > 0 {
2628             // 4 = "() {".len()
2629             let (indent, multi_line_budget) = match context.config.indent_style() {
2630                 IndentStyle::Block => {
2631                     let indent = indent.block_indent(context.config);
2632                     (indent, context.budget(indent.width() + 1))
2633                 }
2634                 IndentStyle::Visual => {
2635                     let indent = indent + result.len() + 1;
2636                     let multi_line_overhead = match fn_brace_style {
2637                         FnBraceStyle::SameLine => 4,
2638                         _ => 2,
2639                     } + indent.width();
2640                     (indent, context.budget(multi_line_overhead))
2641                 }
2642             };
2643
2644             return Some((one_line_budget, multi_line_budget, indent));
2645         }
2646     }
2647
2648     // Didn't work. we must force vertical layout and put params on a newline.
2649     let new_indent = indent.block_indent(context.config);
2650     let used_space = match context.config.indent_style() {
2651         // 1 = `,`
2652         IndentStyle::Block => new_indent.width() + 1,
2653         // Account for `)` and possibly ` {`.
2654         IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
2655     };
2656     Some((0, context.budget(used_space), new_indent))
2657 }
2658
2659 fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {
2660     let predicate_count = where_clause.predicates.len();
2661
2662     if config.where_single_line() && predicate_count == 1 {
2663         return FnBraceStyle::SameLine;
2664     }
2665     let brace_style = config.brace_style();
2666
2667     let use_next_line = brace_style == BraceStyle::AlwaysNextLine
2668         || (brace_style == BraceStyle::SameLineWhere && predicate_count > 0);
2669     if use_next_line {
2670         FnBraceStyle::NextLine
2671     } else {
2672         FnBraceStyle::SameLine
2673     }
2674 }
2675
2676 fn rewrite_generics(
2677     context: &RewriteContext<'_>,
2678     ident: &str,
2679     generics: &ast::Generics,
2680     shape: Shape,
2681 ) -> Option<String> {
2682     // FIXME: convert bounds to where-clauses where they get too big or if
2683     // there is a where-clause at all.
2684
2685     if generics.params.is_empty() {
2686         return Some(ident.to_owned());
2687     }
2688
2689     let params = generics.params.iter();
2690     overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
2691 }
2692
2693 fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
2694     match config.indent_style() {
2695         IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2),
2696         IndentStyle::Block => {
2697             // 1 = ","
2698             shape
2699                 .block()
2700                 .block_indent(config.tab_spaces())
2701                 .with_max_width(config)
2702                 .sub_width(1)
2703         }
2704     }
2705 }
2706
2707 fn rewrite_where_clause_rfc_style(
2708     context: &RewriteContext<'_>,
2709     where_clause: &ast::WhereClause,
2710     shape: Shape,
2711     terminator: &str,
2712     span_end: Option<BytePos>,
2713     span_end_before_where: BytePos,
2714     where_clause_option: WhereClauseOption,
2715 ) -> Option<String> {
2716     let (where_keyword, allow_single_line) = rewrite_where_keyword(
2717         context,
2718         where_clause,
2719         shape,
2720         span_end_before_where,
2721         where_clause_option,
2722     )?;
2723
2724     // 1 = `,`
2725     let clause_shape = shape
2726         .block()
2727         .with_max_width(context.config)
2728         .block_left(context.config.tab_spaces())?
2729         .sub_width(1)?;
2730     let force_single_line = context.config.where_single_line()
2731         && where_clause.predicates.len() == 1
2732         && !where_clause_option.veto_single_line;
2733
2734     let preds_str = rewrite_bounds_on_where_clause(
2735         context,
2736         where_clause,
2737         clause_shape,
2738         terminator,
2739         span_end,
2740         where_clause_option,
2741         force_single_line,
2742     )?;
2743
2744     // 6 = `where `
2745     let clause_sep =
2746         if allow_single_line && !preds_str.contains('\n') && 6 + preds_str.len() <= shape.width
2747             || force_single_line
2748         {
2749             Cow::from(" ")
2750         } else {
2751             clause_shape.indent.to_string_with_newline(context.config)
2752         };
2753
2754     Some(format!("{}{}{}", where_keyword, clause_sep, preds_str))
2755 }
2756
2757 /// Rewrite `where` and comment around it.
2758 fn rewrite_where_keyword(
2759     context: &RewriteContext<'_>,
2760     where_clause: &ast::WhereClause,
2761     shape: Shape,
2762     span_end_before_where: BytePos,
2763     where_clause_option: WhereClauseOption,
2764 ) -> Option<(String, bool)> {
2765     let block_shape = shape.block().with_max_width(context.config);
2766     // 1 = `,`
2767     let clause_shape = block_shape
2768         .block_left(context.config.tab_spaces())?
2769         .sub_width(1)?;
2770
2771     let comment_separator = |comment: &str, shape: Shape| {
2772         if comment.is_empty() {
2773             Cow::from("")
2774         } else {
2775             shape.indent.to_string_with_newline(context.config)
2776         }
2777     };
2778
2779     let (span_before, span_after) =
2780         missing_span_before_after_where(span_end_before_where, where_clause);
2781     let (comment_before, comment_after) =
2782         rewrite_comments_before_after_where(context, span_before, span_after, shape)?;
2783
2784     let starting_newline = match where_clause_option.snuggle {
2785         WhereClauseSpace::Space if comment_before.is_empty() => Cow::from(" "),
2786         WhereClauseSpace::None => Cow::from(""),
2787         _ => block_shape.indent.to_string_with_newline(context.config),
2788     };
2789
2790     let newline_before_where = comment_separator(&comment_before, shape);
2791     let newline_after_where = comment_separator(&comment_after, clause_shape);
2792     let result = format!(
2793         "{}{}{}where{}{}",
2794         starting_newline, comment_before, newline_before_where, newline_after_where, comment_after
2795     );
2796     let allow_single_line = where_clause_option.allow_single_line
2797         && comment_before.is_empty()
2798         && comment_after.is_empty();
2799
2800     Some((result, allow_single_line))
2801 }
2802
2803 /// Rewrite bounds on a where clause.
2804 fn rewrite_bounds_on_where_clause(
2805     context: &RewriteContext<'_>,
2806     where_clause: &ast::WhereClause,
2807     shape: Shape,
2808     terminator: &str,
2809     span_end: Option<BytePos>,
2810     where_clause_option: WhereClauseOption,
2811     force_single_line: bool,
2812 ) -> Option<String> {
2813     let span_start = where_clause.predicates[0].span().lo();
2814     // If we don't have the start of the next span, then use the end of the
2815     // predicates, but that means we miss comments.
2816     let len = where_clause.predicates.len();
2817     let end_of_preds = where_clause.predicates[len - 1].span().hi();
2818     let span_end = span_end.unwrap_or(end_of_preds);
2819     let items = itemize_list(
2820         context.snippet_provider,
2821         where_clause.predicates.iter(),
2822         terminator,
2823         ",",
2824         |pred| pred.span().lo(),
2825         |pred| pred.span().hi(),
2826         |pred| pred.rewrite(context, shape),
2827         span_start,
2828         span_end,
2829         false,
2830     );
2831     let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
2832         SeparatorTactic::Never
2833     } else {
2834         context.config.trailing_comma()
2835     };
2836
2837     // shape should be vertical only and only if we have `force_single_line` option enabled
2838     // and the number of items of the where-clause is equal to 1
2839     let shape_tactic = if force_single_line {
2840         DefinitiveListTactic::Horizontal
2841     } else {
2842         DefinitiveListTactic::Vertical
2843     };
2844
2845     let fmt = ListFormatting::new(shape, context.config)
2846         .tactic(shape_tactic)
2847         .trailing_separator(comma_tactic)
2848         .preserve_newline(true);
2849     write_list(&items.collect::<Vec<_>>(), &fmt)
2850 }
2851
2852 fn rewrite_where_clause(
2853     context: &RewriteContext<'_>,
2854     where_clause: &ast::WhereClause,
2855     brace_style: BraceStyle,
2856     shape: Shape,
2857     on_new_line: bool,
2858     terminator: &str,
2859     span_end: Option<BytePos>,
2860     span_end_before_where: BytePos,
2861     where_clause_option: WhereClauseOption,
2862 ) -> Option<String> {
2863     if where_clause.predicates.is_empty() {
2864         return Some(String::new());
2865     }
2866
2867     if context.config.indent_style() == IndentStyle::Block {
2868         return rewrite_where_clause_rfc_style(
2869             context,
2870             where_clause,
2871             shape,
2872             terminator,
2873             span_end,
2874             span_end_before_where,
2875             where_clause_option,
2876         );
2877     }
2878
2879     let extra_indent = Indent::new(context.config.tab_spaces(), 0);
2880
2881     let offset = match context.config.indent_style() {
2882         IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
2883         // 6 = "where ".len()
2884         IndentStyle::Visual => shape.indent + extra_indent + 6,
2885     };
2886     // FIXME: if indent_style != Visual, then the budgets below might
2887     // be out by a char or two.
2888
2889     let budget = context.config.max_width() - offset.width();
2890     let span_start = where_clause.predicates[0].span().lo();
2891     // If we don't have the start of the next span, then use the end of the
2892     // predicates, but that means we miss comments.
2893     let len = where_clause.predicates.len();
2894     let end_of_preds = where_clause.predicates[len - 1].span().hi();
2895     let span_end = span_end.unwrap_or(end_of_preds);
2896     let items = itemize_list(
2897         context.snippet_provider,
2898         where_clause.predicates.iter(),
2899         terminator,
2900         ",",
2901         |pred| pred.span().lo(),
2902         |pred| pred.span().hi(),
2903         |pred| pred.rewrite(context, Shape::legacy(budget, offset)),
2904         span_start,
2905         span_end,
2906         false,
2907     );
2908     let item_vec = items.collect::<Vec<_>>();
2909     // FIXME: we don't need to collect here
2910     let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget);
2911
2912     let mut comma_tactic = context.config.trailing_comma();
2913     // Kind of a hack because we don't usually have trailing commas in where-clauses.
2914     if comma_tactic == SeparatorTactic::Vertical || where_clause_option.suppress_comma {
2915         comma_tactic = SeparatorTactic::Never;
2916     }
2917
2918     let fmt = ListFormatting::new(Shape::legacy(budget, offset), context.config)
2919         .tactic(tactic)
2920         .trailing_separator(comma_tactic)
2921         .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
2922         .preserve_newline(true);
2923     let preds_str = write_list(&item_vec, &fmt)?;
2924
2925     let end_length = if terminator == "{" {
2926         // If the brace is on the next line we don't need to count it otherwise it needs two
2927         // characters " {"
2928         match brace_style {
2929             BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0,
2930             BraceStyle::PreferSameLine => 2,
2931         }
2932     } else if terminator == "=" {
2933         2
2934     } else {
2935         terminator.len()
2936     };
2937     if on_new_line
2938         || preds_str.contains('\n')
2939         || shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
2940     {
2941         Some(format!(
2942             "\n{}where {}",
2943             (shape.indent + extra_indent).to_string(context.config),
2944             preds_str
2945         ))
2946     } else {
2947         Some(format!(" where {}", preds_str))
2948     }
2949 }
2950
2951 fn missing_span_before_after_where(
2952     before_item_span_end: BytePos,
2953     where_clause: &ast::WhereClause,
2954 ) -> (Span, Span) {
2955     let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo());
2956     // 5 = `where`
2957     let pos_after_where = where_clause.span.lo() + BytePos(5);
2958     let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo());
2959     (missing_span_before, missing_span_after)
2960 }
2961
2962 fn rewrite_comments_before_after_where(
2963     context: &RewriteContext<'_>,
2964     span_before_where: Span,
2965     span_after_where: Span,
2966     shape: Shape,
2967 ) -> Option<(String, String)> {
2968     let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
2969     let after_comment = rewrite_missing_comment(
2970         span_after_where,
2971         shape.block_indent(context.config.tab_spaces()),
2972         context,
2973     )?;
2974     Some((before_comment, after_comment))
2975 }
2976
2977 fn format_header(
2978     context: &RewriteContext<'_>,
2979     item_name: &str,
2980     ident: symbol::Ident,
2981     vis: &ast::Visibility,
2982     offset: Indent,
2983 ) -> String {
2984     let mut result = String::with_capacity(128);
2985     let shape = Shape::indented(offset, context.config);
2986
2987     result.push_str(&format_visibility(context, vis).trim());
2988
2989     // Check for a missing comment between the visibility and the item name.
2990     let after_vis = vis.span.hi();
2991     if let Some(before_item_name) = context
2992         .snippet_provider
2993         .opt_span_before(mk_sp(vis.span.lo(), ident.span.hi()), item_name.trim())
2994     {
2995         let missing_span = mk_sp(after_vis, before_item_name);
2996         if let Some(result_with_comment) = combine_strs_with_missing_comments(
2997             context,
2998             &result,
2999             item_name,
3000             missing_span,
3001             shape,
3002             /* allow_extend */ true,
3003         ) {
3004             result = result_with_comment;
3005         }
3006     }
3007
3008     result.push_str(&rewrite_ident(context, ident));
3009
3010     result
3011 }
3012
3013 #[derive(PartialEq, Eq, Clone, Copy)]
3014 enum BracePos {
3015     None,
3016     Auto,
3017     ForceSameLine,
3018 }
3019
3020 fn format_generics(
3021     context: &RewriteContext<'_>,
3022     generics: &ast::Generics,
3023     brace_style: BraceStyle,
3024     brace_pos: BracePos,
3025     offset: Indent,
3026     span: Span,
3027     used_width: usize,
3028 ) -> Option<String> {
3029     let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
3030     let mut result = rewrite_generics(context, "", generics, shape)?;
3031
3032     // If the generics are not parameterized then generics.span.hi() == 0,
3033     // so we use span.lo(), which is the position after `struct Foo`.
3034     let span_end_before_where = if !generics.params.is_empty() {
3035         generics.span.hi()
3036     } else {
3037         span.lo()
3038     };
3039     let (same_line_brace, missed_comments) = if !generics.where_clause.predicates.is_empty() {
3040         let budget = context.budget(last_line_used_width(&result, offset.width()));
3041         let mut option = WhereClauseOption::snuggled(&result);
3042         if brace_pos == BracePos::None {
3043             option.suppress_comma = true;
3044         }
3045         let where_clause_str = rewrite_where_clause(
3046             context,
3047             &generics.where_clause,
3048             brace_style,
3049             Shape::legacy(budget, offset.block_only()),
3050             true,
3051             "{",
3052             Some(span.hi()),
3053             span_end_before_where,
3054             option,
3055         )?;
3056         result.push_str(&where_clause_str);
3057         (
3058             brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,
3059             // missed comments are taken care of in #rewrite_where_clause
3060             None,
3061         )
3062     } else {
3063         (
3064             brace_pos == BracePos::ForceSameLine
3065                 || (result.contains('\n') && brace_style == BraceStyle::PreferSameLine
3066                     || brace_style != BraceStyle::AlwaysNextLine)
3067                 || trimmed_last_line_width(&result) == 1,
3068             rewrite_missing_comment(
3069                 mk_sp(
3070                     span_end_before_where,
3071                     if brace_pos == BracePos::None {
3072                         span.hi()
3073                     } else {
3074                         context.snippet_provider.span_before(span, "{")
3075                     },
3076                 ),
3077                 shape,
3078                 context,
3079             ),
3080         )
3081     };
3082     // add missing comments
3083     let missed_line_comments = missed_comments
3084         .filter(|missed_comments| !missed_comments.is_empty())
3085         .map_or(false, |missed_comments| {
3086             let is_block = is_last_comment_block(&missed_comments);
3087             let sep = if is_block { " " } else { "\n" };
3088             result.push_str(sep);
3089             result.push_str(&missed_comments);
3090             !is_block
3091         });
3092     if brace_pos == BracePos::None {
3093         return Some(result);
3094     }
3095     let total_used_width = last_line_used_width(&result, used_width);
3096     let remaining_budget = context.budget(total_used_width);
3097     // If the same line brace if forced, it indicates that we are rewriting an item with empty body,
3098     // and hence we take the closer into account as well for one line budget.
3099     // We assume that the closer has the same length as the opener.
3100     let overhead = if brace_pos == BracePos::ForceSameLine {
3101         // 3 = ` {}`
3102         3
3103     } else {
3104         // 2 = ` {`
3105         2
3106     };
3107     let forbid_same_line_brace = missed_line_comments || overhead > remaining_budget;
3108     if !forbid_same_line_brace && same_line_brace {
3109         result.push(' ');
3110     } else {
3111         result.push('\n');
3112         result.push_str(&offset.block_only().to_string(context.config));
3113     }
3114     result.push('{');
3115
3116     Some(result)
3117 }
3118
3119 impl Rewrite for ast::ForeignItem {
3120     fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
3121         let attrs_str = self.attrs.rewrite(context, shape)?;
3122         // Drop semicolon or it will be interpreted as comment.
3123         // FIXME: this may be a faulty span from libsyntax.
3124         let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
3125
3126         let item_str = match self.kind {
3127             ast::ForeignItemKind::Fn(ref fn_kind) => {
3128                 let ast::FnKind(defaultness, ref fn_sig, ref generics, ref block) = **fn_kind;
3129                 if let Some(ref body) = block {
3130                     let mut visitor = FmtVisitor::from_context(context);
3131                     visitor.block_indent = shape.indent;
3132                     visitor.last_pos = self.span.lo();
3133                     let inner_attrs = inner_attributes(&self.attrs);
3134                     let fn_ctxt = visit::FnCtxt::Foreign;
3135                     visitor.visit_fn(
3136                         visit::FnKind::Fn(fn_ctxt, self.ident, &fn_sig, &self.vis, Some(body)),
3137                         generics,
3138                         &fn_sig.decl,
3139                         self.span,
3140                         defaultness,
3141                         Some(&inner_attrs),
3142                     );
3143                     Some(visitor.buffer.to_owned())
3144                 } else {
3145                     rewrite_fn_base(
3146                         context,
3147                         shape.indent,
3148                         self.ident,
3149                         &FnSig::from_method_sig(&fn_sig, generics, self.vis.clone()),
3150                         span,
3151                         FnBraceStyle::None,
3152                     )
3153                     .map(|(s, _, _)| format!("{};", s))
3154                 }
3155             }
3156             ast::ForeignItemKind::Static(ref ty, mutability, _) => {
3157                 // FIXME(#21): we're dropping potential comments in between the
3158                 // function kw here.
3159                 let vis = format_visibility(context, &self.vis);
3160                 let mut_str = format_mutability(mutability);
3161                 let prefix = format!(
3162                     "{}static {}{}:",
3163                     vis,
3164                     mut_str,
3165                     rewrite_ident(context, self.ident)
3166                 );
3167                 // 1 = ;
3168                 rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
3169             }
3170             ast::ForeignItemKind::TyAlias(ref ty_alias_kind) => {
3171                 let ast::TyAliasKind(_, ref generics, ref generic_bounds, ref type_default) =
3172                     **ty_alias_kind;
3173                 rewrite_type_alias(
3174                     self.ident,
3175                     type_default.as_ref(),
3176                     generics,
3177                     Some(generic_bounds),
3178                     &context,
3179                     shape.indent,
3180                     &self.vis,
3181                     self.span,
3182                 )
3183             }
3184             ast::ForeignItemKind::MacCall(ref mac) => {
3185                 rewrite_macro(mac, None, context, shape, MacroPosition::Item)
3186             }
3187         }?;
3188
3189         let missing_span = if self.attrs.is_empty() {
3190             mk_sp(self.span.lo(), self.span.lo())
3191         } else {
3192             mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
3193         };
3194         combine_strs_with_missing_comments(
3195             context,
3196             &attrs_str,
3197             &item_str,
3198             missing_span,
3199             shape,
3200             false,
3201         )
3202     }
3203 }
3204
3205 /// Rewrite the attributes of an item.
3206 fn rewrite_attrs(
3207     context: &RewriteContext<'_>,
3208     item: &ast::Item,
3209     item_str: &str,
3210     shape: Shape,
3211 ) -> Option<String> {
3212     let attrs = filter_inline_attrs(&item.attrs, item.span());
3213     let attrs_str = attrs.rewrite(context, shape)?;
3214
3215     let missed_span = if attrs.is_empty() {
3216         mk_sp(item.span.lo(), item.span.lo())
3217     } else {
3218         mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
3219     };
3220
3221     let allow_extend = if attrs.len() == 1 {
3222         let line_len = attrs_str.len() + 1 + item_str.len();
3223         !attrs.first().unwrap().is_doc_comment()
3224             && context.config.inline_attribute_width() >= line_len
3225     } else {
3226         false
3227     };
3228
3229     combine_strs_with_missing_comments(
3230         context,
3231         &attrs_str,
3232         &item_str,
3233         missed_span,
3234         shape,
3235         allow_extend,
3236     )
3237 }
3238
3239 /// Rewrite an inline mod.
3240 /// The given shape is used to format the mod's attributes.
3241 pub(crate) fn rewrite_mod(
3242     context: &RewriteContext<'_>,
3243     item: &ast::Item,
3244     attrs_shape: Shape,
3245 ) -> Option<String> {
3246     let mut result = String::with_capacity(32);
3247     result.push_str(&*format_visibility(context, &item.vis));
3248     result.push_str("mod ");
3249     result.push_str(rewrite_ident(context, item.ident));
3250     result.push(';');
3251     rewrite_attrs(context, item, &result, attrs_shape)
3252 }
3253
3254 /// Rewrite `extern crate foo;`.
3255 /// The given shape is used to format the extern crate's attributes.
3256 pub(crate) fn rewrite_extern_crate(
3257     context: &RewriteContext<'_>,
3258     item: &ast::Item,
3259     attrs_shape: Shape,
3260 ) -> Option<String> {
3261     assert!(is_extern_crate(item));
3262     let new_str = context.snippet(item.span);
3263     let item_str = if contains_comment(new_str) {
3264         new_str.to_owned()
3265     } else {
3266         let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
3267         String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
3268     };
3269     rewrite_attrs(context, item, &item_str, attrs_shape)
3270 }
3271
3272 /// Returns `true` for `mod foo;`, false for `mod foo { .. }`.
3273 pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
3274     !matches!(
3275         item.kind,
3276         ast::ItemKind::Mod(_, ast::ModKind::Loaded(_, ast::Inline::Yes, _))
3277     )
3278 }
3279
3280 pub(crate) fn is_use_item(item: &ast::Item) -> bool {
3281     matches!(item.kind, ast::ItemKind::Use(_))
3282 }
3283
3284 pub(crate) fn is_extern_crate(item: &ast::Item) -> bool {
3285     matches!(item.kind, ast::ItemKind::ExternCrate(..))
3286 }