components: Fix bindgen! with renamed interfaces (#5968)

This follows the same strategy pioneered by the `wit-bindgen` guest Rust
bindgen which keeps track of the latest name of an interface for how to
refer to an interface.

Closes #5961
This commit is contained in:
Alex Crichton
2023-03-08 17:15:58 -06:00
committed by GitHub
parent 8a08fedc69
commit 9141fcf8cf
3 changed files with 34 additions and 17 deletions

View File

@@ -15,7 +15,7 @@ pub trait RustGenerator<'a> {
fn push_str(&mut self, s: &str);
fn info(&self, ty: TypeId) -> TypeInfo;
fn current_interface(&self) -> Option<InterfaceId>;
fn path_to_interface(&self, interface: InterfaceId) -> Option<String>;
fn print_ty(&mut self, ty: &Type, mode: TypeMode) {
match ty {
@@ -64,19 +64,9 @@ pub trait RustGenerator<'a> {
self.result_name(id)
};
if let TypeOwner::Interface(id) = ty.owner {
if let Some(name) = &self.resolve().interfaces[id].name {
match self.current_interface() {
Some(cur) if cur == id => {}
Some(_other) => {
self.push_str("super::");
self.push_str(&name.to_snake_case());
self.push_str("::");
}
None => {
self.push_str(&name.to_snake_case());
self.push_str("::");
}
}
if let Some(path) = self.path_to_interface(id) {
self.push_str(&path);
self.push_str("::");
}
}
self.push_str(&name);