Skip to content

rustc runs out of memory after reporting faulty macro #33136

Closed
@Mixthos

Description

@Mixthos

Rustc runs out of memory when parsing this (faulty) code:

#[macro_export]
macro_rules! self_referencing {
    ($sname:ty, { $( $field:ident = $val:expr; )* }) => {{
        use std::ptr;
        use std::mem;
        use std::sync::Arc;

        let mut arc = Arc::new($sname {
            $( $field: unsafe { mem::uninitialized() } ),*
        });

        let mut pointer: *mut $sname = Arc::get_mut(&mut arc).unwrap();

        {$(
            println!("m1 {}", stringify!($field));
            let $field = $val;
            println!("m2");
            unsafe { ptr::write(&mut (*pointer).$field, $field); }
            println!("m3");
            let $field = & unsafe { &(*pointer).$field };
            println!("m4");
        )*}

        arc
    }};
}


#[cfg(test)]
#[allow(dead_code)]
#[allow(unused_variables)]
#[allow(unused_mut)]
mod tests {
    #[test]
    fn struct_names() {
        struct S1 {}
        struct S2 {}

        self_referencing!(S1, {});
        self_referencing!(S2, {});
    }
}

I ran this command:

$ RUST_BACKTRACE=1 cargo test
   Compiling self-ref v0.1.1 (file:///D:/Programming/Projects/Mine/self-ref)
src\lib.rs:8:32: 8:38 error: unexpected token: `S1`
src\lib.rs:8         let mut arc = Arc::new($sname {
                                            ^~~~~~
src\lib.rs:8:39: 8:40 error: expected one of `.`, `;`, `?`, or an operator, found `{`
src\lib.rs:8         let mut arc = Arc::new($sname {
                                                   ^

Then it allocated extreme amounts of memory and after a few minutes this happened:

fatal runtime error: out of memory
error: Could not compile `self-ref`.

Meta

rustc 1.10.0-nightly (ed7c56796 2016-04-17)
binary: rustc
commit-hash: ed7c56796ef17f13227a50dc1a72a018b1d5e33f
commit-date: 2016-04-17
host: x86_64-pc-windows-gnu
release: 1.10.0-nightly

The same thing happens on 1.8 stable GNU, though.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions