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