implement Drop from FmtVisitor in order to simplify failures passing

This commit is contained in:
Stéphane Campinas 2018-09-12 13:09:07 +02:00
parent 6f318e3cef
commit 1befc93ba0
No known key found for this signature in database
GPG Key ID: 6D5620D908210133
5 changed files with 22 additions and 18 deletions

View File

@ -522,9 +522,6 @@ pub fn rewrite_block_with_visitor(
let inner_attrs = attrs.map(inner_attributes);
let label_str = rewrite_label(label);
visitor.visit_block(block, inner_attrs.as_ref().map(|a| &**a), has_braces);
if visitor.macro_rewrite_failure {
context.macro_rewrite_failure.replace(true);
}
Some(format!("{}{}{}", prefix, label_str, visitor.buffer))
}

View File

@ -175,7 +175,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
}
self.handler
.handle_formatted_file(path, visitor.buffer, &mut self.report)
.handle_formatted_file(path, visitor.buffer.to_owned(), &mut self.report)
}
}

View File

@ -747,10 +747,6 @@ pub fn format_impl(
visitor.format_missing(item.span.hi() - BytePos(1));
if visitor.macro_rewrite_failure {
context.macro_rewrite_failure.replace(true);
}
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
@ -1110,10 +1106,6 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
visitor.format_missing(item.span.hi() - BytePos(1));
if visitor.macro_rewrite_failure {
context.macro_rewrite_failure.replace(true);
}
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);

View File

@ -69,10 +69,7 @@ impl Rewrite for ast::Item {
visitor.block_indent = shape.indent;
visitor.last_pos = self.span().lo();
visitor.visit_item(self);
if visitor.macro_rewrite_failure {
context.macro_rewrite_failure.replace(true);
}
Some(visitor.buffer)
Some(visitor.buffer.to_owned())
}
}

View File

@ -60,6 +60,7 @@ impl<'a> SnippetProvider<'a> {
}
pub struct FmtVisitor<'a> {
parent_context: Option<&'a RewriteContext<'a>>,
pub parse_session: &'a ParseSess,
pub source_map: &'a SourceMap,
pub buffer: String,
@ -75,7 +76,21 @@ pub struct FmtVisitor<'a> {
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)
}
@ -579,12 +594,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
}
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(
@ -594,6 +611,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
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),