use config::{BraceStyle, Config};
use items::{
format_impl, format_trait, format_trait_alias, is_mod_decl, is_use_item,
- rewrite_associated_impl_type, rewrite_associated_type, rewrite_extern_crate,
- rewrite_type_alias, FnSig, StaticParts, StructParts,
+ rewrite_associated_impl_type, rewrite_associated_type, rewrite_existential_impl_type,
+ rewrite_existential_type, rewrite_extern_crate, rewrite_type_alias, FnSig, StaticParts,
+ StructParts,
};
use macros::{rewrite_macro, rewrite_macro_def, MacroPosition};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use spanned::Spanned;
-use utils::{self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec};
+use utils::{
+ self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec,
+ rewrite_ident, DEPR_SKIP_ANNOTATION,
+};
+use {ErrorKind, FormatReport, FormattingError};
use std::cell::RefCell;
pub snippet_provider: &'a SnippetProvider<'a>,
pub line_number: usize,
pub skipped_range: Vec<(usize, usize)>,
+ pub macro_rewrite_failure: bool,
+ pub(crate) report: FormatReport,
}
impl<'b, 'a: 'b> FmtVisitor<'a> {
self.block_indent = self.block_indent.block_indent(self.config);
self.push_str("{");
- if self.config.remove_blank_lines_at_start_or_end_of_block() {
- if let Some(first_stmt) = b.stmts.first() {
- let attr_lo = inner_attrs
- .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo()))
- .or_else(|| {
- // Attributes for an item in a statement position
- // do not belong to the statement. (rust-lang/rust#34459)
- if let ast::StmtKind::Item(ref item) = first_stmt.node {
- item.attrs.first()
- } else {
- first_stmt.attrs().first()
- }.and_then(|attr| {
- // Some stmts can have embedded attributes.
- // e.g. `match { #![attr] ... }`
- let attr_lo = attr.span.lo();
- if attr_lo < first_stmt.span.lo() {
- Some(attr_lo)
- } else {
- None
- }
- })
- });
-
- let snippet = self.snippet(mk_sp(
- self.last_pos,
- attr_lo.unwrap_or_else(|| first_stmt.span.lo()),
- ));
- let len = CommentCodeSlices::new(snippet)
- .nth(0)
- .and_then(|(kind, _, s)| {
- if kind == CodeCharKind::Normal {
- s.rfind('\n')
+ if let Some(first_stmt) = b.stmts.first() {
+ let attr_lo = inner_attrs
+ .and_then(|attrs| inner_attributes(attrs).first().map(|attr| attr.span.lo()))
+ .or_else(|| {
+ // Attributes for an item in a statement position
+ // do not belong to the statement. (rust-lang/rust#34459)
+ if let ast::StmtKind::Item(ref item) = first_stmt.node {
+ item.attrs.first()
+ } else {
+ first_stmt.attrs().first()
+ }.and_then(|attr| {
+ // Some stmts can have embedded attributes.
+ // e.g. `match { #![attr] ... }`
+ let attr_lo = attr.span.lo();
+ if attr_lo < first_stmt.span.lo() {
+ Some(attr_lo)
} else {
None
}
- });
- if let Some(len) = len {
- self.last_pos = self.last_pos + BytePos::from_usize(len);
- }
+ })
+ });
+
+ let snippet = self.snippet(mk_sp(
+ self.last_pos,
+ attr_lo.unwrap_or_else(|| first_stmt.span.lo()),
+ ));
+ let len = CommentCodeSlices::new(snippet)
+ .nth(0)
+ .and_then(|(kind, _, s)| {
+ if kind == CodeCharKind::Normal {
+ s.rfind('\n')
+ } else {
+ None
+ }
+ });
+ if let Some(len) = len {
+ self.last_pos = self.last_pos + BytePos::from_usize(len);
}
}
}
let mut remove_len = BytePos(0);
- if self.config.remove_blank_lines_at_start_or_end_of_block() {
- if let Some(stmt) = b.stmts.last() {
- let snippet = self.snippet(mk_sp(
- stmt.span.hi(),
- source!(self, b.span).hi() - brace_compensation,
- ));
- let len = CommentCodeSlices::new(snippet)
- .last()
- .and_then(|(kind, _, s)| {
- if kind == CodeCharKind::Normal && s.trim().is_empty() {
- Some(s.len())
- } else {
- None
- }
- });
- if let Some(len) = len {
- remove_len = BytePos::from_usize(len);
- }
+ if let Some(stmt) = b.stmts.last() {
+ let snippet = self.snippet(mk_sp(
+ stmt.span.hi(),
+ source!(self, b.span).hi() - brace_compensation,
+ ));
+ let len = CommentCodeSlices::new(snippet)
+ .last()
+ .and_then(|(kind, _, s)| {
+ if kind == CodeCharKind::Normal && s.trim().is_empty() {
+ Some(s.len())
+ } else {
+ None
+ }
+ });
+ if let Some(len) = len {
+ remove_len = BytePos::from_usize(len);
}
}
// Note that this only gets called for function definitions. Required methods
// on traits do not get handled here.
+ // FIXME(topecongiro) Format async fn (#2812).
fn visit_fn(
&mut self,
fk: visit::FnKind,
let indent = self.block_indent;
let block;
let rewrite = match fk {
- visit::FnKind::ItemFn(ident, _, _, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
+ visit::FnKind::ItemFn(ident, _, _, b) | visit::FnKind::Method(ident, _, _, b) => {
block = b;
self.rewrite_fn(
indent,
let filtered_attrs;
let mut attrs = &item.attrs;
match item.node {
+ // For use items, skip rewriting attributes. Just check for a skip attribute.
+ ast::ItemKind::Use(..) => {
+ if contains_skip(attrs) {
+ self.push_skipped_with_span(item.span());
+ return;
+ }
+ }
// Module is inline, in this case we treat it like any other item.
_ if !is_mod_decl(item) => {
if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
let rw = format_trait(&self.get_context(), item, self.block_indent);
self.push_rewrite(item.span, rw);
}
- ast::ItemKind::TraitAlias(ref generics, ref ty_param_bounds) => {
+ ast::ItemKind::TraitAlias(ref generics, ref generic_bounds) => {
let shape = Shape::indented(self.block_indent, self.config);
let rw = format_trait_alias(
&self.get_context(),
item.ident,
generics,
- ty_param_bounds,
+ generic_bounds,
shape,
);
self.push_rewrite(item.span, rw);
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => {
self.visit_static(&StaticParts::from_item(item));
}
- ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
+ ast::ItemKind::Fn(ref decl, fn_header, ref generics, ref body) => {
let inner_attrs = inner_attributes(&item.attrs);
self.visit_fn(
- visit::FnKind::ItemFn(item.ident, unsafety, constness, abi, &item.vis, body),
+ visit::FnKind::ItemFn(item.ident, fn_header, &item.vis, body),
generics,
decl,
item.span,
ty,
generics,
&item.vis,
- item.span,
+ );
+ self.push_rewrite(item.span, rewrite);
+ }
+ ast::ItemKind::Existential(ref generic_bounds, ref generics) => {
+ let rewrite = rewrite_existential_type(
+ &self.get_context(),
+ self.block_indent,
+ item.ident,
+ generic_bounds,
+ generics,
+ &item.vis,
);
self.push_rewrite(item.span, rewrite);
}
Some(&inner_attrs),
);
}
- ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
+ ast::TraitItemKind::Type(ref generic_bounds, ref type_default) => {
let rewrite = rewrite_associated_type(
ti.ident,
type_default.as_ref(),
- Some(type_param_bounds),
+ Some(generic_bounds),
&self.get_context(),
self.block_indent,
);
);
self.push_rewrite(ii.span, rewrite);
}
+ ast::ImplItemKind::Existential(ref generic_bounds) => {
+ let rewrite = rewrite_existential_impl_type(
+ &self.get_context(),
+ ii.ident,
+ generic_bounds,
+ self.block_indent,
+ );
+ self.push_rewrite(ii.span, rewrite);
+ }
ast::ImplItemKind::Macro(ref mac) => {
self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
}
// 1 = ;
let shape = self.shape().sub_width(1).unwrap();
- let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
+ let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos));
self.push_rewrite(mac.span, rewrite);
}
}
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
- FmtVisitor::from_codemap(ctx.parse_session, ctx.config, ctx.snippet_provider)
+ FmtVisitor::from_codemap(
+ ctx.parse_session,
+ ctx.config,
+ ctx.snippet_provider,
+ ctx.report.clone(),
+ )
}
- pub fn from_codemap(
+ pub(crate) fn from_codemap(
parse_session: &'a ParseSess,
config: &'a Config,
snippet_provider: &'a SnippetProvider,
+ report: FormatReport,
) -> FmtVisitor<'a> {
FmtVisitor {
parse_session,
snippet_provider,
line_number: 0,
skipped_range: vec![],
+ macro_rewrite_failure: false,
+ report,
}
}
// Returns true if we should skip the following item.
pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
+ for attr in attrs {
+ if attr.name() == DEPR_SKIP_ANNOTATION {
+ let file_name = self.codemap.span_to_filename(attr.span).into();
+ self.report.append(
+ file_name,
+ vec![FormattingError::from_span(
+ &attr.span,
+ &self.codemap,
+ ErrorKind::DeprecatedAttr,
+ )],
+ );
+ } else if attr.path.segments[0].ident.to_string() == "rustfmt" {
+ if attr.path.segments.len() == 1
+ || attr.path.segments[1].ident.to_string() != "skip"
+ {
+ let file_name = self.codemap.span_to_filename(attr.span).into();
+ self.report.append(
+ file_name,
+ vec![FormattingError::from_span(
+ &attr.span,
+ &self.codemap,
+ ErrorKind::BadAttr,
+ )],
+ );
+ }
+ }
+ }
if contains_skip(attrs) {
return true;
}
attrs: &[ast::Attribute],
is_internal: bool,
) {
- self.push_str(&*utils::format_visibility(vis));
+ let vis_str = utils::format_visibility(&self.get_context(), vis);
+ self.push_str(&*vis_str);
self.push_str("mod ");
- self.push_str(&ident.to_string());
+ // Calling `to_owned()` to work around borrow checker.
+ let ident_str = rewrite_ident(&self.get_context(), ident).to_owned();
+ self.push_str(&ident_str);
if is_internal {
match self.config.brace_style() {
}
pub fn skip_empty_lines(&mut self, end_pos: BytePos) {
- while let Some(pos) = self.snippet_provider
+ while let Some(pos) = self
+ .snippet_provider
.opt_span_after(mk_sp(self.last_pos, end_pos), "\n")
{
if let Some(snippet) = self.opt_snippet(mk_sp(self.last_pos, pos)) {
}
}
+ pub fn with_context<F>(&mut self, f: F) -> Option<String>
+ where
+ F: Fn(&RewriteContext) -> Option<String>,
+ {
+ let result;
+ let macro_rewrite_failure = {
+ let context = self.get_context();
+ result = f(&context);
+ unsafe { *context.macro_rewrite_failure.as_ptr() }
+ };
+ self.macro_rewrite_failure |= macro_rewrite_failure;
+ result
+ }
+
pub fn get_context(&self) -> RewriteContext {
RewriteContext {
parse_session: self.parse_session,
is_if_else_block: RefCell::new(false),
force_one_line_chain: RefCell::new(false),
snippet_provider: self.snippet_provider,
+ macro_rewrite_failure: RefCell::new(false),
+ report: self.report.clone(),
}
}
}