Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_symbol_mangling/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ fn compute_symbol_name<'tcx>(
// However, we don't have the wasm import module map there yet.
tcx.is_foreign_item(def_id)
&& tcx.sess.target.is_like_wasm
&& tcx.wasm_import_module_map(LOCAL_CRATE).contains_key(&def_id.into())
&& tcx.wasm_import_module_map(def_id.krate).contains_key(&def_id.into())
};

if !wasm_import_module_exception_force_mangling {
Expand Down
7 changes: 7 additions & 0 deletions tests/ui/wasm/auxiliary/link-name-in-foreign-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![no_std]

#[link(wasm_import_module = "test")]
unsafe extern "C" {
#[link_name = "close"]
pub fn close(x: u32) -> u32;
}
22 changes: 22 additions & 0 deletions tests/ui/wasm/wasm-link-name-in-foreign-crate-respected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//@ only-wasm32
//@ aux-build:link-name-in-foreign-crate.rs
//@ compile-flags: --crate-type cdylib
//@ build-pass
//@ no-prefer-dynamic

extern crate link_name_in_foreign_crate;

// This test that the definition of a function named `close`, which collides
// with the `close` function in libc in theory, is handled correctly in
// cross-crate situations. The `link_name_in_foreign_crate` dependency declares
// `close` from a non-`env` wasm import module and then this crate attempts to
// use the symbol. This should properly ensure that the wasm module name is
// tagged as `test` and the `close` symbol, to LLD, is mangled, to avoid
// colliding with the `close` symbol in libc itself.

#[unsafe(no_mangle)]
pub extern "C" fn foo() {
unsafe {
link_name_in_foreign_crate::close(1);
}
}
Loading