From ebc752bac6019b45f330877e0f828d79cf2a4f73 Mon Sep 17 00:00:00 2001 From: Ryan Levick Date: Mon, 15 Apr 2024 15:43:15 +0200 Subject: [PATCH] Loosen restriction on top-level resource alias imports. Previously, we assumed that all resource aliases had some owning interface, but that might not be the case. Users can alias resources at the world level. This lifts two restrictions that erroneously encoded this incorrect assumption. Fixes https://github.com/bytecodealliance/wac/issues/81 --- crates/wac-graph/src/encoding.rs | 1 - .../graphs/import-resource-alias/encoded.wat | 25 +++++++++++++++++++ .../graphs/import-resource-alias/graph.json | 14 +++++++++++ .../graphs/import-resource-alias/world.wit | 7 ++++++ crates/wac-types/src/package.rs | 7 ++---- 5 files changed, 48 insertions(+), 6 deletions(-) create mode 100644 crates/wac-graph/tests/graphs/import-resource-alias/encoded.wat create mode 100644 crates/wac-graph/tests/graphs/import-resource-alias/graph.json create mode 100644 crates/wac-graph/tests/graphs/import-resource-alias/world.wit diff --git a/crates/wac-graph/src/encoding.rs b/crates/wac-graph/src/encoding.rs index 3efd2a2..8e8d58c 100644 --- a/crates/wac-graph/src/encoding.rs +++ b/crates/wac-graph/src/encoding.rs @@ -577,7 +577,6 @@ impl<'a> TypeEncoder<'a> { } fn own(&self, state: &mut State, res: ResourceId) -> u32 { - assert!(!state.scopes.is_empty()); let res = state.current.resources[self.0[res].name.as_str()]; let index = state.current.encodable.type_count(); state.current.encodable.ty().defined_type().own(res); diff --git a/crates/wac-graph/tests/graphs/import-resource-alias/encoded.wat b/crates/wac-graph/tests/graphs/import-resource-alias/encoded.wat new file mode 100644 index 0000000..2cf8734 --- /dev/null +++ b/crates/wac-graph/tests/graphs/import-resource-alias/encoded.wat @@ -0,0 +1,25 @@ +(component + (import "x" (type (;0;) (sub resource))) + (import "y" (type (;1;) (eq 0))) + (type (;2;) (own 0)) + (type (;3;) (own 1)) + (type (;4;) (func (param "x" 2) (result 3))) + (import "f" (func (;0;) (type 4))) + (type (;5;) + (component + (import "x" (type (;0;) (sub resource))) + (import "y" (type (;1;) (eq 0))) + (type (;2;) (own 0)) + (type (;3;) (own 1)) + (type (;4;) (func (param "x" 2) (result 3))) + (import "f" (func (;0;) (type 4))) + ) + ) + (import "unlocked-dep=" (component (;0;) (type 5))) + (instance (;0;) (instantiate 0 + (with "x" (type 0)) + (with "y" (type 1)) + (with "f" (func 0)) + ) + ) +) diff --git a/crates/wac-graph/tests/graphs/import-resource-alias/graph.json b/crates/wac-graph/tests/graphs/import-resource-alias/graph.json new file mode 100644 index 0000000..6413c42 --- /dev/null +++ b/crates/wac-graph/tests/graphs/import-resource-alias/graph.json @@ -0,0 +1,14 @@ +{ + "packages": [ + { + "name": "test:foo", + "path": "world.wit" + } + ], + "nodes": [ + { + "type": "instantiation", + "package": 0 + } + ] +} \ No newline at end of file diff --git a/crates/wac-graph/tests/graphs/import-resource-alias/world.wit b/crates/wac-graph/tests/graphs/import-resource-alias/world.wit new file mode 100644 index 0000000..5748535 --- /dev/null +++ b/crates/wac-graph/tests/graphs/import-resource-alias/world.wit @@ -0,0 +1,7 @@ +package example:foo; + +world w { + resource x; + type y = x; + import f: func(x: x) -> y; +} \ No newline at end of file diff --git a/crates/wac-types/src/package.rs b/crates/wac-types/src/package.rs index 8811fea..b4ce2dd 100644 --- a/crates/wac-types/src/package.rs +++ b/crates/wac-types/src/package.rs @@ -765,11 +765,8 @@ impl<'a> TypeConverter<'a> { let alias_id = self.types.add_resource(Resource { name: name.to_owned(), alias: Some(ResourceAlias { - owner: match self - .find_owner(ComponentAnyTypeId::Resource(id)) - .expect("should have owner") - { - (Owner::Interface(id), _) => Some(*id), + owner: match self.find_owner(ComponentAnyTypeId::Resource(id)) { + Some((Owner::Interface(id), _)) => Some(*id), _ => None, }, source: *resource_id,