Skip to content

Commit

Permalink
error types: resolve type definition id by chasing alias ids (bytecod…
Browse files Browse the repository at this point in the history
  • Loading branch information
Pat Hickey authored May 12, 2023
1 parent d339d69 commit a729a2a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
15 changes: 14 additions & 1 deletion crates/wit-bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,9 +1027,10 @@ impl<'a> InterfaceGenerator<'a> {
_ => return None,
};
let error_typeid = match result.err? {
Type::Id(id) => id,
Type::Id(id) => resolve_type_definition_id(&self.resolve, id),
_ => return None,
};

self.trappable_error_types(owner)
.find(|(wit_error_typeid, _)| error_typeid == *wit_error_typeid)
.map(|(_, rust_errortype)| (result, rust_errortype))
Expand Down Expand Up @@ -1486,3 +1487,15 @@ impl<'a> RustGenerator<'a> for InterfaceGenerator<'a> {
self.gen.types.get(ty)
}
}

/// When an interface `use`s a type from another interface, it creates a new TypeId
/// referring to the definition TypeId. Chase this chain of references down to
/// a TypeId for type's definition.
fn resolve_type_definition_id(resolve: &Resolve, mut id: TypeId) -> TypeId {
loop {
match resolve.types[id].kind {
TypeDefKind::Type(Type::Id(def_id)) => id = def_id,
_ => return id,
}
}
}
3 changes: 2 additions & 1 deletion crates/wit-bindgen/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ impl Types {
_ => continue,
};
if let Some(Type::Id(id)) = err {
self.type_info.get_mut(id).unwrap().error = true;
let id = super::resolve_type_definition_id(resolve, *id);
self.type_info.get_mut(&id).unwrap().error = true;
}
}
}
Expand Down

0 comments on commit a729a2a

Please sign in to comment.