Skip to content

Commit

Permalink
Print trappable error types from other interfaces with their module path
Browse files Browse the repository at this point in the history
  • Loading branch information
elliottt committed Aug 3, 2023
1 parent 5f3308b commit 5f242d2
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1227,7 +1227,10 @@ impl<'a> InterfaceGenerator<'a> {
}
}

fn special_case_trappable_error(&self, results: &Results) -> Option<(&'a Result_, String)> {
fn special_case_trappable_error(
&self,
results: &Results,
) -> Option<(&'a Result_, TypeId, String)> {
// We fillin a special trappable error type in the case when a function has just one
// result, which is itself a `result<a, e>`, and the `e` is *not* a primitive
// (i.e. defined in std) type, and matches the typename given by the user.
Expand All @@ -1250,7 +1253,7 @@ impl<'a> InterfaceGenerator<'a> {

let rust_type = self.trappable_errors.get(&error_typeid)?;

Some((result, rust_type.clone()))
Some((result, error_typeid, rust_type.clone()))
}

fn generate_add_to_linker(&mut self, id: InterfaceId, name: &str) {
Expand Down Expand Up @@ -1431,7 +1434,9 @@ impl<'a> InterfaceGenerator<'a> {
self.push_str(")");
self.push_str(" -> ");

if let Some((r, error_typename)) = self.special_case_trappable_error(&func.results) {
if let Some((r, error_id, error_typename)) =
self.special_case_trappable_error(&func.results)
{
// Functions which have a single result `result<ok,err>` get special
// cased to use the host_wasmtime_rust::Error<err>, making it possible
// for them to trap or use `?` to propogate their errors
Expand All @@ -1442,6 +1447,12 @@ impl<'a> InterfaceGenerator<'a> {
self.push_str("()");
}
self.push_str(",");
if let TypeOwner::Interface(id) = self.resolve.types[error_id].owner {
if let Some(path) = self.path_to_interface(id) {
self.push_str(&path);
self.push_str("::");
}
}
self.push_str(&error_typename);
self.push_str(">");
} else {
Expand Down

0 comments on commit 5f242d2

Please sign in to comment.