From 08de0ea65769811c23415322c653e97560a38e19 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Tue, 26 Sep 2023 12:29:34 -0500 Subject: [PATCH] Fix the `exports` key for packages-with-versions (#677) Previously one location looking up `exports` would account for the version and another wouldn't which caused the `ERROR` case to leak through by accident. These are now kept in sync to ensure that the right errors get surfaced. --- crates/rust/src/lib.rs | 18 ++++-------------- crates/rust/tests/codegen.rs | 30 +++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/crates/rust/src/lib.rs b/crates/rust/src/lib.rs index 31fd0a74f..aa3d3bef3 100644 --- a/crates/rust/src/lib.rs +++ b/crates/rust/src/lib.rs @@ -296,24 +296,14 @@ impl WorldGenerator for RustWasm { id: InterfaceId, _files: &mut Files, ) -> Result<()> { - let (pkg, inner_name) = match name { - WorldKey::Name(name) => (None, name), + let inner_name = match name { + WorldKey::Name(name) => name, WorldKey::Interface(id) => { let interface = &resolve.interfaces[*id]; - ( - Some(&resolve.packages[interface.package.unwrap()].name), - interface.name.as_ref().unwrap(), - ) + interface.name.as_ref().unwrap() } }; - let path = format!( - "{}{inner_name}", - if let Some(pkg) = pkg { - format!("{}:{}/", pkg.namespace, pkg.name) - } else { - String::new() - } - ); + let path = resolve.id_of(id).unwrap_or(inner_name.to_string()); let mut gen = self.interface(Identifier::Interface(id, name), None, resolve, false); let (snake, pkg) = gen.start_append_submodule(name); gen.types(id); diff --git a/crates/rust/tests/codegen.rs b/crates/rust/tests/codegen.rs index fdebf1bbd..00dcf7e73 100644 --- a/crates/rust/tests/codegen.rs +++ b/crates/rust/tests/codegen.rs @@ -134,7 +134,6 @@ mod symbol_does_not_conflict { bar: func() -> string } - world foo { export foo1 export foo2 @@ -272,3 +271,32 @@ mod owned_resource_deref_mut { } } } + +mod package_with_versions { + wit_bindgen::generate!({ + inline: " + package my:inline@0.0.0 + + interface foo { + resource bar { + constructor() + } + } + + world baz { + export foo + } + ", + exports: { + "my:inline/foo@0.0.0/bar": Resource + } + }); + + pub struct Resource; + + impl exports::my::inline::foo::GuestBar for Resource { + fn new() -> Self { + loop {} + } + } +}