Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate definitions for enums in generated bindings #483

Closed
aiwenar opened this issue Jul 16, 2018 · 2 comments · Fixed by #485
Closed

Duplicate definitions for enums in generated bindings #483

aiwenar opened this issue Jul 16, 2018 · 2 comments · Fixed by #485
Labels

Comments

@aiwenar
Copy link

aiwenar commented Jul 16, 2018

When a wasm_bindgen annotated enumeration is defined in a module, any item from that module is used somewhere else in a crate, and crate was compiled in release mode, then wasm-bindgen generates JS constant for that enumeration once for definition, plus once for every other module where an item from the same module as enum is used. This does not happen when compiled in debug mode.

Last tested on 2018-07-16, commit 1d3e8f4.

Reduced test case

Cargo.toml
[package]
name = "case"
version = "0.0.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
wasm-bindgen = "0.2.11"

[patch.crates-io]
wasm-bindgen = { git = "https://github.com/rustwasm/wasm-bindgen.git" }
src/lib.rs
#![feature(proc_macro, wasm_custom_section, wasm_import_module)]

extern crate wasm_bindgen;

use wasm_bindgen::prelude::*;

pub mod module;

#[wasm_bindgen]
pub fn test() -> module::Struct {
    module::Struct(0)
}
lib/module.rs
use wasm_bindgen::prelude::*;

#[wasm_bindgen]
pub struct Struct(pub usize);

#[wasm_bindgen]
#[derive(Debug)]
pub enum Enum {
    A,
}

When compiled in release mode wasm-bindgen outputs the following

/* tslint:disable */
import * as wasm from './case_bg';

/**
*/
export const Enum = Object.freeze({ A:0, });
/**
* @returns {Struct}
*/
export function test() {
    return Struct.__construct(wasm.test());
}

/**
*/
export const Enum = Object.freeze({ A:0, });
/**
*/
export class Struct {
// Rest cut for brevity

Note that export const Enum appears twice. When a third module containing a function returning Struct was added, export const Enum appeared three times. When compiled in release mode export const Enum appears only once, regardless of number of modules.

@alexcrichton
Copy link
Contributor

Thanks for the report! This is a bug due to the recent LLVM upgrade. This is specifically a bug internally with LLVM's handling of multiple codegen units. I'm gonna try to get a fix upstream into LLVM (or work around this locally ourselves). In any case, a workaround for now is:

# at the bottom of Cargo.toml
[profile.release]
codegen-units = 1

and that should do the trick!

@alexcrichton
Copy link
Contributor

I've filed https://bugs.llvm.org/show_bug.cgi?id=38184 for the upstream LLVM report. I'll submit a local fix to wasm-bindgen shortly

alexcrichton added a commit to alexcrichton/wasm-bindgen that referenced this issue Jul 16, 2018
This commit adds a hack to the `wasm-bindgen` CLI tool to work around rustwasm#483 which
is present on nightly Rust with the recent LLVM upgrade. Hopefully this'll carry
us forward until the [upstream bug][1] is fixed.

Closes rustwasm#483

[1]: https://bugs.llvm.org/show_bug.cgi?id=38184
fitzgen added a commit that referenced this issue Jul 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants