use config::lists::*;
use regex::Regex;
use rustc_target::spec::abi;
-use syntax::codemap::{self, BytePos, Span};
+use syntax::source_map::{self, BytePos, Span};
use syntax::visit;
use syntax::{ast, ptr, symbol};
-use codemap::{LineRangeUtils, SpanUtils};
use comment::{
combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
use overflow;
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
+use source_map::{LineRangeUtils, SpanUtils};
use spanned::Spanned;
use utils::*;
use vertical::rewrite_with_alignment;
use visitor::FmtVisitor;
-const DEFAULT_VISIBILITY: ast::Visibility = codemap::Spanned {
+const DEFAULT_VISIBILITY: ast::Visibility = source_map::Spanned {
node: ast::VisibilityKind::Inherited,
- span: codemap::DUMMY_SP,
+ span: source_map::DUMMY_SP,
};
fn type_annotation_separator(config: &Config) -> &str {
decl: &'a ast::FnDecl,
generics: &'a ast::Generics,
abi: abi::Abi,
+ is_async: ast::IsAsync,
constness: ast::Constness,
defaultness: ast::Defaultness,
unsafety: ast::Unsafety,
decl,
generics,
abi: abi::Abi::Rust,
+ is_async: ast::IsAsync::NotAsync,
constness: ast::Constness::NotConst,
defaultness: ast::Defaultness::Final,
unsafety: ast::Unsafety::Normal,
) -> FnSig<'a> {
FnSig {
unsafety: method_sig.header.unsafety,
+ is_async: method_sig.header.asyncness,
constness: method_sig.header.constness.node,
defaultness: ast::Defaultness::Final,
abi: method_sig.header.abi,
generics,
abi: fn_header.abi,
constness: fn_header.constness.node,
+ is_async: fn_header.asyncness,
defaultness,
unsafety: fn_header.unsafety,
visibility: visibility.clone(),
result.push_str(format_defaultness(self.defaultness));
result.push_str(format_constness(self.constness));
result.push_str(format_unsafety(self.unsafety));
+ result.push_str(format_async(self.is_async));
result.push_str(&format_abi(
self.abi,
context.config.force_explicit_abi(),
return None;
}
- let codemap = self.get_context().codemap;
+ let source_map = self.get_context().source_map;
if self.config.empty_item_single_line()
- && is_empty_block(block, None, codemap)
+ && is_empty_block(block, None, source_map)
&& self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
{
return Some(format!("{}{{}}", fn_str));
}
- if self.config.fn_single_line() && is_simple_block_stmt(block, None, codemap) {
+ if self.config.fn_single_line() && is_simple_block_stmt(block, None, source_map) {
let rewrite = {
if let Some(stmt) = block.stmts.first() {
match stmt_expr(stmt) {
}
let shape = self.shape().sub_width(2)?;
- let fmt = ListFormatting {
- tactic: DefinitiveListTactic::Vertical,
- separator: ",",
- trailing_separator: self.config.trailing_comma(),
- separator_place: SeparatorPlace::Back,
- shape,
- ends_with_newline: true,
- preserve_newline: true,
- nested: false,
- config: self.config,
- };
+ let fmt = ListFormatting::new(shape, self.config)
+ .trailing_separator(self.config.trailing_comma())
+ .preserve_newline(true);
let list = write_list(&items, &fmt)?;
result.push_str(&list);
buffer.push((self.buffer.clone(), item.clone()));
self.buffer.clear();
}
- // type -> const -> macro -> method
+ // type -> existential -> const -> macro -> method
use ast::ImplItemKind::*;
fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
match (a, b) {
- (Type(..), Type(..)) | (Const(..), Const(..)) => false,
+ (Type(..), Type(..))
+ | (Const(..), Const(..))
+ | (Existential(..), Existential(..)) => false,
_ => true,
}
}
buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
+ (Type(..), Type(..))
+ | (Const(..), Const(..))
+ | (Macro(..), Macro(..))
+ | (Existential(..), Existential(..)) => a.ident.as_str().cmp(&b.ident.as_str()),
+ (Method(..), Method(..)) => a.span.lo().cmp(&b.span.lo()),
(Type(..), _) => Ordering::Less,
(_, Type(..)) => Ordering::Greater,
+ (Existential(..), _) => Ordering::Less,
+ (_, Existential(..)) => Ordering::Greater,
(Const(..), _) => Ordering::Less,
(_, Const(..)) => Ordering::Greater,
(Macro(..), _) => Ordering::Less,
(_, Macro(..)) => Ordering::Greater,
- _ => a.span.lo().cmp(&b.span.lo()),
});
let mut prev_kind = None;
for (buf, item) in buffer {
result.push_str(format_defaultness(defaultness));
result.push_str(format_unsafety(unsafety));
- let lo = context.snippet_provider.span_after(item.span, "impl");
- let hi = match *trait_ref {
- Some(ref tr) => tr.path.span.lo(),
- None => self_ty.span.lo(),
- };
let shape = generics_shape_from_config(
context.config,
Shape::indented(offset + last_line_width(&result), context.config),
0,
)?;
- let generics_str = rewrite_generics(context, "impl", generics, shape, mk_sp(lo, hi))?;
+ let generics_str = rewrite_generics(context, "impl", generics, shape)?;
result.push_str(&generics_str);
let polarity_str = if polarity == ast::ImplPolarity::Negative {
let body_lo = context.snippet_provider.span_after(item.span, "{");
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
- let generics_str = rewrite_generics(
- context,
- rewrite_ident(context, item.ident),
- generics,
- shape,
- mk_sp(item.span.lo(), body_lo),
- )?;
+ let generics_str =
+ rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
result.push_str(&generics_str);
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
let alias = rewrite_ident(context, ident);
// 6 = "trait ", 2 = " ="
let g_shape = shape.offset_left(6)?.sub_width(2)?;
- let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?;
+ let generics_str = rewrite_generics(context, &alias, generics, g_shape)?;
let lhs = format!("trait {} =", generics_str);
// 1 = ";"
rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";")
Some(generics) => {
let budget = context.budget(last_line_width(&header_str));
let shape = Shape::legacy(budget, offset);
- let g_span = mk_sp(span.lo(), body_lo);
- let generics_str = rewrite_generics(context, "", generics, shape, g_span)?;
+ let generics_str = rewrite_generics(context, "", generics, shape)?;
result.push_str(&generics_str);
let where_budget = context.budget(last_line_width(&result));
Some(result)
}
-pub fn rewrite_type_alias(
+fn rewrite_type_prefix(
context: &RewriteContext,
indent: Indent,
+ prefix: &str,
ident: ast::Ident,
- ty: &ast::Ty,
generics: &ast::Generics,
- vis: &ast::Visibility,
- span: Span,
) -> Option<String> {
let mut result = String::with_capacity(128);
-
- result.push_str(&format_visibility(context, vis));
- result.push_str("type ");
+ result.push_str(prefix);
+ let ident_str = rewrite_ident(context, ident);
// 2 = `= `
- let g_shape = Shape::indented(indent, context.config)
- .offset_left(result.len())?
- .sub_width(2)?;
- let g_span = mk_sp(
- context.snippet_provider.span_after(span, "type"),
- ty.span.lo(),
- );
- let generics_str = rewrite_generics(
- context,
- rewrite_ident(context, ident),
- generics,
- g_shape,
- g_span,
- )?;
- result.push_str(&generics_str);
+ if generics.params.is_empty() {
+ result.push_str(ident_str)
+ } else {
+ let g_shape = Shape::indented(indent, context.config)
+ .offset_left(result.len())?
+ .sub_width(2)?;
+ let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
+ result.push_str(&generics_str);
+ }
let where_budget = context.budget(last_line_width(&result));
let option = WhereClauseOption::snuggled(&result);
Shape::legacy(where_budget, indent),
Density::Vertical,
"=",
- Some(span.hi()),
+ None,
generics.span.hi(),
option,
false,
)?;
result.push_str(&where_clause_str);
- if where_clause_str.is_empty() {
- result.push_str(" =");
+
+ Some(result)
+}
+
+fn rewrite_type_item<R: Rewrite>(
+ context: &RewriteContext,
+ indent: Indent,
+ prefix: &str,
+ suffix: &str,
+ ident: ast::Ident,
+ rhs: &R,
+ generics: &ast::Generics,
+ vis: &ast::Visibility,
+) -> Option<String> {
+ let mut result = String::with_capacity(128);
+ result.push_str(&rewrite_type_prefix(
+ context,
+ indent,
+ &format!("{}{} ", format_visibility(context, vis), prefix),
+ ident,
+ generics,
+ )?);
+
+ if generics.where_clause.predicates.is_empty() {
+ result.push_str(suffix);
} else {
- result.push_str(&format!(
- "{}=",
- indent.to_string_with_newline(context.config)
- ));
+ result.push_str(&indent.to_string_with_newline(context.config));
+ result.push_str(suffix.trim_left());
}
// 1 = ";"
- let ty_shape = Shape::indented(indent, context.config).sub_width(1)?;
- rewrite_assign_rhs(context, result, ty, ty_shape).map(|s| s + ";")
+ let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
+ rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
+}
+
+pub fn rewrite_type_alias(
+ context: &RewriteContext,
+ indent: Indent,
+ ident: ast::Ident,
+ ty: &ast::Ty,
+ generics: &ast::Generics,
+ vis: &ast::Visibility,
+) -> Option<String> {
+ rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
+}
+
+pub fn rewrite_existential_type(
+ context: &RewriteContext,
+ indent: Indent,
+ ident: ast::Ident,
+ generic_bounds: &ast::GenericBounds,
+ generics: &ast::Generics,
+ vis: &ast::Visibility,
+) -> Option<String> {
+ rewrite_type_item(
+ context,
+ indent,
+ "existential type",
+ ":",
+ ident,
+ generic_bounds,
+ generics,
+ vis,
+ )
}
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
}
}
+pub fn rewrite_existential_impl_type(
+ context: &RewriteContext,
+ ident: ast::Ident,
+ generic_bounds: &ast::GenericBounds,
+ indent: Indent,
+) -> Option<String> {
+ rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
+ .map(|s| format!("existential {}", s))
+}
+
pub fn rewrite_associated_impl_type(
ident: ast::Ident,
defaultness: ast::Defaultness,
ty_opt: Option<&ptr::P<ast::Ty>>,
- generic_bounds_opt: Option<&ast::GenericBounds>,
context: &RewriteContext,
indent: Indent,
) -> Option<String> {
- let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?;
+ let result = rewrite_associated_type(ident, ty_opt, None, context, indent)?;
match defaultness {
ast::Defaultness::Default => Some(format!("default {}", result)),
offset: used_width,
};
let fd = fn_sig.decl;
- let g_span = mk_sp(span.lo(), fd.output.span().lo());
let generics_str = rewrite_generics(
context,
rewrite_ident(context, ident),
fn_sig.generics,
shape,
- g_span,
)?;
result.push_str(&generics_str);
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
- let fmt = ListFormatting {
- tactic,
- separator: ",",
- trailing_separator: if variadic {
- SeparatorTactic::Never
- } else {
- trailing_comma
- },
- separator_place: SeparatorPlace::Back,
- shape: Shape::legacy(budget, indent),
- ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
- preserve_newline: true,
- nested: false,
- config: context.config,
+ let trailing_separator = if variadic {
+ SeparatorTactic::Never
+ } else {
+ trailing_comma
};
-
+ let fmt = ListFormatting::new(Shape::legacy(budget, indent), context.config)
+ .tactic(tactic)
+ .trailing_separator(trailing_separator)
+ .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
+ .preserve_newline(true);
write_list(&arg_items, &fmt)
}
ident: &str,
generics: &ast::Generics,
shape: Shape,
- span: Span,
) -> Option<String> {
// FIXME: convert bounds to where clauses where they get too big or if
// there is a where clause at all.
}
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
- overflow::rewrite_with_angle_brackets(context, ident, params, shape, span)
+ overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
}
pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
DefinitiveListTactic::Vertical
};
- let fmt = ListFormatting {
- tactic: shape_tactic,
- separator: ",",
- trailing_separator: comma_tactic,
- separator_place: SeparatorPlace::Back,
- shape: clause_shape,
- ends_with_newline: true,
- preserve_newline: true,
- nested: false,
- config: context.config,
- };
+ let fmt = ListFormatting::new(clause_shape, context.config)
+ .tactic(shape_tactic)
+ .trailing_separator(comma_tactic)
+ .preserve_newline(true);
let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
let comment_separator = |comment: &str, shape: Shape| {
comma_tactic = SeparatorTactic::Never;
}
- let fmt = ListFormatting {
- tactic,
- separator: ",",
- trailing_separator: comma_tactic,
- separator_place: SeparatorPlace::Back,
- shape: Shape::legacy(budget, offset),
- ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
- preserve_newline: true,
- nested: false,
- config: context.config,
- };
+ let fmt = ListFormatting::new(Shape::legacy(budget, offset), context.config)
+ .tactic(tactic)
+ .trailing_separator(comma_tactic)
+ .ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
+ .preserve_newline(true);
let preds_str = write_list(&item_vec, &fmt)?;
let end_length = if terminator == "{" {
used_width: usize,
) -> Option<String> {
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
- let mut result = rewrite_generics(context, "", generics, shape, span)?;
+ let mut result = rewrite_generics(context, "", generics, shape)?;
let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
let budget = context.budget(last_line_used_width(&result, offset.width()));