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_safety(of_trait.safety));
964    } else {
965        result.push_str(format_constness(*constness));
966    }
967
968    let shape = if context.config.style_edition() >= StyleEdition::Edition2024 {
969        Shape::indented(offset + last_line_width(&result), context.config)
970    } else {
971        generics_shape_from_config(
972            context.config,
973            Shape::indented(offset + last_line_width(&result), context.config),
974            0,
975            item.span,
976        )?
977    };
978    let generics_str = rewrite_generics(context, "impl", generics, shape)?;
979    result.push_str(&generics_str);
980
981    let trait_ref_overhead;
982    if let Some(of_trait) = of_trait.as_deref() {
983        result.push_str(format_constness_right(*constness));
984        let polarity_str = match of_trait.polarity {
985            ast::ImplPolarity::Negative(_) => "!",
986            ast::ImplPolarity::Positive => "",
987        };
988        let result_len = last_line_width(&result);
989        result.push_str(&rewrite_trait_ref(
990            context,
991            &of_trait.trait_ref,
992            offset,
993            polarity_str,
994            result_len,
995        )?);
996        trait_ref_overhead = " for".len();
997    } else {
998        trait_ref_overhead = 0;
999    }
1000
1001    // 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        constness,
1159        is_auto,
1160        safety,
1161        ident,
1162        ref generics,
1163        ref bounds,
1164        ref items,
1165    } = *trait_;
1166
1167    let mut result = String::with_capacity(128);
1168    let header = format!(
1169        "{}{}{}{}trait ",
1170        format_visibility(context, &item.vis),
1171        format_constness(constness),
1172        format_safety(safety),
1173        format_auto(is_auto),
1174    );
1175    result.push_str(&header);
1176
1177    let body_lo = context.snippet_provider.span_after(item.span, "{");
1178
1179    let shape = Shape::indented(offset, context.config).offset_left(result.len(), item.span)?;
1180    let generics_str = rewrite_generics(context, rewrite_ident(context, ident), generics, shape)?;
1181    result.push_str(&generics_str);
1182
1183    // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1184    if !bounds.is_empty() {
1185        // Retrieve *unnormalized* ident (See #6069)
1186        let source_ident = context.snippet(ident.span);
1187        let ident_hi = context.snippet_provider.span_after(item.span, source_ident);
1188        let bound_hi = bounds.last().unwrap().span().hi();
1189        let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
1190        if contains_comment(snippet) {
1191            return Err(RewriteError::Unknown);
1192        }
1193
1194        result = rewrite_assign_rhs_with(
1195            context,
1196            result + ":",
1197            bounds,
1198            shape,
1199            &RhsAssignKind::Bounds,
1200            RhsTactics::ForceNextLineWithoutIndent,
1201        )?;
1202    }
1203
1204    // Rewrite where-clause.
1205    if !generics.where_clause.predicates.is_empty() {
1206        let where_on_new_line = context.config.indent_style() != IndentStyle::Block;
1207
1208        let where_budget = context.budget(last_line_width(&result));
1209        let pos_before_where = if bounds.is_empty() {
1210            generics.where_clause.span.lo()
1211        } else {
1212            bounds[bounds.len() - 1].span().hi()
1213        };
1214        let option = WhereClauseOption::snuggled(&generics_str);
1215        let where_clause_str = rewrite_where_clause(
1216            context,
1217            &generics.where_clause,
1218            context.config.brace_style(),
1219            Shape::legacy(where_budget, offset.block_only()),
1220            where_on_new_line,
1221            "{",
1222            None,
1223            pos_before_where,
1224            option,
1225        )?;
1226
1227        // If the where-clause cannot fit on the same line,
1228        // put the where-clause on a new line
1229        if !where_clause_str.contains('\n')
1230            && last_line_width(&result) + where_clause_str.len() + offset.width()
1231                > context.config.comment_width()
1232        {
1233            let width = offset.block_indent + context.config.tab_spaces() - 1;
1234            let where_indent = Indent::new(0, width);
1235            result.push_str(&where_indent.to_string_with_newline(context.config));
1236        }
1237        result.push_str(&where_clause_str);
1238    } else {
1239        let item_snippet = context.snippet(item.span);
1240        if let Some(lo) = item_snippet.find('/') {
1241            // 1 = `{`
1242            let comment_hi = if generics.params.len() > 0 {
1243                generics.span.lo() - BytePos(1)
1244            } else {
1245                body_lo - BytePos(1)
1246            };
1247            let comment_lo = item.span.lo() + BytePos(lo as u32);
1248            if comment_lo < comment_hi {
1249                match recover_missing_comment_in_span(
1250                    mk_sp(comment_lo, comment_hi),
1251                    Shape::indented(offset, context.config),
1252                    context,
1253                    last_line_width(&result),
1254                ) {
1255                    Ok(ref missing_comment) if !missing_comment.is_empty() => {
1256                        result.push_str(missing_comment);
1257                    }
1258                    _ => (),
1259                }
1260            }
1261        }
1262    }
1263
1264    let block_span = mk_sp(generics.where_clause.span.hi(), item.span.hi());
1265    let snippet = context.snippet(block_span);
1266    let open_pos = snippet.find_uncommented("{").unknown_error()? + 1;
1267
1268    match context.config.brace_style() {
1269        _ if last_line_contains_single_line_comment(&result)
1270            || last_line_width(&result) + 2 > context.budget(offset.width()) =>
1271        {
1272            result.push_str(&offset.to_string_with_newline(context.config));
1273        }
1274        _ if context.config.empty_item_single_line()
1275            && items.is_empty()
1276            && !result.contains('\n')
1277            && !contains_comment(&snippet[open_pos..]) =>
1278        {
1279            result.push_str(" {}");
1280            return Ok(result);
1281        }
1282        BraceStyle::AlwaysNextLine => {
1283            result.push_str(&offset.to_string_with_newline(context.config));
1284        }
1285        BraceStyle::PreferSameLine => result.push(' '),
1286        BraceStyle::SameLineWhere => {
1287            if result.contains('\n')
1288                || (!generics.where_clause.predicates.is_empty() && !items.is_empty())
1289            {
1290                result.push_str(&offset.to_string_with_newline(context.config));
1291            } else {
1292                result.push(' ');
1293            }
1294        }
1295    }
1296    result.push('{');
1297
1298    let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
1299
1300    if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
1301        let mut visitor = FmtVisitor::from_context(context);
1302        visitor.block_indent = offset.block_only().block_indent(context.config);
1303        visitor.last_pos = block_span.lo() + BytePos(open_pos as u32);
1304
1305        for item in items {
1306            visitor.visit_trait_item(item);
1307        }
1308
1309        visitor.format_missing(item.span.hi() - BytePos(1));
1310
1311        let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
1312
1313        result.push_str(&inner_indent_str);
1314        result.push_str(visitor.buffer.trim());
1315        result.push_str(&outer_indent_str);
1316    } else if result.contains('\n') {
1317        result.push_str(&outer_indent_str);
1318    }
1319
1320    result.push('}');
1321    Ok(result)
1322}
1323
1324pub(crate) struct TraitAliasBounds<'a> {
1325    generic_bounds: &'a ast::GenericBounds,
1326    generics: &'a ast::Generics,
1327}
1328
1329impl<'a> Rewrite for TraitAliasBounds<'a> {
1330    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1331        self.rewrite_result(context, shape).ok()
1332    }
1333
1334    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1335        let generic_bounds_str = self.generic_bounds.rewrite_result(context, shape)?;
1336
1337        let mut option = WhereClauseOption::new(true, WhereClauseSpace::None);
1338        option.allow_single_line();
1339
1340        let where_str = rewrite_where_clause(
1341            context,
1342            &self.generics.where_clause,
1343            context.config.brace_style(),
1344            shape,
1345            false,
1346            ";",
1347            None,
1348            self.generics.where_clause.span.lo(),
1349            option,
1350        )?;
1351
1352        let fits_single_line = !generic_bounds_str.contains('\n')
1353            && !where_str.contains('\n')
1354            && generic_bounds_str.len() + where_str.len() < shape.width;
1355        let space = if generic_bounds_str.is_empty() || where_str.is_empty() {
1356            Cow::from("")
1357        } else if fits_single_line {
1358            Cow::from(" ")
1359        } else {
1360            shape.indent.to_string_with_newline(context.config)
1361        };
1362
1363        Ok(format!("{generic_bounds_str}{space}{where_str}"))
1364    }
1365}
1366
1367pub(crate) fn format_trait_alias(
1368    context: &RewriteContext<'_>,
1369    ta: &ast::TraitAlias,
1370    vis: &ast::Visibility,
1371    span: Span,
1372    shape: Shape,
1373) -> RewriteResult {
1374    let alias = rewrite_ident(context, ta.ident);
1375    // 6 = "trait ", 2 = " ="
1376    let g_shape = shape.offset_left(6, span)?.sub_width(2, span)?;
1377    let generics_str = rewrite_generics(context, alias, &ta.generics, g_shape)?;
1378    let vis_str = format_visibility(context, vis);
1379    let constness = format_constness(ta.constness);
1380    let lhs = format!("{vis_str}{constness}trait {generics_str} =");
1381    // 1 = ";"
1382    let trait_alias_bounds = TraitAliasBounds {
1383        generic_bounds: &ta.bounds,
1384        generics: &ta.generics,
1385    };
1386    let result = rewrite_assign_rhs(
1387        context,
1388        lhs,
1389        &trait_alias_bounds,
1390        &RhsAssignKind::Bounds,
1391        shape.sub_width(1, ta.generics.span)?,
1392    )?;
1393    Ok(result + ";")
1394}
1395
1396fn format_unit_struct(
1397    context: &RewriteContext<'_>,
1398    p: &StructParts<'_>,
1399    offset: Indent,
1400) -> Option<String> {
1401    let header_str = format_header(context, p.prefix, p.ident, p.vis, offset);
1402    let generics_str = if let Some(generics) = p.generics {
1403        let hi = context.snippet_provider.span_before_last(p.span, ";");
1404        format_generics(
1405            context,
1406            generics,
1407            context.config.brace_style(),
1408            BracePos::None,
1409            offset,
1410            // make a span that starts right after `struct Foo`
1411            mk_sp(p.ident.span.hi(), hi),
1412            last_line_width(&header_str),
1413        )?
1414    } else {
1415        String::new()
1416    };
1417    Some(format!("{header_str}{generics_str};"))
1418}
1419
1420pub(crate) fn format_struct_struct(
1421    context: &RewriteContext<'_>,
1422    struct_parts: &StructParts<'_>,
1423    fields: &[ast::FieldDef],
1424    offset: Indent,
1425    one_line_width: Option<usize>,
1426) -> Option<String> {
1427    let mut result = String::with_capacity(1024);
1428    let span = struct_parts.span;
1429
1430    let header_str = struct_parts.format_header(context, offset);
1431    result.push_str(&header_str);
1432
1433    let header_hi = struct_parts.ident.span.hi();
1434    let body_lo = if let Some(generics) = struct_parts.generics {
1435        // Adjust the span to start at the end of the generic arguments before searching for the '{'
1436        let span = span.with_lo(generics.where_clause.span.hi());
1437        context.snippet_provider.span_after(span, "{")
1438    } else {
1439        context.snippet_provider.span_after(span, "{")
1440    };
1441
1442    let generics_str = match struct_parts.generics {
1443        Some(g) => format_generics(
1444            context,
1445            g,
1446            context.config.brace_style(),
1447            if fields.is_empty() {
1448                BracePos::ForceSameLine
1449            } else {
1450                BracePos::Auto
1451            },
1452            offset,
1453            // make a span that starts right after `struct Foo`
1454            mk_sp(header_hi, body_lo),
1455            last_line_width(&result),
1456        )?,
1457        None => {
1458            // 3 = ` {}`, 2 = ` {`.
1459            let overhead = if fields.is_empty() { 3 } else { 2 };
1460            if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty())
1461                || context.config.max_width() < overhead + result.len()
1462            {
1463                format!("\n{}{{", offset.block_only().to_string(context.config))
1464            } else {
1465                " {".to_owned()
1466            }
1467        }
1468    };
1469    // 1 = `}`
1470    let overhead = if fields.is_empty() { 1 } else { 0 };
1471    let total_width = result.len() + generics_str.len() + overhead;
1472    if !generics_str.is_empty()
1473        && !generics_str.contains('\n')
1474        && total_width > context.config.max_width()
1475    {
1476        result.push('\n');
1477        result.push_str(&offset.to_string(context.config));
1478        result.push_str(generics_str.trim_start());
1479    } else {
1480        result.push_str(&generics_str);
1481    }
1482
1483    if fields.is_empty() {
1484        let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
1485        format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
1486        return Some(result);
1487    }
1488
1489    // 3 = ` ` and ` }`
1490    let one_line_budget = context.budget(result.len() + 3 + offset.width());
1491    let one_line_budget =
1492        one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
1493
1494    let items_str = rewrite_with_alignment(
1495        fields,
1496        context,
1497        Shape::indented(offset.block_indent(context.config), context.config).sub_width_opt(1)?,
1498        mk_sp(body_lo, span.hi()),
1499        one_line_budget,
1500    )?;
1501
1502    if !items_str.contains('\n')
1503        && !result.contains('\n')
1504        && items_str.len() <= one_line_budget
1505        && !last_line_contains_single_line_comment(&items_str)
1506    {
1507        Some(format!("{result} {items_str} }}"))
1508    } else {
1509        Some(format!(
1510            "{}\n{}{}\n{}}}",
1511            result,
1512            offset
1513                .block_indent(context.config)
1514                .to_string(context.config),
1515            items_str,
1516            offset.to_string(context.config)
1517        ))
1518    }
1519}
1520
1521fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
1522    match vis.kind {
1523        ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
1524        _ => default_span.lo(),
1525    }
1526}
1527
1528// Format tuple or struct without any fields. We need to make sure that the comments
1529// inside the delimiters are preserved.
1530fn format_empty_struct_or_tuple(
1531    context: &RewriteContext<'_>,
1532    span: Span,
1533    offset: Indent,
1534    result: &mut String,
1535    opener: &str,
1536    closer: &str,
1537) {
1538    // 3 = " {}" or "();"
1539    let used_width = last_line_used_width(result, offset.width()) + 3;
1540    if used_width > context.config.max_width() {
1541        result.push_str(&offset.to_string_with_newline(context.config))
1542    }
1543    result.push_str(opener);
1544
1545    // indented shape for proper indenting of multi-line comments
1546    let shape = Shape::indented(offset.block_indent(context.config), context.config);
1547    match rewrite_missing_comment(span, shape, context) {
1548        Ok(ref s) if s.is_empty() => (),
1549        Ok(ref s) => {
1550            let is_multi_line = !is_single_line(s);
1551            if is_multi_line || first_line_contains_single_line_comment(s) {
1552                let nested_indent_str = offset
1553                    .block_indent(context.config)
1554                    .to_string_with_newline(context.config);
1555                result.push_str(&nested_indent_str);
1556            }
1557            result.push_str(s);
1558            if is_multi_line || last_line_contains_single_line_comment(s) {
1559                result.push_str(&offset.to_string_with_newline(context.config));
1560            }
1561        }
1562        Err(_) => result.push_str(context.snippet(span)),
1563    }
1564    result.push_str(closer);
1565}
1566
1567fn format_tuple_struct(
1568    context: &RewriteContext<'_>,
1569    struct_parts: &StructParts<'_>,
1570    fields: &[ast::FieldDef],
1571    offset: Indent,
1572) -> Option<String> {
1573    let mut result = String::with_capacity(1024);
1574    let span = struct_parts.span;
1575
1576    let header_str = struct_parts.format_header(context, offset);
1577    result.push_str(&header_str);
1578
1579    let body_lo = if fields.is_empty() {
1580        let lo = get_bytepos_after_visibility(struct_parts.vis, span);
1581        context
1582            .snippet_provider
1583            .span_after(mk_sp(lo, span.hi()), "(")
1584    } else {
1585        fields[0].span.lo()
1586    };
1587    let body_hi = if fields.is_empty() {
1588        context
1589            .snippet_provider
1590            .span_after(mk_sp(body_lo, span.hi()), ")")
1591    } else {
1592        // This is a dirty hack to work around a missing `)` from the span of the last field.
1593        let last_arg_span = fields[fields.len() - 1].span;
1594        context
1595            .snippet_provider
1596            .opt_span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
1597            .unwrap_or_else(|| last_arg_span.hi())
1598    };
1599
1600    let where_clause_str = match struct_parts.generics {
1601        Some(generics) => {
1602            let budget = context.budget(last_line_width(&header_str));
1603            let shape = Shape::legacy(budget, offset);
1604            let generics_str = rewrite_generics(context, "", generics, shape).ok()?;
1605            result.push_str(&generics_str);
1606
1607            let where_budget = context.budget(last_line_width(&result));
1608            let option = WhereClauseOption::new(true, WhereClauseSpace::Newline);
1609            rewrite_where_clause(
1610                context,
1611                &generics.where_clause,
1612                context.config.brace_style(),
1613                Shape::legacy(where_budget, offset.block_only()),
1614                false,
1615                ";",
1616                None,
1617                body_hi,
1618                option,
1619            )
1620            .ok()?
1621        }
1622        None => "".to_owned(),
1623    };
1624
1625    if fields.is_empty() {
1626        let body_hi = context
1627            .snippet_provider
1628            .span_before(mk_sp(body_lo, span.hi()), ")");
1629        let inner_span = mk_sp(body_lo, body_hi);
1630        format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
1631    } else {
1632        let lo = if let Some(generics) = struct_parts.generics {
1633            generics.span.hi()
1634        } else {
1635            struct_parts.ident.span.hi()
1636        };
1637        let shape = Shape::indented(offset, context.config).sub_width_opt(1)?;
1638        result = overflow::rewrite_with_parens(
1639            context,
1640            &result,
1641            fields.iter(),
1642            shape,
1643            mk_sp(lo, span.hi()),
1644            context.config.fn_call_width(),
1645            None,
1646        )
1647        .ok()?;
1648    }
1649
1650    if !where_clause_str.is_empty()
1651        && !where_clause_str.contains('\n')
1652        && (result.contains('\n')
1653            || offset.block_indent + result.len() + where_clause_str.len() + 1
1654                > context.config.max_width())
1655    {
1656        // We need to put the where-clause on a new line, but we didn't
1657        // know that earlier, so the where-clause will not be indented properly.
1658        result.push('\n');
1659        result.push_str(
1660            &(offset.block_only() + (context.config.tab_spaces() - 1)).to_string(context.config),
1661        );
1662    }
1663    result.push_str(&where_clause_str);
1664
1665    Some(result)
1666}
1667
1668#[derive(Clone, Copy)]
1669pub(crate) enum ItemVisitorKind {
1670    Item,
1671    AssocTraitItem,
1672    AssocImplItem,
1673    ForeignItem,
1674}
1675
1676struct TyAliasRewriteInfo<'c, 'g>(
1677    &'c RewriteContext<'c>,
1678    Indent,
1679    &'g ast::Generics,
1680    &'g ast::WhereClause,
1681    symbol::Ident,
1682    Span,
1683);
1684
1685pub(crate) fn rewrite_type_alias<'a>(
1686    ty_alias_kind: &ast::TyAlias,
1687    vis: &ast::Visibility,
1688    context: &RewriteContext<'a>,
1689    indent: Indent,
1690    visitor_kind: ItemVisitorKind,
1691    span: Span,
1692) -> RewriteResult {
1693    use ItemVisitorKind::*;
1694
1695    let ast::TyAlias {
1696        defaultness,
1697        ident,
1698        ref generics,
1699        ref bounds,
1700        ref ty,
1701        ref after_where_clause,
1702    } = *ty_alias_kind;
1703    let ty_opt = ty.as_ref();
1704    let rhs_hi = ty
1705        .as_ref()
1706        .map_or(generics.where_clause.span.hi(), |ty| ty.span.hi());
1707    let rw_info = &TyAliasRewriteInfo(context, indent, generics, after_where_clause, ident, span);
1708    let op_ty = opaque_ty(ty);
1709    // Type Aliases are formatted slightly differently depending on the context
1710    // in which they appear, whether they are opaque, and whether they are associated.
1711    // https://rustc-dev-guide.rust-lang.org/opaque-types-type-alias-impl-trait.html
1712    // https://github.com/rust-dev-tools/fmt-rfcs/blob/master/guide/items.md#type-aliases
1713    match (visitor_kind, &op_ty) {
1714        (Item | AssocTraitItem | ForeignItem, Some(op_bounds)) => {
1715            let op = OpaqueType { bounds: op_bounds };
1716            rewrite_ty(rw_info, Some(bounds), Some(&op), rhs_hi, vis)
1717        }
1718        (Item | AssocTraitItem | ForeignItem, None) => {
1719            rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
1720        }
1721        (AssocImplItem, _) => {
1722            let result = if let Some(op_bounds) = op_ty {
1723                let op = OpaqueType { bounds: op_bounds };
1724                rewrite_ty(
1725                    rw_info,
1726                    Some(bounds),
1727                    Some(&op),
1728                    rhs_hi,
1729                    &DEFAULT_VISIBILITY,
1730                )
1731            } else {
1732                rewrite_ty(rw_info, Some(bounds), ty_opt, rhs_hi, vis)
1733            }?;
1734            match defaultness {
1735                ast::Defaultness::Default(..) => Ok(format!("default {result}")),
1736                _ => Ok(result),
1737            }
1738        }
1739    }
1740}
1741
1742fn rewrite_ty<R: Rewrite>(
1743    rw_info: &TyAliasRewriteInfo<'_, '_>,
1744    generic_bounds_opt: Option<&ast::GenericBounds>,
1745    rhs: Option<&R>,
1746    // the span of the end of the RHS (or the end of the generics, if there is no RHS)
1747    rhs_hi: BytePos,
1748    vis: &ast::Visibility,
1749) -> RewriteResult {
1750    let mut result = String::with_capacity(128);
1751    let TyAliasRewriteInfo(context, indent, generics, after_where_clause, ident, span) = *rw_info;
1752    result.push_str(&format!("{}type ", format_visibility(context, vis)));
1753    let ident_str = rewrite_ident(context, ident);
1754
1755    if generics.params.is_empty() {
1756        result.push_str(ident_str)
1757    } else {
1758        // 2 = `= `
1759        let g_shape = Shape::indented(indent, context.config);
1760        let g_shape = g_shape
1761            .offset_left(result.len(), span)?
1762            .sub_width(2, span)?;
1763        let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
1764        result.push_str(&generics_str);
1765    }
1766
1767    if let Some(bounds) = generic_bounds_opt {
1768        if !bounds.is_empty() {
1769            // 2 = `: `
1770            let shape = Shape::indented(indent, context.config);
1771            let shape = shape.offset_left(result.len() + 2, span)?;
1772            let type_bounds = bounds
1773                .rewrite_result(context, shape)
1774                .map(|s| format!(": {}", s))?;
1775            result.push_str(&type_bounds);
1776        }
1777    }
1778
1779    let where_budget = context.budget(last_line_width(&result));
1780    let mut option = WhereClauseOption::snuggled(&result);
1781    if rhs.is_none() {
1782        option.suppress_comma();
1783    }
1784    let before_where_clause_str = rewrite_where_clause(
1785        context,
1786        &generics.where_clause,
1787        context.config.brace_style(),
1788        Shape::legacy(where_budget, indent),
1789        false,
1790        "=",
1791        None,
1792        generics.span.hi(),
1793        option,
1794    )?;
1795    result.push_str(&before_where_clause_str);
1796
1797    let mut result = if let Some(ty) = rhs {
1798        // If there are any where clauses, add a newline before the assignment.
1799        // If there is a before where clause, do not indent, but if there is
1800        // only an after where clause, additionally indent the type.
1801        if !generics.where_clause.predicates.is_empty() {
1802            result.push_str(&indent.to_string_with_newline(context.config));
1803        } else if !after_where_clause.predicates.is_empty() {
1804            result.push_str(
1805                &indent
1806                    .block_indent(context.config)
1807                    .to_string_with_newline(context.config),
1808            );
1809        } else {
1810            result.push(' ');
1811        }
1812
1813        let comment_span = context
1814            .snippet_provider
1815            .opt_span_before(span, "=")
1816            .map(|op_lo| mk_sp(generics.where_clause.span.hi(), op_lo));
1817
1818        let lhs = match comment_span {
1819            Some(comment_span)
1820                if contains_comment(
1821                    context
1822                        .snippet_provider
1823                        .span_to_snippet(comment_span)
1824                        .unknown_error()?,
1825                ) =>
1826            {
1827                let comment_shape = if !generics.where_clause.predicates.is_empty() {
1828                    Shape::indented(indent, context.config)
1829                } else {
1830                    let shape = Shape::indented(indent, context.config);
1831                    shape.block_left(context.config.tab_spaces(), span)?
1832                };
1833
1834                combine_strs_with_missing_comments(
1835                    context,
1836                    result.trim_end(),
1837                    "=",
1838                    comment_span,
1839                    comment_shape,
1840                    true,
1841                )?
1842            }
1843            _ => format!("{result}="),
1844        };
1845
1846        // 1 = `;` unless there's a trailing where clause
1847        let shape = Shape::indented(indent, context.config);
1848        let shape = if after_where_clause.predicates.is_empty() {
1849            Shape::indented(indent, context.config).sub_width(1, span)?
1850        } else {
1851            shape
1852        };
1853        rewrite_assign_rhs(context, lhs, &*ty, &RhsAssignKind::Ty, shape)?
1854    } else {
1855        result
1856    };
1857
1858    if !after_where_clause.predicates.is_empty() {
1859        let option = WhereClauseOption::new(true, WhereClauseSpace::Newline);
1860        let after_where_clause_str = rewrite_where_clause(
1861            context,
1862            &after_where_clause,
1863            context.config.brace_style(),
1864            Shape::indented(indent, context.config),
1865            false,
1866            ";",
1867            None,
1868            rhs_hi,
1869            option,
1870        )?;
1871        result.push_str(&after_where_clause_str);
1872    }
1873
1874    result += ";";
1875    Ok(result)
1876}
1877
1878fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1879    (
1880        if config.space_before_colon() { " " } else { "" },
1881        if config.space_after_colon() { " " } else { "" },
1882    )
1883}
1884
1885pub(crate) fn rewrite_struct_field_prefix(
1886    context: &RewriteContext<'_>,
1887    field: &ast::FieldDef,
1888) -> RewriteResult {
1889    let vis = format_visibility(context, &field.vis);
1890    let safety = format_safety(field.safety);
1891    let type_annotation_spacing = type_annotation_spacing(context.config);
1892    Ok(match field.ident {
1893        Some(name) => format!(
1894            "{vis}{safety}{}{}:",
1895            rewrite_ident(context, name),
1896            type_annotation_spacing.0
1897        ),
1898        None => format!("{vis}{safety}"),
1899    })
1900}
1901
1902impl Rewrite for ast::FieldDef {
1903    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1904        self.rewrite_result(context, shape).ok()
1905    }
1906
1907    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1908        rewrite_struct_field(context, self, shape, 0)
1909    }
1910}
1911
1912pub(crate) fn rewrite_struct_field(
1913    context: &RewriteContext<'_>,
1914    field: &ast::FieldDef,
1915    shape: Shape,
1916    lhs_max_width: usize,
1917) -> RewriteResult {
1918    // FIXME(default_field_values): Implement formatting.
1919    if field.default.is_some() {
1920        return Err(RewriteError::Unknown);
1921    }
1922
1923    if contains_skip(&field.attrs) {
1924        return Ok(context.snippet(field.span()).to_owned());
1925    }
1926
1927    let type_annotation_spacing = type_annotation_spacing(context.config);
1928    let prefix = rewrite_struct_field_prefix(context, field)?;
1929
1930    let attrs_str = field.attrs.rewrite_result(context, shape)?;
1931    let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
1932    let missing_span = if field.attrs.is_empty() {
1933        mk_sp(field.span.lo(), field.span.lo())
1934    } else {
1935        mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
1936    };
1937    let mut spacing = String::from(if field.ident.is_some() {
1938        type_annotation_spacing.1
1939    } else {
1940        ""
1941    });
1942    // Try to put everything on a single line.
1943    let attr_prefix = combine_strs_with_missing_comments(
1944        context,
1945        &attrs_str,
1946        &prefix,
1947        missing_span,
1948        shape,
1949        attrs_extendable,
1950    )?;
1951    let overhead = trimmed_last_line_width(&attr_prefix);
1952    let lhs_offset = lhs_max_width.saturating_sub(overhead);
1953    for _ in 0..lhs_offset {
1954        spacing.push(' ');
1955    }
1956    // In this extreme case we will be missing a space between an attribute and a field.
1957    if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
1958        spacing.push(' ');
1959    }
1960
1961    let orig_ty = shape
1962        .offset_left_opt(overhead + spacing.len())
1963        .and_then(|ty_shape| field.ty.rewrite_result(context, ty_shape).ok());
1964
1965    if let Some(ref ty) = orig_ty {
1966        if !ty.contains('\n') && !contains_comment(context.snippet(missing_span)) {
1967            return Ok(attr_prefix + &spacing + ty);
1968        }
1969    }
1970
1971    let is_prefix_empty = prefix.is_empty();
1972    // We must use multiline. We are going to put attributes and a field on different lines.
1973    let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, &RhsAssignKind::Ty, shape)?;
1974    // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1975    let field_str = if is_prefix_empty {
1976        field_str.trim_start()
1977    } else {
1978        &field_str
1979    };
1980    combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
1981}
1982
1983pub(crate) struct StaticParts<'a> {
1984    prefix: &'a str,
1985    safety: ast::Safety,
1986    vis: &'a ast::Visibility,
1987    ident: symbol::Ident,
1988    generics: Option<&'a ast::Generics>,
1989    ty: &'a ast::Ty,
1990    mutability: ast::Mutability,
1991    expr_opt: Option<&'a ast::Expr>,
1992    defaultness: Option<ast::Defaultness>,
1993    span: Span,
1994}
1995
1996impl<'a> StaticParts<'a> {
1997    pub(crate) fn from_item(item: &'a ast::Item) -> Self {
1998        let (defaultness, prefix, safety, ident, ty, mutability, expr_opt, generics) =
1999            match &item.kind {
2000                ast::ItemKind::Static(s) => (
2001                    None,
2002                    "static",
2003                    s.safety,
2004                    s.ident,
2005                    &s.ty,
2006                    s.mutability,
2007                    s.expr.as_deref(),
2008                    None,
2009                ),
2010                ast::ItemKind::Const(c) => (
2011                    Some(c.defaultness),
2012                    if c.rhs_kind.is_type_const() {
2013                        "type const"
2014                    } else {
2015                        "const"
2016                    },
2017                    ast::Safety::Default,
2018                    c.ident,
2019                    &c.ty,
2020                    ast::Mutability::Not,
2021                    c.rhs_kind.expr(),
2022                    Some(&c.generics),
2023                ),
2024                _ => unreachable!(),
2025            };
2026        StaticParts {
2027            prefix,
2028            safety,
2029            vis: &item.vis,
2030            ident,
2031            generics,
2032            ty,
2033            mutability,
2034            expr_opt,
2035            defaultness,
2036            span: item.span,
2037        }
2038    }
2039
2040    pub(crate) fn from_trait_item(ti: &'a ast::AssocItem, ident: Ident) -> Self {
2041        let (defaultness, ty, expr_opt, generics, prefix) = match &ti.kind {
2042            ast::AssocItemKind::Const(c) => {
2043                let prefix = if c.rhs_kind.is_type_const() {
2044                    "type const"
2045                } else {
2046                    "const"
2047                };
2048                (
2049                    c.defaultness,
2050                    &c.ty,
2051                    c.rhs_kind.expr(),
2052                    Some(&c.generics),
2053                    prefix,
2054                )
2055            }
2056            _ => unreachable!(),
2057        };
2058        StaticParts {
2059            prefix,
2060            safety: ast::Safety::Default,
2061            vis: &ti.vis,
2062            ident,
2063            generics,
2064            ty,
2065            mutability: ast::Mutability::Not,
2066            expr_opt,
2067            defaultness: Some(defaultness),
2068            span: ti.span,
2069        }
2070    }
2071
2072    pub(crate) fn from_impl_item(ii: &'a ast::AssocItem, ident: Ident) -> Self {
2073        let (defaultness, ty, expr_opt, generics, prefix) = match &ii.kind {
2074            ast::AssocItemKind::Const(c) => {
2075                let prefix = if c.rhs_kind.is_type_const() {
2076                    "type const"
2077                } else {
2078                    "const"
2079                };
2080                (
2081                    c.defaultness,
2082                    &c.ty,
2083                    c.rhs_kind.expr(),
2084                    Some(&c.generics),
2085                    prefix,
2086                )
2087            }
2088            _ => unreachable!(),
2089        };
2090        StaticParts {
2091            prefix,
2092            safety: ast::Safety::Default,
2093            vis: &ii.vis,
2094            ident,
2095            generics,
2096            ty,
2097            mutability: ast::Mutability::Not,
2098            expr_opt,
2099            defaultness: Some(defaultness),
2100            span: ii.span,
2101        }
2102    }
2103}
2104
2105fn rewrite_static(
2106    context: &RewriteContext<'_>,
2107    static_parts: &StaticParts<'_>,
2108    offset: Indent,
2109) -> Option<String> {
2110    // For now, if this static (or const) has generics, then bail.
2111    if static_parts
2112        .generics
2113        .is_some_and(|g| !g.params.is_empty() || !g.where_clause.is_empty())
2114    {
2115        return None;
2116    }
2117
2118    let colon = colon_spaces(context.config);
2119    let mut prefix = format!(
2120        "{}{}{}{} {}{}{}",
2121        format_visibility(context, static_parts.vis),
2122        static_parts.defaultness.map_or("", format_defaultness),
2123        format_safety(static_parts.safety),
2124        static_parts.prefix,
2125        format_mutability(static_parts.mutability),
2126        rewrite_ident(context, static_parts.ident),
2127        colon,
2128    );
2129    // 2 = " =".len()
2130    let ty_shape =
2131        Shape::indented(offset.block_only(), context.config).offset_left_opt(prefix.len() + 2)?;
2132    let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
2133        Some(ty_str) => ty_str,
2134        None => {
2135            if prefix.ends_with(' ') {
2136                prefix.pop();
2137            }
2138            let nested_indent = offset.block_indent(context.config);
2139            let nested_shape = Shape::indented(nested_indent, context.config);
2140            let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
2141            format!(
2142                "{}{}",
2143                nested_indent.to_string_with_newline(context.config),
2144                ty_str
2145            )
2146        }
2147    };
2148
2149    if let Some(expr) = static_parts.expr_opt {
2150        let comments_lo = context.snippet_provider.span_after(static_parts.span, "=");
2151        let expr_lo = expr.span.lo();
2152        let comments_span = mk_sp(comments_lo, expr_lo);
2153
2154        let lhs = format!("{prefix}{ty_str} =");
2155
2156        // 1 = ;
2157        let remaining_width = context.budget(offset.block_indent + 1);
2158        rewrite_assign_rhs_with_comments(
2159            context,
2160            &lhs,
2161            expr,
2162            Shape::legacy(remaining_width, offset.block_only()),
2163            &RhsAssignKind::Expr(&expr.kind, expr.span),
2164            RhsTactics::Default,
2165            comments_span,
2166            true,
2167        )
2168        .ok()
2169        .map(|res| recover_comment_removed(res, static_parts.span, context))
2170        .map(|s| if s.ends_with(';') { s } else { s + ";" })
2171    } else {
2172        Some(format!("{prefix}{ty_str};"))
2173    }
2174}
2175
2176// FIXME(calebcartwright) - This is a hack around a bug in the handling of TyKind::ImplTrait.
2177// This should be removed once that bug is resolved, with the type alias formatting using the
2178// defined Ty for the RHS directly.
2179// https://github.com/rust-lang/rustfmt/issues/4373
2180// https://github.com/rust-lang/rustfmt/issues/5027
2181struct OpaqueType<'a> {
2182    bounds: &'a ast::GenericBounds,
2183}
2184
2185impl<'a> Rewrite for OpaqueType<'a> {
2186    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2187        let shape = shape.offset_left_opt(5)?; // `impl `
2188        self.bounds
2189            .rewrite(context, shape)
2190            .map(|s| format!("impl {}", s))
2191    }
2192}
2193
2194impl Rewrite for ast::FnRetTy {
2195    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2196        self.rewrite_result(context, shape).ok()
2197    }
2198
2199    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
2200        match *self {
2201            ast::FnRetTy::Default(_) => Ok(String::new()),
2202            ast::FnRetTy::Ty(ref ty) => {
2203                let arrow_width = "-> ".len();
2204                if context.config.style_edition() <= StyleEdition::Edition2021
2205                    || context.config.indent_style() == IndentStyle::Visual
2206                {
2207                    let inner_width = shape
2208                        .width
2209                        .checked_sub(arrow_width)
2210                        .max_width_error(shape.width, self.span())?;
2211                    return ty
2212                        .rewrite_result(
2213                            context,
2214                            Shape::legacy(inner_width, shape.indent + arrow_width),
2215                        )
2216                        .map(|r| format!("-> {}", r));
2217                }
2218
2219                let shape = shape.offset_left(arrow_width, self.span())?;
2220
2221                ty.rewrite_result(context, shape)
2222                    .map(|s| format!("-> {}", s))
2223            }
2224        }
2225    }
2226}
2227
2228fn is_empty_infer(ty: &ast::Ty, pat_span: Span) -> bool {
2229    match ty.kind {
2230        ast::TyKind::Infer => ty.span.hi() == pat_span.hi(),
2231        _ => false,
2232    }
2233}
2234
2235/// Recover any missing comments between the param and the type.
2236///
2237/// # Returns
2238///
2239/// A 2-len tuple with the comment before the colon in first position, and the comment after the
2240/// colon in second position.
2241fn get_missing_param_comments(
2242    context: &RewriteContext<'_>,
2243    pat_span: Span,
2244    ty_span: Span,
2245    shape: Shape,
2246) -> (String, String) {
2247    let missing_comment_span = mk_sp(pat_span.hi(), ty_span.lo());
2248
2249    let span_before_colon = {
2250        let missing_comment_span_hi = context
2251            .snippet_provider
2252            .span_before(missing_comment_span, ":");
2253        mk_sp(pat_span.hi(), missing_comment_span_hi)
2254    };
2255    let span_after_colon = {
2256        let missing_comment_span_lo = context
2257            .snippet_provider
2258            .span_after(missing_comment_span, ":");
2259        mk_sp(missing_comment_span_lo, ty_span.lo())
2260    };
2261
2262    let comment_before_colon = rewrite_missing_comment(span_before_colon, shape, context)
2263        .ok()
2264        .filter(|comment| !comment.is_empty())
2265        .map_or(String::new(), |comment| format!(" {}", comment));
2266    let comment_after_colon = rewrite_missing_comment(span_after_colon, shape, context)
2267        .ok()
2268        .filter(|comment| !comment.is_empty())
2269        .map_or(String::new(), |comment| format!("{} ", comment));
2270    (comment_before_colon, comment_after_colon)
2271}
2272
2273impl Rewrite for ast::Param {
2274    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
2275        self.rewrite_result(context, shape).ok()
2276    }
2277
2278    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
2279        let param_attrs_result = self
2280            .attrs
2281            .rewrite_result(context, Shape::legacy(shape.width, shape.indent))?;
2282        // N.B. Doc comments aren't typically valid syntax, but could appear
2283        // in the presence of certain macros - https://github.com/rust-lang/rustfmt/issues/4936
2284        let (span, has_multiple_attr_lines, has_doc_comments) = if !self.attrs.is_empty() {
2285            let num_attrs = self.attrs.len();
2286            (
2287                mk_sp(self.attrs[num_attrs - 1].span.hi(), self.pat.span.lo()),
2288                param_attrs_result.contains('\n'),
2289                self.attrs.iter().any(|a| a.is_doc_comment()),
2290            )
2291        } else {
2292            (mk_sp(self.span.lo(), self.span.lo()), false, false)
2293        };
2294
2295        if let Some(ref explicit_self) = self.to_self() {
2296            rewrite_explicit_self(
2297                context,
2298                explicit_self,
2299                &param_attrs_result,
2300                span,
2301                shape,
2302                has_multiple_attr_lines,
2303            )
2304        } else if is_named_param(self) {
2305            let param_name = &self
2306                .pat
2307                .rewrite_result(context, Shape::legacy(shape.width, shape.indent))?;
2308            let mut result = combine_strs_with_missing_comments(
2309                context,
2310                &param_attrs_result,
2311                param_name,
2312                span,
2313                shape,
2314                !has_multiple_attr_lines && !has_doc_comments,
2315            )?;
2316
2317            if !is_empty_infer(&*self.ty, self.pat.span) {
2318                let (before_comment, after_comment) =
2319                    get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
2320                result.push_str(&before_comment);
2321                result.push_str(colon_spaces(context.config));
2322                result.push_str(&after_comment);
2323                let overhead = last_line_width(&result);
2324                let max_width = shape
2325                    .width
2326                    .checked_sub(overhead)
2327                    .max_width_error(shape.width, self.span())?;
2328                if let Ok(ty_str) = self
2329                    .ty
2330                    .rewrite_result(context, Shape::legacy(max_width, shape.indent))
2331                {
2332                    result.push_str(&ty_str);
2333                } else {
2334                    let prev_str = if param_attrs_result.is_empty() {
2335                        param_attrs_result
2336                    } else {
2337                        param_attrs_result + &shape.to_string_with_newline(context.config)
2338                    };
2339
2340                    result = combine_strs_with_missing_comments(
2341                        context,
2342                        &prev_str,
2343                        param_name,
2344                        span,
2345                        shape,
2346                        !has_multiple_attr_lines,
2347                    )?;
2348                    result.push_str(&before_comment);
2349                    result.push_str(colon_spaces(context.config));
2350                    result.push_str(&after_comment);
2351                    let overhead = last_line_width(&result);
2352                    let max_width = shape
2353                        .width
2354                        .checked_sub(overhead)
2355                        .max_width_error(shape.width, self.span())?;
2356                    let ty_str = self
2357                        .ty
2358                        .rewrite_result(context, Shape::legacy(max_width, shape.indent))?;
2359                    result.push_str(&ty_str);
2360                }
2361            }
2362
2363            Ok(result)
2364        } else {
2365            self.ty.rewrite_result(context, shape)
2366        }
2367    }
2368}
2369
2370fn rewrite_opt_lifetime(
2371    context: &RewriteContext<'_>,
2372    lifetime: Option<ast::Lifetime>,
2373) -> RewriteResult {
2374    let Some(l) = lifetime else {
2375        return Ok(String::new());
2376    };
2377    let mut result = l.rewrite_result(
2378        context,
2379        Shape::legacy(context.config.max_width(), Indent::empty()),
2380    )?;
2381    result.push(' ');
2382    Ok(result)
2383}
2384
2385fn rewrite_explicit_self(
2386    context: &RewriteContext<'_>,
2387    explicit_self: &ast::ExplicitSelf,
2388    param_attrs: &str,
2389    span: Span,
2390    shape: Shape,
2391    has_multiple_attr_lines: bool,
2392) -> RewriteResult {
2393    let self_str = match explicit_self.node {
2394        ast::SelfKind::Region(lt, m) => {
2395            let mut_str = format_mutability(m);
2396            let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2397            format!("&{lifetime_str}{mut_str}self")
2398        }
2399        ast::SelfKind::Pinned(lt, m) => {
2400            let mut_str = m.ptr_str();
2401            let lifetime_str = rewrite_opt_lifetime(context, lt)?;
2402            format!("&{lifetime_str}pin {mut_str} self")
2403        }
2404        ast::SelfKind::Explicit(ref ty, mutability) => {
2405            let type_str = ty.rewrite_result(
2406                context,
2407                Shape::legacy(context.config.max_width(), Indent::empty()),
2408            )?;
2409            format!("{}self: {}", format_mutability(mutability), type_str)
2410        }
2411        ast::SelfKind::Value(mutability) => format!("{}self", format_mutability(mutability)),
2412    };
2413    Ok(combine_strs_with_missing_comments(
2414        context,
2415        param_attrs,
2416        &self_str,
2417        span,
2418        shape,
2419        !has_multiple_attr_lines,
2420    )?)
2421}
2422
2423pub(crate) fn span_lo_for_param(param: &ast::Param) -> BytePos {
2424    if param.attrs.is_empty() {
2425        if is_named_param(param) {
2426            param.pat.span.lo()
2427        } else {
2428            param.ty.span.lo()
2429        }
2430    } else {
2431        param.attrs[0].span.lo()
2432    }
2433}
2434
2435pub(crate) fn span_hi_for_param(context: &RewriteContext<'_>, param: &ast::Param) -> BytePos {
2436    match param.ty.kind {
2437        ast::TyKind::Infer if context.snippet(param.ty.span) == "_" => param.ty.span.hi(),
2438        ast::TyKind::Infer if is_named_param(param) => param.pat.span.hi(),
2439        _ => param.ty.span.hi(),
2440    }
2441}
2442
2443pub(crate) fn is_named_param(param: &ast::Param) -> bool {
2444    !matches!(param.pat.kind, ast::PatKind::Missing)
2445}
2446
2447#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2448pub(crate) enum FnBraceStyle {
2449    SameLine,
2450    NextLine,
2451    None,
2452}
2453
2454// Return type is (result, force_new_line_for_brace)
2455fn rewrite_fn_base(
2456    context: &RewriteContext<'_>,
2457    indent: Indent,
2458    ident: symbol::Ident,
2459    fn_sig: &FnSig<'_>,
2460    span: Span,
2461    fn_brace_style: FnBraceStyle,
2462) -> Result<(String, bool, bool), RewriteError> {
2463    let mut force_new_line_for_brace = false;
2464
2465    let where_clause = &fn_sig.generics.where_clause;
2466
2467    let mut result = String::with_capacity(1024);
2468    result.push_str(&fn_sig.to_str(context));
2469
2470    // fn foo
2471    result.push_str("fn ");
2472
2473    // Generics.
2474    let overhead = if let FnBraceStyle::SameLine = fn_brace_style {
2475        // 4 = `() {`
2476        4
2477    } else {
2478        // 2 = `()`
2479        2
2480    };
2481    let used_width = last_line_used_width(&result, indent.width());
2482    let one_line_budget = context.budget(used_width + overhead);
2483    let shape = Shape {
2484        width: one_line_budget,
2485        indent,
2486        offset: used_width,
2487    };
2488    let fd = fn_sig.decl;
2489    let generics_str = rewrite_generics(
2490        context,
2491        rewrite_ident(context, ident),
2492        &fn_sig.generics,
2493        shape,
2494    )?;
2495    result.push_str(&generics_str);
2496
2497    let snuggle_angle_bracket = generics_str
2498        .lines()
2499        .last()
2500        .map_or(false, |l| l.trim_start().len() == 1);
2501
2502    // Note that the width and indent don't really matter, we'll re-layout the
2503    // return type later anyway.
2504    let ret_str = fd
2505        .output
2506        .rewrite_result(context, Shape::indented(indent, context.config))?;
2507
2508    let multi_line_ret_str = ret_str.contains('\n');
2509    let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
2510
2511    // Params.
2512    let (one_line_budget, multi_line_budget, mut param_indent) = compute_budgets_for_params(
2513        context,
2514        &result,
2515        indent,
2516        ret_str_len,
2517        fn_brace_style,
2518        multi_line_ret_str,
2519    );
2520
2521    debug!(
2522        "rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, param_indent: {:?}",
2523        one_line_budget, multi_line_budget, param_indent
2524    );
2525
2526    result.push('(');
2527    // Check if vertical layout was forced.
2528    if one_line_budget == 0
2529        && !snuggle_angle_bracket
2530        && context.config.indent_style() == IndentStyle::Visual
2531    {
2532        result.push_str(&param_indent.to_string_with_newline(context.config));
2533    }
2534
2535    let params_end = if fd.inputs.is_empty() {
2536        context
2537            .snippet_provider
2538            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), ")")
2539    } else {
2540        let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
2541        context.snippet_provider.span_after(last_span, ")")
2542    };
2543    let params_span = mk_sp(
2544        context
2545            .snippet_provider
2546            .span_after(mk_sp(fn_sig.generics.span.hi(), span.hi()), "("),
2547        params_end,
2548    );
2549    let param_str = rewrite_params(
2550        context,
2551        &fd.inputs,
2552        one_line_budget,
2553        multi_line_budget,
2554        indent,
2555        param_indent,
2556        params_span,
2557        fd.c_variadic(),
2558    )?;
2559
2560    let put_params_in_block = match context.config.indent_style() {
2561        IndentStyle::Block => param_str.contains('\n') || param_str.len() > one_line_budget,
2562        _ => false,
2563    } && !fd.inputs.is_empty();
2564
2565    let mut params_last_line_contains_comment = false;
2566    let mut no_params_and_over_max_width = false;
2567
2568    if put_params_in_block {
2569        param_indent = indent.block_indent(context.config);
2570        result.push_str(&param_indent.to_string_with_newline(context.config));
2571        result.push_str(&param_str);
2572        result.push_str(&indent.to_string_with_newline(context.config));
2573        result.push(')');
2574    } else {
2575        result.push_str(&param_str);
2576        let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
2577        // Put the closing brace on the next line if it overflows the max width.
2578        // 1 = `)`
2579        let closing_paren_overflow_max_width =
2580            fd.inputs.is_empty() && used_width + 1 > context.config.max_width();
2581        // If the last line of params contains comment, we cannot put the closing paren
2582        // on the same line.
2583        params_last_line_contains_comment = param_str
2584            .lines()
2585            .last()
2586            .map_or(false, |last_line| last_line.contains("//"));
2587
2588        if context.config.style_edition() >= StyleEdition::Edition2024 {
2589            if closing_paren_overflow_max_width {
2590                result.push(')');
2591                result.push_str(&indent.to_string_with_newline(context.config));
2592                no_params_and_over_max_width = true;
2593            } else if params_last_line_contains_comment {
2594                result.push_str(&indent.to_string_with_newline(context.config));
2595                result.push(')');
2596                no_params_and_over_max_width = true;
2597            } else {
2598                result.push(')');
2599            }
2600        } else {
2601            if closing_paren_overflow_max_width || params_last_line_contains_comment {
2602                result.push_str(&indent.to_string_with_newline(context.config));
2603            }
2604            result.push(')');
2605        }
2606    }
2607
2608    // Return type.
2609    if let ast::FnRetTy::Ty(..) = fd.output {
2610        let ret_should_indent = match context.config.indent_style() {
2611            // If our params are block layout then we surely must have space.
2612            IndentStyle::Block if put_params_in_block || fd.inputs.is_empty() => false,
2613            _ if params_last_line_contains_comment => false,
2614            _ if result.contains('\n') || multi_line_ret_str => true,
2615            _ => {
2616                // If the return type would push over the max width, then put the return type on
2617                // a new line. With the +1 for the signature length an additional space between
2618                // the closing parenthesis of the param and the arrow '->' is considered.
2619                let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
2620
2621                // If there is no where-clause, take into account the space after the return type
2622                // and the brace.
2623                if where_clause.predicates.is_empty() {
2624                    sig_length += 2;
2625                }
2626
2627                sig_length > context.config.max_width()
2628            }
2629        };
2630        let ret_shape = if ret_should_indent {
2631            if context.config.style_edition() <= StyleEdition::Edition2021
2632                || context.config.indent_style() == IndentStyle::Visual
2633            {
2634                let indent = if param_str.is_empty() {
2635                    // Aligning with nonexistent params looks silly.
2636                    force_new_line_for_brace = true;
2637                    indent + 4
2638                } else {
2639                    // FIXME: we might want to check that using the param indent
2640                    // doesn't blow our budget, and if it does, then fallback to
2641                    // the where-clause indent.
2642                    param_indent
2643                };
2644
2645                result.push_str(&indent.to_string_with_newline(context.config));
2646                Shape::indented(indent, context.config)
2647            } else {
2648                let mut ret_shape = Shape::indented(indent, context.config);
2649                if param_str.is_empty() {
2650                    // Aligning with nonexistent params looks silly.
2651                    force_new_line_for_brace = true;
2652                    ret_shape = if context.use_block_indent() {
2653                        ret_shape.offset_left_opt(4).unwrap_or(ret_shape)
2654                    } else {
2655                        ret_shape.indent = ret_shape.indent + 4;
2656                        ret_shape
2657                    };
2658                }
2659
2660                result.push_str(&ret_shape.indent.to_string_with_newline(context.config));
2661                ret_shape
2662            }
2663        } else {
2664            if context.config.style_edition() >= StyleEdition::Edition2024 {
2665                if !param_str.is_empty() || !no_params_and_over_max_width {
2666                    result.push(' ');
2667                }
2668            } else {
2669                result.push(' ');
2670            }
2671
2672            let ret_shape = Shape::indented(indent, context.config);
2673            ret_shape
2674                .offset_left_opt(last_line_width(&result))
2675                .unwrap_or(ret_shape)
2676        };
2677
2678        if multi_line_ret_str || ret_should_indent {
2679            // Now that we know the proper indent and width, we need to
2680            // re-layout the return type.
2681            let ret_str = fd.output.rewrite_result(context, ret_shape)?;
2682            result.push_str(&ret_str);
2683        } else {
2684            result.push_str(&ret_str);
2685        }
2686
2687        // Comment between return type and the end of the decl.
2688        let snippet_lo = fd.output.span().hi();
2689        if where_clause.predicates.is_empty() {
2690            let snippet_hi = span.hi();
2691            let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2692            // Try to preserve the layout of the original snippet.
2693            let original_starts_with_newline = snippet
2694                .find(|c| c != ' ')
2695                .map_or(false, |i| starts_with_newline(&snippet[i..]));
2696            let original_ends_with_newline = snippet
2697                .rfind(|c| c != ' ')
2698                .map_or(false, |i| snippet[i..].ends_with('\n'));
2699            let snippet = snippet.trim();
2700            if !snippet.is_empty() {
2701                result.push(if original_starts_with_newline {
2702                    '\n'
2703                } else {
2704                    ' '
2705                });
2706                result.push_str(snippet);
2707                if original_ends_with_newline {
2708                    force_new_line_for_brace = true;
2709                }
2710            }
2711        }
2712    }
2713
2714    let pos_before_where = match fd.output {
2715        ast::FnRetTy::Default(..) => params_span.hi(),
2716        ast::FnRetTy::Ty(ref ty) => ty.span.hi(),
2717    };
2718
2719    let is_params_multi_lined = param_str.contains('\n');
2720
2721    let space = if put_params_in_block && ret_str.is_empty() {
2722        WhereClauseSpace::Space
2723    } else {
2724        WhereClauseSpace::Newline
2725    };
2726    let mut option = WhereClauseOption::new(fn_brace_style == FnBraceStyle::None, space);
2727    if is_params_multi_lined {
2728        option.veto_single_line();
2729    }
2730    let where_clause_str = rewrite_where_clause(
2731        context,
2732        &where_clause,
2733        context.config.brace_style(),
2734        Shape::indented(indent, context.config),
2735        true,
2736        "{",
2737        Some(span.hi()),
2738        pos_before_where,
2739        option,
2740    )?;
2741    // If there are neither where-clause nor return type, we may be missing comments between
2742    // params and `{`.
2743    if where_clause_str.is_empty() {
2744        if let ast::FnRetTy::Default(ret_span) = fd.output {
2745            match recover_missing_comment_in_span(
2746                // from after the closing paren to right before block or semicolon
2747                mk_sp(ret_span.lo(), span.hi()),
2748                shape,
2749                context,
2750                last_line_width(&result),
2751            ) {
2752                Ok(ref missing_comment) if !missing_comment.is_empty() => {
2753                    result.push_str(missing_comment);
2754                    force_new_line_for_brace = true;
2755                }
2756                _ => (),
2757            }
2758        }
2759    }
2760
2761    result.push_str(&where_clause_str);
2762
2763    let ends_with_comment = last_line_contains_single_line_comment(&result);
2764    force_new_line_for_brace |= ends_with_comment;
2765    force_new_line_for_brace |=
2766        is_params_multi_lined && context.config.where_single_line() && !where_clause_str.is_empty();
2767    Ok((result, ends_with_comment, force_new_line_for_brace))
2768}
2769
2770/// Kind of spaces to put before `where`.
2771#[derive(Copy, Clone)]
2772enum WhereClauseSpace {
2773    /// A single space.
2774    Space,
2775    /// A new line.
2776    Newline,
2777    /// Nothing.
2778    None,
2779}
2780
2781#[derive(Copy, Clone)]
2782struct WhereClauseOption {
2783    suppress_comma: bool, // Force no trailing comma
2784    snuggle: WhereClauseSpace,
2785    allow_single_line: bool, // Try single line where-clause instead of vertical layout
2786    veto_single_line: bool,  // Disallow a single-line where-clause.
2787}
2788
2789impl WhereClauseOption {
2790    fn new(suppress_comma: bool, snuggle: WhereClauseSpace) -> WhereClauseOption {
2791        WhereClauseOption {
2792            suppress_comma,
2793            snuggle,
2794            allow_single_line: false,
2795            veto_single_line: false,
2796        }
2797    }
2798
2799    fn snuggled(current: &str) -> WhereClauseOption {
2800        WhereClauseOption {
2801            suppress_comma: false,
2802            snuggle: if last_line_width(current) == 1 {
2803                WhereClauseSpace::Space
2804            } else {
2805                WhereClauseSpace::Newline
2806            },
2807            allow_single_line: false,
2808            veto_single_line: false,
2809        }
2810    }
2811
2812    fn suppress_comma(&mut self) {
2813        self.suppress_comma = true
2814    }
2815
2816    fn allow_single_line(&mut self) {
2817        self.allow_single_line = true
2818    }
2819
2820    fn snuggle(&mut self) {
2821        self.snuggle = WhereClauseSpace::Space
2822    }
2823
2824    fn veto_single_line(&mut self) {
2825        self.veto_single_line = true;
2826    }
2827}
2828
2829fn rewrite_params(
2830    context: &RewriteContext<'_>,
2831    params: &[ast::Param],
2832    one_line_budget: usize,
2833    multi_line_budget: usize,
2834    indent: Indent,
2835    param_indent: Indent,
2836    span: Span,
2837    variadic: bool,
2838) -> RewriteResult {
2839    if params.is_empty() {
2840        let comment = context
2841            .snippet(mk_sp(
2842                span.lo(),
2843                // to remove ')'
2844                span.hi() - BytePos(1),
2845            ))
2846            .trim();
2847        return Ok(comment.to_owned());
2848    }
2849    let param_items: Vec<_> = itemize_list(
2850        context.snippet_provider,
2851        params.iter(),
2852        ")",
2853        ",",
2854        |param| span_lo_for_param(param),
2855        |param| param.ty.span.hi(),
2856        |param| {
2857            param
2858                .rewrite_result(context, Shape::legacy(multi_line_budget, param_indent))
2859                .or_else(|_| Ok(context.snippet(param.span()).to_owned()))
2860        },
2861        span.lo(),
2862        span.hi(),
2863        false,
2864    )
2865    .collect();
2866
2867    let tactic = definitive_tactic(
2868        &param_items,
2869        context
2870            .config
2871            .fn_params_layout()
2872            .to_list_tactic(param_items.len()),
2873        Separator::Comma,
2874        one_line_budget,
2875    );
2876    let budget = match tactic {
2877        DefinitiveListTactic::Horizontal => one_line_budget,
2878        _ => multi_line_budget,
2879    };
2880    let indent = match context.config.indent_style() {
2881        IndentStyle::Block => indent.block_indent(context.config),
2882        IndentStyle::Visual => param_indent,
2883    };
2884    let trailing_separator = if variadic {
2885        SeparatorTactic::Never
2886    } else {
2887        match context.config.indent_style() {
2888            IndentStyle::Block => context.config.trailing_comma(),
2889            IndentStyle::Visual => SeparatorTactic::Never,
2890        }
2891    };
2892    let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
2893        .tactic(tactic)
2894        .trailing_separator(trailing_separator)
2895        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
2896        .preserve_newline(true);
2897    write_list(&param_items, &fmt)
2898}
2899
2900fn compute_budgets_for_params(
2901    context: &RewriteContext<'_>,
2902    result: &str,
2903    indent: Indent,
2904    ret_str_len: usize,
2905    fn_brace_style: FnBraceStyle,
2906    force_vertical_layout: bool,
2907) -> (usize, usize, Indent) {
2908    debug!(
2909        "compute_budgets_for_params {} {:?}, {}, {:?}",
2910        result.len(),
2911        indent,
2912        ret_str_len,
2913        fn_brace_style,
2914    );
2915    // Try keeping everything on the same line.
2916    if !result.contains('\n') && !force_vertical_layout {
2917        // 2 = `()`, 3 = `() `, space is before ret_string.
2918        let overhead = if ret_str_len == 0 { 2 } else { 3 };
2919        let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
2920        match fn_brace_style {
2921            FnBraceStyle::None => used_space += 1,     // 1 = `;`
2922            FnBraceStyle::SameLine => used_space += 2, // 2 = `{}`
2923            FnBraceStyle::NextLine => (),
2924        }
2925        let one_line_budget = context.budget(used_space);
2926
2927        if one_line_budget > 0 {
2928            // 4 = "() {".len()
2929            let (indent, multi_line_budget) = match context.config.indent_style() {
2930                IndentStyle::Block => {
2931                    let indent = indent.block_indent(context.config);
2932                    (indent, context.budget(indent.width() + 1))
2933                }
2934                IndentStyle::Visual => {
2935                    let indent = indent + result.len() + 1;
2936                    let multi_line_overhead = match fn_brace_style {
2937                        FnBraceStyle::SameLine => 4,
2938                        _ => 2,
2939                    } + indent.width();
2940                    (indent, context.budget(multi_line_overhead))
2941                }
2942            };
2943
2944            return (one_line_budget, multi_line_budget, indent);
2945        }
2946    }
2947
2948    // Didn't work. we must force vertical layout and put params on a newline.
2949    let new_indent = indent.block_indent(context.config);
2950    let used_space = match context.config.indent_style() {
2951        // 1 = `,`
2952        IndentStyle::Block => new_indent.width() + 1,
2953        // Account for `)` and possibly ` {`.
2954        IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
2955    };
2956    (0, context.budget(used_space), new_indent)
2957}
2958
2959fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> FnBraceStyle {
2960    let predicate_count = where_clause.predicates.len();
2961
2962    if config.where_single_line() && predicate_count == 1 {
2963        return FnBraceStyle::SameLine;
2964    }
2965    let brace_style = config.brace_style();
2966
2967    let use_next_line = brace_style == BraceStyle::AlwaysNextLine
2968        || (brace_style == BraceStyle::SameLineWhere && predicate_count > 0);
2969    if use_next_line {
2970        FnBraceStyle::NextLine
2971    } else {
2972        FnBraceStyle::SameLine
2973    }
2974}
2975
2976fn rewrite_generics(
2977    context: &RewriteContext<'_>,
2978    ident: &str,
2979    generics: &ast::Generics,
2980    shape: Shape,
2981) -> RewriteResult {
2982    // FIXME: convert bounds to where-clauses where they get too big or if
2983    // there is a where-clause at all.
2984
2985    if generics.params.is_empty() {
2986        return Ok(ident.to_owned());
2987    }
2988
2989    let params = generics.params.iter();
2990    overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
2991}
2992
2993fn generics_shape_from_config(
2994    config: &Config,
2995    shape: Shape,
2996    offset: usize,
2997    span: Span,
2998) -> Result<Shape, ExceedsMaxWidthError> {
2999    match config.indent_style() {
3000        IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2, span),
3001        IndentStyle::Block => {
3002            // 1 = ","
3003            shape
3004                .block()
3005                .block_indent(config.tab_spaces())
3006                .with_max_width(config)
3007                .sub_width(1, span)
3008        }
3009    }
3010}
3011
3012fn rewrite_where_clause_rfc_style(
3013    context: &RewriteContext<'_>,
3014    predicates: &[ast::WherePredicate],
3015    where_span: Span,
3016    shape: Shape,
3017    terminator: &str,
3018    span_end: Option<BytePos>,
3019    span_end_before_where: BytePos,
3020    where_clause_option: WhereClauseOption,
3021) -> RewriteResult {
3022    let (where_keyword, allow_single_line) = rewrite_where_keyword(
3023        context,
3024        predicates,
3025        where_span,
3026        shape,
3027        span_end_before_where,
3028        where_clause_option,
3029    )?;
3030
3031    // 1 = `,`
3032    let clause_shape = shape
3033        .block()
3034        .with_max_width(context.config)
3035        .block_left(context.config.tab_spaces(), where_span)?
3036        .sub_width(1, where_span)?;
3037    let force_single_line = context.config.where_single_line()
3038        && predicates.len() == 1
3039        && !where_clause_option.veto_single_line;
3040
3041    let preds_str = rewrite_bounds_on_where_clause(
3042        context,
3043        predicates,
3044        clause_shape,
3045        terminator,
3046        span_end,
3047        where_clause_option,
3048        force_single_line,
3049    )?;
3050
3051    // 6 = `where `
3052    let clause_sep =
3053        if allow_single_line && !preds_str.contains('\n') && 6 + preds_str.len() <= shape.width
3054            || force_single_line
3055        {
3056            Cow::from(" ")
3057        } else {
3058            clause_shape.indent.to_string_with_newline(context.config)
3059        };
3060
3061    Ok(format!("{where_keyword}{clause_sep}{preds_str}"))
3062}
3063
3064/// Rewrite `where` and comment around it.
3065fn rewrite_where_keyword(
3066    context: &RewriteContext<'_>,
3067    predicates: &[ast::WherePredicate],
3068    where_span: Span,
3069    shape: Shape,
3070    span_end_before_where: BytePos,
3071    where_clause_option: WhereClauseOption,
3072) -> Result<(String, bool), RewriteError> {
3073    let block_shape = shape.block().with_max_width(context.config);
3074    // 1 = `,`
3075    let clause_shape = block_shape
3076        .block_left(context.config.tab_spaces(), where_span)?
3077        .sub_width(1, where_span)?;
3078
3079    let comment_separator = |comment: &str, shape: Shape| {
3080        if comment.is_empty() {
3081            Cow::from("")
3082        } else {
3083            shape.indent.to_string_with_newline(context.config)
3084        }
3085    };
3086
3087    let (span_before, span_after) =
3088        missing_span_before_after_where(span_end_before_where, predicates, where_span);
3089    let (comment_before, comment_after) =
3090        rewrite_comments_before_after_where(context, span_before, span_after, shape)?;
3091
3092    let starting_newline = match where_clause_option.snuggle {
3093        WhereClauseSpace::Space if comment_before.is_empty() => Cow::from(" "),
3094        WhereClauseSpace::None => Cow::from(""),
3095        _ => block_shape.indent.to_string_with_newline(context.config),
3096    };
3097
3098    let newline_before_where = comment_separator(&comment_before, shape);
3099    let newline_after_where = comment_separator(&comment_after, clause_shape);
3100    let result = format!(
3101        "{starting_newline}{comment_before}{newline_before_where}where\
3102{newline_after_where}{comment_after}"
3103    );
3104    let allow_single_line = where_clause_option.allow_single_line
3105        && comment_before.is_empty()
3106        && comment_after.is_empty();
3107
3108    Ok((result, allow_single_line))
3109}
3110
3111/// Rewrite bounds on a where clause.
3112fn rewrite_bounds_on_where_clause(
3113    context: &RewriteContext<'_>,
3114    predicates: &[ast::WherePredicate],
3115    shape: Shape,
3116    terminator: &str,
3117    span_end: Option<BytePos>,
3118    where_clause_option: WhereClauseOption,
3119    force_single_line: bool,
3120) -> RewriteResult {
3121    let span_start = predicates[0].span().lo();
3122    // If we don't have the start of the next span, then use the end of the
3123    // predicates, but that means we miss comments.
3124    let len = predicates.len();
3125    let end_of_preds = predicates[len - 1].span().hi();
3126    let span_end = span_end.unwrap_or(end_of_preds);
3127    let items = itemize_list(
3128        context.snippet_provider,
3129        predicates.iter(),
3130        terminator,
3131        ",",
3132        |pred| pred.span().lo(),
3133        |pred| pred.span().hi(),
3134        |pred| pred.rewrite_result(context, shape),
3135        span_start,
3136        span_end,
3137        false,
3138    );
3139    let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
3140        SeparatorTactic::Never
3141    } else {
3142        context.config.trailing_comma()
3143    };
3144
3145    // shape should be vertical only and only if we have `force_single_line` option enabled
3146    // and the number of items of the where-clause is equal to 1
3147    let shape_tactic = if force_single_line {
3148        DefinitiveListTactic::Horizontal
3149    } else {
3150        DefinitiveListTactic::Vertical
3151    };
3152
3153    let preserve_newline = context.config.style_edition() <= StyleEdition::Edition2021;
3154
3155    let fmt = ListFormatting::new(shape, context.config)
3156        .tactic(shape_tactic)
3157        .trailing_separator(comma_tactic)
3158        .preserve_newline(preserve_newline);
3159    write_list(&items.collect::<Vec<_>>(), &fmt)
3160}
3161
3162fn rewrite_where_clause(
3163    context: &RewriteContext<'_>,
3164    where_clause: &ast::WhereClause,
3165    brace_style: BraceStyle,
3166    shape: Shape,
3167    on_new_line: bool,
3168    terminator: &str,
3169    span_end: Option<BytePos>,
3170    span_end_before_where: BytePos,
3171    where_clause_option: WhereClauseOption,
3172) -> RewriteResult {
3173    let ast::WhereClause {
3174        ref predicates,
3175        span: where_span,
3176        has_where_token: _,
3177    } = *where_clause;
3178
3179    if predicates.is_empty() {
3180        return Ok(String::new());
3181    }
3182
3183    if context.config.indent_style() == IndentStyle::Block {
3184        return rewrite_where_clause_rfc_style(
3185            context,
3186            predicates,
3187            where_span,
3188            shape,
3189            terminator,
3190            span_end,
3191            span_end_before_where,
3192            where_clause_option,
3193        );
3194    }
3195
3196    let extra_indent = Indent::new(context.config.tab_spaces(), 0);
3197
3198    let offset = match context.config.indent_style() {
3199        IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
3200        // 6 = "where ".len()
3201        IndentStyle::Visual => shape.indent + extra_indent + 6,
3202    };
3203    // FIXME: if indent_style != Visual, then the budgets below might
3204    // be out by a char or two.
3205
3206    let budget = context.config.max_width() - offset.width();
3207    let span_start = predicates[0].span().lo();
3208    // If we don't have the start of the next span, then use the end of the
3209    // predicates, but that means we miss comments.
3210    let len = predicates.len();
3211    let end_of_preds = predicates[len - 1].span().hi();
3212    let span_end = span_end.unwrap_or(end_of_preds);
3213    let items = itemize_list(
3214        context.snippet_provider,
3215        predicates.iter(),
3216        terminator,
3217        ",",
3218        |pred| pred.span().lo(),
3219        |pred| pred.span().hi(),
3220        |pred| pred.rewrite_result(context, Shape::legacy(budget, offset)),
3221        span_start,
3222        span_end,
3223        false,
3224    );
3225    let item_vec = items.collect::<Vec<_>>();
3226    // FIXME: we don't need to collect here
3227    let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget);
3228
3229    let mut comma_tactic = context.config.trailing_comma();
3230    // Kind of a hack because we don't usually have trailing commas in where-clauses.
3231    if comma_tactic == SeparatorTactic::Vertical || where_clause_option.suppress_comma {
3232        comma_tactic = SeparatorTactic::Never;
3233    }
3234
3235    let fmt = ListFormatting::new(Shape::legacy(budget, offset), context.config)
3236        .tactic(tactic)
3237        .trailing_separator(comma_tactic)
3238        .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
3239        .preserve_newline(true);
3240    let preds_str = write_list(&item_vec, &fmt)?;
3241
3242    let end_length = if terminator == "{" {
3243        // If the brace is on the next line we don't need to count it otherwise it needs two
3244        // characters " {"
3245        match brace_style {
3246            BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0,
3247            BraceStyle::PreferSameLine => 2,
3248        }
3249    } else if terminator == "=" {
3250        2
3251    } else {
3252        terminator.len()
3253    };
3254    if on_new_line
3255        || preds_str.contains('\n')
3256        || shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
3257    {
3258        Ok(format!(
3259            "\n{}where {}",
3260            (shape.indent + extra_indent).to_string(context.config),
3261            preds_str
3262        ))
3263    } else {
3264        Ok(format!(" where {preds_str}"))
3265    }
3266}
3267
3268fn missing_span_before_after_where(
3269    before_item_span_end: BytePos,
3270    predicates: &[ast::WherePredicate],
3271    where_span: Span,
3272) -> (Span, Span) {
3273    let missing_span_before = mk_sp(before_item_span_end, where_span.lo());
3274    // 5 = `where`
3275    let pos_after_where = where_span.lo() + BytePos(5);
3276    let missing_span_after = mk_sp(pos_after_where, predicates[0].span().lo());
3277    (missing_span_before, missing_span_after)
3278}
3279
3280fn rewrite_comments_before_after_where(
3281    context: &RewriteContext<'_>,
3282    span_before_where: Span,
3283    span_after_where: Span,
3284    shape: Shape,
3285) -> Result<(String, String), RewriteError> {
3286    let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
3287    let after_comment = rewrite_missing_comment(
3288        span_after_where,
3289        shape.block_indent(context.config.tab_spaces()),
3290        context,
3291    )?;
3292    Ok((before_comment, after_comment))
3293}
3294
3295fn format_header(
3296    context: &RewriteContext<'_>,
3297    item_name: &str,
3298    ident: symbol::Ident,
3299    vis: &ast::Visibility,
3300    offset: Indent,
3301) -> String {
3302    let mut result = String::with_capacity(128);
3303    let shape = Shape::indented(offset, context.config);
3304
3305    result.push_str(format_visibility(context, vis).trim());
3306
3307    // Check for a missing comment between the visibility and the item name.
3308    let after_vis = vis.span.hi();
3309    if let Some(before_item_name) = context
3310        .snippet_provider
3311        .opt_span_before(mk_sp(vis.span.lo(), ident.span.hi()), item_name.trim())
3312    {
3313        let missing_span = mk_sp(after_vis, before_item_name);
3314        if let Ok(result_with_comment) = combine_strs_with_missing_comments(
3315            context,
3316            &result,
3317            item_name,
3318            missing_span,
3319            shape,
3320            /* allow_extend */ true,
3321        ) {
3322            result = result_with_comment;
3323        }
3324    }
3325
3326    result.push_str(rewrite_ident(context, ident));
3327
3328    result
3329}
3330
3331#[derive(PartialEq, Eq, Clone, Copy)]
3332enum BracePos {
3333    None,
3334    Auto,
3335    ForceSameLine,
3336}
3337
3338fn format_generics(
3339    context: &RewriteContext<'_>,
3340    generics: &ast::Generics,
3341    brace_style: BraceStyle,
3342    brace_pos: BracePos,
3343    offset: Indent,
3344    span: Span,
3345    used_width: usize,
3346) -> Option<String> {
3347    let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
3348    let mut result = rewrite_generics(context, "", generics, shape).ok()?;
3349
3350    // If the generics are not parameterized then generics.span.hi() == 0,
3351    // so we use span.lo(), which is the position after `struct Foo`.
3352    let span_end_before_where = if !generics.params.is_empty() {
3353        generics.span.hi()
3354    } else {
3355        span.lo()
3356    };
3357    let (same_line_brace, missed_comments) = if !generics.where_clause.predicates.is_empty() {
3358        let budget = context.budget(last_line_used_width(&result, offset.width()));
3359        let mut option = WhereClauseOption::snuggled(&result);
3360        if brace_pos == BracePos::None {
3361            option.suppress_comma = true;
3362        }
3363        let where_clause_str = rewrite_where_clause(
3364            context,
3365            &generics.where_clause,
3366            brace_style,
3367            Shape::legacy(budget, offset.block_only()),
3368            true,
3369            "{",
3370            Some(span.hi()),
3371            span_end_before_where,
3372            option,
3373        )
3374        .ok()?;
3375        result.push_str(&where_clause_str);
3376        (
3377            brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine,
3378            // missed comments are taken care of in #rewrite_where_clause
3379            None,
3380        )
3381    } else {
3382        (
3383            brace_pos == BracePos::ForceSameLine
3384                || (result.contains('\n') && brace_style == BraceStyle::PreferSameLine
3385                    || brace_style != BraceStyle::AlwaysNextLine)
3386                || trimmed_last_line_width(&result) == 1,
3387            rewrite_missing_comment(
3388                mk_sp(
3389                    span_end_before_where,
3390                    if brace_pos == BracePos::None {
3391                        span.hi()
3392                    } else {
3393                        context.snippet_provider.span_before_last(span, "{")
3394                    },
3395                ),
3396                shape,
3397                context,
3398            )
3399            .ok(),
3400        )
3401    };
3402    // add missing comments
3403    let missed_line_comments = missed_comments
3404        .filter(|missed_comments| !missed_comments.is_empty())
3405        .map_or(false, |missed_comments| {
3406            let is_block = is_last_comment_block(&missed_comments);
3407            let sep = if is_block { " " } else { "\n" };
3408            result.push_str(sep);
3409            result.push_str(&missed_comments);
3410            !is_block
3411        });
3412    if brace_pos == BracePos::None {
3413        return Some(result);
3414    }
3415    let total_used_width = last_line_used_width(&result, used_width);
3416    let remaining_budget = context.budget(total_used_width);
3417    // If the same line brace if forced, it indicates that we are rewriting an item with empty body,
3418    // and hence we take the closer into account as well for one line budget.
3419    // We assume that the closer has the same length as the opener.
3420    let overhead = if brace_pos == BracePos::ForceSameLine {
3421        // 3 = ` {}`
3422        3
3423    } else {
3424        // 2 = ` {`
3425        2
3426    };
3427    let forbid_same_line_brace = missed_line_comments || overhead > remaining_budget;
3428    if !forbid_same_line_brace && same_line_brace {
3429        result.push(' ');
3430    } else {
3431        result.push('\n');
3432        result.push_str(&offset.block_only().to_string(context.config));
3433    }
3434    result.push('{');
3435
3436    Some(result)
3437}
3438
3439impl Rewrite for ast::ForeignItem {
3440    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
3441        self.rewrite_result(context, shape).ok()
3442    }
3443
3444    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
3445        let attrs_str = self.attrs.rewrite_result(context, shape)?;
3446        // Drop semicolon or it will be interpreted as comment.
3447        // FIXME: this may be a faulty span from libsyntax.
3448        let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
3449
3450        let item_str = match self.kind {
3451            ast::ForeignItemKind::Fn(ref fn_kind) => {
3452                let ast::Fn {
3453                    defaultness,
3454                    ref sig,
3455                    ident,
3456                    ref generics,
3457                    ref body,
3458                    ..
3459                } = **fn_kind;
3460                if body.is_some() {
3461                    let mut visitor = FmtVisitor::from_context(context);
3462                    visitor.block_indent = shape.indent;
3463                    visitor.last_pos = self.span.lo();
3464                    let inner_attrs = inner_attributes(&self.attrs);
3465                    let fn_ctxt = visit::FnCtxt::Foreign;
3466                    visitor.visit_fn(
3467                        ident,
3468                        visit::FnKind::Fn(fn_ctxt, &self.vis, fn_kind),
3469                        &sig.decl,
3470                        self.span,
3471                        defaultness,
3472                        Some(&inner_attrs),
3473                    );
3474                    Ok(visitor.buffer.to_owned())
3475                } else {
3476                    rewrite_fn_base(
3477                        context,
3478                        shape.indent,
3479                        ident,
3480                        &FnSig::from_method_sig(sig, generics, &self.vis, defaultness),
3481                        span,
3482                        FnBraceStyle::None,
3483                    )
3484                    .map(|(s, _, _)| format!("{};", s))
3485                }
3486            }
3487            ast::ForeignItemKind::Static(ref static_foreign_item) => {
3488                // FIXME(#21): we're dropping potential comments in between the
3489                // function kw here.
3490                let vis = format_visibility(context, &self.vis);
3491                let safety = format_safety(static_foreign_item.safety);
3492                let mut_str = format_mutability(static_foreign_item.mutability);
3493                let prefix = format!(
3494                    "{}{}static {}{}:",
3495                    vis,
3496                    safety,
3497                    mut_str,
3498                    rewrite_ident(context, static_foreign_item.ident)
3499                );
3500                // 1 = ;
3501                rewrite_assign_rhs(
3502                    context,
3503                    prefix,
3504                    &static_foreign_item.ty,
3505                    &RhsAssignKind::Ty,
3506                    shape.sub_width(1, static_foreign_item.ty.span)?,
3507                )
3508                .map(|s| s + ";")
3509            }
3510            ast::ForeignItemKind::TyAlias(ref ty_alias) => {
3511                let kind = ItemVisitorKind::ForeignItem;
3512                rewrite_type_alias(ty_alias, &self.vis, context, shape.indent, kind, self.span)
3513            }
3514            ast::ForeignItemKind::MacCall(ref mac) => {
3515                rewrite_macro(mac, context, shape, MacroPosition::Item)
3516            }
3517        }?;
3518
3519        let missing_span = if self.attrs.is_empty() {
3520            mk_sp(self.span.lo(), self.span.lo())
3521        } else {
3522            mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
3523        };
3524        combine_strs_with_missing_comments(
3525            context,
3526            &attrs_str,
3527            &item_str,
3528            missing_span,
3529            shape,
3530            false,
3531        )
3532    }
3533}
3534
3535/// Rewrite the attributes of an item.
3536fn rewrite_attrs(
3537    context: &RewriteContext<'_>,
3538    item: &ast::Item,
3539    item_str: &str,
3540    shape: Shape,
3541) -> RewriteResult {
3542    let attrs = filter_inline_attrs(&item.attrs, item.span());
3543    let attrs_str = attrs.rewrite_result(context, shape)?;
3544
3545    let missed_span = if attrs.is_empty() {
3546        mk_sp(item.span.lo(), item.span.lo())
3547    } else {
3548        mk_sp(attrs[attrs.len() - 1].span.hi(), item.span.lo())
3549    };
3550
3551    let allow_extend = if attrs.len() == 1 {
3552        let line_len = attrs_str.len() + 1 + item_str.len();
3553        !attrs.first().unwrap().is_doc_comment()
3554            && context.config.inline_attribute_width() >= line_len
3555    } else {
3556        false
3557    };
3558
3559    combine_strs_with_missing_comments(
3560        context,
3561        &attrs_str,
3562        item_str,
3563        missed_span,
3564        shape,
3565        allow_extend,
3566    )
3567}
3568
3569/// Rewrite an inline mod.
3570/// The given shape is used to format the mod's attributes.
3571pub(crate) fn rewrite_mod(
3572    context: &RewriteContext<'_>,
3573    item: &ast::Item,
3574    ident: Ident,
3575    attrs_shape: Shape,
3576) -> RewriteResult {
3577    let mut result = String::with_capacity(32);
3578    result.push_str(&*format_visibility(context, &item.vis));
3579    result.push_str("mod ");
3580    result.push_str(rewrite_ident(context, ident));
3581    result.push(';');
3582    rewrite_attrs(context, item, &result, attrs_shape)
3583}
3584
3585/// Rewrite `extern crate foo;`.
3586/// The given shape is used to format the extern crate's attributes.
3587pub(crate) fn rewrite_extern_crate(
3588    context: &RewriteContext<'_>,
3589    item: &ast::Item,
3590    attrs_shape: Shape,
3591) -> RewriteResult {
3592    assert!(is_extern_crate(item));
3593    let new_str = context.snippet(item.span);
3594    let item_str = if contains_comment(new_str) {
3595        new_str.to_owned()
3596    } else {
3597        let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
3598        String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
3599    };
3600    rewrite_attrs(context, item, &item_str, attrs_shape)
3601}
3602
3603/// Returns `true` for `mod foo;`, false for `mod foo { .. }`.
3604pub(crate) fn is_mod_decl(item: &ast::Item) -> bool {
3605    !matches!(
3606        item.kind,
3607        ast::ItemKind::Mod(_, _, ast::ModKind::Loaded(_, ast::Inline::Yes, _))
3608    )
3609}
3610
3611pub(crate) fn is_use_item(item: &ast::Item) -> bool {
3612    matches!(item.kind, ast::ItemKind::Use(_))
3613}
3614
3615pub(crate) fn is_extern_crate(item: &ast::Item) -> bool {
3616    matches!(item.kind, ast::ItemKind::ExternCrate(..))
3617}