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::UnsafeBinder(ref binder) => {
1034                let mut result = String::new();
1035                if binder.generic_params.is_empty() {
1036                    // We always want to write `unsafe<>` since `unsafe<> Ty`
1037                    // and `Ty` are distinct types.
1038                    result.push_str("unsafe<> ")
1039                } else if let Some(ref lifetime_str) =
1040                    rewrite_bound_params(context, shape, &binder.generic_params)
1041                {
1042                    result.push_str("unsafe<");
1043                    result.push_str(lifetime_str);
1044                    result.push_str("> ");
1045                }
1046
1047                let inner_ty_shape = if context.use_block_indent() {
1048                    shape.offset_left(result.len(), self.span())?
1049                } else {
1050                    shape
1051                        .visual_indent(result.len())
1052                        .sub_width(result.len(), self.span())?
1053                };
1054
1055                let rewrite = binder.inner_ty.rewrite_result(context, inner_ty_shape)?;
1056                result.push_str(&rewrite);
1057                Ok(result)
1058            }
1059        }
1060    }
1061}
1062
1063impl Rewrite for ast::TyPat {
1064    fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
1065        self.rewrite_result(context, shape).ok()
1066    }
1067
1068    fn rewrite_result(&self, context: &RewriteContext<'_>, shape: Shape) -> RewriteResult {
1069        match self.kind {
1070            ast::TyPatKind::Range(ref lhs, ref rhs, ref end_kind) => {
1071                rewrite_range_pat(context, shape, lhs, rhs, end_kind, self.span)
1072            }
1073            ast::TyPatKind::Or(ref variants) => {
1074                let mut first = true;
1075                let mut s = String::new();
1076                for variant in variants {
1077                    if first {
1078                        first = false
1079                    } else {
1080                        s.push_str(" | ");
1081                    }
1082                    s.push_str(&variant.rewrite_result(context, shape)?);
1083                }
1084                Ok(s)
1085            }
1086            ast::TyPatKind::NotNull | ast::TyPatKind::Err(_) => Err(RewriteError::Unknown),
1087        }
1088    }
1089}
1090
1091fn rewrite_fn_ptr(
1092    fn_ptr: &ast::FnPtrTy,
1093    span: Span,
1094    context: &RewriteContext<'_>,
1095    shape: Shape,
1096) -> RewriteResult {
1097    debug!("rewrite_bare_fn {:#?}", shape);
1098
1099    let mut result = String::with_capacity(128);
1100
1101    if let Some(ref lifetime_str) = rewrite_bound_params(context, shape, &fn_ptr.generic_params) {
1102        result.push_str("for<");
1103        // 6 = "for<> ".len(), 4 = "for<".
1104        // This doesn't work out so nicely for multiline situation with lots of
1105        // rightward drift. If that is a problem, we could use the list stuff.
1106        result.push_str(lifetime_str);
1107        result.push_str("> ");
1108    }
1109
1110    result.push_str(crate::utils::format_safety(fn_ptr.safety));
1111
1112    result.push_str(&format_extern(
1113        fn_ptr.ext,
1114        context.config.force_explicit_abi(),
1115    ));
1116
1117    result.push_str("fn");
1118
1119    let func_ty_shape = if context.use_block_indent() {
1120        shape.offset_left(result.len(), span)?
1121    } else {
1122        shape
1123            .visual_indent(result.len())
1124            .sub_width(result.len(), span)?
1125    };
1126
1127    let rewrite = format_function_type(
1128        fn_ptr.decl.inputs.iter(),
1129        &fn_ptr.decl.output,
1130        fn_ptr.decl.c_variadic(),
1131        span,
1132        context,
1133        func_ty_shape,
1134    )?;
1135
1136    result.push_str(&rewrite);
1137
1138    Ok(result)
1139}
1140
1141fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
1142    let is_trait = |b: &ast::GenericBound| match b {
1143        ast::GenericBound::Outlives(..) => false,
1144        ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => true,
1145    };
1146    let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
1147    let last_trait_index = generic_bounds.iter().rposition(is_trait);
1148    let first_lifetime_index = generic_bounds.iter().position(is_lifetime);
1149    match (last_trait_index, first_lifetime_index) {
1150        (Some(last_trait_index), Some(first_lifetime_index)) => {
1151            last_trait_index < first_lifetime_index
1152        }
1153        _ => true,
1154    }
1155}
1156
1157fn join_bounds(
1158    context: &RewriteContext<'_>,
1159    shape: Shape,
1160    items: &[ast::GenericBound],
1161    need_indent: bool,
1162) -> RewriteResult {
1163    join_bounds_inner(context, shape, items, need_indent, false)
1164}
1165
1166fn join_bounds_inner(
1167    context: &RewriteContext<'_>,
1168    shape: Shape,
1169    items: &[ast::GenericBound],
1170    need_indent: bool,
1171    force_newline: bool,
1172) -> RewriteResult {
1173    debug_assert!(!items.is_empty());
1174
1175    let generic_bounds_in_order = is_generic_bounds_in_order(items);
1176    let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
1177        ast::GenericBound::Outlives(..) => true,
1178        // We treat `use<>` like a trait bound here.
1179        ast::GenericBound::Trait(..) | ast::GenericBound::Use(..) => last_line_extendable(s),
1180    };
1181
1182    // Whether a GenericBound item is a PathSegment segment that includes internal array
1183    // that contains more than one item
1184    let is_item_with_multi_items_array = |item: &ast::GenericBound| match item {
1185        ast::GenericBound::Trait(ref poly_trait_ref, ..) => {
1186            let segments = &poly_trait_ref.trait_ref.path.segments;
1187            if segments.len() > 1 {
1188                true
1189            } else {
1190                if let Some(args_in) = &segments[0].args {
1191                    matches!(
1192                        args_in.deref(),
1193                        ast::GenericArgs::AngleBracketed(bracket_args)
1194                            if bracket_args.args.len() > 1
1195                    )
1196                } else {
1197                    false
1198                }
1199            }
1200        }
1201        ast::GenericBound::Use(args, _) => args.len() > 1,
1202        _ => false,
1203    };
1204
1205    let result = items.iter().enumerate().try_fold(
1206        (String::new(), None, false),
1207        |(strs, prev_trailing_span, prev_extendable), (i, item)| {
1208            let trailing_span = if i < items.len() - 1 {
1209                let hi = context
1210                    .snippet_provider
1211                    .span_before(mk_sp(items[i + 1].span().lo(), item.span().hi()), "+");
1212
1213                Some(mk_sp(item.span().hi(), hi))
1214            } else {
1215                None
1216            };
1217            let (leading_span, has_leading_comment) = if i > 0 {
1218                let lo = context
1219                    .snippet_provider
1220                    .span_after(mk_sp(items[i - 1].span().hi(), item.span().lo()), "+");
1221
1222                let span = mk_sp(lo, item.span().lo());
1223
1224                let has_comments = contains_comment(context.snippet(span));
1225
1226                (Some(mk_sp(lo, item.span().lo())), has_comments)
1227            } else {
1228                (None, false)
1229            };
1230            let prev_has_trailing_comment = match prev_trailing_span {
1231                Some(ts) => contains_comment(context.snippet(ts)),
1232                _ => false,
1233            };
1234
1235            let shape = if need_indent && force_newline {
1236                shape
1237                    .block_indent(context.config.tab_spaces())
1238                    .with_max_width(context.config)
1239            } else {
1240                shape
1241            };
1242            let whitespace = if force_newline && (!prev_extendable || !generic_bounds_in_order) {
1243                shape
1244                    .indent
1245                    .to_string_with_newline(context.config)
1246                    .to_string()
1247            } else {
1248                String::from(" ")
1249            };
1250
1251            let joiner = match context.config.type_punctuation_density() {
1252                TypeDensity::Compressed => String::from("+"),
1253                TypeDensity::Wide => whitespace + "+ ",
1254            };
1255            let joiner = if has_leading_comment {
1256                joiner.trim_end()
1257            } else {
1258                &joiner
1259            };
1260            let joiner = if prev_has_trailing_comment {
1261                joiner.trim_start()
1262            } else {
1263                joiner
1264            };
1265
1266            let (extendable, trailing_str) = if i == 0 {
1267                let bound_str = item.rewrite_result(context, shape)?;
1268                (is_bound_extendable(&bound_str, item), bound_str)
1269            } else {
1270                let bound_str = &item.rewrite_result(context, shape)?;
1271                match leading_span {
1272                    Some(ls) if has_leading_comment => (
1273                        is_bound_extendable(bound_str, item),
1274                        combine_strs_with_missing_comments(
1275                            context, joiner, bound_str, ls, shape, true,
1276                        )?,
1277                    ),
1278                    _ => (
1279                        is_bound_extendable(bound_str, item),
1280                        String::from(joiner) + bound_str,
1281                    ),
1282                }
1283            };
1284            match prev_trailing_span {
1285                Some(ts) if prev_has_trailing_comment => combine_strs_with_missing_comments(
1286                    context,
1287                    &strs,
1288                    &trailing_str,
1289                    ts,
1290                    shape,
1291                    true,
1292                )
1293                .map(|v| (v, trailing_span, extendable)),
1294                _ => Ok((strs + &trailing_str, trailing_span, extendable)),
1295            }
1296        },
1297    )?;
1298
1299    // Whether to retry with a forced newline:
1300    //   Only if result is not already multiline and did not exceed line width,
1301    //   and either there is more than one item;
1302    //       or the single item is of type `Trait`,
1303    //          and any of the internal arrays contains more than one item;
1304    let retry_with_force_newline = match context.config.style_edition() {
1305        style_edition @ _ if style_edition <= StyleEdition::Edition2021 => {
1306            !force_newline
1307                && items.len() > 1
1308                && (result.0.contains('\n') || result.0.len() > shape.width)
1309        }
1310        _ if force_newline => false,
1311        _ if (!result.0.contains('\n') && result.0.len() <= shape.width) => false,
1312        _ if items.len() > 1 => true,
1313        _ => is_item_with_multi_items_array(&items[0]),
1314    };
1315
1316    if retry_with_force_newline {
1317        join_bounds_inner(context, shape, items, need_indent, true)
1318    } else {
1319        Ok(result.0)
1320    }
1321}
1322
1323pub(crate) fn opaque_ty(ty: &Option<Box<ast::Ty>>) -> Option<&ast::GenericBounds> {
1324    ty.as_ref().and_then(|t| match &t.kind {
1325        ast::TyKind::ImplTrait(_, bounds) => Some(bounds),
1326        _ => None,
1327    })
1328}
1329
1330pub(crate) fn can_be_overflowed_type(
1331    context: &RewriteContext<'_>,
1332    ty: &ast::Ty,
1333    len: usize,
1334) -> bool {
1335    match ty.kind {
1336        ast::TyKind::Tup(..) => context.use_block_indent() && len == 1,
1337        ast::TyKind::Ref(_, ref mutty)
1338        | ast::TyKind::PinnedRef(_, ref mutty)
1339        | ast::TyKind::Ptr(ref mutty) => can_be_overflowed_type(context, &*mutty.ty, len),
1340        _ => false,
1341    }
1342}
1343
1344/// Returns `None` if there is no `GenericParam` in the list
1345pub(crate) fn rewrite_bound_params(
1346    context: &RewriteContext<'_>,
1347    shape: Shape,
1348    generic_params: &[ast::GenericParam],
1349) -> Option<String> {
1350    let result = generic_params
1351        .iter()
1352        .map(|param| param.rewrite(context, shape))
1353        .collect::<Option<Vec<_>>>()?
1354        .join(", ");
1355    if result.is_empty() {
1356        None
1357    } else {
1358        Some(result)
1359    }
1360}