Skip to content

Commit f5ae630

Browse files
authored
Rollup merge of rust-lang#136426 - oli-obk:push-nkpuulwurykn, r=compiler-errors
Explain why we retroactively change a static initializer to have a different type I keep getting confused about it and in turn confused `@GuillaumeGomez` while trying to explain it badly
2 parents 0429945 + b892636 commit f5ae630

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

compiler/rustc_codegen_llvm/src/consts.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,14 @@ impl<'ll> CodegenCx<'ll, '_> {
420420
let g = if val_llty == llty {
421421
g
422422
} else {
423-
// If we created the global with the wrong type,
424-
// correct the type.
423+
// codegen_static_initializer creates the global value just from the
424+
// `Allocation` data by generating one big struct value that is just
425+
// all the bytes and pointers after each other. This will almost never
426+
// match the type that the static was declared with. Unfortunately
427+
// we can't just LLVMConstBitCast our way out of it because that has very
428+
// specific rules on what can be cast. So instead of adding a new way to
429+
// generate static initializers that match the static's type, we picked
430+
// the easier option and retroactively change the type of the static item itself.
425431
let name = llvm::get_value_name(g).to_vec();
426432
llvm::set_value_name(g, b"");
427433

0 commit comments

Comments
 (0)