Skip to content

Commit 3d5528c

Browse files
committed
Auto merge of rust-lang#123310 - compiler-errors:nested-static-codegen-attrs, r=oli-obk
Don't inherit codegen attrs from parent static Putting this up partly for discussion and partly for review. Specifically, in rust-lang#121644, `@oli-obk` designed a system that creates new static items for representing nested allocations in statics. However, in that PR, oli made it so that these statics inherited the codegen attrs from the parent. This causes problems such as colliding symbols with `#[export_name]` and ICEs with `#[no_mangle]` since these synthetic statics have no `tcx.item_name(..)`. So the question is, is there any case where we *do* want to inherit codegen attrs from the parent? The only one that seems a bit suspicious is the thread-local attribute. And there may be some interesting interactions with the coverage attributes as well... Fixes (after backport) rust-lang#123274. Fixes rust-lang#123243. cc rust-lang#121644. r? `@oli-obk` cc `@nnethercote` `@RalfJung` (reviewers on that pr)
2 parents 871df0d + 4ff8a9b commit 3d5528c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

compiler/rustc_const_eval/src/interpret/intern.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_ast::Mutability;
1818
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
1919
use rustc_errors::ErrorGuaranteed;
2020
use rustc_hir as hir;
21+
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
2122
use rustc_middle::mir::interpret::{ConstAllocation, CtfeProvenance, InterpResult};
2223
use rustc_middle::query::TyCtxtAt;
2324
use rustc_middle::ty::layout::TyAndLayout;
@@ -106,13 +107,17 @@ fn intern_as_new_static<'tcx>(
106107
DefKind::Static { mutability: alloc.0.mutability, nested: true },
107108
);
108109
tcx.set_nested_alloc_id_static(alloc_id, feed.def_id());
109-
feed.codegen_fn_attrs(tcx.codegen_fn_attrs(static_id).clone());
110+
111+
// These do not inherit the codegen attrs of the parent static allocation, since
112+
// it doesn't make sense for them to inherit their `#[no_mangle]` and `#[link_name = ..]`
113+
// and the like.
114+
feed.codegen_fn_attrs(CodegenFnAttrs::new());
115+
110116
feed.eval_static_initializer(Ok(alloc));
111117
feed.generics_of(tcx.generics_of(static_id).clone());
112118
feed.def_ident_span(tcx.def_ident_span(static_id));
113119
feed.explicit_predicates_of(tcx.explicit_predicates_of(static_id));
114-
115-
feed.feed_hir()
120+
feed.feed_hir();
116121
}
117122

118123
/// How a constant value should be interned.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//@ build-pass
2+
3+
// Make sure that the nested static allocation for `FOO` doesn't inherit `no_mangle`.
4+
#[no_mangle]
5+
pub static mut FOO: &mut [i32] = &mut [42];
6+
7+
// Make sure that the nested static allocation for `BAR` doesn't inherit `export_name`.
8+
#[export_name = "BAR_"]
9+
pub static mut BAR: &mut [i32] = &mut [42];
10+
11+
fn main() {}

0 commit comments

Comments
 (0)