Skip to content

rustc: wasm_import_module confuses module names of identically-named functions #50021

Description

@lostman

The following code illustrates the issue:

#![allow(dead_code, unused_imports)]
#![feature(lang_items, core_intrinsics)]
#![feature(custom_attribute)] // <- required for wasm_import_module
#![feature(wasm_import_module)]
#![no_std]
use core::intrinsics;

#[lang = "panic_fmt"]
#[no_mangle]
pub extern fn rust_begin_panic(
    _msg: core::fmt::Arguments,
    _file: &'static str,
    _line: u32,
    _column: u32) -> ! {
        unsafe { intrinsics::abort() }
}

mod m1 {
    #[wasm_import_module = "m1"]
    extern "C" {
        pub fn f();
    }
    #[wasm_import_module = "m1"]
    extern "C" {
        pub fn g();
    }
}

mod m2 {
    #[wasm_import_module = "m2"]
    extern "C" {
        pub fn f(_: i32);
    }
}

#[no_mangle]
pub unsafe fn run() {
    m1::g();

    // In generated code, expected:
    // (import "m2" "f" (func $f (param i32)))
    // but got:
    // (import "m1" "f" (func $f (param i32)))
    m2::f(0);
}

To compile:

❯ rustc \
--crate-type=cdylib \
--target=wasm32-unknown-unknown \
-o result.wasm \
src/lib.rs

Then, using binaryen's wasm-dis:

❯ wasm-dis foo.wasm | grep import
(import "m1" "g" (func $g))
(import "m1" "f" (func $f (param i32)))

The type of f is correct since m1::f() takes no arguments, but the module name is not. Looks like rustc has the right function but with wrong module name.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-linkageArea: linking into static, shared libraries and binariesC-bugCategory: This is a bug.O-wasmTarget: WASM (WebAssembly), http://webassembly.org/T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions