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