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