propagate errors about failing to rewrite a macro

This commit is contained in:
Stéphane Campinas 2018-09-07 14:48:52 +02:00
parent 6ada5b51cc
commit 6f318e3cef
No known key found for this signature in database
GPG key ID: 6D5620D908210133
10 changed files with 228 additions and 4 deletions

View file

@ -522,6 +522,9 @@ 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

@ -747,6 +747,10 @@ 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);
@ -1106,6 +1110,10 @@ 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,6 +69,9 @@ 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)
}
}
@ -406,7 +409,15 @@ pub fn rewrite_macro_def(
";",
|branch| branch.span.lo(),
|branch| branch.span.hi(),
|branch| branch.rewrite(context, arm_shape, multi_branch_style),
|branch| match branch.rewrite(context, arm_shape, multi_branch_style) {
Some(v) => Some(v),
// if the rewrite returned None because a macro could not be rewritten, then return the
// original body
None if *context.macro_rewrite_failure.borrow() == true => {
Some(context.snippet(branch.body).trim().to_string())
}
None => None,
},
context.snippet_provider.span_after(span, "{"),
span.hi(),
false,

View file

@ -97,7 +97,8 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
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().clone();
let rewrite = self.with_context(|ctx| stmt.rewrite(&ctx, shape));
self.push_rewrite(stmt.span(), rewrite)
}
}
@ -350,11 +351,14 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
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.clone();
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.clone();
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) => {

View file

@ -0,0 +1,44 @@
macro_rules! atomic_bits {
// the println macro cannot be rewritten because of the asm macro
($type:ty, $ldrex:expr, $strex:expr) => {
impl AtomicBits for $type {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
unsafe fn store_excl(self, address: usize) -> bool {
let status: $type;
println!("{}",
status);
status == 0
}
}
};
// the println macro should be rewritten here
($type:ty) => {
fn some_func(self) {
let status: $type;
println!("{}", status);
}
};
// unrewritale macro in func
($type:ty, $ldrex:expr) => {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
}
}

View file

@ -0,0 +1,44 @@
macro_rules! atomic_bits {
// the println macro cannot be rewritten because of the asm macro
($type:ty, $ldrex:expr, $strex:expr) => {
trait $type {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
unsafe fn store_excl(self, address: usize) -> bool {
let status: $type;
println!("{}",
status);
status == 0
}
}
};
// the println macro should be rewritten here
($type:ty) => {
fn some_func(self) {
let status: $type;
println!("{}", status);
}
};
// unrewritale macro in func
($type:ty, $ldrex:expr) => {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
}
}

View file

@ -0,0 +1,11 @@
macro_rules! atomic_bits {
($ldrex:expr) => {
execute(|| {
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
})
};
}

View file

@ -0,0 +1,44 @@
macro_rules! atomic_bits {
// the println macro cannot be rewritten because of the asm macro
($type:ty, $ldrex:expr, $strex:expr) => {
impl AtomicBits for $type {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
unsafe fn store_excl(self, address: usize) -> bool {
let status: $type;
println!("{}",
status);
status == 0
}
}
};
// the println macro should be rewritten here
($type:ty) => {
fn some_func(self) {
let status: $type;
println!("{}", status);
}
};
// unrewritale macro in func
($type:ty, $ldrex:expr) => {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
}
}

View file

@ -0,0 +1,11 @@
macro_rules! atomic_bits {
($ldrex:expr) => {
some_macro!(pub fn foo() {
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
})
};
}

View file

@ -0,0 +1,44 @@
macro_rules! atomic_bits {
// the println macro cannot be rewritten because of the asm macro
($type:ty, $ldrex:expr, $strex:expr) => {
trait $type {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
unsafe fn store_excl(self, address: usize) -> bool {
let status: $type;
println!("{}",
status);
status == 0
}
}
};
// the println macro should be rewritten here
($type:ty) => {
fn some_func(self) {
let status: $type;
println!("{}", status);
}
};
// unrewritale macro in func
($type:ty, $ldrex:expr) => {
unsafe fn load_excl(address: usize) -> Self {
let raw: $type;
asm!($ldrex
: "=r"(raw)
: "r"(address)
:
: "volatile");
raw
}
}
}