about summary refs log tree commit diff
path: root/compiler/rustc_codegen_llvm/src/consts.rs
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2025-02-02 23:06:57 +0100
committerGitHub <noreply@github.com>2025-02-02 23:06:57 +0100
commitf5ae630f10d16630e673c3fb9f525a17f09a79eb (patch)
treedc4e986cad1e85ecfe16de64500bd677e630c480 /compiler/rustc_codegen_llvm/src/consts.rs
parent04299454762744d18f2e323b30dd35bfd90109e2 (diff)
parentb89263605a0e4c584f9fee40390cf224596420f3 (diff)
downloadrust-f5ae630f10d16630e673c3fb9f525a17f09a79eb.tar.gz
rust-f5ae630f10d16630e673c3fb9f525a17f09a79eb.zip
Rollup merge of #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
Diffstat (limited to 'compiler/rustc_codegen_llvm/src/consts.rs')
-rw-r--r--compiler/rustc_codegen_llvm/src/consts.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/compiler/rustc_codegen_llvm/src/consts.rs b/compiler/rustc_codegen_llvm/src/consts.rs
index 771ebf2057f..c6855dd42e5 100644
--- a/compiler/rustc_codegen_llvm/src/consts.rs
+++ b/compiler/rustc_codegen_llvm/src/consts.rs
@@ -420,8 +420,14 @@ impl<'ll> CodegenCx<'ll, '_> {
             let g = if val_llty == llty {
                 g
             } else {
-                // If we created the global with the wrong type,
-                // correct the type.
+                // codegen_static_initializer creates the global value just from the
+                // `Allocation` data by generating one big struct value that is just
+                // all the bytes and pointers after each other. This will almost never
+                // match the type that the static was declared with. Unfortunately
+                // we can't just LLVMConstBitCast our way out of it because that has very
+                // specific rules on what can be cast. So instead of adding a new way to
+                // generate static initializers that match the static's type, we picked
+                // the easier option and retroactively change the type of the static item itself.
                 let name = llvm::get_value_name(g).to_vec();
                 llvm::set_value_name(g, b"");