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