Skip to main content

rustfmt_nightly/
items.rs

1// Formatting top-level items - functions, structs, enums, traits, impls.
2
3use 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
49// Statements of the form
50// let pat: ty = init; or let pat: ty = init else { .. };
51impl 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        // FIXME(super_let): Implement formatting
69        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        // 4 = "let ".len()
92        let pat_shape = shape.offset_left(4, self.span())?;
93        // 1 = ;
94        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        // String that is placed within the assignment pattern and expression.
100        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                // 2 = ` =`
112                .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            // 1 = trailing semicolon;
131            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                // Strip attributes and comments to check if newline is needed before the else
144                // keyword from the initializer part. (#5901)
145                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                // At this point we've written `let {pat} = {expr} else' into the buffer, and we
163                // want to calculate up front if there's room to write the divergent block on the
164                // same line. The available space varies based on indentation so we clamp the width
165                // on the smaller of `shape.width` and `single_line_let_else_max_width`.
166                let max_width =
167                    std::cmp::min(shape.width, context.config.single_line_let_else_max_width());
168
169                // If available_space hits zero we know for sure this will be a multi-lined block
170                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                // +1 for the trailing `;`
187                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                    // writing this on one line would exceed the available width
191                    // so rewrite the else block over multiple lines.
192                    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
204/// When the initializer expression is multi-lined, then the else keyword and opening brace of the
205/// block ( i.e. "else {") should be put on the same line as the end of the initializer expression
206/// if all the following are true:
207///
208/// 1. The initializer expression ends with one or more closing parentheses, square brackets,
209///    or braces
210/// 2. There is nothing else on that line
211/// 3. That line is not indented beyond the indent on the first line of the let keyword
212fn 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        // initializer expression is single lined. The "else {" can only be placed on the same line
220        // as the initializer expression if there is enough room for it.
221        // 7 = ` else {`
222        return init_shape.width.saturating_sub(init_str.len()) >= 7;
223    }
224
225    // 1. The initializer expression ends with one or more `)`, `]`, `}`.
226    if !init_str.ends_with([')', ']', '}']) {
227        return false;
228    }
229
230    // 2. There is nothing else on that line
231    // For example, there are no comments
232    let else_kw_snippet = context.snippet(else_kw_span).trim();
233    if else_kw_snippet != "else" {
234        return false;
235    }
236
237    // 3. The last line of the initializer expression is not indented beyond the `let` keyword
238    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// FIXME convert to using rewrite style rather than visitor
260// FIXME format modules in this style
261#[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    // Stmt(&'a ast::Stmt),
293    // Field(&'a ast::ExprField),
294    // Variant(&'a ast::Variant),
295    // Item(&'a ast::Item),
296    ForeignItem(&'a ast::ForeignItem),
297}
298
299/// Represents a fn's signature.
300pub(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        // Vis defaultness constness unsafety abi.
356        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            // FIXME: this skips comments between the extern keyword and the opening
381            // brace.
382            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        // 2 = ` {`
438        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        // Drop semicolon or it will be interpreted as comment.
459        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 `result` ends with a comment, then remember to add a newline
472        if ends_with_comment {
473            result.push_str(&indent.to_string_with_newline(context.config));
474        }
475
476        // Re-attach semicolon
477        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            // make a span that starts right after `enum Foo`
558            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    // Format the body of an enum definition
576    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            // 1 = "}"
585            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        // If enum variants have discriminants, try to vertically align those,
601        // provided the discrims are not shifted too much  to the right
602        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        // cut the list at the point of longest discrim shorter than the threshold
610        // All of the discrims under the threshold will get padded, and all above - left as is.
611        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        // If one of the variants use multiple lines, use multi-lined formatting for all variants.
644        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    // Variant of an enum.
663    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            // StyleEdition::Edition20{15|18|21} formatting that was off by 1. See issue #5801
681            field.attrs.rewrite(&context, shape.sub_width_opt(1)?)?
682        };
683        // sub_width(1) to take the trailing comma into account
684        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(&lty.ty, &rty.ty) || both_opaque(&lty.ty, &rty.ty) =>
733                {
734                    false
735                }
736                (Const(..), Const(..)) => false,
737                _ => true,
738            };
739
740            // Create visitor for each items, then reorder them.
741            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(&lty.ty, &rty.ty) || both_opaque(&lty.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                // Make sure that there are at least a single empty line between
773                // different impl items.
774                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 there is no where-clause, we may have missing comments between the trait name and
844    // the opening brace.
845    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 there is only one where-clause predicate
865            // and the where-clause spans multiple lines,
866            // then recover the suppressed comma in single line where-clause formatting
867            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    // this is an impl body snippet(impl SampleImpl { /* here */ })
897    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_constness(*constness));
964        result.push_str(format_safety(of_trait.safety));
965    } else {
966        result.push_str(format_constness(*constness));
967    }
968
969    let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
970        Shape::indented(offset + last_line_width(&result), context.config)
971    } else {
972        generics_shape_from_config(
973            context.config,
974            Shape::indented(offset + last_line_width(&result), context.config),
975            0,
976            item.span,
977        )?
978    };
979    let generics_str = rewrite_generics(context, "impl", generics, shape)?;
980    result.push_str(&generics_str);
981
982    let trait_ref_overhead;
983    if let Some(of_trait) = of_trait.as_deref() {
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    // Try to put the self type in a single line.
1002    let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
1003        // If there is no where-clause adapt budget for type formatting to take space and curly
1004        // brace into account.
1005        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    // 1 = space before the type.
1014    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    // Couldn't fit the self type on a single line, put it on a new line.
1028    result.push('\n');
1029    // Add indentation of one additional tab.
1030    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    // 1 = space between generics and trait_ref
1052    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    // We could not make enough space for trait_ref, so put it on new line.
1060    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        ref impl_restriction,
1159        constness,
1160        is_auto,
1161        safety,
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_impl_restriction(context, impl_restriction),
1173        format_constness(constness),
1174        format_safety(safety),
1175        format_auto(is_auto),
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    // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1186    if !bounds.is_empty() {
1187        // Retrieve *unnormalized* ident (See #6069)
1188        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    // Rewrite where-clause.
1207    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 the where-clause cannot fit on the same line,
1230        // put the where-clause on a new line
1231        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            // 1 = `{`
1244            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    // 6 = "trait ", 2 = " ="
1378    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    // 1 = ";"
1384    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            // make a span that starts right after `struct Foo`
1413            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        // Adjust the span to start at the end of the generic arguments before searching for the '{'
1438        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            // make a span that starts right after `struct Foo`
1456            mk_sp(header_hi, body_lo),
1457            last_line_width(&result),
1458        )?,
1459        None => {
1460            // 3 = ` {}`, 2 = ` {`.
1461            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    // 1 = `}`
1472    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    // 3 = ` ` and ` }`
1492    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
1530// Format tuple or struct without any fields. We need to make sure that the comments
1531// inside the delimiters are preserved.
1532fn 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    // 3 = " {}" or "();"
1541    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    // indented shape for proper indenting of multi-line comments
1548    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        // This is a dirty hack to work around a missing `)` from the span of the last field.
1595        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        // We need to put the where-clause on a new line, but we didn't
1659        // know that earlier, so the where-clause will not be indented properly.
1660        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    // Type Aliases are formatted slightly differently depending on the context
1712    // in which they appear, whether they are opaque, and whether they are associated.
1713    // https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
1714    // https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
1715    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    // the span of the end of the RHS (or the end of the generics, if there is no RHS)
1749    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        // 2 = `= `
1761        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            // 2 = `: `
1772            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 there are any where clauses, add a newline before the assignment.
1801        // If there is a before where clause, do not indent, but if there is
1802        // only an after where clause, additionally indent the type.
1803        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        // 1 = `;` unless there's a trailing where clause
1849        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 mut_restriction = format_mut_restriction(context, &field.mut_restriction);
1893    let safety = format_safety(field.safety);
1894    let type_annotation_spacing = type_annotation_spacing(context.config);
1895    Ok(match field.ident {
1896        Some(name) => format!(
1897            "{vis}{mut_restriction}{safety}{}{}:",
1898            rewrite_ident(context, name),
1899            type_annotation_spacing.0
1900        ),
1901        None => format!("{vis}{mut_restriction}{safety}"),
1902    })
1903}
1904
1905impl Rewrite for ast::FieldDef {
1906    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1907        self.rewrite_result(context, shape).ok()
1908    }
1909
1910    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1911        rewrite_struct_field(context, self, shape, 0)
1912    }
1913}
1914
1915pub(crate) fn rewrite_struct_field(
1916    context: &RewriteContext<'_>,
1917    field: &ast::FieldDef,
1918    shape: Shape,
1919    lhs_max_width: usize,
1920) -> RewriteResult {
1921    // FIXME(default_field_values): Implement formatting.
1922    if field.default.is_some() {
1923        return Err(RewriteError::Unknown);
1924    }
1925
1926    if contains_skip(&field.attrs) {
1927        return Ok(context.snippet(field.span()).to_owned());
1928    }
1929
1930    let type_annotation_spacing = type_annotation_spacing(context.config);
1931    let prefix = rewrite_struct_field_prefix(context, field)?;
1932
1933    let attrs_str = field.attrs.rewrite_result(context, shape)?;
1934    let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
1935    let missing_span = if field.attrs.is_empty() {
1936        mk_sp(field.span.lo(), field.span.lo())
1937    } else {
1938        mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
1939    };
1940    let mut spacing = String::from(if field.ident.is_some() {
1941        type_annotation_spacing.1
1942    } else {
1943        ""
1944    });
1945    // Try to put everything on a single line.
1946    let attr_prefix = combine_strs_with_missing_comments(
1947        context,
1948        &attrs_str,
1949        &prefix,
1950        missing_span,
1951        shape,
1952        attrs_extendable,
1953    )?;
1954    let overhead = trimmed_last_line_width(&attr_prefix);
1955    let lhs_offset = lhs_max_width.saturating_sub(overhead);
1956    for _ in 0..lhs_offset {
1957        spacing.push(' ');
1958    }
1959    // In this extreme case we will be missing a space between an attribute and a field.
1960    if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
1961        spacing.push(' ');
1962    }
1963
1964    let orig_ty = shape
1965        .offset_left_opt(overhead + spacing.len())
1966        .and_then(|ty_shape| field.ty.rewrite_result(context, ty_shape).ok());
1967
1968    if let Some(ref ty) = orig_ty {
1969        if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
1970            return Ok(attr_prefix + &spacing + ty);
1971        }
1972    }
1973
1974    let is_prefix_empty = prefix.is_empty();
1975    // We must use multiline. We are going to put attributes and a field on different lines.
1976    let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
1977    // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1978    let field_str = if is_prefix_empty {
1979        field_str.trim_start()
1980    } else {
1981        &field_str
1982    };
1983    combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
1984}
1985
1986pub(crate) struct StaticParts<'a> {
1987    prefix: &'a str,
1988    safety: ast::Safety,
1989    vis: &'a ast::Visibility,
1990    ident: symbol::Ident,
1991    generics: Option<&'a ast::Generics>,
1992    ty: &'a ast::Ty,
1993    mutability: ast::Mutability,
1994    expr_opt: Option<&'a ast::Expr>,
1995    defaultness: Option<ast::Defaultness>,
1996    span: Span,
1997}
1998
1999impl<'a> StaticParts<'a> {
2000    pub(crate) fn from_item(item: &'a ast::Item) -> Self {
2001        let (defaultness, prefix, safety, ident, ty, mutability, expr_opt, generics) =
2002            match &item.kind {
2003                ast::ItemKind::Static(s) => (
2004                    None,
2005                    "static",
2006                    s.safety,
2007                    s.ident,
2008                    &s.ty,
2009                    s.mutability,
2010                    s.expr.as_deref(),
2011                    None,
2012                ),
2013                ast::ItemKind::Const(c) => (
2014                    Some(c.defaultness),
2015                    if c.rhs_kind.is_type_const() {
2016                        "type const"
2017                    } else {
2018                        "const"
2019                    },
2020                    ast::Safety::Default,
2021                    c.ident,
2022                    &c.ty,
2023                    ast::Mutability::Not,
2024                    c.rhs_kind.expr(),
2025                    Some(&c.generics),
2026                ),
2027                _ => unreachable!(),
2028            };
2029        StaticParts {
2030            prefix,
2031            safety,
2032            vis: &item.vis,
2033            ident,
2034            generics,
2035            ty,
2036            mutability,
2037            expr_opt,
2038            defaultness,
2039            span: item.span,
2040        }
2041    }
2042
2043    pub(crate) fn from_trait_item(ti: &'a ast::AssocItem, ident: Ident) -> Self {
2044        let (defaultness, ty, expr_opt, generics, prefix) = match &ti.kind {
2045            ast::AssocItemKind::Const(c) => {
2046                let prefix = if c.rhs_kind.is_type_const() {
2047                    "type const"
2048                } else {
2049                    "const"
2050                };
2051                (
2052                    c.defaultness,
2053                    &c.ty,
2054                    c.rhs_kind.expr(),
2055                    Some(&c.generics),
2056                    prefix,
2057                )
2058            }
2059            _ => unreachable!(),
2060        };
2061        StaticParts {
2062            prefix,
2063            safety: ast::Safety::Default,
2064            vis: &ti.vis,
2065            ident,
2066            generics,
2067            ty,
2068            mutability: ast::Mutability::Not,
2069            expr_opt,
2070            defaultness: Some(defaultness),
2071            span: ti.span,
2072        }
2073    }
2074
2075    pub(crate) fn from_impl_item(ii: &'a ast::AssocItem, ident: Ident) -> Self {
2076        let (defaultness, ty, expr_opt, generics, prefix) = match &ii.kind {
2077            ast::AssocItemKind::Const(c) => {
2078                let prefix = if c.rhs_kind.is_type_const() {
2079                    "type const"
2080                } else {
2081                    "const"
2082                };
2083                (
2084                    c.defaultness,
2085                    &c.ty,
2086                    c.rhs_kind.expr(),
2087                    Some(&c.generics),
2088                    prefix,
2089                )
2090            }
2091            _ => unreachable!(),
2092        };
2093        StaticParts {
2094            prefix,
2095            safety: ast::Safety::Default,
2096            vis: &ii.vis,
2097            ident,
2098            generics,
2099            ty,
2100            mutability: ast::Mutability::Not,
2101            expr_opt,
2102            defaultness: Some(defaultness),
2103            span: ii.span,
2104        }
2105    }
2106}
2107
2108fn rewrite_static(
2109    context: &RewriteContext<'_>,
2110    static_parts: &StaticParts<'_>,
2111    offset: Indent,
2112) -> Option<String> {
2113    // For now, if this static (or const) has generics, then bail.
2114    if static_parts
2115        .generics
2116        .is_some_and(|g| !g.params.is_empty() || !g.where_clause.is_empty())
2117    {
2118        return None;
2119    }
2120
2121    let colon = colon_spaces(context.config);
2122    let mut prefix = format!(
2123        "{}{}{}{} {}{}{}",
2124        format_visibility(context, static_parts.vis),
2125        static_parts.defaultness.map_or("", format_defaultness),
2126        format_safety(static_parts.safety),
2127        static_parts.prefix,
2128        format_mutability(static_parts.mutability),
2129        rewrite_ident(context, static_parts.ident),
2130        colon,
2131    );
2132    // 2 = " =".len()
2133    let ty_shape =
2134        Shape::indented(offset.block_only(), context.config).offset_left_opt(prefix.len() + 2)?;
2135    let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
2136        Some(ty_str) => ty_str,
2137        None => {
2138            if prefix.ends_with(' ') {
2139                prefix.pop();
2140            }
2141            let nested_indent = offset.block_indent(context.config);
2142            let nested_shape = Shape::indented(nested_indent, context.config);
2143            let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
2144            format!(
2145                "{}{}",
2146                nested_indent.to_string_with_newline(context.config),
2147                ty_str
2148            )
2149        }
2150    };
2151
2152    if let Some(expr) = static_parts.expr_opt {
2153        let comments_lo = context.snippet_provider.span_after(static_parts.span, "=");
2154        let expr_lo = expr.span.lo();
2155        let comments_span = mk_sp(comments_lo, expr_lo);
2156
2157        let lhs = format!("{prefix}{ty_str} =");
2158
2159        // 1 = ;
2160        let remaining_width = context.budget(offset.block_indent + 1);
2161        rewrite_assign_rhs_with_comments(
2162            context,
2163            &lhs,
2164            expr,
2165            Shape::legacy(remaining_width, offset.block_only()),
2166            &RhsAssignKind::Expr(&expr.kind, expr.span),
2167            RhsTactics::Default,
2168            comments_span,
2169            true,
2170        )
2171        .ok()
2172        .map(|res| recover_comment_removed(res, static_parts.span, context))
2173        .map(|s| if s.ends_with(';') { s } else { s + ";" })
2174    } else {
2175        Some(format!("{prefix}{ty_str};"))
2176    }
2177}
2178
2179// FIXME(calebcartwright) - This is a hack around a bug in the handling of TyKind::ImplTrait.
2180// This should be removed once that bug is resolved, with the type alias formatting using the
2181// defined Ty for the RHS directly.
2182// https://github.com/rust-lang/rustfmt/issues/4373
2183// https://github.com/rust-lang/rustfmt/issues/5027
2184struct OpaqueType<'a> {
2185    bounds: &'a ast::GenericBounds,
2186}
2187
2188impl<'a> Rewrite for OpaqueType<'a> {
2189    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2190        let shape = shape.offset_left_opt(5)?; // `impl `
2191        self.bounds
2192            .rewrite(context, shape)
2193            .map(|s| format!("impl {}", s))
2194    }
2195}
2196
2197impl Rewrite for ast::FnRetTy {
2198    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2199        self.rewrite_result(context, shape).ok()
2200    }
2201
2202    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
2203        match *self {
2204            ast::FnRetTy::Default(_) => Ok(String::new()),
2205            ast::FnRetTy::Ty(ref ty) => {
2206                let arrow_width = "-> ".len();
2207                if context.config.style_edition() <= StyleEdition::Edition2021
2208                    || context.config.indent_style() == IndentStyle::Visual
2209                {
2210                    let inner_width = shape
2211                        .width
2212                        .checked_sub(arrow_width)
2213                        .max_width_error(shape.width, self.span())?;
2214                    return ty
2215                        .rewrite_result(
2216                            context,
2217                            Shape::legacy(inner_width, shape.indent + arrow_width),
2218                        )
2219                        .map(|r| format!("-> {}", r));
2220                }
2221
2222                let shape = shape.offset_left(arrow_width, self.span())?;
2223
2224                ty.rewrite_result(context, shape)
2225                    .map(|s| format!("-> {}", s))
2226            }
2227        }
2228    }
2229}
2230
2231fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
2232    match ty.kind {
2233        ast::TyKind::Infer => ty.span.hi() == pat_span.hi(),
2234        _ => false,
2235    }
2236}
2237
2238/// Recover any missing comments between the param and the type.
2239///
2240/// # Returns
2241///
2242/// A 2-len tuple with the comment before the colon in first position, and the comment after the
2243/// colon in second position.
2244fn get_missing_param_comments(
2245    context: &RewriteContext<'_>,
2246    pat_span: Span,
2247    ty_span: Span,
2248    shape: Shape,
2249) -> (String, String) {
2250    let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());
2251
2252    let span_before_colon = {
2253        let missing_comment_span_hi = context
2254            .snippet_provider
2255            .span_before(missing_comment_span, ":");
2256        mk_sp(pat_span.hi(), missing_comment_span_hi)
2257    };
2258    let span_after_colon = {
2259        let missing_comment_span_lo = context
2260            .snippet_provider
2261            .span_after(missing_comment_span, ":");
2262        mk_sp(missing_comment_span_lo, ty_span.lo())
2263    };
2264
2265    let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
2266        .ok()
2267        .filter(|comment| !comment.is_empty())
2268        .map_or(String::new(), |comment| format!(" {}", comment));
2269    let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
2270        .ok()
2271        .filter(|comment| !comment.is_empty())
2272        .map_or(String::new(), |comment| format!("{} ", comment));
2273    (comment_before_colon, comment_after_colon)
2274}
2275
2276impl Rewrite for ast::Param {
2277    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2278        self.rewrite_result(context, shape).ok()
2279    }
2280
2281    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
2282        let param_attrs_result = self
2283            .attrs
2284            .rewrite_result(context, Shape::legacy(shape.width, shape.indent))?;
2285        // N.B. Doc comments aren't typically valid syntax, but could appear
2286        // in the presence of certain macros - https://github.com/rust-lang/rustfmt/issues/4936
2287        let (span, has_multiple_attr_lines, has_doc_comments) = if !self.attrs.is_empty() {
2288            let num_attrs = self.attrs.len();
2289            (
2290                mk_sp(self.attrs[num_attrs - 1].span.hi(), self.pat.span.lo()),
2291                param_attrs_result.contains('\n'),
2292                self.attrs.iter().any(|a| a.is_doc_comment()),
2293            )
2294        } else {
2295            (mk_sp(self.span.lo(), self.span.lo()), false, false)
2296        };
2297
2298        if let Some(ref explicit_self) = self.to_self() {
2299            rewrite_explicit_self(
2300                context,
2301                explicit_self,
2302                &param_attrs_result,
2303                span,
2304                shape,
2305                has_multiple_attr_lines,
2306            )
2307        } else if is_named_param(self) {
2308            let param_name = &self
2309                .pat
2310                .rewrite_result(context, Shape::legacy(shape.width, shape.indent))?;
2311            let mut result = combine_strs_with_missing_comments(
2312                context,
2313                &param_attrs_result,
2314                param_name,
2315                span,
2316                shape,
2317                !has_multiple_attr_lines && !has_doc_comments,
2318            )?;
2319
2320            if !is_empty_infer(&*self.ty, self.pat.span) {
2321                let (before_comment, after_comment) =
2322                    get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
2323                result.push_str(&before_comment);
2324                result.push_str(colon_spaces(context.config));
2325                result.push_str(&after_comment);
2326                let overhead = last_line_width(&result);
2327                let max_width = shape
2328                    .width
2329                    .checked_sub(overhead)
2330                    .max_width_error(shape.width, self.span())?;
2331                if let Ok(ty_str) = self
2332                    .ty
2333                    .rewrite_result(context, Shape::legacy(max_width, shape.indent))
2334                {
2335                    result.push_str(&ty_str);
2336                } else {
2337                    let prev_str = if param_attrs_result.is_empty() {
2338                        param_attrs_result
2339                    } else {
2340                        param_attrs_result + &shape.to_string_with_newline(context.config)
2341                    };
2342
2343                    result = combine_strs_with_missing_comments(
2344                        context,
2345                        &prev_str,
2346                        param_name,
2347                        span,
2348                        shape,
2349                        !has_multiple_attr_lines,
2350                    )?;
2351                    result.push_str(&before_comment);
2352                    result.push_str(colon_spaces(context.config));
2353                    result.push_str(&after_comment);
2354                    let overhead = last_line_width(&result);
2355                    let max_width = shape
2356                        .width
2357                        .checked_sub(overhead)
2358                        .max_width_error(shape.width, self.span())?;
2359                    let ty_str = self
2360                        .ty
2361                        .rewrite_result(context, Shape::legacy(max_width, shape.indent))?;
2362                    result.push_str(&ty_str);
2363                }
2364            }
2365
2366            Ok(result)
2367        } else {
2368            self.ty.rewrite_result(context, shape)
2369        }
2370    }
2371}
2372
2373fn rewrite_opt_lifetime(
2374    context: &RewriteContext<'_>,
2375    lifetime: Option<ast::Lifetime>,
2376) -> RewriteResult {
2377    let Some(l) = lifetime else {
2378        return Ok(String::new());
2379    };
2380    let mut result = l.rewrite_result(
2381        context,
2382        Shape::legacy(context.config.max_width(), Indent::empty()),
2383    )?;
2384    result.push(' ');
2385    Ok(result)
2386}
2387
2388fn rewrite_explicit_self(
2389    context: &RewriteContext<'_>,
2390    explicit_self: &ast::ExplicitSelf,
2391    param_attrs: &str,
2392    span: Span,
2393    shape: Shape,
2394    has_multiple_attr_lines: bool,
2395) -> RewriteResult {
2396    let self_str = match explicit_self.node {
2397        ast::SelfKind::Region(lt, m) => {
2398            let mut_str = format_mutability(m);
2399            let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2400            format!("&{lifetime_str}{mut_str}self")
2401        }
2402        ast::SelfKind::Pinned(lt, m) => {
2403            let mut_str = m.ptr_str();
2404            let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2405            format!("&{lifetime_str}pin {mut_str} self")
2406        }
2407        ast::SelfKind::Explicit(ref ty, mutability) => {
2408            let type_str = ty.rewrite_result(
2409                context,
2410                Shape::legacy(context.config.max_width(), Indent::empty()),
2411            )?;
2412            format!("{}self: {}", format_mutability(mutability), type_str)
2413        }
2414        ast::SelfKind::Value(mutability) => format!("{}self", format_mutability(mutability)),
2415    };
2416    Ok(combine_strs_with_missing_comments(
2417        context,
2418        param_attrs,
2419        &self_str,
2420        span,
2421        shape,
2422        !has_multiple_attr_lines,
2423    )?)
2424}
2425
2426pub(crate) fn span_lo_for_param(param: &ast::Param) -> BytePos {
2427    if param.attrs.is_empty() {
2428        if is_named_param(param) {
2429            param.pat.span.lo()
2430        } else {
2431            param.ty.span.lo()
2432        }
2433    } else {
2434        param.attrs[0].span.lo()
2435    }
2436}
2437
2438pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param) -> BytePos {
2439    match param.ty.kind {
2440        ast::TyKind::Infer if context.snippet(param.ty.span) == "_" => param.ty.span.hi(),
2441        ast::TyKind::Infer if is_named_param(param) => param.pat.span.hi(),
2442        _ => param.ty.span.hi(),
2443    }
2444}
2445
2446pub(crate) fn is_named_param(param: &ast::Param) -> bool {
2447    !matches!(param.pat.kind, ast::PatKind::Missing)
2448}
2449
2450#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2451pub(crate) enum FnBraceStyle {
2452    SameLine,
2453    NextLine,
2454    None,
2455}
2456
2457// Return type is (result, force_new_line_for_brace)
2458fn rewrite_fn_base(
2459    context: &RewriteContext<'_>,
2460    indent: Indent,
2461    ident: symbol::Ident,
2462    fn_sig: &FnSig<'_>,
2463    span: Span,
2464    fn_brace_style: FnBraceStyle,
2465) -> Result<(String, bool, bool), RewriteError> {
2466    let mut force_new_line_for_brace = false;
2467
2468    let where_clause = &fn_sig.generics.where_clause;
2469
2470    let mut result = String::with_capacity(1024);
2471    result.push_str(&fn_sig.to_str(context));
2472
2473    // fn foo
2474    result.push_str("fn ");
2475
2476    // Generics.
2477    let overhead = if let FnBraceStyle::SameLine = fn_brace_style {
2478        // 4 = `() {`
2479        4
2480    } else {
2481        // 2 = `()`
2482        2
2483    };
2484    let used_width = last_line_used_width(&result, indent.width());
2485    let one_line_budget = context.budget(used_width + overhead);
2486    let shape = Shape {
2487        width: one_line_budget,
2488        indent,
2489        offset: used_width,
2490    };
2491    let fd = fn_sig.decl;
2492    let generics_str = rewrite_generics(
2493        context,
2494        rewrite_ident(context, ident),
2495        &fn_sig.generics,
2496        shape,
2497    )?;
2498    result.push_str(&generics_str);
2499
2500    let snuggle_angle_bracket = generics_str
2501        .lines()
2502        .last()
2503        .map_or(false, |l| l.trim_start().len() == 1);
2504
2505    // Note that the width and indent don't really matter, we'll re-layout the
2506    // return type later anyway.
2507    let ret_str = fd
2508        .output
2509        .rewrite_result(context, Shape::indented(indent, context.config))?;
2510
2511    let multi_line_ret_str = ret_str.contains('\n');
2512    let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
2513
2514    // Params.
2515    let (one_line_budget, multi_line_budget, mut param_indent) = compute_budgets_for_params(
2516        context,
2517        &result,
2518        indent,
2519        ret_str_len,
2520        fn_brace_style,
2521        multi_line_ret_str,
2522    );
2523
2524    debug!(
2525        "rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
2526        one_line_budget, multi_line_budget, param_indent
2527    );
2528
2529    result.push('(');
2530    // Check if vertical layout was forced.
2531    if one_line_budget == 0
2532        && !snuggle_angle_bracket
2533        && context.config.indent_style() == IndentStyle::Visual
2534    {
2535        result.push_str(&param_indent.to_string_with_newline(context.config));
2536    }
2537
2538    let params_end = if fd.inputs.is_empty() {
2539        context
2540            .snippet_provider
2541            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), ")")
2542    } else {
2543        let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
2544        context.snippet_provider.span_after(last_span, ")")
2545    };
2546    let params_span = mk_sp(
2547        context
2548            .snippet_provider
2549            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), "("),
2550        params_end,
2551    );
2552    let param_str = rewrite_params(
2553        context,
2554        &fd.inputs,
2555        one_line_budget,
2556        multi_line_budget,
2557        indent,
2558        param_indent,
2559        params_span,
2560        fd.c_variadic(),
2561    )?;
2562
2563    let put_params_in_block = match context.config.indent_style() {
2564        IndentStyle::Block => param_str.contains('\n') || param_str.len() > one_line_budget,
2565        _ => false,
2566    } && !fd.inputs.is_empty();
2567
2568    let mut params_last_line_contains_comment = false;
2569    let mut no_params_and_over_max_width = false;
2570
2571    if put_params_in_block {
2572        param_indent = indent.block_indent(context.config);
2573        result.push_str(&param_indent.to_string_with_newline(context.config));
2574        result.push_str(&param_str);
2575        result.push_str(&indent.to_string_with_newline(context.config));
2576        result.push(')');
2577    } else {
2578        result.push_str(&param_str);
2579        let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
2580        // Put the closing brace on the next line if it overflows the max width.
2581        // 1 = `)`
2582        let closing_paren_overflow_max_width =
2583            fd.inputs.is_empty() && used_width + 1 > context.config.max_width();
2584        // If the last line of params contains comment, we cannot put the closing paren
2585        // on the same line.
2586        params_last_line_contains_comment = param_str
2587            .lines()
2588            .last()
2589            .map_or(false, |last_line| last_line.contains("//"));
2590
2591        if context.config.style_edition() >= StyleEdition::Edition2024 {
2592            if closing_paren_overflow_max_width {
2593                result.push(')');
2594                result.push_str(&indent.to_string_with_newline(context.config));
2595                no_params_and_over_max_width = true;
2596            } else if params_last_line_contains_comment {
2597                result.push_str(&indent.to_string_with_newline(context.config));
2598                result.push(')');
2599                no_params_and_over_max_width = true;
2600            } else {
2601                result.push(')');
2602            }
2603        } else {
2604            if closing_paren_overflow_max_width || params_last_line_contains_comment {
2605                result.push_str(&indent.to_string_with_newline(context.config));
2606            }
2607            result.push(')');
2608        }
2609    }
2610
2611    // Return type.
2612    if let ast::FnRetTy::Ty(..) = fd.output {
2613        let ret_should_indent = match context.config.indent_style() {
2614            // If our params are block layout then we surely must have space.
2615            IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false,
2616            _ if params_last_line_contains_comment => false,
2617            _ if result.contains('\n') || multi_line_ret_str => true,
2618            _ => {
2619                // If the return type would push over the max width, then put the return type on
2620                // a new line. With the +1 for the signature length an additional space between
2621                // the closing parenthesis of the param and the arrow '->' is considered.
2622                let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
2623
2624                // If there is no where-clause, take into account the space after the return type
2625                // and the brace.
2626                if where_clause.predicates.is_empty() {
2627                    sig_length += 2;
2628                }
2629
2630                sig_length > context.config.max_width()
2631            }
2632        };
2633        let ret_shape = if ret_should_indent {
2634            if context.config.style_edition() <= StyleEdition::Edition2021
2635                || context.config.indent_style() == IndentStyle::Visual
2636            {
2637                let indent = if param_str.is_empty() {
2638                    // Aligning with nonexistent params looks silly.
2639                    force_new_line_for_brace = true;
2640                    indent + 4
2641                } else {
2642                    // FIXME: we might want to check that using the param indent
2643                    // doesn't blow our budget, and if it does, then fallback to
2644                    // the where-clause indent.
2645                    param_indent
2646                };
2647
2648                result.push_str(&indent.to_string_with_newline(context.config));
2649                Shape::indented(indent, context.config)
2650            } else {
2651                let mut ret_shape = Shape::indented(indent, context.config);
2652                if param_str.is_empty() {
2653                    // Aligning with nonexistent params looks silly.
2654                    force_new_line_for_brace = true;
2655                    ret_shape = if context.use_block_indent() {
2656                        ret_shape.offset_left_opt(4).unwrap_or(ret_shape)
2657                    } else {
2658                        ret_shape.indent = ret_shape.indent + 4;
2659                        ret_shape
2660                    };
2661                }
2662
2663                result.push_str(&ret_shape.indent.to_string_with_newline(context.config));
2664                ret_shape
2665            }
2666        } else {
2667            if context.config.style_edition() >= StyleEdition::Edition2024 {
2668                if !param_str.is_empty() || !no_params_and_over_max_width {
2669                    result.push(' ');
2670                }
2671            } else {
2672                result.push(' ');
2673            }
2674
2675            let ret_shape = Shape::indented(indent, context.config);
2676            ret_shape
2677                .offset_left_opt(last_line_width(&result))
2678                .unwrap_or(ret_shape)
2679        };
2680
2681        if multi_line_ret_str || ret_should_indent {
2682            // Now that we know the proper indent and width, we need to
2683            // re-layout the return type.
2684            let ret_str = fd.output.rewrite_result(context, ret_shape)?;
2685            result.push_str(&ret_str);
2686        } else {
2687            result.push_str(&ret_str);
2688        }
2689
2690        // Comment between return type and the end of the decl.
2691        let snippet_lo = fd.output.span().hi();
2692        if where_clause.predicates.is_empty() {
2693            let snippet_hi = span.hi();
2694            let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2695            // Try to preserve the layout of the original snippet.
2696            let original_starts_with_newline = snippet
2697                .find(|c| c != ' ')
2698                .map_or(false, |i| starts_with_newline(&snippet[i..]));
2699            let original_ends_with_newline = snippet
2700                .rfind(|c| c != ' ')
2701                .map_or(false, |i| snippet[i..].ends_with('\n'));
2702            let snippet = snippet.trim();
2703            if !snippet.is_empty() {
2704                result.push(if original_starts_with_newline {
2705                    '\n'
2706                } else {
2707                    ' '
2708                });
2709                result.push_str(snippet);
2710                if original_ends_with_newline {
2711                    force_new_line_for_brace = true;
2712                }
2713            }
2714        }
2715    }
2716
2717    let pos_before_where = match fd.output {
2718        ast::FnRetTy::Default(..) => params_span.hi(),
2719        ast::FnRetTy::Ty(ref ty) => ty.span.hi(),
2720    };
2721
2722    let is_params_multi_lined = param_str.contains('\n');
2723
2724    let space = if put_params_in_block && ret_str.is_empty() {
2725        WhereClauseSpace::Space
2726    } else {
2727        WhereClauseSpace::Newline
2728    };
2729    let mut option = WhereClauseOption::new(fn_brace_style == FnBraceStyle::None, space);
2730    if is_params_multi_lined {
2731        option.veto_single_line();
2732    }
2733    let where_clause_str = rewrite_where_clause(
2734        context,
2735        &where_clause,
2736        context.config.brace_style(),
2737        Shape::indented(indent, context.config),
2738        true,
2739        "{",
2740        Some(span.hi()),
2741        pos_before_where,
2742        option,
2743    )?;
2744    // If there are neither where-clause nor return type, we may be missing comments between
2745    // params and `{`.
2746    if where_clause_str.is_empty() {
2747        if let ast::FnRetTy::Default(ret_span) = fd.output {
2748            match recover_missing_comment_in_span(
2749                // from after the closing paren to right before block or semicolon
2750                mk_sp(ret_span.lo(), span.hi()),
2751                shape,
2752                context,
2753                last_line_width(&result),
2754            ) {
2755                Ok(ref missing_comment) if !missing_comment.is_empty() => {
2756                    result.push_str(missing_comment);
2757                    force_new_line_for_brace = true;
2758                }
2759                _ => (),
2760            }
2761        }
2762    }
2763
2764    result.push_str(&where_clause_str);
2765
2766    let ends_with_comment = last_line_contains_single_line_comment(&result);
2767    force_new_line_for_brace |= ends_with_comment;
2768    force_new_line_for_brace |=
2769        is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2770    Ok((result, ends_with_comment, force_new_line_for_brace))
2771}
2772
2773/// Kind of spaces to put before `where`.
2774#[derive(Copy, Clone)]
2775enum WhereClauseSpace {
2776    /// A single space.
2777    Space,
2778    /// A new line.
2779    Newline,
2780    /// Nothing.
2781    None,
2782}
2783
2784#[derive(Copy, Clone)]
2785struct WhereClauseOption {
2786    suppress_comma: bool, // Force no trailing comma
2787    snuggle: WhereClauseSpace,
2788    allow_single_line: bool, // Try single line where-clause instead of vertical layout
2789    veto_single_line: bool,  // Disallow a single-line where-clause.
2790}
2791
2792impl WhereClauseOption {
2793    fn new(suppress_comma: bool, snuggle: WhereClauseSpace) -> WhereClauseOption {
2794        WhereClauseOption {
2795            suppress_comma,
2796            snuggle,
2797            allow_single_line: false,
2798            veto_single_line: false,
2799        }
2800    }
2801
2802    fn snuggled(current: &str) -> WhereClauseOption {
2803        WhereClauseOption {
2804            suppress_comma: false,
2805            snuggle: if last_line_width(current) == 1 {
2806                WhereClauseSpace::Space
2807            } else {
2808                WhereClauseSpace::Newline
2809            },
2810            allow_single_line: false,
2811            veto_single_line: false,
2812        }
2813    }
2814
2815    fn suppress_comma(&mut self) {
2816        self.suppress_comma = true
2817    }
2818
2819    fn allow_single_line(&mut self) {
2820        self.allow_single_line = true
2821    }
2822
2823    fn snuggle(&mut self) {
2824        self.snuggle = WhereClauseSpace::Space
2825    }
2826
2827    fn veto_single_line(&mut self) {
2828        self.veto_single_line = true;
2829    }
2830}
2831
2832fn rewrite_params(
2833    context: &RewriteContext<'_>,
2834    params: &[ast::Param],
2835    one_line_budget: usize,
2836    multi_line_budget: usize,
2837    indent: Indent,
2838    param_indent: Indent,
2839    span: Span,
2840    variadic: bool,
2841) -> RewriteResult {
2842    if params.is_empty() {
2843        let comment = context
2844            .snippet(mk_sp(
2845                span.lo(),
2846                // to remove ')'
2847                span.hi() - BytePos(1),
2848            ))
2849            .trim();
2850        return Ok(comment.to_owned());
2851    }
2852    let param_items: Vec<_> = itemize_list(
2853        context.snippet_provider,
2854        params.iter(),
2855        ")",
2856        ",",
2857        |param| span_lo_for_param(param),
2858        |param| param.ty.span.hi(),
2859        |param| {
2860            param
2861                .rewrite_result(context, Shape::legacy(multi_line_budget, param_indent))
2862                .or_else(|_| Ok(context.snippet(param.span()).to_owned()))
2863        },
2864        span.lo(),
2865        span.hi(),
2866        false,
2867    )
2868    .collect();
2869
2870    let tactic = definitive_tactic(
2871        &param_items,
2872        context
2873            .config
2874            .fn_params_layout()
2875            .to_list_tactic(param_items.len()),
2876        Separator::Comma,
2877        one_line_budget,
2878    );
2879    let budget = match tactic {
2880        DefinitiveListTactic::Horizontal => one_line_budget,
2881        _ => multi_line_budget,
2882    };
2883    let indent = match context.config.indent_style() {
2884        IndentStyle::Block => indent.block_indent(context.config),
2885        IndentStyle::Visual => param_indent,
2886    };
2887    let trailing_separator = if variadic {
2888        SeparatorTactic::Never
2889    } else {
2890        match context.config.indent_style() {
2891            IndentStyle::Block => context.config.trailing_comma(),
2892            IndentStyle::Visual => SeparatorTactic::Never,
2893        }
2894    };
2895    let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
2896        .tactic(tactic)
2897        .trailing_separator(trailing_separator)
2898        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
2899        .preserve_newline(true);
2900    write_list(&param_items, &fmt)
2901}
2902
2903fn compute_budgets_for_params(
2904    context: &RewriteContext<'_>,
2905    result: &str,
2906    indent: Indent,
2907    ret_str_len: usize,
2908    fn_brace_style: FnBraceStyle,
2909    force_vertical_layout: bool,
2910) -> (usize, usize, Indent) {
2911    debug!(
2912        "compute_budgets_for_params {} {:?}, {}, {:?}",
2913        result.len(),
2914        indent,
2915        ret_str_len,
2916        fn_brace_style,
2917    );
2918    // Try keeping everything on the same line.
2919    if !result.contains('\n') && !force_vertical_layout {
2920        // 2 = `()`, 3 = `() `, space is before ret_string.
2921        let overhead = if ret_str_len == 0 { 2 } else { 3 };
2922        let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
2923        match fn_brace_style {
2924            FnBraceStyle::None => used_space += 1,     // 1 = `;`
2925            FnBraceStyle::SameLine => used_space += 2, // 2 = `{}`
2926            FnBraceStyle::NextLine => (),
2927        }
2928        let one_line_budget = context.budget(used_space);
2929
2930        if one_line_budget > 0 {
2931            // 4 = "() {".len()
2932            let (indent, multi_line_budget) = match context.config.indent_style() {
2933                IndentStyle::Block => {
2934                    let indent = indent.block_indent(context.config);
2935                    (indent, context.budget(indent.width() + 1))
2936                }
2937                IndentStyle::Visual => {
2938                    let indent = indent + result.len() + 1;
2939                    let multi_line_overhead = match fn_brace_style {
2940                        FnBraceStyle::SameLine => 4,
2941                        _ => 2,
2942                    } + indent.width();
2943                    (indent, context.budget(multi_line_overhead))
2944                }
2945            };
2946
2947            return (one_line_budget, multi_line_budget, indent);
2948        }
2949    }
2950
2951    // Didn't work. we must force vertical layout and put params on a newline.
2952    let new_indent = indent.block_indent(context.config);
2953    let used_space = match context.config.indent_style() {
2954        // 1 = `,`
2955        IndentStyle::Block => new_indent.width() + 1,
2956        // Account for `)` and possibly ` {`.
2957        IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
2958    };
2959    (0, context.budget(used_space), new_indent)
2960}
2961
2962fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {
2963    let predicate_count = where_clause.predicates.len();
2964
2965    if config.where_single_line() && predicate_count == 1 {
2966        return FnBraceStyle::SameLine;
2967    }
2968    let brace_style = config.brace_style();
2969
2970    let use_next_line = brace_style == BraceStyle::AlwaysNextLine
2971        || (brace_style == BraceStyle::SameLineWhere && predicate_count > 0);
2972    if use_next_line {
2973        FnBraceStyle::NextLine
2974    } else {
2975        FnBraceStyle::SameLine
2976    }
2977}
2978
2979fn rewrite_generics(
2980    context: &RewriteContext<'_>,
2981    ident: &str,
2982    generics: &ast::Generics,
2983    shape: Shape,
2984) -> RewriteResult {
2985    // FIXME: convert bounds to where-clauses where they get too big or if
2986    // there is a where-clause at all.
2987
2988    if generics.params.is_empty() {
2989        return Ok(ident.to_owned());
2990    }
2991
2992    let params = generics.params.iter();
2993    overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
2994}
2995
2996fn generics_shape_from_config(
2997    config: &Config,
2998    shape: Shape,
2999    offset: usize,
3000    span: Span,
3001) -> Result<Shape, ExceedsMaxWidthError> {
3002    match config.indent_style() {
3003        IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2, span),
3004        IndentStyle::Block => {
3005            // 1 = ","
3006            shape
3007                .block()
3008                .block_indent(config.tab_spaces())
3009                .with_max_width(config)
3010                .sub_width(1, span)
3011        }
3012    }
3013}
3014
3015fn rewrite_where_clause_rfc_style(
3016    context: &RewriteContext<'_>,
3017    predicates: &[ast::WherePredicate],
3018    where_span: Span,
3019    shape: Shape,
3020    terminator: &str,
3021    span_end: Option<BytePos>,
3022    span_end_before_where: BytePos,
3023    where_clause_option: WhereClauseOption,
3024) -> RewriteResult {
3025    let (where_keyword, allow_single_line) = rewrite_where_keyword(
3026        context,
3027        predicates,
3028        where_span,
3029        shape,
3030        span_end_before_where,
3031        where_clause_option,
3032    )?;
3033
3034    // 1 = `,`
3035    let clause_shape = shape
3036        .block()
3037        .with_max_width(context.config)
3038        .block_left(context.config.tab_spaces(), where_span)?
3039        .sub_width(1, where_span)?;
3040    let force_single_line = context.config.where_single_line()
3041        && predicates.len() == 1
3042        && !where_clause_option.veto_single_line;
3043
3044    let preds_str = rewrite_bounds_on_where_clause(
3045        context,
3046        predicates,
3047        clause_shape,
3048        terminator,
3049        span_end,
3050        where_clause_option,
3051        force_single_line,
3052    )?;
3053
3054    // 6 = `where `
3055    let clause_sep =
3056        if allow_single_line && !preds_str.contains('\n') && 6 + preds_str.len() <= shape.width
3057            || force_single_line
3058        {
3059            Cow::from(" ")
3060        } else {
3061            clause_shape.indent.to_string_with_newline(context.config)
3062        };
3063
3064    Ok(format!("{where_keyword}{clause_sep}{preds_str}"))
3065}
3066
3067/// Rewrite `where` and comment around it.
3068fn rewrite_where_keyword(
3069    context: &RewriteContext<'_>,
3070    predicates: &[ast::WherePredicate],
3071    where_span: Span,
3072    shape: Shape,
3073    span_end_before_where: BytePos,
3074    where_clause_option: WhereClauseOption,
3075) -> Result<(String, bool), RewriteError> {
3076    let block_shape = shape.block().with_max_width(context.config);
3077    // 1 = `,`
3078    let clause_shape = block_shape
3079        .block_left(context.config.tab_spaces(), where_span)?
3080        .sub_width(1, where_span)?;
3081
3082    let comment_separator = |comment: &str, shape: Shape| {
3083        if comment.is_empty() {
3084            Cow::from("")
3085        } else {
3086            shape.indent.to_string_with_newline(context.config)
3087        }
3088    };
3089
3090    let (span_before, span_after) =
3091        missing_span_before_after_where(span_end_before_where, predicates, where_span);
3092    let (comment_before, comment_after) =
3093        rewrite_comments_before_after_where(context, span_before, span_after, shape)?;
3094
3095    let starting_newline = match where_clause_option.snuggle {
3096        WhereClauseSpace::Space if comment_before.is_empty() => Cow::from(" "),
3097        WhereClauseSpace::None => Cow::from(""),
3098        _ => block_shape.indent.to_string_with_newline(context.config),
3099    };
3100
3101    let newline_before_where = comment_separator(&comment_before, shape);
3102    let newline_after_where = comment_separator(&comment_after, clause_shape);
3103    let result = format!(
3104        "{starting_newline}{comment_before}{newline_before_where}where\
3105{newline_after_where}{comment_after}"
3106    );
3107    let allow_single_line = where_clause_option.allow_single_line
3108        && comment_before.is_empty()
3109        && comment_after.is_empty();
3110
3111    Ok((result, allow_single_line))
3112}
3113
3114/// Rewrite bounds on a where clause.
3115fn rewrite_bounds_on_where_clause(
3116    context: &RewriteContext<'_>,
3117    predicates: &[ast::WherePredicate],
3118    shape: Shape,
3119    terminator: &str,
3120    span_end: Option<BytePos>,
3121    where_clause_option: WhereClauseOption,
3122    force_single_line: bool,
3123) -> RewriteResult {
3124    let span_start = predicates[0].span().lo();
3125    // If we don't have the start of the next span, then use the end of the
3126    // predicates, but that means we miss comments.
3127    let len = predicates.len();
3128    let end_of_preds = predicates[len - 1].span().hi();
3129    let span_end = span_end.unwrap_or(end_of_preds);
3130    let items = itemize_list(
3131        context.snippet_provider,
3132        predicates.iter(),
3133        terminator,
3134        ",",
3135        |pred| pred.span().lo(),
3136        |pred| pred.span().hi(),
3137        |pred| pred.rewrite_result(context, shape),
3138        span_start,
3139        span_end,
3140        false,
3141    );
3142    let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
3143        SeparatorTactic::Never
3144    } else {
3145        context.config.trailing_comma()
3146    };
3147
3148    // shape should be vertical only and only if we have `force_single_line` option enabled
3149    // and the number of items of the where-clause is equal to 1
3150    let shape_tactic = if force_single_line {
3151        DefinitiveListTactic::Horizontal
3152    } else {
3153        DefinitiveListTactic::Vertical
3154    };
3155
3156    let preserve_newline = context.config.style_edition() <= StyleEdition::Edition2021;
3157
3158    let fmt = ListFormatting::new(shape, context.config)
3159        .tactic(shape_tactic)
3160        .trailing_separator(comma_tactic)
3161        .preserve_newline(preserve_newline);
3162    write_list(&items.collect::<Vec<_>>(), &fmt)
3163}
3164
3165fn rewrite_where_clause(
3166    context: &RewriteContext<'_>,
3167    where_clause: &ast::WhereClause,
3168    brace_style: BraceStyle,
3169    shape: Shape,
3170    on_new_line: bool,
3171    terminator: &str,
3172    span_end: Option<BytePos>,
3173    span_end_before_where: BytePos,
3174    where_clause_option: WhereClauseOption,
3175) -> RewriteResult {
3176    let ast::WhereClause {
3177        ref predicates,
3178        span: where_span,
3179        has_where_token: _,
3180    } = *where_clause;
3181
3182    if predicates.is_empty() {
3183        return Ok(String::new());
3184    }
3185
3186    if context.config.indent_style() == IndentStyle::Block {
3187        return rewrite_where_clause_rfc_style(
3188            context,
3189            predicates,
3190            where_span,
3191            shape,
3192            terminator,
3193            span_end,
3194            span_end_before_where,
3195            where_clause_option,
3196        );
3197    }
3198
3199    let extra_indent = Indent::new(context.config.tab_spaces(), 0);
3200
3201    let offset = match context.config.indent_style() {
3202        IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
3203        // 6 = "where ".len()
3204        IndentStyle::Visual => shape.indent + extra_indent + 6,
3205    };
3206    // FIXME: if indent_style != Visual, then the budgets below might
3207    // be out by a char or two.
3208
3209    let budget = context.config.max_width() - offset.width();
3210    let span_start = predicates[0].span().lo();
3211    // If we don't have the start of the next span, then use the end of the
3212    // predicates, but that means we miss comments.
3213    let len = predicates.len();
3214    let end_of_preds = predicates[len - 1].span().hi();
3215    let span_end = span_end.unwrap_or(end_of_preds);
3216    let items = itemize_list(
3217        context.snippet_provider,
3218        predicates.iter(),
3219        terminator,
3220        ",",
3221        |pred| pred.span().lo(),
3222        |pred| pred.span().hi(),
3223        |pred| pred.rewrite_result(context, Shape::legacy(budget, offset)),
3224        span_start,
3225        span_end,
3226        false,
3227    );
3228    let item_vec = items.collect::<Vec<_>>();
3229    // FIXME: we don't need to collect here
3230    let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget);
3231
3232    let mut comma_tactic = context.config.trailing_comma();
3233    // Kind of a hack because we don't usually have trailing commas in where-clauses.
3234    if comma_tactic == SeparatorTactic::Vertical || where_clause_option.suppress_comma {
3235        comma_tactic = SeparatorTactic::Never;
3236    }
3237
3238    let fmt = ListFormatting::new(Shape::legacy(budget, offset), context.config)
3239        .tactic(tactic)
3240        .trailing_separator(comma_tactic)
3241        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
3242        .preserve_newline(true);
3243    let preds_str = write_list(&item_vec, &fmt)?;
3244
3245    let end_length = if terminator == "{" {
3246        // If the brace is on the next line we don't need to count it otherwise it needs two
3247        // characters " {"
3248        match brace_style {
3249            BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0,
3250            BraceStyle::PreferSameLine => 2,
3251        }
3252    } else if terminator == "=" {
3253        2
3254    } else {
3255        terminator.len()
3256    };
3257    if on_new_line
3258        || preds_str.contains('\n')
3259        || shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
3260    {
3261        Ok(format!(
3262            "\n{}where {}",
3263            (shape.indent + extra_indent).to_string(context.config),
3264            preds_str
3265        ))
3266    } else {
3267        Ok(format!(" where {preds_str}"))
3268    }
3269}
3270
3271fn missing_span_before_after_where(
3272    before_item_span_end: BytePos,
3273    predicates: &[ast::WherePredicate],
3274    where_span: Span,
3275) -> (Span, Span) {
3276    let missing_span_before = mk_sp(before_item_span_end, where_span.lo());
3277    // 5 = `where`
3278    let pos_after_where = where_span.lo() + BytePos(5);
3279    let missing_span_after = mk_sp(pos_after_where, predicates[0].span().lo());
3280    (missing_span_before, missing_span_after)
3281}
3282
3283fn rewrite_comments_before_after_where(
3284    context: &RewriteContext<'_>,
3285    span_before_where: Span,
3286    span_after_where: Span,
3287    shape: Shape,
3288) -> Result<(String, String), RewriteError> {
3289    let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
3290    let after_comment = rewrite_missing_comment(
3291        span_after_where,
3292        shape.block_indent(context.config.tab_spaces()),
3293        context,
3294    )?;
3295    Ok((before_comment, after_comment))
3296}
3297
3298fn format_header(
3299    context: &RewriteContext<'_>,
3300    item_name: &str,
3301    ident: symbol::Ident,
3302    vis: &ast::Visibility,
3303    offset: Indent,
3304) -> String {
3305    let mut result = String::with_capacity(128);
3306    let shape = Shape::indented(offset, context.config);
3307
3308    result.push_str(format_visibility(context, vis).trim());
3309
3310    // Check for a missing comment between the visibility and the item name.
3311    let after_vis = vis.span.hi();
3312    if let Some(before_item_name) = context
3313        .snippet_provider
3314        .opt_span_before(mk_sp(vis.span.lo(), ident.span.hi()), item_name.trim())
3315    {
3316        let missing_span = mk_sp(after_vis, before_item_name);
3317        if let Ok(result_with_comment) = combine_strs_with_missing_comments(
3318            context,
3319            &result,
3320            item_name,
3321            missing_span,
3322            shape,
3323            /* allow_extend */ true,
3324        ) {
3325            result = result_with_comment;
3326        }
3327    }
3328
3329    result.push_str(rewrite_ident(context, ident));
3330
3331    result
3332}
3333
3334#[derive(PartialEq, Eq, Clone, Copy)]
3335enum BracePos {
3336    None,
3337    Auto,
3338    ForceSameLine,
3339}
3340
3341fn format_generics(
3342    context: &RewriteContext<'_>,
3343    generics: &ast::Generics,
3344    brace_style: BraceStyle,
3345    brace_pos: BracePos,
3346    offset: Indent,
3347    span: Span,
3348    used_width: usize,
3349) -> Option<String> {
3350    let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
3351    let mut result = rewrite_generics(context, "", generics, shape).ok()?;
3352
3353    // If the generics are not parameterized then generics.span.hi() == 0,
3354    // so we use span.lo(), which is the position after `struct Foo`.
3355    let span_end_before_where = if !generics.params.is_empty() {
3356        generics.span.hi()
3357    } else {
3358        span.lo()
3359    };
3360    let (same_line_brace, missed_comments) = if !generics.where_clause.predicates.is_empty() {
3361        let budget = context.budget(last_line_used_width(&result, offset.width()));
3362        let mut option = WhereClauseOption::snuggled(&result);
3363        if brace_pos == BracePos::None {
3364            option.suppress_comma = true;
3365        }
3366        let where_clause_str = rewrite_where_clause(
3367            context,
3368            &generics.where_clause,
3369            brace_style,
3370            Shape::legacy(budget, offset.block_only()),
3371            true,
3372            "{",
3373            Some(span.hi()),
3374            span_end_before_where,
3375            option,
3376        )
3377        .ok()?;
3378        result.push_str(&where_clause_str);
3379        (
3380            brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,
3381            // missed comments are taken care of in #rewrite_where_clause
3382            None,
3383        )
3384    } else {
3385        (
3386            brace_pos == BracePos::ForceSameLine
3387                || (result.contains('\n') && brace_style == BraceStyle::PreferSameLine
3388                    || brace_style != BraceStyle::AlwaysNextLine)
3389                || trimmed_last_line_width(&result) == 1,
3390            rewrite_missing_comment(
3391                mk_sp(
3392                    span_end_before_where,
3393                    if brace_pos == BracePos::None {
3394                        span.hi()
3395                    } else {
3396                        context.snippet_provider.span_before_last(span, "{")
3397                    },
3398                ),
3399                shape,
3400                context,
3401            )
3402            .ok(),
3403        )
3404    };
3405    // add missing comments
3406    let missed_line_comments = missed_comments
3407        .filter(|missed_comments| !missed_comments.is_empty())
3408        .map_or(false, |missed_comments| {
3409            let is_block = is_last_comment_block(&missed_comments);
3410            let sep = if is_block { " " } else { "\n" };
3411            result.push_str(sep);
3412            result.push_str(&missed_comments);
3413            !is_block
3414        });
3415    if brace_pos == BracePos::None {
3416        return Some(result);
3417    }
3418    let total_used_width = last_line_used_width(&result, used_width);
3419    let remaining_budget = context.budget(total_used_width);
3420    // If the same line brace if forced, it indicates that we are rewriting an item with empty body,
3421    // and hence we take the closer into account as well for one line budget.
3422    // We assume that the closer has the same length as the opener.
3423    let overhead = if brace_pos == BracePos::ForceSameLine {
3424        // 3 = ` {}`
3425        3
3426    } else {
3427        // 2 = ` {`
3428        2
3429    };
3430    let forbid_same_line_brace = missed_line_comments || overhead > remaining_budget;
3431    if !forbid_same_line_brace && same_line_brace {
3432        result.push(' ');
3433    } else {
3434        result.push('\n');
3435        result.push_str(&offset.block_only().to_string(context.config));
3436    }
3437    result.push('{');
3438
3439    Some(result)
3440}
3441
3442impl Rewrite for ast::ForeignItem {
3443    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
3444        self.rewrite_result(context, shape).ok()
3445    }
3446
3447    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
3448        let attrs_str = self.attrs.rewrite_result(context, shape)?;
3449        // Drop semicolon or it will be interpreted as comment.
3450        // FIXME: this may be a faulty span from libsyntax.
3451        let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
3452
3453        let item_str = match self.kind {
3454            ast::ForeignItemKind::Fn(ref fn_kind) => {
3455                let ast::Fn {
3456                    defaultness,
3457                    ref sig,
3458                    ident,
3459                    ref generics,
3460                    ref body,
3461                    ..
3462                } = **fn_kind;
3463                if body.is_some() {
3464                    let mut visitor = FmtVisitor::from_context(context);
3465                    visitor.block_indent = shape.indent;
3466                    visitor.last_pos = self.span.lo();
3467                    let inner_attrs = inner_attributes(&self.attrs);
3468                    let fn_ctxt = visit::FnCtxt::Foreign;
3469                    visitor.visit_fn(
3470                        ident,
3471                        visit::FnKind::Fn(fn_ctxt, &self.vis, fn_kind),
3472                        &sig.decl,
3473                        self.span,
3474                        defaultness,
3475                        Some(&inner_attrs),
3476                    );
3477                    Ok(visitor.buffer.to_owned())
3478                } else {
3479                    rewrite_fn_base(
3480                        context,
3481                        shape.indent,
3482                        ident,
3483                        &FnSig::from_method_sig(sig, generics, &self.vis, defaultness),
3484                        span,
3485                        FnBraceStyle::None,
3486                    )
3487                    .map(|(s, _, _)| format!("{};", s))
3488                }
3489            }
3490            ast::ForeignItemKind::Static(ref static_foreign_item) => {
3491                // FIXME(#21): we're dropping potential comments in between the
3492                // function kw here.
3493                let vis = format_visibility(context, &self.vis);
3494                let safety = format_safety(static_foreign_item.safety);
3495                let mut_str = format_mutability(static_foreign_item.mutability);
3496                let prefix = format!(
3497                    "{}{}static {}{}:",
3498                    vis,
3499                    safety,
3500                    mut_str,
3501                    rewrite_ident(context, static_foreign_item.ident)
3502                );
3503                // 1 = ;
3504                rewrite_assign_rhs(
3505                    context,
3506                    prefix,
3507                    &static_foreign_item.ty,
3508                    &RhsAssignKind::Ty,
3509                    shape.sub_width(1, static_foreign_item.ty.span)?,
3510                )
3511                .map(|s| s + ";")
3512            }
3513            ast::ForeignItemKind::TyAlias(ref ty_alias) => {
3514                let kind = ItemVisitorKind::ForeignItem;
3515                rewrite_type_alias(ty_alias, &self.vis, context, shape.indent, kind, self.span)
3516            }
3517            ast::ForeignItemKind::MacCall(ref mac) => {
3518                rewrite_macro(mac, context, shape, MacroPosition::Item)
3519            }
3520        }?;
3521
3522        let missing_span = if self.attrs.is_empty() {
3523            mk_sp(self.span.lo(), self.span.lo())
3524        } else {
3525            mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
3526        };
3527        combine_strs_with_missing_comments(
3528            context,
3529            &attrs_str,
3530            &item_str,
3531            missing_span,
3532            shape,
3533            false,
3534        )
3535    }
3536}
3537
3538/// Rewrite the attributes of an item.
3539fn rewrite_attrs(
3540    context: &RewriteContext<'_>,
3541    item: &ast::Item,
3542    item_str: &str,
3543    shape: Shape,
3544) -> RewriteResult {
3545    let attrs = filter_inline_attrs(&item.attrs, item.span());
3546    let attrs_str = attrs.rewrite_result(context, shape)?;
3547
3548    let missed_span = if attrs.is_empty() {
3549        mk_sp(item.span.lo(), item.span.lo())
3550    } else {
3551        mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
3552    };
3553
3554    let allow_extend = if attrs.len() == 1 {
3555        let line_len = attrs_str.len() + 1 + item_str.len();
3556        !attrs.first().unwrap().is_doc_comment()
3557            && context.config.inline_attribute_width() >= line_len
3558    } else {
3559        false
3560    };
3561
3562    combine_strs_with_missing_comments(
3563        context,
3564        &attrs_str,
3565        item_str,
3566        missed_span,
3567        shape,
3568        allow_extend,
3569    )
3570}
3571
3572/// Rewrite an inline mod.
3573/// The given shape is used to format the mod's attributes.
3574pub(crate) fn rewrite_mod(
3575    context: &RewriteContext<'_>,
3576    item: &ast::Item,
3577    ident: Ident,
3578    attrs_shape: Shape,
3579) -> RewriteResult {
3580    let mut result = String::with_capacity(32);
3581    result.push_str(&*format_visibility(context, &item.vis));
3582    result.push_str("mod ");
3583    result.push_str(rewrite_ident(context, ident));
3584    result.push(';');
3585    rewrite_attrs(context, item, &result, attrs_shape)
3586}
3587
3588/// Rewrite `extern crate foo;`.
3589/// The given shape is used to format the extern crate's attributes.
3590pub(crate) fn rewrite_extern_crate(
3591    context: &RewriteContext<'_>,
3592    item: &ast::Item,
3593    attrs_shape: Shape,
3594) -> RewriteResult {
3595    assert!(is_extern_crate(item));
3596    let new_str = context.snippet(item.span);
3597    let item_str = if contains_comment(new_str) {
3598        new_str.to_owned()
3599    } else {
3600        let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
3601        String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
3602    };
3603    rewrite_attrs(context, item, &item_str, attrs_shape)
3604}
3605
3606/// Returns `true` for `mod foo;`, false for `mod foo { .. }`.
3607pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
3608    !matches!(
3609        item.kind,
3610        ast::ItemKind::Mod(_, _, ast::ModKind::Loaded(_, ast::Inline::Yes, _))
3611    )
3612}
3613
3614pub(crate) fn is_use_item(item: &ast::Item) -> bool {
3615    matches!(item.kind, ast::ItemKind::Use(_))
3616}
3617
3618pub(crate) fn is_extern_crate(item: &ast::Item) -> bool {
3619    matches!(item.kind, ast::ItemKind::ExternCrate(..))
3620}