Skip to content

Commit 21f4355

Browse files
committed
Fix for "Support Option and similar enums as type of static variable with linkage attribute"
cc rust-lang/rust#104799
1 parent ef1cb95 commit 21f4355

File tree

1 file changed

+50
-27
lines changed

1 file changed

+50
-27
lines changed

src/constant.rs

Lines changed: 50 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,7 @@ fn data_id_for_static(
266266
def_id: DefId,
267267
definition: bool,
268268
) -> DataId {
269-
let rlinkage = tcx.codegen_fn_attrs(def_id).linkage;
270-
let linkage = if definition {
271-
crate::linkage::get_static_linkage(tcx, def_id)
272-
} else if rlinkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak)
273-
|| rlinkage == Some(rustc_middle::mir::mono::Linkage::WeakAny)
274-
{
275-
Linkage::Preemptible
276-
} else {
277-
Linkage::Import
278-
};
269+
let attrs = tcx.codegen_fn_attrs(def_id);
279270

280271
let instance = Instance::mono(tcx, def_id).polymorphize(tcx);
281272
let symbol_name = tcx.symbol_name(instance).name;
@@ -287,22 +278,30 @@ fn data_id_for_static(
287278
};
288279
let align = tcx.layout_of(ParamEnv::reveal_all().and(ty)).unwrap().align.pref.bytes();
289280

290-
let attrs = tcx.codegen_fn_attrs(def_id);
281+
if let Some(import_linkage) = attrs.import_linkage {
282+
assert!(!definition);
291283

292-
let data_id = match module.declare_data(
293-
&*symbol_name,
294-
linkage,
295-
is_mutable,
296-
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
297-
) {
298-
Ok(data_id) => data_id,
299-
Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
300-
"attempt to declare `{symbol_name}` as static, but it was already declared as function"
301-
)),
302-
Err(err) => Err::<_, _>(err).unwrap(),
303-
};
284+
let linkage = if import_linkage == rustc_middle::mir::mono::Linkage::ExternalWeak
285+
|| import_linkage == rustc_middle::mir::mono::Linkage::WeakAny
286+
{
287+
Linkage::Preemptible
288+
} else {
289+
Linkage::Import
290+
};
291+
292+
let data_id = match module.declare_data(
293+
&*symbol_name,
294+
linkage,
295+
is_mutable,
296+
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
297+
) {
298+
Ok(data_id) => data_id,
299+
Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
300+
"attempt to declare `{symbol_name}` as static, but it was already declared as function"
301+
)),
302+
Err(err) => Err::<_, _>(err).unwrap(),
303+
};
304304

305-
if rlinkage.is_some() {
306305
// Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141
307306
// Declare an internal global `extern_with_linkage_foo` which
308307
// is initialized with the address of `foo`. If `foo` is
@@ -324,10 +323,34 @@ fn data_id_for_static(
324323
Err(ModuleError::DuplicateDefinition(_)) => {}
325324
res => res.unwrap(),
326325
}
327-
ref_data_id
328-
} else {
329-
data_id
326+
327+
return ref_data_id;
330328
}
329+
330+
let linkage = if definition {
331+
crate::linkage::get_static_linkage(tcx, def_id)
332+
} else if attrs.linkage == Some(rustc_middle::mir::mono::Linkage::ExternalWeak)
333+
|| attrs.linkage == Some(rustc_middle::mir::mono::Linkage::WeakAny)
334+
{
335+
Linkage::Preemptible
336+
} else {
337+
Linkage::Import
338+
};
339+
340+
let data_id = match module.declare_data(
341+
&*symbol_name,
342+
linkage,
343+
is_mutable,
344+
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
345+
) {
346+
Ok(data_id) => data_id,
347+
Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
348+
"attempt to declare `{symbol_name}` as static, but it was already declared as function"
349+
)),
350+
Err(err) => Err::<_, _>(err).unwrap(),
351+
};
352+
353+
data_id
331354
}
332355

333356
fn define_all_allocs(tcx: TyCtxt<'_>, module: &mut dyn Module, cx: &mut ConstantCx) {

0 commit comments

Comments
 (0)