gccrs
gccrs copied to clipboard
Parsing error when parsing unsafe blocks in macro transcribers
I tried this code:
// { dg-output "1\n2\nNaN\n3\n" }
macro_rules! print_num {
($l:literal) => {
unsafe { printf("%d\n\0" as *const str as *const i8, $l); }
};
}
extern "C" {
fn printf(s: *const i8, ...);
}
// Check to make sure that expanding macros does not break the flow of calls
fn main() -> i32 {
print_num!(1);
print_num!(2);
unsafe {
printf("NaN\n\0" as *const str as *const i8);
}
print_num!(3);
0
}
I expected to see this happen: no error
Instead, this happened:
gcc/testsuite/rust/execute/torture/macros22.rs:5:16: error: unexpected token ‘{’ in some sort of unsafe production
5 | unsafe { printf("%d\n\0" as *const str as *const i8, $l); }
| ^
The temporary fix is to wrap the unsafe block within another block. I suspect we're just missing a call somewhere in the macro transcriber parser
The error comes from the multiple calls to parse_stmt in transcribe_many_stmts. Since parse_stmt directly calls parse_vis_item when seeing UNSAFE tokens, the parser does not understand unsafe blocks: Simply unsafe functions, unsafe traits or unsafe impls.