Skip to content

panic about linkage external and static variables _rust_extern_with_linkage_* #144940

@ywxt

Description

@ywxt

Code

This is simplified from https://github.com/rust-lang/rust/blob/master/tests/ui/linkage-attr/linkage-detect-local-generated-name-collision.rs

It's normal to report errors error: symbol `collision` is already defined if _rust_extern_with_linkage_collision before the main.

However once I moved _rust_extern_with_linkage_collision after the main function, I got a panic.

Playground

#![feature(linkage)]

unsafe extern "C" {
    #[linkage = "external"]
    #[no_mangle]
    pub static collision: *const i32;
}

// #[no_mangle]
// pub static _rust_extern_with_linkage_collision: i32 = 0;  // original code, before main.

pub fn main() {
    unsafe {
        println!("{:p}", &collision);
    }
}

#[no_mangle]
pub static _rust_extern_with_linkage_collision: i32 = 0;

Meta

rustc --version --verbose:

rustc 1.91.0-nightly (0060d5a2a 2025-08-04)
binary: rustc
commit-hash: 0060d5a2a8a86a31f6299311fe64b1d755a91c4f
commit-date: 2025-08-04
host: x86_64-pc-windows-msvc
release: 1.91.0-nightly
LLVM version: 20.1.8

Error output

warning: `#[no_mangle]` has no effect on a foreign static
 --> .\src\example.rs:5:5
  |
5 |     #[no_mangle]
  |     ^^^^^^^^^^^^ help: remove this attribute
6 |     pub static collision: *const i32;
  |     --------------------------------- foreign static
  |
  = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
  = note: symbol names in extern blocks are not mangled
  = note: `#[warn(unused_attributes)]` on by default

thread 'rustc' panicked at compiler\rustc_codegen_llvm\src\consts.rs:399:9:
assertion failed: llvm::LLVMGetInitializer(self.instances.borrow().get(&Instance::mono(self.tcx,
                        def_id)).unwrap()).is_none()
Backtrace

stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/0060d5a2a8a86a31f6299311fe64b1d755a91c4f/library\std\src\panicking.rs:697
   1: core::panicking::panic_fmt
             at /rustc/0060d5a2a8a86a31f6299311fe64b1d755a91c4f/library\core\src\panicking.rs:75
   2: core::panicking::panic
             at /rustc/0060d5a2a8a86a31f6299311fe64b1d755a91c4f/library\core\src\panicking.rs:145
   3: <rustc_codegen_llvm::context::GenericCx<rustc_codegen_llvm::context::FullCx> as rustc_codegen_ssa::traits::statics::StaticCodegenMethods>::codegen_static
   4: <rustc_target::callconv::FnAbi<rustc_middle::ty::Ty> as rustc_codegen_llvm::abi::FnAbiLlvmExt>::apply_attrs_callsite
   5: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::ExtraBackendMethods>::compile_codegen_unit
   6: std::vector<std::basic_string<char,std::char_traits<char>,std::allocator<char> >,std::allocator<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > >::_Construct_n<std::basic_string<char,std::char_traits<char>,std::allocator<char> > * __
   7: <rustc_codegen_llvm::LlvmCodegenBackend as rustc_codegen_ssa::traits::backend::CodegenBackend>::codegen_crate
   8: <rustc_interface::queries::Linker>::codegen_and_build_linker
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions