Skip to content

Commit fb403c3

Browse files
authored
Don't panic on duplicate imports when creating components (#1787)
Return an error instead since that's more appropriate in this context. Closes #1786
1 parent 8f247b9 commit fb403c3

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

crates/wit-component/src/validation.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,13 @@ pub fn validate_module<'a>(
174174
Entry::Vacant(e) => e.insert(IndexMap::new()),
175175
};
176176

177-
assert!(map.insert(import.name, ty).is_none());
177+
if map.insert(import.name, ty).is_some() {
178+
bail!(
179+
"module has duplicate import for `{}::{}`",
180+
import.module,
181+
import.name
182+
);
183+
}
178184
}
179185
_ => bail!("module is only allowed to import functions"),
180186
}

tests/cli/bad-component-new.wat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
;; FAIL: component new %
2+
3+
(module
4+
(import "a" "a" (func))
5+
(import "a" "a" (func))
6+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: failed to encode a component from module
2+
3+
Caused by:
4+
0: failed to decode world from module
5+
1: module was not valid
6+
2: module has duplicate import for `a::a`

0 commit comments

Comments
 (0)