Skip to main content

rustfmt_nightly/
types.rs

1use std::ops::Deref;
2
3use rustc_ast::ast::{self, FnRetTy, Mutability, Term};
4use rustc_span::{BytePos, Pos, Span, symbol::kw};
5use tracing::debug;
6
7use crate::comment::{combine_strs_with_missing_comments, contains_comment};
8use crate::config::lists::*;
9use crate::config::{IndentStyle, StyleEdition, TypeDensity};
10use crate::expr::{
11    ExprType, RhsAssignKind, format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix,
12};
13use crate::lists::{
14    ListFormatting, ListItem, Separator, definitive_tactic, itemize_list, write_list,
15};
16use crate::macros::{MacroPosition, rewrite_macro};
17use crate::overflow;
18use crate::pairs::{PairParts, rewrite_pair};
19use crate::patterns::rewrite_range_pat;
20use crate::rewrite::{Rewrite, RewriteContext, RewriteError, RewriteErrorExt, RewriteResult};
21use crate::shape::Shape;
22use crate::source_map::SpanUtils;
23use crate::spanned::Spanned;
24use crate::utils::{
25    colon_spaces, extra_offset, first_line_width, format_extern, format_mutability,
26    last_line_extendable, last_line_width, mk_sp, rewrite_ident,
27};
28
29#[derive(Copy, Clone, Debug, Eq, PartialEq)]
30pub(crate) enum PathContext {
31    Expr,
32    Type,
33    Import,
34}
35
36// Does not wrap on simple segments.
37pub(crate) fn rewrite_path(
38    context: &RewriteContext<'_>,
39    path_context: PathContext,
40    qself: &Option<Box<ast::QSelf>>,
41    path: &ast::Path,
42    shape: Shape,
43) -> RewriteResult {
44    let skip_count = qself.as_ref().map_or(0, |x| x.position);
45
46    // 32 covers almost all path lengths measured when compiling core, and there isn't a big
47    // downside from allocating slightly more than necessary.
48    let mut result = String::with_capacity(32);
49
50    if path.is_global() && qself.is_none() && path_context != PathContext::Import {
51        result.push_str("::");
52    }
53
54    let mut span_lo = path.span.lo();
55
56    if let Some(qself) = qself {
57        result.push('<');
58
59        let fmt_ty = qself.ty.rewrite_result(context, shape)?;
60        result.push_str(&fmt_ty);
61
62        if skip_count > 0 {
63            result.push_str(" as ");
64            if path.is_global() && path_context != PathContext::Import {
65                result.push_str("::");
66            }
67
68            // 3 = ">::".len()
69            let shape = shape.sub_width(3, path.span)?;
70
71            result = rewrite_path_segments(
72                PathContext::Type,
73                result,
74                path.segments.iter().take(skip_count),
75                span_lo,
76                path.span.hi(),
77                context,
78                shape,
79            )?;
80        }
81
82        result.push_str(">::");
83        span_lo = qself.ty.span.hi() + BytePos(1);
84    }
85
86    rewrite_path_segments(
87        path_context,
88        result,
89        path.segments.iter().skip(skip_count),
90        span_lo,
91        path.span.hi(),
92        context,
93        shape,
94    )
95}
96
97fn rewrite_path_segments<'a, I>(
98    path_context: PathContext,
99    mut buffer: String,
100    iter: I,
101    mut span_lo: BytePos,
102    span_hi: BytePos,
103    context: &RewriteContext<'_>,
104    shape: Shape,
105) -> RewriteResult
106where
107    I: Iterator<Item = &'a ast::PathSegment>,
108{
109    let mut first = true;
110    let shape = shape.visual_indent(0);
111
112    for segment in iter {
113        // Indicates a global path, shouldn't be rendered.
114        if segment.ident.name == kw::PathRoot {
115            continue;
116        }
117        if first {
118            first = false;
119        } else {
120            buffer.push_str("::");
121        }
122
123        let extra_offset = extra_offset(&buffer, shape);
124        let new_shape = shape.shrink_left(extra_offset, mk_sp(span_lo, span_hi))?;
125        let segment_string = rewrite_segment(
126            path_context,
127            segment,
128            &mut span_lo,
129            span_hi,
130            context,
131            new_shape,
132        )?;
133
134        buffer.push_str(&segment_string);
135    }
136
137    Ok(buffer)
138}
139
140#[derive(Debug)]
141pub(crate) enum SegmentParam<'a> {
142    Const(&'a ast::AnonConst),
143    LifeTime(&'a ast::Lifetime),
144    Type(&'a ast::Ty),
145    Binding(&'a ast::AssocItemConstraint),
146}
147
148impl<'a> SegmentParam<'a> {
149    fn from_generic_arg(arg: &ast::GenericArg) -> SegmentParam<'_> {
150        match arg {
151            ast::GenericArg::Lifetime(ref lt) => SegmentParam::LifeTime(lt),
152            ast::GenericArg::Type(ref ty) => SegmentParam::Type(ty),
153            ast::GenericArg::Const(const_) => SegmentParam::Const(const_),
154        }
155    }
156}
157
158impl<'a> Spanned for SegmentParam<'a> {
159    fn span(&self) -> Span {
160        match *self {
161            SegmentParam::Const(const_) => const_.value.span,
162            SegmentParam::LifeTime(lt) => lt.ident.span,
163            SegmentParam::Type(ty) => ty.span,
164            SegmentParam::Binding(binding) => binding.span,
165        }
166    }
167}
168
169impl<'a> Rewrite for SegmentParam<'a> {
170    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
171        self.rewrite_result(context, shape).ok()
172    }
173
174    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
175        match *self {
176            SegmentParam::Const(const_) => const_.rewrite_result(context, shape),
177            SegmentParam::LifeTime(lt) => lt.rewrite_result(context, shape),
178            SegmentParam::Type(ty) => ty.rewrite_result(context, shape),
179            SegmentParam::Binding(atc) => atc.rewrite_result(context, shape),
180        }
181    }
182}
183
184impl Rewrite for ast::PreciseCapturingArg {
185    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
186        self.rewrite_result(context, shape).ok()
187    }
188
189    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
190        match self {
191            ast::PreciseCapturingArg::Lifetime(lt) => lt.rewrite_result(context, shape),
192            ast::PreciseCapturingArg::Arg(p, _) => {
193                rewrite_path(context, PathContext::Type, &None, p, shape)
194            }
195        }
196    }
197}
198
199impl Rewrite for ast::AssocItemConstraint {
200    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
201        self.rewrite_result(context, shape).ok()
202    }
203
204    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
205        use ast::AssocItemConstraintKind::{Bound, Equality};
206
207        let mut result = String::with_capacity(128);
208        result.push_str(rewrite_ident(context, self.ident));
209
210        if let Some(ref gen_args) = self.gen_args {
211            let budget = shape
212                .width
213                .checked_sub(result.len())
214                .max_width_error(shape.width, self.span)?;
215            let shape = Shape::legacy(budget, shape.indent + result.len());
216            let gen_str = rewrite_generic_args(gen_args, context, shape, gen_args.span())?;
217            result.push_str(&gen_str);
218        }
219
220        let infix = match (&self.kind, context.config.type_punctuation_density()) {
221            (Bound { .. }, _) => ": ",
222            (Equality { .. }, TypeDensity::Wide) => " = ",
223            (Equality { .. }, TypeDensity::Compressed) => "=",
224        };
225        result.push_str(infix);
226
227        let budget = shape
228            .width
229            .checked_sub(result.len())
230            .max_width_error(shape.width, self.span)?;
231        let shape = Shape::legacy(budget, shape.indent + result.len());
232        let rewrite = self.kind.rewrite_result(context, shape)?;
233        result.push_str(&rewrite);
234
235        Ok(result)
236    }
237}
238
239impl Rewrite for ast::AssocItemConstraintKind {
240    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
241        self.rewrite_result(context, shape).ok()
242    }
243
244    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
245        match self {
246            ast::AssocItemConstraintKind::Equality { term } => match term {
247                Term::Ty(ty) => ty.rewrite_result(context, shape),
248                Term::Const(c) => c.rewrite_result(context, shape),
249            },
250            ast::AssocItemConstraintKind::Bound { bounds } => bounds.rewrite_result(context, shape),
251        }
252    }
253}
254
255// Formats a path segment. There are some hacks involved to correctly determine
256// the segment's associated span since it's not part of the AST.
257//
258// The span_lo is assumed to be greater than the end of any previous segment's
259// parameters and lesser or equal than the start of current segment.
260//
261// span_hi is assumed equal to the end of the entire path.
262//
263// When the segment contains a positive number of parameters, we update span_lo
264// so that invariants described above will hold for the next segment.
265fn rewrite_segment(
266    path_context: PathContext,
267    segment: &ast::PathSegment,
268    span_lo: &mut BytePos,
269    span_hi: BytePos,
270    context: &RewriteContext<'_>,
271    shape: Shape,
272) -> RewriteResult {
273    let mut result = String::with_capacity(128);
274    result.push_str(rewrite_ident(context, segment.ident));
275
276    let ident_len = result.len();
277    let span = mk_sp(*span_lo, span_hi);
278    let shape = if context.use_block_indent() {
279        shape.offset_left(ident_len, span)?
280    } else {
281        shape.shrink_left(ident_len, span)?
282    };
283
284    if let Some(ref args) = segment.args {
285        let generics_str = rewrite_generic_args(args, context, shape, mk_sp(*span_lo, span_hi))?;
286        match **args {
287            ast::GenericArgs::AngleBracketed(ref data) if !data.args.is_empty() => {
288                // HACK: squeeze out the span between the identifier and the parameters.
289                // The hack is required so that we don't remove the separator inside macro calls.
290                // This does not work in the presence of comment, hoping that people are
291                // sane about where to put their comment.
292                let separator_snippet = context
293                    .snippet(mk_sp(segment.ident.span.hi(), data.span.lo()))
294                    .trim();
295                let force_separator = context.inside_macro() && separator_snippet.starts_with("::");
296                let separator = if path_context == PathContext::Expr || force_separator {
297                    "::"
298                } else {
299                    ""
300                };
301                result.push_str(separator);
302
303                // Update position of last bracket.
304                *span_lo = context
305                    .snippet_provider
306                    .span_after(mk_sp(*span_lo, span_hi), "<");
307            }
308            _ => (),
309        }
310        result.push_str(&generics_str)
311    }
312
313    Ok(result)
314}
315
316fn format_function_type<'a, I>(
317    inputs: I,
318    output: &FnRetTy,
319    variadic: bool,
320    span: Span,
321    context: &RewriteContext<'_>,
322    shape: Shape,
323) -> RewriteResult
324where
325    I: ExactSizeIterator,
326    <I as Iterator>::Item: Deref,
327    <I::Item as Deref>::Target: Rewrite + Spanned + 'a,
328{
329    debug!("format_function_type {:#?}", shape);
330
331    let ty_shape = match context.config.indent_style() {
332        // 4 = " -> "
333        IndentStyle::Block => shape.offset_left(4, span)?,
334        IndentStyle::Visual => shape.block_left(4, span)?,
335    };
336    let output = match *output {
337        FnRetTy::Ty(ref ty) => {
338            let type_str = ty.rewrite_result(context, ty_shape)?;
339            format!(" -> {type_str}")
340        }
341        FnRetTy::Default(..) => String::new(),
342    };
343
344    let list_shape = if context.use_block_indent() {
345        Shape::indented(
346            shape.block().indent.block_indent(context.config),
347            context.config,
348        )
349    } else {
350        // 2 for ()
351        let budget = shape
352            .width
353            .checked_sub(2)
354            .max_width_error(shape.width, span)?;
355        // 1 for (
356        let offset = shape.indent + 1;
357        Shape::legacy(budget, offset)
358    };
359
360    let is_inputs_empty = inputs.len() == 0;
361    let list_lo = context.snippet_provider.span_after(span, "(");
362    let (list_str, tactic) = if is_inputs_empty {
363        let tactic = get_tactics(&[], &output, shape);
364        let list_hi = context.snippet_provider.span_before(span, ")");
365        let comment = context
366            .snippet_provider
367            .span_to_snippet(mk_sp(list_lo, list_hi))
368            .unknown_error()?
369            .trim();
370        let comment = if comment.starts_with("//") {
371            format!(
372                "{}{}{}",
373                &list_shape.indent.to_string_with_newline(context.config),
374                comment,
375                &shape.block().indent.to_string_with_newline(context.config)
376            )
377        } else {
378            comment.to_string()
379        };
380        (comment, tactic)
381    } else {
382        let items = itemize_list(
383            context.snippet_provider,
384            inputs,
385            ")",
386            ",",
387            |arg| arg.span().lo(),
388            |arg| arg.span().hi(),
389            |arg| arg.rewrite_result(context, list_shape),
390            list_lo,
391            span.hi(),
392            false,
393        );
394
395        let item_vec: Vec<_> = items.collect();
396        let tactic = get_tactics(&item_vec, &output, shape);
397        let trailing_separator = if !context.use_block_indent() || variadic {
398            SeparatorTactic::Never
399        } else {
400            context.config.trailing_comma()
401        };
402
403        let fmt = ListFormatting::new(list_shape, context.config)
404            .tactic(tactic)
405            .trailing_separator(trailing_separator)
406            .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
407            .preserve_newline(true);
408        (write_list(&item_vec, &fmt)?, tactic)
409    };
410
411    let args = if tactic == DefinitiveListTactic::Horizontal
412        || !context.use_block_indent()
413        || is_inputs_empty
414    {
415        format!("({list_str})")
416    } else {
417        format!(
418            "({}{}{})",
419            list_shape.indent.to_string_with_newline(context.config),
420            list_str,
421            shape.block().indent.to_string_with_newline(context.config),
422        )
423    };
424    if output.is_empty() || last_line_width(&args) + first_line_width(&output) <= shape.width {
425        Ok(format!("{args}{output}"))
426    } else {
427        Ok(format!(
428            "{}\n{}{}",
429            args,
430            list_shape.indent.to_string(context.config),
431            output.trim_start()
432        ))
433    }
434}
435
436fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
437    colon_spaces(context.config)
438}
439
440// If the return type is multi-lined, then force to use multiple lines for
441// arguments as well.
442fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveListTactic {
443    if output.contains('\n') {
444        DefinitiveListTactic::Vertical
445    } else {
446        definitive_tactic(
447            item_vec,
448            ListTactic::HorizontalVertical,
449            Separator::Comma,
450            // 2 is for the case of ',\n'
451            shape.width.saturating_sub(2 + output.len()),
452        )
453    }
454}
455
456impl Rewrite for ast::WherePredicate {
457    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
458        self.rewrite_result(context, shape).ok()
459    }
460
461    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
462        let attrs_str = self.attrs.rewrite_result(context, shape)?;
463        // FIXME: dead spans?
464        let pred_str = &match self.kind {
465            ast::WherePredicateKind::BoundPredicate(ast::WhereBoundPredicate {
466                ref bound_generic_params,
467                ref bounded_ty,
468                ref bounds,
469                ..
470            }) => {
471                let type_str = bounded_ty.rewrite_result(context, shape)?;
472                let colon = type_bound_colon(context).trim_end();
473                let lhs = if let Some(binder_str) =
474                    rewrite_bound_params(context, shape, bound_generic_params)
475                {
476                    format!("for<{binder_str}> {type_str}{colon}")
477                } else {
478                    format!("{type_str}{colon}")
479                };
480
481                rewrite_assign_rhs(context, lhs, bounds, &RhsAssignKind::Bounds, shape)?
482            }
483            ast::WherePredicateKind::RegionPredicate(ast::WhereRegionPredicate {
484                ref lifetime,
485                ref bounds,
486            }) => rewrite_bounded_lifetime(lifetime, bounds, self.span, context, shape)?,
487            ast::WherePredicateKind::EqPredicate(ast::WhereEqPredicate {
488                ref lhs_ty,
489                ref rhs_ty,
490                ..
491            }) => {
492                let lhs_ty_str = lhs_ty
493                    .rewrite_result(context, shape)
494                    .map(|lhs| lhs + " =")?;
495                rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, &RhsAssignKind::Ty, shape)?
496            }
497        };
498
499        let mut result = String::with_capacity(attrs_str.len() + pred_str.len() + 1);
500        result.push_str(&attrs_str);
501        let pred_start = self.span.lo();
502        let line_len = last_line_width(&attrs_str) + 1 + first_line_width(&pred_str);
503        if let Some(last_attr) = self.attrs.last().filter(|last_attr| {
504            contains_comment(context.snippet(mk_sp(last_attr.span.hi(), pred_start)))
505        }) {
506            result = combine_strs_with_missing_comments(
507                context,
508                &result,
509                &pred_str,
510                mk_sp(last_attr.span.hi(), pred_start),
511                Shape {
512                    width: shape.width.min(context.config.inline_attribute_width()),
513                    ..shape
514                },
515                !last_attr.is_doc_comment(),
516            )?;
517        } else {
518            if !self.attrs.is_empty() {
519                if context.config.inline_attribute_width() < line_len
520                    || self.attrs.len() > 1
521                    || self.attrs.last().is_some_and(|a| a.is_doc_comment())
522                {
523                    result.push_str(&shape.indent.to_string_with_newline(context.config));
524                } else {
525                    result.push(' ');
526                }
527            }
528            result.push_str(&pred_str);
529        }
530
531        Ok(result)
532    }
533}
534
535impl Rewrite for ast::GenericArg {
536    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
537        self.rewrite_result(context, shape).ok()
538    }
539
540    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
541        match *self {
542            ast::GenericArg::Lifetime(ref lt) => lt.rewrite_result(context, shape),
543            ast::GenericArg::Type(ref ty) => ty.rewrite_result(context, shape),
544            ast::GenericArg::Const(ref const_) => const_.rewrite_result(context, shape),
545        }
546    }
547}
548
549fn rewrite_generic_args(
550    gen_args: &ast::GenericArgs,
551    context: &RewriteContext<'_>,
552    shape: Shape,
553    span: Span,
554) -> RewriteResult {
555    match gen_args {
556        ast::GenericArgs::AngleBracketed(ref data) => {
557            if data.args.is_empty() {
558                Ok("".to_owned())
559            } else {
560                let args = data
561                    .args
562                    .iter()
563                    .map(|x| match x {
564                        ast::AngleBracketedArg::Arg(generic_arg) => {
565                            SegmentParam::from_generic_arg(generic_arg)
566                        }
567                        ast::AngleBracketedArg::Constraint(constraint) => {
568                            SegmentParam::Binding(constraint)
569                        }
570                    })
571                    .collect::<Vec<_>>();
572
573                overflow::rewrite_with_angle_brackets(context, "", args.iter(), shape, span)
574            }
575        }
576        ast::GenericArgs::Parenthesized(ref data) => format_function_type(
577            data.inputs.iter().map(|x| &**x),
578            &data.output,
579            false,
580            data.span,
581            context,
582            shape,
583        ),
584        ast::GenericArgs::ParenthesizedElided(..) => Ok("(..)".to_owned()),
585    }
586}
587
588fn rewrite_bounded_lifetime(
589    lt: &ast::Lifetime,
590    bounds: &[ast::GenericBound],
591    span: Span,
592    context: &RewriteContext<'_>,
593    shape: Shape,
594) -> RewriteResult {
595    let result = lt.rewrite_result(context, shape)?;
596
597    if bounds.is_empty() {
598        Ok(result)
599    } else {
600        let colon = type_bound_colon(context);
601        let overhead = last_line_width(&result) + colon.len();
602        let shape = shape.sub_width(overhead, span)?;
603        let result = format!(
604            "{}{}{}",
605            result,
606            colon,
607            join_bounds(context, shape, bounds, true)?
608        );
609        Ok(result)
610    }
611}
612
613impl Rewrite for ast::AnonConst {
614    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
615        self.rewrite_result(context, shape).ok()
616    }
617
618    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
619        format_expr(&self.value, ExprType::SubExpression, context, shape)
620    }
621}
622
623impl Rewrite for ast::Lifetime {
624    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
625        self.rewrite_result(context, shape).ok()
626    }
627
628    fn rewrite_result(&self, context: &RewriteContext<'_>, _: Shape) -> RewriteResult {
629        Ok(context.snippet(self.ident.span).to_owned())
630    }
631}
632
633impl Rewrite for ast::GenericBound {
634    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
635        self.rewrite_result(context, shape).ok()
636    }
637
638    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
639        match *self {
640            ast::GenericBound::Trait(ref poly_trait_ref) => {
641                let snippet = context.snippet(self.span());
642                let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
643                poly_trait_ref
644                    .rewrite_result(context, shape)
645                    .map(|s| if has_paren { format!("({})", s) } else { s })
646            }
647            ast::GenericBound::Use(ref args, span) => {
648                overflow::rewrite_with_angle_brackets(context, "use", args.iter(), shape, span)
649            }
650            ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite_result(context, shape),
651        }
652    }
653}
654
655impl Rewrite for ast::GenericBounds {
656    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
657        self.rewrite_result(context, shape).ok()
658    }
659
660    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
661        if self.is_empty() {
662            return Ok(String::new());
663        }
664
665        join_bounds(context, shape, self, true)
666    }
667}
668
669impl Rewrite for ast::GenericParam {
670    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
671        self.rewrite_result(context, shape).ok()
672    }
673
674    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
675        // FIXME: If there are more than one attributes, this will force multiline.
676        let mut result = self
677            .attrs
678            .rewrite_result(context, shape)
679            .unwrap_or(String::new());
680        let has_attrs = !result.is_empty();
681
682        let mut param = String::with_capacity(128);
683
684        let param_start = if let ast::GenericParamKind::Const {
685            ref ty,
686            span,
687            default,
688        } = &self.kind
689        {
690            param.push_str("const ");
691            param.push_str(rewrite_ident(context, self.ident));
692            param.push_str(": ");
693            param.push_str(&ty.rewrite_result(context, shape)?);
694            if let Some(default) = default {
695                let eq_str = match context.config.type_punctuation_density() {
696                    TypeDensity::Compressed => "=",
697                    TypeDensity::Wide => " = ",
698                };
699                param.push_str(eq_str);
700                let budget = shape
701                    .width
702                    .checked_sub(param.len())
703                    .max_width_error(shape.width, self.span())?;
704                let rewrite =
705                    default.rewrite_result(context, Shape::legacy(budget, shape.indent))?;
706                param.push_str(&rewrite);
707            }
708            span.lo()
709        } else {
710            param.push_str(rewrite_ident(context, self.ident));
711            self.ident.span.lo()
712        };
713
714        if !self.bounds.is_empty() {
715            param.push_str(type_bound_colon(context));
716            param.push_str(&self.bounds.rewrite_result(context, shape)?)
717        }
718        if let ast::GenericParamKind::Type {
719            default: Some(ref def),
720        } = self.kind
721        {
722            let eq_str = match context.config.type_punctuation_density() {
723                TypeDensity::Compressed => "=",
724                TypeDensity::Wide => " = ",
725            };
726            param.push_str(eq_str);
727            let budget = shape
728                .width
729                .checked_sub(param.len())
730                .max_width_error(shape.width, self.span())?;
731            let rewrite =
732                def.rewrite_result(context, Shape::legacy(budget, shape.indent + param.len()))?;
733            param.push_str(&rewrite);
734        }
735
736        if let Some(last_attr) = self.attrs.last().filter(|last_attr| {
737            contains_comment(context.snippet(mk_sp(last_attr.span.hi(), param_start)))
738        }) {
739            result = combine_strs_with_missing_comments(
740                context,
741                &result,
742                &param,
743                mk_sp(last_attr.span.hi(), param_start),
744                shape,
745                !last_attr.is_doc_comment(),
746            )?;
747        } else {
748            // When rewriting generic params, an extra newline should be put
749            // if the attributes end with a doc comment
750            if let Some(true) = self.attrs.last().map(|a| a.is_doc_comment()) {
751                result.push_str(&shape.indent.to_string_with_newline(context.config));
752            } else if has_attrs {
753                result.push(' ');
754            }
755            result.push_str(&param);
756        }
757
758        Ok(result)
759    }
760}
761
762impl Rewrite for ast::PolyTraitRef {
763    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
764        self.rewrite_result(context, shape).ok()
765    }
766
767    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
768        let (binder, shape) = if let Some(lifetime_str) =
769            rewrite_bound_params(context, shape, &self.bound_generic_params)
770        {
771            // 6 is "for<> ".len()
772            let extra_offset = lifetime_str.len() + 6;
773            let shape = shape.offset_left(extra_offset, self.span)?;
774            (format!("for<{lifetime_str}> "), shape)
775        } else {
776            (String::new(), shape)
777        };
778
779        let ast::TraitBoundModifiers {
780            constness,
781            asyncness,
782            polarity,
783        } = self.modifiers;
784        let mut constness = constness.as_str().to_string();
785        if !constness.is_empty() {
786            constness.push(' ');
787        }
788        let mut asyncness = asyncness.as_str().to_string();
789        if !asyncness.is_empty() {
790            asyncness.push(' ');
791        }
792        let polarity = polarity.as_str();
793        let shape = shape.offset_left(constness.len() + polarity.len(), self.span)?;
794
795        let path_str = self.trait_ref.rewrite_result(context, shape)?;
796        Ok(format!(
797            "{binder}{constness}{asyncness}{polarity}{path_str}"
798        ))
799    }
800}
801
802impl Rewrite for ast::TraitRef {
803    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
804        self.rewrite_result(context, shape).ok()
805    }
806
807    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
808        rewrite_path(context, PathContext::Type, &None, &self.path, shape)
809    }
810}
811
812impl Rewrite for ast::Ty {
813    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
814        self.rewrite_result(context, shape).ok()
815    }
816
817    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
818        match self.kind {
819            ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
820                // we have to consider 'dyn' keyword is used or not!!!
821                let (shape, prefix) = match tobj_syntax {
822                    ast::TraitObjectSyntax::Dyn => {
823                        let shape = shape.offset_left(4, self.span())?;
824                        (shape, "dyn ")
825                    }
826                    ast::TraitObjectSyntax::None => (shape, ""),
827                };
828                let mut res = bounds.rewrite_result(context, shape)?;
829                // We may have falsely removed a trailing `+` inside macro call.
830                if context.inside_macro()
831                    && bounds.len() == 1
832                    && context.snippet(self.span).ends_with('+')
833                    && !res.ends_with('+')
834                {
835                    res.push('+');
836                }
837                Ok(format!("{prefix}{res}"))
838            }
839            ast::TyKind::Ptr(ref mt) => {
840                let prefix = match mt.mutbl {
841                    Mutability::Mut => "*mut ",
842                    Mutability::Not => "*const ",
843                };
844
845                rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
846            }
847            ast::TyKind::Ref(ref lifetime, ref mt)
848            | ast::TyKind::PinnedRef(ref lifetime, ref mt) => {
849                let mut_str = format_mutability(mt.mutbl);
850                let mut_len = mut_str.len();
851                let mut result = String::with_capacity(128);
852                result.push('&');
853                let ref_hi = context.snippet_provider.span_after(self.span(), "&");
854                let mut cmnt_lo = ref_hi;
855
856                if let Some(ref lifetime) = *lifetime {
857                    let lt_budget = shape
858                        .width
859                        .checked_sub(2 + mut_len)
860                        .max_width_error(shape.width, self.span())?;
861                    let lt_str = lifetime.rewrite_result(
862                        context,
863                        Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
864                    )?;
865                    let before_lt_span = mk_sp(cmnt_lo, lifetime.ident.span.lo());
866                    if contains_comment(context.snippet(before_lt_span)) {
867                        result = combine_strs_with_missing_comments(
868                            context,
869                            &result,
870                            &lt_str,
871                            before_lt_span,
872                            shape,
873                            true,
874                        )?;
875                    } else {
876                        result.push_str(&lt_str);
877                    }
878                    result.push(' ');
879                    cmnt_lo = lifetime.ident.span.hi();
880                }
881
882                if let ast::TyKind::PinnedRef(..) = self.kind {
883                    result.push_str("pin ");
884                    if ast::Mutability::Not == mt.mutbl {
885                        result.push_str("const ");
886                    }
887                }
888
889                if ast::Mutability::Mut == mt.mutbl {
890                    let mut_hi = context.snippet_provider.span_after(self.span(), "mut");
891                    let before_mut_span = mk_sp(cmnt_lo, mut_hi - BytePos::from_usize(3));
892                    if contains_comment(context.snippet(before_mut_span)) {
893                        result = combine_strs_with_missing_comments(
894                            context,
895                            result.trim_end(),
896                            mut_str,
897                            before_mut_span,
898                            shape,
899                            true,
900                        )?;
901                    } else {
902                        result.push_str(mut_str);
903                    }
904                    cmnt_lo = mut_hi;
905                }
906
907                let before_ty_span = mk_sp(cmnt_lo, mt.ty.span.lo());
908                if contains_comment(context.snippet(before_ty_span)) {
909                    result = combine_strs_with_missing_comments(
910                        context,
911                        result.trim_end(),
912                        &mt.ty.rewrite_result(context, shape)?,
913                        before_ty_span,
914                        shape,
915                        true,
916                    )?;
917                } else {
918                    let used_width = last_line_width(&result);
919                    let budget = shape
920                        .width
921                        .checked_sub(used_width)
922                        .max_width_error(shape.width, self.span())?;
923                    let ty_str = mt.ty.rewrite_result(
924                        context,
925                        Shape::legacy(budget, shape.indent + used_width),
926                    )?;
927                    result.push_str(&ty_str);
928                }
929
930                Ok(result)
931            }
932            // FIXME: we drop any comments here, even though it's a silly place to put
933            // comments.
934            ast::TyKind::Paren(ref ty) => {
935                if context.config.style_edition() <= StyleEdition::Edition2021
936                    || context.config.indent_style() == IndentStyle::Visual
937                {
938                    let budget = shape
939                        .width
940                        .checked_sub(2)
941                        .max_width_error(shape.width, self.span())?;
942                    return ty
943                        .rewrite_result(context, Shape::legacy(budget, shape.indent + 1))
944                        .map(|ty_str| format!("({})", ty_str));
945                }
946
947                // 2 = ()
948                if let Some(sh) = shape.sub_width_opt(2) {
949                    if let Ok(ref s) = ty.rewrite_result(context, sh) {
950                        if !s.contains('\n') {
951                            return Ok(format!("({s})"));
952                        }
953                    }
954                }
955
956                let indent_str = shape.indent.to_string_with_newline(context.config);
957                let shape = shape
958                    .block_indent(context.config.tab_spaces())
959                    .with_max_width(context.config);
960                let rw = ty.rewrite_result(context, shape)?;
961                Ok(format!(
962                    "({}{}{})",
963                    shape.to_string_with_newline(context.config),
964                    rw,
965                    indent_str
966                ))
967            }
968            ast::TyKind::Slice(ref ty) => {
969                let budget = shape
970                    .width
971                    .checked_sub(4)
972                    .max_width_error(shape.width, self.span())?;
973                ty.rewrite_result(context, Shape::legacy(budget, shape.indent + 1))
974                    .map(|ty_str| format!("[{}]", ty_str))
975            }
976            ast::TyKind::Tup(ref items) => {
977                rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
978            }
979            ast::TyKind::Path(ref q_self, ref path) => {
980                rewrite_path(context, PathContext::Type, q_self, path, shape)
981            }
982            ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
983                &**ty,
984                &*repeats.value,
985                PairParts::new("[", "; ", "]"),
986                context,
987                shape,
988                SeparatorPlace::Back,
989            ),
990            ast::TyKind::Infer => {
991                if shape.width >= 1 {
992                    Ok("_".to_owned())
993                } else {
994                    Err(RewriteError::ExceedsMaxWidth {
995                        configured_width: shape.width,
996                        span: self.span(),
997                    })
998                }
999            }
1000            ast::TyKind::FnPtr(ref fn_ptr) => rewrite_fn_ptr(fn_ptr, self.span, context, shape),
1001            ast::TyKind::Never => Ok(String::from("!")),
1002            ast::TyKind::MacCall(ref mac) => {
1003                rewrite_macro(mac, context, shape, MacroPosition::Expression)
1004            }
1005            ast::TyKind::ImplicitSelf => Ok(String::from("")),
1006            ast::TyKind::ImplTrait(_, ref it) => {
1007                // Empty trait is not a parser error.
1008                if it.is_empty() {
1009                    return Ok("impl".to_owned());
1010                }
1011                let rw = if context.config.style_edition() <= StyleEdition::Edition2021 {
1012                    it.rewrite_result(context, shape)
1013                } else if context.config.style_edition() == StyleEdition::Edition2024 {
1014                    join_bounds(context, shape, it, false)
1015                } else {
1016                    let offset = "impl ".len();
1017                    let shape = shape.offset_left(offset, self.span())?;
1018                    join_bounds(context, shape, it, false)
1019                };
1020
1021                rw.map(|it_str| {
1022                    let space = if it_str.is_empty() { "" } else { " " };
1023                    format!("impl{}{}", space, it_str)
1024                })
1025            }
1026            ast::TyKind::CVarArgs => Ok("...".to_owned()),
1027            ast::TyKind::Dummy | ast::TyKind::Err(_) => Ok(context.snippet(self.span).to_owned()),
1028            ast::TyKind::Pat(ref ty, ref pat) => {
1029                let ty = ty.rewrite_result(context, shape)?;
1030                let pat = pat.rewrite_result(context, shape)?;
1031                Ok(format!("{ty} is {pat}"))
1032            }
1033            ast::TyKind::FieldOf(ref ty, ref variant, ref field) => {
1034                let ty = ty.rewrite_result(context, shape)?;
1035                if let Some(variant) = variant {
1036                    Ok(format!("builtin # field_of({ty}, {variant}.{field})"))
1037                } else {
1038                    Ok(format!("builtin # field_of({ty}, {field})"))
1039                }
1040            }
1041            ast::TyKind::UnsafeBinder(ref binder) => {
1042                let mut result = String::new();
1043                if binder.generic_params.is_empty() {
1044                    // We always want to write `unsafe<>` since `unsafe<> Ty`
1045                    // and `Ty` are distinct types.
1046                    result.push_str("unsafe<> ")
1047                } else if let Some(ref lifetime_str) =
1048                    rewrite_bound_params(context, shape, &binder.generic_params)
1049                {
1050                    result.push_str("unsafe<");
1051                    result.push_str(lifetime_str);
1052                    result.push_str("> ");
1053                }
1054
1055                let inner_ty_shape = if context.use_block_indent() {
1056                    shape.offset_left(result.len(), self.span())?
1057                } else {
1058                    shape
1059                        .visual_indent(result.len())
1060                        .sub_width(result.len(), self.span())?
1061                };
1062
1063                let rewrite = binder.inner_ty.rewrite_result(context, inner_ty_shape)?;
1064                result.push_str(&rewrite);
1065                Ok(result)
1066            }
1067        }
1068    }
1069}
1070
1071impl Rewrite for ast::TyPat {
1072    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1073        self.rewrite_result(context, shape).ok()
1074    }
1075
1076    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1077        match self.kind {
1078            ast::TyPatKind::Range(ref lhs, ref rhs, ref end_kind) => {
1079                rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
1080            }
1081            ast::TyPatKind::Or(ref variants) => {
1082                let mut first = true;
1083                let mut s = String::new();
1084                for variant in variants {
1085                    if first {
1086                        first = false
1087                    } else {
1088                        s.push_str(" | ");
1089                    }
1090                    s.push_str(&variant.rewrite_result(context, shape)?);
1091                }
1092                Ok(s)
1093            }
1094            ast::TyPatKind::NotNull | ast::TyPatKind::Err(_) => Err(RewriteError::Unknown),
1095        }
1096    }
1097}
1098
1099fn rewrite_fn_ptr(
1100    fn_ptr: &ast::FnPtrTy,
1101    span: Span,
1102    context: &RewriteContext<'_>,
1103    shape: Shape,
1104) -> RewriteResult {
1105    debug!("rewrite_bare_fn {:#?}", shape);
1106
1107    let mut result = String::with_capacity(128);
1108
1109    if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &fn_ptr.generic_params) {
1110        result.push_str("for<");
1111        // 6 = "for<> ".len(), 4 = "for<".
1112        // This doesn't work out so nicely for multiline situation with lots of
1113        // rightward drift. If that is a problem, we could use the list stuff.
1114        result.push_str(lifetime_str);
1115        result.push_str("> ");
1116    }
1117
1118    result.push_str(crate::utils::format_safety(fn_ptr.safety));
1119
1120    result.push_str(&format_extern(
1121        fn_ptr.ext,
1122        context.config.force_explicit_abi(),
1123    ));
1124
1125    result.push_str("fn");
1126
1127    let func_ty_shape = if context.use_block_indent() {
1128        shape.offset_left(result.len(), span)?
1129    } else {
1130        shape
1131            .visual_indent(result.len())
1132            .sub_width(result.len(), span)?
1133    };
1134
1135    let rewrite = format_function_type(
1136        fn_ptr.decl.inputs.iter(),
1137        &fn_ptr.decl.output,
1138        fn_ptr.decl.c_variadic(),
1139        span,
1140        context,
1141        func_ty_shape,
1142    )?;
1143
1144    result.push_str(&rewrite);
1145
1146    Ok(result)
1147}
1148
1149fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
1150    let is_trait = |b: &ast::GenericBound| match b {
1151        ast::GenericBound::Outlives(..) => false,
1152        ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => true,
1153    };
1154    let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
1155    let last_trait_index = generic_bounds.iter().rposition(is_trait);
1156    let first_lifetime_index = generic_bounds.iter().position(is_lifetime);
1157    match (last_trait_index, first_lifetime_index) {
1158        (Some(last_trait_index), Some(first_lifetime_index)) => {
1159            last_trait_index < first_lifetime_index
1160        }
1161        _ => true,
1162    }
1163}
1164
1165fn join_bounds(
1166    context: &RewriteContext<'_>,
1167    shape: Shape,
1168    items: &[ast::GenericBound],
1169    need_indent: bool,
1170) -> RewriteResult {
1171    join_bounds_inner(context, shape, items, need_indent, false)
1172}
1173
1174fn join_bounds_inner(
1175    context: &RewriteContext<'_>,
1176    shape: Shape,
1177    items: &[ast::GenericBound],
1178    need_indent: bool,
1179    force_newline: bool,
1180) -> RewriteResult {
1181    debug_assert!(!items.is_empty());
1182
1183    let generic_bounds_in_order = is_generic_bounds_in_order(items);
1184    let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
1185        ast::GenericBound::Outlives(..) => true,
1186        // We treat `use<>` like a trait bound here.
1187        ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => last_line_extendable(s),
1188    };
1189
1190    // Whether a GenericBound item is a PathSegment segment that includes internal array
1191    // that contains more than one item
1192    let is_item_with_multi_items_array = |item: &ast::GenericBound| match item {
1193        ast::GenericBound::Trait(ref poly_trait_ref, ..) => {
1194            let segments = &poly_trait_ref.trait_ref.path.segments;
1195            if segments.len() > 1 {
1196                true
1197            } else {
1198                if let Some(args_in) = &segments[0].args {
1199                    matches!(
1200                        args_in.deref(),
1201                        ast::GenericArgs::AngleBracketed(bracket_args)
1202                            if bracket_args.args.len() > 1
1203                    )
1204                } else {
1205                    false
1206                }
1207            }
1208        }
1209        ast::GenericBound::Use(args, _) => args.len() > 1,
1210        _ => false,
1211    };
1212
1213    let result = items.iter().enumerate().try_fold(
1214        (String::new(), None, false),
1215        |(strs, prev_trailing_span, prev_extendable), (i, item)| {
1216            let trailing_span = if i < items.len() - 1 {
1217                let hi = context
1218                    .snippet_provider
1219                    .span_before(mk_sp(items[i + 1].span().lo(), item.span().hi()), "+");
1220
1221                Some(mk_sp(item.span().hi(), hi))
1222            } else {
1223                None
1224            };
1225            let (leading_span, has_leading_comment) = if i > 0 {
1226                let lo = context
1227                    .snippet_provider
1228                    .span_after(mk_sp(items[i - 1].span().hi(), item.span().lo()), "+");
1229
1230                let span = mk_sp(lo, item.span().lo());
1231
1232                let has_comments = contains_comment(context.snippet(span));
1233
1234                (Some(mk_sp(lo, item.span().lo())), has_comments)
1235            } else {
1236                (None, false)
1237            };
1238            let prev_has_trailing_comment = match prev_trailing_span {
1239                Some(ts) => contains_comment(context.snippet(ts)),
1240                _ => false,
1241            };
1242
1243            let shape = if need_indent && force_newline {
1244                shape
1245                    .block_indent(context.config.tab_spaces())
1246                    .with_max_width(context.config)
1247            } else {
1248                shape
1249            };
1250            let whitespace = if force_newline && (!prev_extendable || !generic_bounds_in_order) {
1251                shape
1252                    .indent
1253                    .to_string_with_newline(context.config)
1254                    .to_string()
1255            } else {
1256                String::from(" ")
1257            };
1258
1259            let joiner = match context.config.type_punctuation_density() {
1260                TypeDensity::Compressed => String::from("+"),
1261                TypeDensity::Wide => whitespace + "+ ",
1262            };
1263            let joiner = if has_leading_comment {
1264                joiner.trim_end()
1265            } else {
1266                &joiner
1267            };
1268            let joiner = if prev_has_trailing_comment {
1269                joiner.trim_start()
1270            } else {
1271                joiner
1272            };
1273
1274            let (extendable, trailing_str) = if i == 0 {
1275                let bound_str = item.rewrite_result(context, shape)?;
1276                (is_bound_extendable(&bound_str, item), bound_str)
1277            } else {
1278                let bound_str = &item.rewrite_result(context, shape)?;
1279                match leading_span {
1280                    Some(ls) if has_leading_comment => (
1281                        is_bound_extendable(bound_str, item),
1282                        combine_strs_with_missing_comments(
1283                            context, joiner, bound_str, ls, shape, true,
1284                        )?,
1285                    ),
1286                    _ => (
1287                        is_bound_extendable(bound_str, item),
1288                        String::from(joiner) + bound_str,
1289                    ),
1290                }
1291            };
1292            match prev_trailing_span {
1293                Some(ts) if prev_has_trailing_comment => combine_strs_with_missing_comments(
1294                    context,
1295                    &strs,
1296                    &trailing_str,
1297                    ts,
1298                    shape,
1299                    true,
1300                )
1301                .map(|v| (v, trailing_span, extendable)),
1302                _ => Ok((strs + &trailing_str, trailing_span, extendable)),
1303            }
1304        },
1305    )?;
1306
1307    // Whether to retry with a forced newline:
1308    //   Only if result is not already multiline and did not exceed line width,
1309    //   and either there is more than one item;
1310    //       or the single item is of type `Trait`,
1311    //          and any of the internal arrays contains more than one item;
1312    let retry_with_force_newline = match context.config.style_edition() {
1313        style_edition @ _ if style_edition <= StyleEdition::Edition2021 => {
1314            !force_newline
1315                && items.len() > 1
1316                && (result.0.contains('\n') || result.0.len() > shape.width)
1317        }
1318        _ if force_newline => false,
1319        _ if (!result.0.contains('\n') && result.0.len() <= shape.width) => false,
1320        _ if items.len() > 1 => true,
1321        _ => is_item_with_multi_items_array(&items[0]),
1322    };
1323
1324    if retry_with_force_newline {
1325        join_bounds_inner(context, shape, items, need_indent, true)
1326    } else {
1327        Ok(result.0)
1328    }
1329}
1330
1331pub(crate) fn opaque_ty(ty: &Option<Box<ast::Ty>>) -> Option<&ast::GenericBounds> {
1332    ty.as_ref().and_then(|t| match &t.kind {
1333        ast::TyKind::ImplTrait(_, bounds) => Some(bounds),
1334        _ => None,
1335    })
1336}
1337
1338pub(crate) fn can_be_overflowed_type(
1339    context: &RewriteContext<'_>,
1340    ty: &ast::Ty,
1341    len: usize,
1342) -> bool {
1343    match ty.kind {
1344        ast::TyKind::Tup(..) => context.use_block_indent() && len == 1,
1345        ast::TyKind::Ref(_, ref mutty)
1346        | ast::TyKind::PinnedRef(_, ref mutty)
1347        | ast::TyKind::Ptr(ref mutty) => can_be_overflowed_type(context, &*mutty.ty, len),
1348        _ => false,
1349    }
1350}
1351
1352/// Returns `None` if there is no `GenericParam` in the list
1353pub(crate) fn rewrite_bound_params(
1354    context: &RewriteContext<'_>,
1355    shape: Shape,
1356    generic_params: &[ast::GenericParam],
1357) -> Option<String> {
1358    let result = generic_params
1359        .iter()
1360        .map(|param| param.rewrite(context, shape))
1361        .collect::<Option<Vec<_>>>()?
1362        .join(", ");
1363    if result.is_empty() {
1364        None
1365    } else {
1366        Some(result)
1367    }
1368}