Skip to content

Commit abd2984

Browse files
committed
Also include interfaces imported implicitly through uses in check
Signed-off-by: Ryan Levick <ryan.levick@fermyon.com>
1 parent 9369149 commit abd2984

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

crates/wac-parser/src/resolution.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2712,15 +2712,17 @@ impl<'a> AstResolver<'a> {
27122712
world: WorldId,
27132713
) -> ResolutionResult<()> {
27142714
let world = &state.graph.types()[world];
2715+
// The interfaces imported implicitly through uses.
2716+
let implicit_imported_interfaces = world.implicit_imported_interfaces(state.graph.types());
27152717
let mut cache = Default::default();
27162718
let mut checker = SubtypeChecker::new(&mut cache);
27172719

27182720
// The output is allowed to import a subset of the world's imports
27192721
checker.invert();
27202722
for (name, item_kind, import_node) in state.graph.imports() {
2721-
let expected = world
2722-
.imports
2723+
let expected = implicit_imported_interfaces
27232724
.get(name)
2725+
.or_else(|| world.imports.get(name))
27242726
.ok_or_else(|| Error::ImportNotInTarget {
27252727
name: name.to_owned(),
27262728
world: path.string.to_owned(),

crates/wac-parser/tests/resolution/fail/targets-implicit-imports.wac

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package test:comp targets test:comp/foo;
22

33
interface indirect-dependency {
44
variant my-variant {
5-
foo
5+
foo,
6+
// Extra variant that the instance does not have
7+
bar
68
}
79
}
810

@@ -13,7 +15,7 @@ interface direct-dependency {
1315
}
1416

1517
world foo {
16-
import a: direct-dependency;
18+
import direct-dependency;
1719
}
1820

1921

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
failed to resolve document
22

3-
× import `a` has a mismatched type for target world `test:comp/foo`
4-
╰─▶ expected instance, found function
3+
× import `test:comp/indirect-dependency` has a mismatched type for target world `test:comp/foo`
4+
├─▶ mismatched type for export `my-variant`
5+
╰─▶ expected a variant case count of 2, found a count of 1
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
(component
2-
(import "a" (func))
3-
(export "b" (func 0))
4-
)
2+
(type (;0;)
3+
(instance
4+
(type (;0;) (variant (case "foo")))
5+
(export (;1;) "my-variant" (type (eq 0)))
6+
)
7+
)
8+
(import "test:comp/indirect-dependency" (instance (;0;) (type 0)))
9+
(alias export 0 "my-variant" (type (;1;)))
10+
(type (;2;)
11+
(instance
12+
(alias outer 1 1 (type (;0;)))
13+
(export (;1;) "my-variant" (type (eq 0)))
14+
(type (;2;) (func (result 1)))
15+
(export (;0;) "fun" (func (type 2)))
16+
)
17+
)
18+
(import "test:comp/direct-dependency" (instance (;1;) (type 2)))
19+
)

crates/wac-types/src/component.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,30 @@ impl World {
983983

984984
Ok(())
985985
}
986+
987+
/// The interfaces imported implicitly through uses.
988+
pub fn implicit_imported_interfaces<'a>(
989+
&'a self,
990+
types: &'a Types,
991+
) -> IndexMap<&str, ItemKind> {
992+
let mut interfaces = IndexMap::new();
993+
for (_, import) in self.imports.iter() {
994+
if let ItemKind::Instance(interface_id) = import {
995+
let import = &types[*interface_id];
996+
for (_, used_item) in &import.uses {
997+
let used_interface_id = used_item.interface;
998+
let used_interface = &types[used_interface_id];
999+
// The id must be set since used interfaces are always named.
1000+
let used_interface_name = used_interface.id.as_ref().unwrap();
1001+
interfaces.insert(
1002+
used_interface_name.as_str(),
1003+
ItemKind::Instance(used_interface_id),
1004+
);
1005+
}
1006+
}
1007+
}
1008+
interfaces
1009+
}
9861010
}
9871011

9881012
/// Represents a kind of an extern item.

0 commit comments

Comments
 (0)