Skip to content

Commit 5c2ab46

Browse files
committed
Fix wasm_import_module attribute cross-crate
This commit fixes an accidental regression from rust-lang#144678 where wasm targets would now accidentally use the wrong import module map for a symbol causing a symbol to skip mangling. This can result in compilation failures when symbols are used in cross-crate situations. Closes rust-lang#148347
1 parent 17e7324 commit 5c2ab46

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

compiler/rustc_symbol_mangling/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ fn compute_symbol_name<'tcx>(
228228
// However, we don't have the wasm import module map there yet.
229229
tcx.is_foreign_item(def_id)
230230
&& tcx.sess.target.is_like_wasm
231-
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&def_id.into())
231+
&& tcx.wasm_import_module_map(def_id.krate).contains_key(&def_id.into())
232232
};
233233

234234
if !wasm_import_module_exception_force_mangling {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![no_std]
2+
3+
#[link(wasm_import_module = "test")]
4+
unsafe extern "C" {
5+
#[link_name = "close"]
6+
pub fn close(x: u32) -> u32;
7+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//@ only-wasm32
2+
//@ aux-build:link-name-in-foreign-crate.rs
3+
//@ compile-flags: --crate-type cdylib
4+
//@ build-pass
5+
//@ no-prefer-dynamic
6+
7+
extern crate link_name_in_foreign_crate;
8+
9+
// This test that the definition of a function named `close`, which collides
10+
// with the `close` function in libc in theory, is handled correctly in
11+
// cross-crate situations. The `link_name_in_foreign_crate` dependency declares
12+
// `close` from a non-`env` wasm import module and then this crate attempts to
13+
// use the symbol. This should properly ensure that the wasm module name is
14+
// tagged as `test` and the `close` symbol, to LLD, is mangled, to avoid
15+
// colliding with the `close` symbol in libc itself.
16+
17+
#[unsafe(no_mangle)]
18+
pub extern "C" fn foo() {
19+
unsafe {
20+
link_name_in_foreign_crate::close(1);
21+
}
22+
}

0 commit comments

Comments
 (0)