Skip to content

Commit

Permalink
Build correct interface_names cache
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Levick <[email protected]>
  • Loading branch information
rylev committed Oct 10, 2023
1 parent 2f807e6 commit bad91ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 38 deletions.
26 changes: 4 additions & 22 deletions crates/rust/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,36 +159,18 @@ impl InterfaceGenerator<'_> {
path_to_root
}

pub fn start_append_submodule(&mut self, name: &WorldKey) -> (String, Option<PackageName>) {
pub fn start_append_submodule(&mut self, name: &WorldKey) -> (String, Vec<String>) {
let snake = match name {
WorldKey::Name(name) => to_rust_ident(name),
WorldKey::Interface(id) => {
to_rust_ident(self.resolve.interfaces[*id].name.as_ref().unwrap())
}
};
let pkg = match name {
WorldKey::Name(_) => None,
WorldKey::Interface(id) => {
let pkg = self.resolve.interfaces[*id].package.unwrap();
Some(self.resolve.packages[pkg].name.clone())
}
};
let module_path = crate::compute_module_path(name, &self.resolve, !self.in_import);
if let Identifier::Interface(id, _) = self.identifier {
let mut path = String::new();
if !self.in_import {
path.push_str("exports::");
}
if let Some(name) = &pkg {
path.push_str(&format!(
"{}::{}::",
name.namespace.to_snake_case(),
name.name.to_snake_case()
));
}
path.push_str(&snake);
self.gen.interface_names.insert(id, path);
self.gen.interface_names.insert(id, module_path.join("::"));
}
(snake, pkg)
(snake, module_path)
}

pub fn finish_append_submodule(mut self, snake: &str, module_path: Vec<String>) {
Expand Down
23 changes: 8 additions & 15 deletions crates/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,12 +315,11 @@ impl WorldGenerator for RustWasm {
resolve,
true,
);
let (snake, pkg) = gen.start_append_submodule(name);
let (snake, module_path) = gen.start_append_submodule(name);
gen.types(id);

gen.generate_imports(resolve.interfaces[id].functions.values());

let module_path = compute_module_path(name, resolve, id, pkg, false);
gen.finish_append_submodule(&snake, module_path);
}

Expand Down Expand Up @@ -357,14 +356,13 @@ impl WorldGenerator for RustWasm {
};
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);
let (snake, module_path) = gen.start_append_submodule(name);
gen.types(id);
gen.generate_exports(
&ExportKey::Name(path),
Some(name),
resolve.interfaces[id].functions.values(),
)?;
let module_path = compute_module_path(name, resolve, id, pkg, true);
gen.finish_append_submodule(&snake, module_path);
Ok(())
}
Expand Down Expand Up @@ -533,13 +531,7 @@ impl WorldGenerator for RustWasm {
}
}

fn compute_module_path(
name: &WorldKey,
resolve: &Resolve,
id: InterfaceId,
pkg: Option<PackageName>,
is_export: bool,
) -> Vec<String> {
fn compute_module_path(name: &WorldKey, resolve: &Resolve, is_export: bool) -> Vec<String> {
let mut path = Vec::new();
if is_export {
path.push("exports".to_string());
Expand All @@ -548,11 +540,12 @@ fn compute_module_path(
WorldKey::Name(name) => {
path.push(name.to_snake_case());
}
WorldKey::Interface(_) => {
let iface = &resolve.interfaces[id];
let pkgname = pkg.unwrap();
WorldKey::Interface(id) => {
let iface = &resolve.interfaces[*id];
let pkg = iface.package.unwrap();
let pkgname = resolve.packages[pkg].name.clone();
path.push(pkgname.namespace.to_snake_case());
path.push(name_package_module(resolve, iface.package.unwrap()));
path.push(name_package_module(resolve, pkg));
path.push(iface.name.as_ref().unwrap().to_snake_case());
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/codegen/multiversion/deps/v1/root.wit
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package my:dep@0.1.0;

interface a {
type foo = u8;
x: func();
}
3 changes: 2 additions & 1 deletion tests/codegen/multiversion/deps/v2/root.wit
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package my:dep@0.2.0;

interface a {
x: func();
use my:dep/a@0.1.0.{foo};
x: func() -> foo;
}

0 comments on commit bad91ec

Please sign in to comment.