gccrs icon indicating copy to clipboard operation
gccrs copied to clipboard

Parsing error when parsing unsafe blocks in macro transcribers

Open CohenArthur opened this issue 3 years ago • 2 comments

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); }
      |                ^

CohenArthur avatar Jul 28 '22 08:07 CohenArthur

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

CohenArthur avatar Jul 28 '22 08:07 CohenArthur

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.

CohenArthur avatar Aug 16 '22 12:08 CohenArthur