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 spanned::Spanned;
use utils::{
self, contains_skip, count_newlines, inner_attributes, mk_sp, ptr_vec_to_ref_vec,
- DEPR_SKIP_ANNOTATION,
+ rewrite_ident, DEPR_SKIP_ANNOTATION,
};
use {ErrorKind, FormatReport, FormattingError};
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,
}
// 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 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);
}
snippet_provider,
line_number: 0,
skipped_range: vec![],
+ macro_rewrite_failure: false,
report,
}
}
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);
+ let file_name = self.codemap.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
if attr.path.segments.len() == 1
|| attr.path.segments[1].ident.to_string() != "skip"
{
- let file_name = self.codemap.span_to_filename(attr.span);
+ let file_name = self.codemap.span_to_filename(attr.span).into();
self.report.append(
file_name,
vec![FormattingError::from_span(
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 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(),
}
}