Skip to content

ICE: Can't combine functions in the same CGU that reference both a static and an extern static with the same name #66464

Closed
@rinon

Description

@rinon

The following code, when compiled in release mode with -C codegen-units=2 ICEs with the error below (I initially found this with the default 16 CGUs on a much larger codebase). I'm not entirely sure that the check at https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/consts.rs#L236 is needed. However, if we're moving a reference to a static to a different CGU, we shouldn't define the static at all, but only declare it. Can we get rid of the match across Item or ForeignItem and treat all references to a static in a different CGU as if it were foreign, regardless of whether it was foreign in HIR?

error: internal compiler error: src/librustc_codegen_llvm/consts.rs:237: Conflicting symbol names for static?
--> src/main.rs:26:5
   |
26 |     pub static mut external: u32 = 0;
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
mod a {
    extern "C" {
        #[no_mangle]
        pub static mut external: u32;
    }

    #[no_mangle]
    pub fn foo() {
        unsafe { dbg!(&external as *const u32); }
    }
}

mod b {
    mod c {
        #[no_mangle]
        pub fn bar() {
            unsafe { dbg!(&external as *const u32); }
        }

        use super::external;
    }

    pub use c::bar;

    #[no_mangle]
    pub static mut external: u32 = 0;
}

fn main() {
    a::foo();
    b::bar();
    chonky_func();
    chonky_func2();
}

fn chonky_func() {
    println!("We need to make the main module big enough");
    println!("so it forces c and a to get combined");
}

fn chonky_func2() {
    println!("Here's more stuff to print");
    println!("And more");
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: Code generationA-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️P-highHigh priorityT-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