1 use std::iter::ExactSizeIterator;
4 use syntax::ast::{self, FunctionRetTy, Mutability};
5 use syntax::source_map::{self, BytePos, Span};
6 use syntax::symbol::kw;
8 use crate::config::lists::*;
9 use crate::config::{IndentStyle, TypeDensity};
10 use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
12 definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
14 use crate::macros::{rewrite_macro, MacroPosition};
16 use crate::pairs::{rewrite_pair, PairParts};
17 use crate::rewrite::{Rewrite, RewriteContext};
18 use crate::shape::Shape;
19 use crate::source_map::SpanUtils;
20 use crate::spanned::Spanned;
22 colon_spaces, extra_offset, first_line_width, format_abi, format_mutability,
23 last_line_extendable, last_line_width, mk_sp, rewrite_ident,
26 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
27 pub(crate) enum PathContext {
33 // Does not wrap on simple segments.
34 pub(crate) fn rewrite_path(
35 context: &RewriteContext<'_>,
36 path_context: PathContext,
37 qself: Option<&ast::QSelf>,
41 let skip_count = qself.map_or(0, |x| x.position);
43 let mut result = if path.is_global() && qself.is_none() && path_context != PathContext::Import {
49 let mut span_lo = path.span.lo();
51 if let Some(qself) = qself {
54 let fmt_ty = qself.ty.rewrite(context, shape)?;
55 result.push_str(&fmt_ty);
58 result.push_str(" as ");
59 if path.is_global() && path_context != PathContext::Import {
60 result.push_str("::");
64 let shape = shape.sub_width(3)?;
66 result = rewrite_path_segments(
69 path.segments.iter().take(skip_count),
77 result.push_str(">::");
78 span_lo = qself.ty.span.hi() + BytePos(1);
81 rewrite_path_segments(
84 path.segments.iter().skip(skip_count),
92 fn rewrite_path_segments<'a, I>(
93 path_context: PathContext,
98 context: &RewriteContext<'_>,
102 I: Iterator<Item = &'a ast::PathSegment>,
104 let mut first = true;
105 let shape = shape.visual_indent(0);
107 for segment in iter {
108 // Indicates a global path, shouldn't be rendered.
109 if segment.ident.name == kw::PathRoot {
115 buffer.push_str("::");
118 let extra_offset = extra_offset(&buffer, shape);
119 let new_shape = shape.shrink_left(extra_offset)?;
120 let segment_string = rewrite_segment(
129 buffer.push_str(&segment_string);
136 pub(crate) enum SegmentParam<'a> {
137 Const(&'a ast::AnonConst),
138 LifeTime(&'a ast::Lifetime),
140 Binding(&'a ast::AssocTyConstraint),
143 impl<'a> SegmentParam<'a> {
144 fn from_generic_arg(arg: &ast::GenericArg) -> SegmentParam<'_> {
146 ast::GenericArg::Lifetime(ref lt) => SegmentParam::LifeTime(lt),
147 ast::GenericArg::Type(ref ty) => SegmentParam::Type(ty),
148 ast::GenericArg::Const(const_) => SegmentParam::Const(const_),
153 impl<'a> Spanned for SegmentParam<'a> {
154 fn span(&self) -> Span {
156 SegmentParam::Const(const_) => const_.value.span,
157 SegmentParam::LifeTime(lt) => lt.ident.span,
158 SegmentParam::Type(ty) => ty.span,
159 SegmentParam::Binding(binding) => binding.span,
164 impl<'a> Rewrite for SegmentParam<'a> {
165 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
167 SegmentParam::Const(const_) => const_.rewrite(context, shape),
168 SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
169 SegmentParam::Type(ty) => ty.rewrite(context, shape),
170 SegmentParam::Binding(assoc_ty_constraint) => {
171 let mut result = match assoc_ty_constraint.kind {
172 ast::AssocTyConstraintKind::Bound { .. } => {
173 format!("{}: ", rewrite_ident(context, assoc_ty_constraint.ident))
175 ast::AssocTyConstraintKind::Equality { .. } => {
176 match context.config.type_punctuation_density() {
177 TypeDensity::Wide => {
178 format!("{} = ", rewrite_ident(context, assoc_ty_constraint.ident))
180 TypeDensity::Compressed => {
181 format!("{}=", rewrite_ident(context, assoc_ty_constraint.ident))
187 let budget = shape.width.checked_sub(result.len())?;
188 let rewrite = assoc_ty_constraint
190 .rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
191 result.push_str(&rewrite);
198 impl Rewrite for ast::AssocTyConstraintKind {
199 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
201 ast::AssocTyConstraintKind::Equality { ty } => ty.rewrite(context, shape),
202 ast::AssocTyConstraintKind::Bound { bounds } => bounds.rewrite(context, shape),
207 // Formats a path segment. There are some hacks involved to correctly determine
208 // the segment's associated span since it's not part of the AST.
210 // The span_lo is assumed to be greater than the end of any previous segment's
211 // parameters and lesser or equal than the start of current segment.
213 // span_hi is assumed equal to the end of the entire path.
215 // When the segment contains a positive number of parameters, we update span_lo
216 // so that invariants described above will hold for the next segment.
218 path_context: PathContext,
219 segment: &ast::PathSegment,
220 span_lo: &mut BytePos,
222 context: &RewriteContext<'_>,
224 ) -> Option<String> {
225 let mut result = String::with_capacity(128);
226 result.push_str(rewrite_ident(context, segment.ident));
228 let ident_len = result.len();
229 let shape = if context.use_block_indent() {
230 shape.offset_left(ident_len)?
232 shape.shrink_left(ident_len)?
235 if let Some(ref args) = segment.args {
237 ast::GenericArgs::AngleBracketed(ref data)
238 if !data.args.is_empty() || !data.constraints.is_empty() =>
240 let param_list = data
243 .map(SegmentParam::from_generic_arg)
244 .chain(data.constraints.iter().map(|x| SegmentParam::Binding(&*x)))
245 .collect::<Vec<_>>();
247 // HACK: squeeze out the span between the identifier and the parameters.
248 // The hack is requried so that we don't remove the separator inside macro calls.
249 // This does not work in the presence of comment, hoping that people are
250 // sane about where to put their comment.
251 let separator_snippet = context
252 .snippet(mk_sp(segment.ident.span.hi(), data.span.lo()))
254 let force_separator = context.inside_macro() && separator_snippet.starts_with("::");
255 let separator = if path_context == PathContext::Expr || force_separator {
260 result.push_str(separator);
262 let generics_str = overflow::rewrite_with_angle_brackets(
267 mk_sp(*span_lo, span_hi),
270 // Update position of last bracket.
273 .span_after(mk_sp(*span_lo, span_hi), "<");
275 result.push_str(&generics_str)
277 ast::GenericArgs::Parenthesized(ref data) => {
278 let output = match data.output {
279 Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
280 None => FunctionRetTy::Default(source_map::DUMMY_SP),
282 result.push_str(&format_function_type(
283 data.inputs.iter().map(|x| &**x),
298 fn format_function_type<'a, I>(
300 output: &FunctionRetTy,
303 context: &RewriteContext<'_>,
307 I: ExactSizeIterator,
308 <I as Iterator>::Item: Deref,
309 <I::Item as Deref>::Target: Rewrite + Spanned + 'a,
311 debug!("format_function_type {:#?}", shape);
313 let ty_shape = match context.config.indent_style() {
315 IndentStyle::Block => shape.offset_left(4)?,
316 IndentStyle::Visual => shape.block_left(4)?,
318 let output = match *output {
319 FunctionRetTy::Ty(ref ty) => {
320 let type_str = ty.rewrite(context, ty_shape)?;
321 format!(" -> {}", type_str)
323 FunctionRetTy::Default(..) => String::new(),
326 let list_shape = if context.use_block_indent() {
328 shape.block().indent.block_indent(context.config),
333 let budget = shape.width.checked_sub(2)?;
335 let offset = shape.indent + 1;
336 Shape::legacy(budget, offset)
339 let is_inputs_empty = inputs.len() == 0;
340 let list_lo = context.snippet_provider.span_after(span, "(");
341 let (list_str, tactic) = if is_inputs_empty {
342 let tactic = get_tactics(&[], &output, shape);
343 let list_hi = context.snippet_provider.span_before(span, ")");
344 let comment = context
346 .span_to_snippet(mk_sp(list_lo, list_hi))?
348 let comment = if comment.starts_with("//") {
351 &list_shape.indent.to_string_with_newline(context.config),
353 &shape.block().indent.to_string_with_newline(context.config)
360 let items = itemize_list(
361 context.snippet_provider,
365 |arg| arg.span().lo(),
366 |arg| arg.span().hi(),
367 |arg| arg.rewrite(context, list_shape),
373 let item_vec: Vec<_> = items.collect();
374 let tactic = get_tactics(&item_vec, &output, shape);
375 let trailing_separator = if !context.use_block_indent() || variadic {
376 SeparatorTactic::Never
378 context.config.trailing_comma()
381 let fmt = ListFormatting::new(list_shape, context.config)
383 .trailing_separator(trailing_separator)
384 .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
385 .preserve_newline(true);
386 (write_list(&item_vec, &fmt)?, tactic)
389 let args = if tactic == DefinitiveListTactic::Horizontal
390 || !context.use_block_indent()
393 format!("({})", list_str)
397 list_shape.indent.to_string_with_newline(context.config),
399 shape.block().indent.to_string_with_newline(context.config),
402 if output.is_empty() || last_line_width(&args) + first_line_width(&output) <= shape.width {
403 Some(format!("{}{}", args, output))
408 list_shape.indent.to_string(context.config),
414 fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
415 colon_spaces(context.config)
418 // If the return type is multi-lined, then force to use multiple lines for
419 // arguments as well.
420 fn get_tactics(item_vec: &[ListItem], output: &str, shape: Shape) -> DefinitiveListTactic {
421 if output.contains('\n') {
422 DefinitiveListTactic::Vertical
426 ListTactic::HorizontalVertical,
428 // 2 is for the case of ',\n'
429 shape.width.saturating_sub(2 + output.len()),
434 impl Rewrite for ast::WherePredicate {
435 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
436 // FIXME: dead spans?
437 let result = match *self {
438 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
439 ref bound_generic_params,
444 let type_str = bounded_ty.rewrite(context, shape)?;
445 let colon = type_bound_colon(context).trim_end();
446 let lhs = if let Some(lifetime_str) =
447 rewrite_lifetime_param(context, shape, bound_generic_params)
449 format!("for<{}> {}{}", lifetime_str, type_str, colon)
451 format!("{}{}", type_str, colon)
454 rewrite_assign_rhs(context, lhs, bounds, shape)?
456 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
460 }) => rewrite_bounded_lifetime(lifetime, bounds, context, shape)?,
461 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
466 let lhs_ty_str = lhs_ty.rewrite(context, shape).map(|lhs| lhs + " =")?;
467 rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, shape)?
475 impl Rewrite for ast::GenericArg {
476 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
478 ast::GenericArg::Lifetime(ref lt) => lt.rewrite(context, shape),
479 ast::GenericArg::Type(ref ty) => ty.rewrite(context, shape),
480 ast::GenericArg::Const(ref const_) => const_.rewrite(context, shape),
485 fn rewrite_bounded_lifetime(
487 bounds: &[ast::GenericBound],
488 context: &RewriteContext<'_>,
490 ) -> Option<String> {
491 let result = lt.rewrite(context, shape)?;
493 if bounds.is_empty() {
496 let colon = type_bound_colon(context);
497 let overhead = last_line_width(&result) + colon.len();
498 let result = format!(
502 join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
508 impl Rewrite for ast::AnonConst {
509 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
510 format_expr(&self.value, ExprType::SubExpression, context, shape)
514 impl Rewrite for ast::Lifetime {
515 fn rewrite(&self, context: &RewriteContext<'_>, _: Shape) -> Option<String> {
516 Some(rewrite_ident(context, self.ident).to_owned())
520 impl Rewrite for ast::GenericBound {
521 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
523 ast::GenericBound::Trait(ref poly_trait_ref, trait_bound_modifier) => {
524 let snippet = context.snippet(self.span());
525 let has_paren = snippet.starts_with('(') && snippet.ends_with(')');
526 let rewrite = match trait_bound_modifier {
527 ast::TraitBoundModifier::None => poly_trait_ref.rewrite(context, shape),
528 ast::TraitBoundModifier::Maybe => poly_trait_ref
529 .rewrite(context, shape.offset_left(1)?)
530 .map(|s| format!("?{}", s)),
532 rewrite.map(|s| if has_paren { format!("({})", s) } else { s })
534 ast::GenericBound::Outlives(ref lifetime) => lifetime.rewrite(context, shape),
539 impl Rewrite for ast::GenericBounds {
540 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
542 return Some(String::new());
545 join_bounds(context, shape, self, true)
549 impl Rewrite for ast::GenericParam {
550 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
551 let mut result = String::with_capacity(128);
552 // FIXME: If there are more than one attributes, this will force multiline.
553 match self.attrs.rewrite(context, shape) {
554 Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
558 if let syntax::ast::GenericParamKind::Const { ref ty } = &self.kind {
559 result.push_str("const ");
560 result.push_str(rewrite_ident(context, self.ident));
561 result.push_str(": ");
562 result.push_str(&ty.rewrite(context, shape)?);
564 result.push_str(rewrite_ident(context, self.ident));
567 if !self.bounds.is_empty() {
568 result.push_str(type_bound_colon(context));
569 result.push_str(&self.bounds.rewrite(context, shape)?)
571 if let ast::GenericParamKind::Type {
572 default: Some(ref def),
575 let eq_str = match context.config.type_punctuation_density() {
576 TypeDensity::Compressed => "=",
577 TypeDensity::Wide => " = ",
579 result.push_str(eq_str);
580 let budget = shape.width.checked_sub(result.len())?;
582 def.rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
583 result.push_str(&rewrite);
590 impl Rewrite for ast::PolyTraitRef {
591 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
592 if let Some(lifetime_str) =
593 rewrite_lifetime_param(context, shape, &self.bound_generic_params)
595 // 6 is "for<> ".len()
596 let extra_offset = lifetime_str.len() + 6;
599 .rewrite(context, shape.offset_left(extra_offset)?)?;
601 Some(format!("for<{}> {}", lifetime_str, path_str))
603 self.trait_ref.rewrite(context, shape)
608 impl Rewrite for ast::TraitRef {
609 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
610 rewrite_path(context, PathContext::Type, None, &self.path, shape)
614 impl Rewrite for ast::Ty {
615 fn rewrite(&self, context: &RewriteContext<'_>, shape: Shape) -> Option<String> {
617 ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
618 // we have to consider 'dyn' keyword is used or not!!!
619 let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn;
620 // 4 is length of 'dyn '
621 let shape = if is_dyn { shape.offset_left(4)? } else { shape };
622 let mut res = bounds.rewrite(context, shape)?;
623 // We may have falsely removed a trailing `+` inside macro call.
624 if context.inside_macro() && bounds.len() == 1 {
625 if context.snippet(self.span).ends_with('+') && !res.ends_with('+') {
630 Some(format!("dyn {}", res))
635 ast::TyKind::Ptr(ref mt) => {
636 let prefix = match mt.mutbl {
637 Mutability::Mutable => "*mut ",
638 Mutability::Immutable => "*const ",
641 rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
643 ast::TyKind::Rptr(ref lifetime, ref mt) => {
644 let mut_str = format_mutability(mt.mutbl);
645 let mut_len = mut_str.len();
646 Some(match *lifetime {
647 Some(ref lifetime) => {
648 let lt_budget = shape.width.checked_sub(2 + mut_len)?;
649 let lt_str = lifetime.rewrite(
651 Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
653 let lt_len = lt_str.len();
654 let budget = shape.width.checked_sub(2 + mut_len + lt_len)?;
661 Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len)
666 let budget = shape.width.checked_sub(1 + mut_len)?;
672 Shape::legacy(budget, shape.indent + 1 + mut_len)
678 // FIXME: we drop any comments here, even though it's a silly place to put
680 ast::TyKind::Paren(ref ty) => {
681 let budget = shape.width.checked_sub(2)?;
682 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
683 .map(|ty_str| format!("({})", ty_str))
685 ast::TyKind::Slice(ref ty) => {
686 let budget = shape.width.checked_sub(4)?;
687 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
688 .map(|ty_str| format!("[{}]", ty_str))
690 ast::TyKind::Tup(ref items) => {
691 rewrite_tuple(context, items.iter(), self.span, shape, items.len() == 1)
693 ast::TyKind::Path(ref q_self, ref path) => {
694 rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
696 ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
699 PairParts::new("[", "; ", "]"),
702 SeparatorPlace::Back,
704 ast::TyKind::Infer => {
705 if shape.width >= 1 {
711 ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
712 ast::TyKind::Never => Some(String::from("!")),
713 ast::TyKind::Mac(ref mac) => {
714 rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
716 ast::TyKind::ImplicitSelf => Some(String::from("")),
717 ast::TyKind::ImplTrait(_, ref it) => {
718 // Empty trait is not a parser error.
719 it.rewrite(context, shape).map(|it_str| {
720 let space = if it_str.is_empty() { "" } else { " " };
721 format!("impl{}{}", space, it_str)
724 ast::TyKind::CVarArgs => Some("...".to_owned()),
725 ast::TyKind::Err | ast::TyKind::Typeof(..) => unreachable!(),
731 bare_fn: &ast::BareFnTy,
733 context: &RewriteContext<'_>,
735 ) -> Option<String> {
736 debug!("rewrite_bare_fn {:#?}", shape);
738 let mut result = String::with_capacity(128);
740 if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params)
742 result.push_str("for<");
743 // 6 = "for<> ".len(), 4 = "for<".
744 // This doesn't work out so nicely for multiline situation with lots of
745 // rightward drift. If that is a problem, we could use the list stuff.
746 result.push_str(lifetime_str);
747 result.push_str("> ");
750 result.push_str(crate::utils::format_unsafety(bare_fn.unsafety));
752 result.push_str(&format_abi(
754 context.config.force_explicit_abi(),
758 result.push_str("fn");
760 let func_ty_shape = if context.use_block_indent() {
761 shape.offset_left(result.len())?
763 shape.visual_indent(result.len()).sub_width(result.len())?
766 let rewrite = format_function_type(
767 bare_fn.decl.inputs.iter(),
768 &bare_fn.decl.output,
769 bare_fn.decl.c_variadic,
775 result.push_str(&rewrite);
780 fn is_generic_bounds_in_order(generic_bounds: &[ast::GenericBound]) -> bool {
781 let is_trait = |b: &ast::GenericBound| match b {
782 ast::GenericBound::Outlives(..) => false,
783 ast::GenericBound::Trait(..) => true,
785 let is_lifetime = |b: &ast::GenericBound| !is_trait(b);
786 let last_trait_index = generic_bounds.iter().rposition(is_trait);
787 let first_lifetime_index = generic_bounds.iter().position(is_lifetime);
788 match (last_trait_index, first_lifetime_index) {
789 (Some(last_trait_index), Some(first_lifetime_index)) => {
790 last_trait_index < first_lifetime_index
797 context: &RewriteContext<'_>,
799 items: &[ast::GenericBound],
801 ) -> Option<String> {
802 debug_assert!(!items.is_empty());
804 // Try to join types in a single line
805 let joiner = match context.config.type_punctuation_density() {
806 TypeDensity::Compressed => "+",
807 TypeDensity::Wide => " + ",
809 let type_strs = items
811 .map(|item| item.rewrite(context, shape))
812 .collect::<Option<Vec<_>>>()?;
813 let result = type_strs.join(joiner);
814 if items.len() <= 1 || (!result.contains('\n') && result.len() <= shape.width) {
818 // We need to use multiple lines.
819 let (type_strs, offset) = if need_indent {
820 // Rewrite with additional indentation.
821 let nested_shape = shape.block_indent(context.config.tab_spaces());
822 let type_strs = items
824 .map(|item| item.rewrite(context, nested_shape))
825 .collect::<Option<Vec<_>>>()?;
826 (type_strs, nested_shape.indent)
828 (type_strs, shape.indent)
831 let is_bound_extendable = |s: &str, b: &ast::GenericBound| match b {
832 ast::GenericBound::Outlives(..) => true,
833 ast::GenericBound::Trait(..) => last_line_extendable(s),
835 let mut result = String::with_capacity(128);
836 result.push_str(&type_strs[0]);
837 let mut can_be_put_on_the_same_line = is_bound_extendable(&result, &items[0]);
838 let generic_bounds_in_order = is_generic_bounds_in_order(items);
839 for (bound, bound_str) in items[1..].iter().zip(type_strs[1..].iter()) {
840 if generic_bounds_in_order && can_be_put_on_the_same_line {
841 result.push_str(joiner);
843 result.push_str(&offset.to_string_with_newline(context.config));
844 result.push_str("+ ");
846 result.push_str(bound_str);
847 can_be_put_on_the_same_line = is_bound_extendable(bound_str, bound);
853 pub(crate) fn can_be_overflowed_type(
854 context: &RewriteContext<'_>,
859 ast::TyKind::Tup(..) => context.use_block_indent() && len == 1,
860 ast::TyKind::Rptr(_, ref mutty) | ast::TyKind::Ptr(ref mutty) => {
861 can_be_overflowed_type(context, &*mutty.ty, len)
867 /// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
868 fn rewrite_lifetime_param(
869 context: &RewriteContext<'_>,
871 generic_params: &[ast::GenericParam],
872 ) -> Option<String> {
873 let result = generic_params
875 .filter(|p| match p.kind {
876 ast::GenericParamKind::Lifetime => true,
879 .map(|lt| lt.rewrite(context, shape))
880 .collect::<Option<Vec<_>>>()?
882 if result.is_empty() {