Skip to content

Commit

Permalink
Don't panic on duplicate imports when creating components
Browse files Browse the repository at this point in the history
Return an error instead since that's more appropriate in this context.

Closes bytecodealliance#1786
  • Loading branch information
alexcrichton committed Sep 13, 2024
1 parent 8f247b9 commit 4588291
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
8 changes: 7 additions & 1 deletion crates/wit-component/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,13 @@ pub fn validate_module<'a>(
Entry::Vacant(e) => e.insert(IndexMap::new()),
};

assert!(map.insert(import.name, ty).is_none());
if map.insert(import.name, ty).is_some() {
bail!(
"module has duplicate import for `{}::{}`",
import.module,
import.name
);
}
}
_ => bail!("module is only allowed to import functions"),
}
Expand Down
6 changes: 6 additions & 0 deletions tests/cli/bad-component-new.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
;; FAIL: component new %

(module
(import "a" "a" (func))
(import "a" "a" (func))
)
6 changes: 6 additions & 0 deletions tests/cli/bad-component-new.wat.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
error: failed to encode a component from module

Caused by:
0: failed to decode world from module
1: module was not valid
2: module has duplicate import for `a::a`

0 comments on commit 4588291

Please sign in to comment.