Skip to content

Commit 3207c9f

Browse files
committed
Report an error on incompatible symbol definitions
1 parent a6b602d commit 3207c9f

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/abi/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod comments;
44
mod pass_mode;
55
mod returning;
66

7+
use cranelift_module::ModuleError;
78
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
89
use rustc_middle::ty::layout::FnAbiOf;
910
use rustc_target::abi::call::{Conv, FnAbi};
@@ -69,7 +70,17 @@ pub(crate) fn import_function<'tcx>(
6970
) -> FuncId {
7071
let name = tcx.symbol_name(inst).name;
7172
let sig = get_function_sig(tcx, module.isa().triple(), inst);
72-
module.declare_function(name, Linkage::Import, &sig).unwrap()
73+
match module.declare_function(name, Linkage::Import, &sig) {
74+
Ok(func_id) => func_id,
75+
Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
76+
"attempt to declare `{name}` as function, but it was already declared as static"
77+
)),
78+
Err(ModuleError::IncompatibleSignature(_, prev_sig, new_sig)) => tcx.sess.fatal(&format!(
79+
"attempt to declare `{name}` with signature {new_sig:?}, \
80+
but it was already declared with signature {prev_sig:?}"
81+
)),
82+
Err(err) => Err::<_, _>(err).unwrap(),
83+
}
7384
}
7485

7586
impl<'tcx> FunctionCx<'_, '_, 'tcx> {

src/constant.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,18 @@ fn data_id_for_static(
316316

317317
let attrs = tcx.codegen_fn_attrs(def_id);
318318

319-
let data_id = module
320-
.declare_data(
321-
&*symbol_name,
322-
linkage,
323-
is_mutable,
324-
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
325-
)
326-
.unwrap();
319+
let data_id = match module.declare_data(
320+
&*symbol_name,
321+
linkage,
322+
is_mutable,
323+
attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL),
324+
) {
325+
Ok(data_id) => data_id,
326+
Err(ModuleError::IncompatibleDeclaration(_)) => tcx.sess.fatal(&format!(
327+
"attempt to declare `{symbol_name}` as static, but it was already declared as function"
328+
)),
329+
Err(err) => Err::<_, _>(err).unwrap(),
330+
};
327331

328332
if rlinkage.is_some() {
329333
// Comment copied from https://github.com/rust-lang/rust/blob/45060c2a66dfd667f88bd8b94261b28a58d85bd5/src/librustc_codegen_llvm/consts.rs#L141

0 commit comments

Comments
 (0)