1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use std::iter::ExactSizeIterator;
15 use syntax::ast::{self, FunctionRetTy, Mutability};
16 use syntax::codemap::{self, BytePos, Span};
17 use syntax::symbol::keywords;
19 use codemap::SpanUtils;
20 use config::{IndentStyle, TypeDensity};
22 rewrite_assign_rhs, rewrite_pair, rewrite_tuple, rewrite_unary_prefix, PairParts, ToExpr,
24 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
25 use macros::{rewrite_macro, MacroPosition};
27 use rewrite::{Rewrite, RewriteContext};
31 colon_spaces, extra_offset, first_line_width, format_abi, format_mutability, last_line_width,
35 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
36 pub enum PathContext {
42 // Does not wrap on simple segments.
44 context: &RewriteContext,
45 path_context: PathContext,
46 qself: Option<&ast::QSelf>,
50 let skip_count = qself.map_or(0, |x| x.position);
52 let mut result = if path.is_global() && qself.is_none() && path_context != PathContext::Import {
58 let mut span_lo = path.span.lo();
60 if let Some(qself) = qself {
63 let fmt_ty = qself.ty.rewrite(context, shape)?;
64 result.push_str(&fmt_ty);
67 result.push_str(" as ");
68 if path.is_global() && path_context != PathContext::Import {
69 result.push_str("::");
73 let shape = shape.sub_width(3)?;
75 result = rewrite_path_segments(
78 path.segments.iter().take(skip_count),
86 result.push_str(">::");
87 span_lo = qself.ty.span.hi() + BytePos(1);
90 rewrite_path_segments(
93 path.segments.iter().skip(skip_count),
101 fn rewrite_path_segments<'a, I>(
102 path_context: PathContext,
105 mut span_lo: BytePos,
107 context: &RewriteContext,
111 I: Iterator<Item = &'a ast::PathSegment>,
113 let mut first = true;
114 let shape = shape.visual_indent(0);
116 for segment in iter {
117 // Indicates a global path, shouldn't be rendered.
118 if segment.ident.name == keywords::CrateRoot.name() {
124 buffer.push_str("::");
127 let extra_offset = extra_offset(&buffer, shape);
128 let new_shape = shape.shrink_left(extra_offset)?;
129 let segment_string = rewrite_segment(
138 buffer.push_str(&segment_string);
145 enum SegmentParam<'a> {
146 LifeTime(&'a ast::Lifetime),
148 Binding(&'a ast::TypeBinding),
151 impl<'a> Spanned for SegmentParam<'a> {
152 fn span(&self) -> Span {
154 SegmentParam::LifeTime(lt) => lt.ident.span,
155 SegmentParam::Type(ty) => ty.span,
156 SegmentParam::Binding(binding) => binding.span,
161 impl<'a> ToExpr for SegmentParam<'a> {
162 fn to_expr(&self) -> Option<&ast::Expr> {
166 fn can_be_overflowed(&self, context: &RewriteContext, len: usize) -> bool {
168 SegmentParam::Type(ty) => ty.can_be_overflowed(context, len),
174 impl<'a> Rewrite for SegmentParam<'a> {
175 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
177 SegmentParam::LifeTime(lt) => lt.rewrite(context, shape),
178 SegmentParam::Type(ty) => ty.rewrite(context, shape),
179 SegmentParam::Binding(binding) => {
180 let mut result = match context.config.type_punctuation_density() {
181 TypeDensity::Wide => format!("{} = ", binding.ident),
182 TypeDensity::Compressed => format!("{}=", binding.ident),
184 let budget = shape.width.checked_sub(result.len())?;
185 let rewrite = binding
187 .rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
188 result.push_str(&rewrite);
195 // Formats a path segment. There are some hacks involved to correctly determine
196 // the segment's associated span since it's not part of the AST.
198 // The span_lo is assumed to be greater than the end of any previous segment's
199 // parameters and lesser or equal than the start of current segment.
201 // span_hi is assumed equal to the end of the entire path.
203 // When the segment contains a positive number of parameters, we update span_lo
204 // so that invariants described above will hold for the next segment.
206 path_context: PathContext,
207 segment: &ast::PathSegment,
208 span_lo: &mut BytePos,
210 context: &RewriteContext,
212 ) -> Option<String> {
213 let mut result = String::with_capacity(128);
214 result.push_str(&segment.ident.name.as_str());
216 let ident_len = result.len();
217 let shape = if context.use_block_indent() {
218 shape.offset_left(ident_len)?
220 shape.shrink_left(ident_len)?
223 if let Some(ref params) = segment.parameters {
225 ast::PathParameters::AngleBracketed(ref data)
226 if !data.lifetimes.is_empty()
227 || !data.types.is_empty()
228 || !data.bindings.is_empty() =>
230 let param_list = data
233 .map(SegmentParam::LifeTime)
234 .chain(data.types.iter().map(|x| SegmentParam::Type(&*x)))
235 .chain(data.bindings.iter().map(|x| SegmentParam::Binding(&*x)))
236 .collect::<Vec<_>>();
238 let separator = if path_context == PathContext::Expr {
243 result.push_str(separator);
245 let generics_str = overflow::rewrite_with_angle_brackets(
248 ¶m_list.iter().map(|e| &*e).collect::<Vec<_>>(),
250 mk_sp(*span_lo, span_hi),
253 // Update position of last bracket.
256 .span_after(mk_sp(*span_lo, span_hi), "<");
258 result.push_str(&generics_str)
260 ast::PathParameters::Parenthesized(ref data) => {
261 let output = match data.output {
262 Some(ref ty) => FunctionRetTy::Ty(ty.clone()),
263 None => FunctionRetTy::Default(codemap::DUMMY_SP),
265 result.push_str(&format_function_type(
266 data.inputs.iter().map(|x| &**x),
281 fn format_function_type<'a, I>(
283 output: &FunctionRetTy,
286 context: &RewriteContext,
290 I: ExactSizeIterator,
291 <I as Iterator>::Item: Deref,
292 <I::Item as Deref>::Target: Rewrite + Spanned + 'a,
294 // Code for handling variadics is somewhat duplicated for items, but they
295 // are different enough to need some serious refactoring to share code.
299 <T as Deref>::Target: Rewrite + Spanned,
305 let variadic_arg = if variadic {
306 let variadic_start = context.snippet_provider.span_before(span, "...");
307 Some(ArgumentKind::Variadic(variadic_start))
313 let budget = shape.width.checked_sub(2)?;
315 let offset = match context.config.indent_style() {
316 IndentStyle::Block => {
319 .block_indent(context.config.tab_spaces())
322 IndentStyle::Visual => shape.indent + 1,
324 let list_shape = Shape::legacy(budget, offset);
325 let list_lo = context.snippet_provider.span_after(span, "(");
326 let items = itemize_list(
327 context.snippet_provider,
328 inputs.map(ArgumentKind::Regular).chain(variadic_arg),
332 ArgumentKind::Regular(ref ty) => ty.span().lo(),
333 ArgumentKind::Variadic(start) => start,
336 ArgumentKind::Regular(ref ty) => ty.span().hi(),
337 ArgumentKind::Variadic(start) => start + BytePos(3),
340 ArgumentKind::Regular(ref ty) => ty.rewrite(context, list_shape),
341 ArgumentKind::Variadic(_) => Some("...".to_owned()),
348 let item_vec: Vec<_> = items.collect();
350 let tactic = definitive_tactic(
352 ListTactic::HorizontalVertical,
357 let fmt = ListFormatting {
360 trailing_separator: if !context.use_block_indent() || variadic {
361 SeparatorTactic::Never
363 context.config.trailing_comma()
365 separator_place: SeparatorPlace::Back,
367 ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
368 preserve_newline: true,
369 config: context.config,
372 let list_str = write_list(&item_vec, &fmt)?;
374 let ty_shape = match context.config.indent_style() {
376 IndentStyle::Block => shape.offset_left(4)?,
377 IndentStyle::Visual => shape.block_left(4)?,
379 let output = match *output {
380 FunctionRetTy::Ty(ref ty) => {
381 let type_str = ty.rewrite(context, ty_shape)?;
382 format!(" -> {}", type_str)
384 FunctionRetTy::Default(..) => String::new(),
387 let args = if (!list_str.contains('\n') || list_str.is_empty()) && !output.contains('\n')
388 || !context.use_block_indent()
390 format!("({})", list_str)
394 offset.to_string_with_newline(context.config),
396 shape.block().indent.to_string_with_newline(context.config),
399 if last_line_width(&args) + first_line_width(&output) <= shape.width {
400 Some(format!("{}{}", args, output))
405 offset.to_string(context.config),
411 fn type_bound_colon(context: &RewriteContext) -> &'static str {
413 context.config.space_before_colon(),
414 context.config.space_after_colon(),
418 impl Rewrite for ast::WherePredicate {
419 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
421 let result = match *self {
422 ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate {
423 ref bound_generic_params,
428 let type_str = bounded_ty.rewrite(context, shape)?;
429 let colon = type_bound_colon(context).trim_right();
430 let lhs = if let Some(lifetime_str) =
431 rewrite_lifetime_param(context, shape, bound_generic_params)
433 format!("for<{}> {}{}", lifetime_str, type_str, colon)
435 format!("{}{}", type_str, colon)
438 rewrite_assign_rhs(context, lhs, bounds, shape)?
440 ast::WherePredicate::RegionPredicate(ast::WhereRegionPredicate {
444 }) => rewrite_bounded_lifetime(lifetime, bounds, context, shape)?,
445 ast::WherePredicate::EqPredicate(ast::WhereEqPredicate {
450 let lhs_ty_str = lhs_ty.rewrite(context, shape).map(|lhs| lhs + " =")?;
451 rewrite_assign_rhs(context, lhs_ty_str, &**rhs_ty, shape)?
459 impl Rewrite for ast::LifetimeDef {
460 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
461 rewrite_bounded_lifetime(&self.lifetime, &self.bounds, context, shape)
465 fn rewrite_bounded_lifetime(
467 bounds: &[ast::Lifetime],
468 context: &RewriteContext,
470 ) -> Option<String> {
471 let result = lt.rewrite(context, shape)?;
473 if bounds.is_empty() {
476 let colon = type_bound_colon(context);
477 let overhead = last_line_width(&result) + colon.len();
478 let result = format!(
482 join_bounds(context, shape.sub_width(overhead)?, bounds, true)?
488 impl Rewrite for ast::TyParamBound {
489 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
491 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::None) => {
492 tref.rewrite(context, shape)
494 ast::TyParamBound::TraitTyParamBound(ref tref, ast::TraitBoundModifier::Maybe) => Some(
495 format!("?{}", tref.rewrite(context, shape.offset_left(1)?)?),
497 ast::TyParamBound::RegionTyParamBound(ref l) => l.rewrite(context, shape),
502 impl Rewrite for ast::Lifetime {
503 fn rewrite(&self, _: &RewriteContext, _: Shape) -> Option<String> {
504 Some(self.ident.to_string())
508 /// A simple wrapper over type param bounds in trait.
510 pub struct TraitTyParamBounds<'a> {
511 inner: &'a ast::TyParamBounds,
514 impl<'a> Rewrite for TraitTyParamBounds<'a> {
515 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
516 join_bounds(context, shape, self.inner, false)
520 impl Rewrite for ast::TyParamBounds {
521 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
522 join_bounds(context, shape, self, true)
526 impl Rewrite for ast::TyParam {
527 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
528 let mut result = String::with_capacity(128);
529 // FIXME: If there are more than one attributes, this will force multiline.
530 match self.attrs.rewrite(context, shape) {
531 Some(ref rw) if !rw.is_empty() => result.push_str(&format!("{} ", rw)),
534 result.push_str(&self.ident.to_string());
535 if !self.bounds.is_empty() {
536 result.push_str(type_bound_colon(context));
537 result.push_str(&self.bounds.rewrite(context, shape)?)
539 if let Some(ref def) = self.default {
540 let eq_str = match context.config.type_punctuation_density() {
541 TypeDensity::Compressed => "=",
542 TypeDensity::Wide => " = ",
544 result.push_str(eq_str);
545 let budget = shape.width.checked_sub(result.len())?;
546 let rewrite = def.rewrite(context, Shape::legacy(budget, shape.indent + result.len()))?;
547 result.push_str(&rewrite);
554 impl Rewrite for ast::PolyTraitRef {
555 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
556 if let Some(lifetime_str) =
557 rewrite_lifetime_param(context, shape, &self.bound_generic_params)
559 // 6 is "for<> ".len()
560 let extra_offset = lifetime_str.len() + 6;
563 .rewrite(context, shape.offset_left(extra_offset)?)?;
565 Some(format!("for<{}> {}", lifetime_str, path_str))
567 self.trait_ref.rewrite(context, shape)
572 impl Rewrite for ast::TraitRef {
573 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
574 rewrite_path(context, PathContext::Type, None, &self.path, shape)
578 impl Rewrite for ast::Ty {
579 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
581 ast::TyKind::TraitObject(ref bounds, tobj_syntax) => {
582 // we have to consider 'dyn' keyword is used or not!!!
583 let is_dyn = tobj_syntax == ast::TraitObjectSyntax::Dyn;
584 // 4 is length of 'dyn '
585 let shape = if is_dyn { shape.offset_left(4)? } else { shape };
586 let res = bounds.rewrite(context, shape)?;
588 Some(format!("dyn {}", res))
593 ast::TyKind::Ptr(ref mt) => {
594 let prefix = match mt.mutbl {
595 Mutability::Mutable => "*mut ",
596 Mutability::Immutable => "*const ",
599 rewrite_unary_prefix(context, prefix, &*mt.ty, shape)
601 ast::TyKind::Rptr(ref lifetime, ref mt) => {
602 let mut_str = format_mutability(mt.mutbl);
603 let mut_len = mut_str.len();
604 Some(match *lifetime {
605 Some(ref lifetime) => {
606 let lt_budget = shape.width.checked_sub(2 + mut_len)?;
607 let lt_str = lifetime.rewrite(
609 Shape::legacy(lt_budget, shape.indent + 2 + mut_len),
611 let lt_len = lt_str.len();
612 let budget = shape.width.checked_sub(2 + mut_len + lt_len)?;
619 Shape::legacy(budget, shape.indent + 2 + mut_len + lt_len)
624 let budget = shape.width.checked_sub(1 + mut_len)?;
630 Shape::legacy(budget, shape.indent + 1 + mut_len)
636 // FIXME: we drop any comments here, even though it's a silly place to put
638 ast::TyKind::Paren(ref ty) => {
639 let budget = shape.width.checked_sub(2)?;
640 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
641 .map(|ty_str| format!("({})", ty_str))
643 ast::TyKind::Slice(ref ty) => {
644 let budget = shape.width.checked_sub(4)?;
645 ty.rewrite(context, Shape::legacy(budget, shape.indent + 1))
646 .map(|ty_str| format!("[{}]", ty_str))
648 ast::TyKind::Tup(ref items) => rewrite_tuple(
650 &::utils::ptr_vec_to_ref_vec(items),
654 ast::TyKind::Path(ref q_self, ref path) => {
655 rewrite_path(context, PathContext::Type, q_self.as_ref(), path, shape)
657 ast::TyKind::Array(ref ty, ref repeats) => rewrite_pair(
660 PairParts::new("[", "; ", "]"),
663 SeparatorPlace::Back,
665 ast::TyKind::Infer => {
666 if shape.width >= 1 {
672 ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
673 ast::TyKind::Never => Some(String::from("!")),
674 ast::TyKind::Mac(ref mac) => {
675 rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
677 ast::TyKind::ImplicitSelf => Some(String::from("")),
678 ast::TyKind::ImplTrait(ref it) => it
679 .rewrite(context, shape)
680 .map(|it_str| format!("impl {}", it_str)),
681 ast::TyKind::Err | ast::TyKind::Typeof(..) => unreachable!(),
687 bare_fn: &ast::BareFnTy,
689 context: &RewriteContext,
691 ) -> Option<String> {
692 let mut result = String::with_capacity(128);
694 if let Some(ref lifetime_str) = rewrite_lifetime_param(context, shape, &bare_fn.generic_params)
696 result.push_str("for<");
697 // 6 = "for<> ".len(), 4 = "for<".
698 // This doesn't work out so nicely for multiline situation with lots of
699 // rightward drift. If that is a problem, we could use the list stuff.
700 result.push_str(lifetime_str);
701 result.push_str("> ");
704 result.push_str(::utils::format_unsafety(bare_fn.unsafety));
706 result.push_str(&format_abi(
708 context.config.force_explicit_abi(),
712 result.push_str("fn");
714 let func_ty_shape = shape.offset_left(result.len())?;
716 let rewrite = format_function_type(
717 bare_fn.decl.inputs.iter(),
718 &bare_fn.decl.output,
719 bare_fn.decl.variadic,
725 result.push_str(&rewrite);
731 context: &RewriteContext,
739 // Try to join types in a single line
740 let joiner = match context.config.type_punctuation_density() {
741 TypeDensity::Compressed => "+",
742 TypeDensity::Wide => " + ",
744 let type_strs = items
746 .map(|item| item.rewrite(context, shape))
747 .collect::<Option<Vec<_>>>()?;
748 let result = type_strs.join(joiner);
749 if items.len() == 1 || (!result.contains('\n') && result.len() <= shape.width) {
753 // We need to use multiple lines.
754 let (type_strs, offset) = if need_indent {
755 // Rewrite with additional indentation.
756 let nested_shape = shape.block_indent(context.config.tab_spaces());
757 let type_strs = items
759 .map(|item| item.rewrite(context, nested_shape))
760 .collect::<Option<Vec<_>>>()?;
761 (type_strs, nested_shape.indent)
763 (type_strs, shape.indent)
766 let joiner = format!("{}+ ", offset.to_string_with_newline(context.config));
767 Some(type_strs.join(&joiner))
770 pub fn can_be_overflowed_type(context: &RewriteContext, ty: &ast::Ty, len: usize) -> bool {
772 ast::TyKind::Tup(..) => context.use_block_indent() && len == 1,
773 ast::TyKind::Rptr(_, ref mutty) | ast::TyKind::Ptr(ref mutty) => {
774 can_be_overflowed_type(context, &*mutty.ty, len)
780 /// Returns `None` if there is no `LifetimeDef` in the given generic parameters.
781 fn rewrite_lifetime_param(
782 context: &RewriteContext,
784 generic_params: &[ast::GenericParam],
785 ) -> Option<String> {
786 let result = generic_params
788 .filter(|p| p.is_lifetime_param())
789 .map(|lt| lt.rewrite(context, shape))
790 .collect::<Option<Vec<_>>>()?
792 if result.is_empty() {