Skip to content

Commit

Permalink
wit-parser: Fix types leaking across interfaces (#822)
Browse files Browse the repository at this point in the history
This fixes an internal bug where types were accidentally leaking across
interfaces, panicking when used. This commit fixes the issue by properly
fixing the "reset the state" after resolution of each interface and then
additionally adds some more testing per-interface to the `ui` tests.
  • Loading branch information
alexcrichton committed Nov 16, 2022
1 parent 40ef021 commit e15e768
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion crates/wit-parser/src/ast/resolve.rs
Expand Up @@ -35,7 +35,6 @@ impl Resolver {
for interface in document.interfaces() {
let name = &interface.name.name;
let instance = self.resolve(name, &interface.items, &interface.docs)?;
*self = Resolver::default();

if interface_map.insert(name.to_string(), instance).is_some() {
return Err(Error {
Expand Down Expand Up @@ -174,6 +173,8 @@ impl Resolver {
}
}

self.anon_types.clear();

Ok(Interface {
name: name.to_string(),
docs: self.docs(docs),
Expand Down
18 changes: 18 additions & 0 deletions crates/wit-parser/tests/all.rs
Expand Up @@ -116,6 +116,7 @@ impl Runner<'_> {
}
} else {
let instance = result?;
test_world(&instance);
to_json(&instance)
};

Expand Down Expand Up @@ -379,3 +380,20 @@ fn to_json(world: &World) -> String {
ty.map(translate_type)
}
}

fn test_world(world: &World) {
for (_, interface) in world.imports.iter() {
test_interface(interface);
}
for (_, interface) in world.exports.iter() {
test_interface(interface);
}
if let Some(default) = &world.default {
test_interface(default);
}
}

fn test_interface(interface: &Interface) {
let mut sizes = SizeAlign::default();
sizes.fill(interface);
}
8 changes: 8 additions & 0 deletions crates/wit-parser/tests/ui/shared-types.wit
@@ -0,0 +1,8 @@
world foo {
import foo: interface {
a: func() -> list<u8>
}
default export interface {
a: func() -> tuple<list<u8>>
}
}
50 changes: 50 additions & 0 deletions crates/wit-parser/tests/ui/shared-types.wit.result
@@ -0,0 +1,50 @@
{
"name": "foo",
"default": {
"types": [
{
"idx": 0,
"list": "u8"
},
{
"idx": 1,
"tuple": {
"types": [
"type-0"
]
}
}
],
"functions": [
{
"name": "a",
"params": [],
"results": [
"type-1"
]
}
]
},
"imports": [
[
"foo",
{
"types": [
{
"idx": 0,
"list": "u8"
}
],
"functions": [
{
"name": "a",
"params": [],
"results": [
"type-0"
]
}
]
}
]
]
}

0 comments on commit e15e768

Please sign in to comment.