Skip to content

Commit e775fdc

Browse files
committed
Don't try to get a ty for a nested allocation
Fixes rust-lang/rustc_codegen_cranelift#1464
1 parent 9f162c4 commit e775fdc

File tree

2 files changed

+37
-13
lines changed

2 files changed

+37
-13
lines changed

example/std_example.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,14 @@ fn main() {
167167
transmute_fat_pointer();
168168

169169
rust_call_abi();
170+
171+
const fn no_str() -> Option<Box<str>> {
172+
None
173+
}
174+
175+
static STATIC_WITH_MAYBE_NESTED_BOX: &Option<Box<str>> = &no_str();
176+
177+
println!("{:?}", STATIC_WITH_MAYBE_NESTED_BOX);
170178
}
171179

172180
fn panic(_: u128) {

src/constant.rs

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ pub(crate) fn codegen_tls_ref<'tcx>(
5353
let call = fx.bcx.ins().call(func_ref, &[]);
5454
fx.bcx.func.dfg.first_result(call)
5555
} else {
56-
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
56+
let data_id = data_id_for_static(
57+
fx.tcx, fx.module, def_id, false,
58+
// For a declaration the stated mutability doesn't matter.
59+
false,
60+
);
5761
let local_data_id = fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
5862
if fx.clif_comments.enabled() {
5963
fx.add_comment(local_data_id, format!("tls {:?}", def_id));
@@ -164,7 +168,11 @@ pub(crate) fn codegen_const_value<'tcx>(
164168
}
165169
GlobalAlloc::Static(def_id) => {
166170
assert!(fx.tcx.is_static(def_id));
167-
let data_id = data_id_for_static(fx.tcx, fx.module, def_id, false);
171+
let data_id = data_id_for_static(
172+
fx.tcx, fx.module, def_id, false,
173+
// For a declaration the stated mutability doesn't matter.
174+
false,
175+
);
168176
let local_data_id =
169177
fx.module.declare_data_in_func(data_id, &mut fx.bcx.func);
170178
if fx.clif_comments.enabled() {
@@ -232,21 +240,19 @@ fn data_id_for_static(
232240
module: &mut dyn Module,
233241
def_id: DefId,
234242
definition: bool,
243+
definition_writable: bool,
235244
) -> DataId {
236245
let attrs = tcx.codegen_fn_attrs(def_id);
237246

238247
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
239248
let symbol_name = tcx.symbol_name(instance).name;
240-
let ty = instance.ty(tcx, ParamEnv::reveal_all());
241-
let is_mutable = if tcx.is_mutable_static(def_id) {
242-
true
243-
} else {
244-
!ty.is_freeze(tcx, ParamEnv::reveal_all())
245-
};
246-
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();
247249

248250
if let Some(import_linkage) = attrs.import_linkage {
249251
assert!(!definition);
252+
assert!(!tcx.is_mutable_static(def_id));
253+
254+
let ty = instance.ty(tcx, ParamEnv::reveal_all());
255+
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();
250256

251257
let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak
252258
|| import_linkage == rustc_middle::mir::mono::Linkage::WeakAny
@@ -259,7 +265,7 @@ fn data_id_for_static(
259265
let data_id = match module.declare_data(
260266
symbol_name,
261267
linkage,
262-
is_mutable,
268+
false,
263269
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
264270
) {
265271
Ok(data_id) => data_id,
@@ -307,7 +313,7 @@ fn data_id_for_static(
307313
let data_id = match module.declare_data(
308314
symbol_name,
309315
linkage,
310-
is_mutable,
316+
definition_writable,
311317
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
312318
) {
313319
Ok(data_id) => data_id,
@@ -341,7 +347,13 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
341347

342348
let alloc = tcx.eval_static_initializer(def_id).unwrap();
343349

344-
let data_id = data_id_for_static(tcx, module, def_id, true);
350+
let data_id = data_id_for_static(
351+
tcx,
352+
module,
353+
def_id,
354+
true,
355+
alloc.inner().mutability == Mutability::Mut,
356+
);
345357
(data_id, alloc, section_name)
346358
}
347359
};
@@ -421,7 +433,11 @@ fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut Constant
421433
// Don't push a `TodoItem::Static` here, as it will cause statics used by
422434
// multiple crates to be duplicated between them. It isn't necessary anyway,
423435
// as it will get pushed by `codegen_static` when necessary.
424-
data_id_for_static(tcx, module, def_id, false)
436+
data_id_for_static(
437+
tcx, module, def_id, false,
438+
// For a declaration the stated mutability doesn't matter.
439+
false,
440+
)
425441
}
426442
};
427443

0 commit comments

Comments
 (0)