}
pub struct FmtVisitor<'a> {
+ parent_context: Option<&'a RewriteContext<'a>>,
pub parse_session: &'a ParseSess,
pub source_map: &'a SourceMap,
pub buffer: String,
pub(crate) report: FormatReport,
}
+impl<'a> Drop for FmtVisitor<'a> {
+ fn drop(&mut self) {
+ if let Some(ctx) = self.parent_context {
+ if self.macro_rewrite_failure {
+ ctx.macro_rewrite_failure.replace(true);
+ }
+ }
+ }
+}
+
impl<'b, 'a: 'b> FmtVisitor<'a> {
+ fn set_parent_context(&mut self, context: &'a RewriteContext) {
+ self.parent_context = Some(context);
+ }
+
pub fn shape(&self) -> Shape {
Shape::indented(self.block_indent, self.config)
}
if contains_skip(get_attrs_from_stmt(stmt)) {
self.push_skipped_with_span(stmt.span());
} else {
- let rewrite = stmt.rewrite(&self.get_context(), self.shape());
+ let shape = self.shape();
+ let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
self.push_rewrite(stmt.span(), rewrite)
}
}
item.attrs.first()
} else {
first_stmt.attrs().first()
- }.and_then(|attr| {
+ }
+ .and_then(|attr| {
// Some stmts can have embedded attributes.
// e.g. `match { #![attr] ... }`
let attr_lo = attr.span.lo();
let where_span_end = snippet
.find_uncommented("{")
.map(|x| BytePos(x as u32) + source!(self, item.span).lo());
- let rw = format_impl(&self.get_context(), item, self.block_indent, where_span_end);
+ let block_indent = self.block_indent;
+ let rw =
+ self.with_context(|ctx| format_impl(&ctx, item, block_indent, where_span_end));
self.push_rewrite(item.span, rw);
}
ast::ItemKind::Trait(..) => {
- let rw = format_trait(&self.get_context(), item, self.block_indent);
+ let block_indent = self.block_indent;
+ let rw = self.with_context(|ctx| format_trait(&ctx, item, block_indent));
self.push_rewrite(item.span, rw);
}
ast::ItemKind::TraitAlias(ref generics, ref generic_bounds) => {
let rewrite = rewrite_associated_type(
ti.ident,
type_default.as_ref(),
+ &ti.generics,
Some(generic_bounds),
&self.get_context(),
self.block_indent,
ii.ident,
ii.defaultness,
Some(ty),
+ &ii.generics,
&self.get_context(),
self.block_indent,
);
let rewrite = rewrite_existential_impl_type(
&self.get_context(),
ii.ident,
+ &ii.generics,
generic_bounds,
self.block_indent,
);
}
pub fn from_context(ctx: &'a RewriteContext) -> FmtVisitor<'a> {
- FmtVisitor::from_source_map(
+ let mut visitor = FmtVisitor::from_source_map(
ctx.parse_session,
ctx.config,
ctx.snippet_provider,
ctx.report.clone(),
- )
+ );
+ visitor.set_parent_context(ctx);
+ visitor
}
pub(crate) fn from_source_map(
report: FormatReport,
) -> FmtVisitor<'a> {
FmtVisitor {
+ parent_context: None,
parse_session,
source_map: parse_session.source_map(),
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
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.source_map.span_to_filename(attr.span).into();
- self.report.append(
- file_name,
- vec![FormattingError::from_span(
- attr.span,
- &self.source_map,
- ErrorKind::BadAttr,
- )],
- );
- }
+ } else if attr.path.segments[0].ident.to_string() == "rustfmt"
+ && (attr.path.segments.len() == 1
+ || attr.path.segments[1].ident.to_string() != "skip")
+ {
+ let file_name = self.source_map.span_to_filename(attr.span).into();
+ self.report.append(
+ file_name,
+ vec![FormattingError::from_span(
+ attr.span,
+ &self.source_map,
+ ErrorKind::BadAttr,
+ )],
+ );
}
}
if contains_skip(attrs) {
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;
+ let context = self.get_context();
+ let result = f(&context);
+ self.macro_rewrite_failure |= *context.macro_rewrite_failure.borrow();
result
}