Change:
<!-- Thank you for finding an Internal Compiler Error! If possible, try to provide a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for how to create smaller examples. -- <!-- If you're using the stable version of the compiler, you should also check if the bug also exists in the beta or nightly versions. --:
Forgive the unsafe mess :)
Using a self built rustc, I get this (already ered): I also got this recently:
I managed to recude the code a lot. The problem is, that the error happens on random. I'm not sure why or what, but when running it ten times () it happens maybe once... would you mind pinging the LLVM crew? I don't know what to do here :(
With a custom built llvm (and asserts on) I will now get this error every time
ping llvm
Hey LLVM ICE-breakers! This bug has been identified as a good "LLVM ICE-breaking candidate". In case it's useful, here are some [instructions] for tackling these sorts of bugs. Maybe take a look? Thanks! <3 [instructions]: https://rustc-dev- cc
So, I got this: The key is to have two identical named extern variables with a different type. They have be called and inlined for this to work. I'm not sure if bisecting here is helpful because it will probably bisect to the LLVM12 merge. But the LLVM people here on the repo should be able to find something :) Have fun
Still spurious.
Yes and no. So the key here is to have a LLVM backend with asserts enabled. There are the -alt builds which have asserts enabled and they give me the same result as my local built rustc (which is unsurprising and expected ;) ) You can use to install them.
Assigning priority based on the discussion for in the of the Prioritization Working Group. label +regression-from-stable-to-stable +P-high
Still an issue even with the LLVM13 upgrade :/
Valgrind gave me a little more clue where this is coming from for . (Assuming it is indeed the same issue, but it seems likely.)
seems to be fixed.. thank you!

Discussion:
extern "C" LLVMValueRef LLVMRustGetOrInsertGlobal(LLVMModuleRef M, const char *Name, size_t NameLen, LLVMTypeRef Ty) { <ins> Module *Mod = unwrap(M); </ins> StringRef NameRef(Name, NameLen); <del> return wrap(unwrap(M)->getOrInsertGlobal(NameRef, unwrap(Ty))); </del> <ins> // We don't use Module::getOrInsertGlobal because that returns a Constant*, // which may either be the real GlobalVariable*, or a constant bitcast of it // if our type doesn't match the original declaration. We always want the // GlobalVariable* so we can access linkage, visibility, etc. GlobalVariable *GV = Mod->getGlobalVariable(NameRef, true); if (!GV) GV = new GlobalVariable(*Mod, unwrap(Ty), false, GlobalValue::ExternalLinkage, nullptr, NameRef); return wrap(GV); </ins> } extern "C" LLVMValueRef

<ins> // build-pass // compile-flags: --crate-type=rlib --emit=llvm-ir -Cno-prepopulate-passes // This test declares globals by the same name with different types, which // caused problems because Module::getOrInsertGlobal would return a Constant* // bitcast instead of a GlobalVariable* that could access linkage/visibility. // In alt builds with LLVM assertions this would fail: // // rustc: /checkout/src/llvm-project/llvm/include/llvm/Support/Casting.h:269: // typename cast_retty<X, Y *>::ret_type llvm::cast(Y *) [X = llvm::GlobalValue, Y = llvm::Value]: // Assertion `isa<X>(Val) && "cast<Ty>() argument of incompatible type!"' failed. // // In regular builds, the bad cast was UB, like "Invalid LLVMRustVisibility value!" pub mod before { #[no_mangle] pub static GLOBAL1: [u8; 1] = [1]; } pub mod inner { extern "C" { pub static GLOBAL1: u8; pub static GLOBAL2: u8; } pub fn call() { drop(unsafe { (GLOBAL1, GLOBAL2) }); } } pub mod after { #[no_mangle] pub static GLOBAL2: [u8; 1] = [2]; } </ins>

<ins> // build-pass // compile-flags: --crate-type=rlib --emit=llvm-ir -Cno-prepopulate-passes // This is a variant of issue-91050-1.rs -- see there for an explanation. pub mod before { extern "C" { pub static GLOBAL1: [u8; 1]; } pub unsafe fn do_something_with_array() -> u8 { GLOBAL1[0] } } pub mod inner { extern "C" { pub static GLOBAL1: u8; } pub unsafe fn call() -> u8 { GLOBAL1 + 42 } } </ins>