Skip to content

Commit 1c6a7a5

Browse files
authored
fix(rust): Use type_path for exported borrowed resource (#1534)
* fix borrowed handle lift * rename export_cabi trait name from T to T_ * fix
1 parent 51080a0 commit 1c6a7a5

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

crates/rust/src/bindgen.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -395,20 +395,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
395395

396396
let dealiased_resource = dealias(resolve, *resource);
397397

398+
let name = self.r#gen.type_path(dealiased_resource, true);
398399
let result = if is_own {
399-
let name = self.r#gen.type_path(dealiased_resource, true);
400400
format!("{name}::from_handle({op} as u32)")
401401
} else if self.r#gen.is_exported_resource(*resource) {
402-
let name = resolve.types[*resource]
403-
.name
404-
.as_deref()
405-
.unwrap()
406-
.to_upper_camel_case();
407402
format!("{name}Borrow::lift({op} as u32 as usize)")
408403
} else {
409404
let tmp = format!("handle{}", self.tmp());
410405
self.handle_decls.push(format!("let {tmp};"));
411-
let name = self.r#gen.type_path(dealiased_resource, true);
412406
format!(
413407
"{{\n
414408
{tmp} = {name}::from_handle({op} as u32);
@@ -871,14 +865,14 @@ impl Bindgen for FunctionBindgen<'_, '_> {
871865
self.push_str(&prev_src);
872866
let constructor_type = match &func.kind {
873867
FunctionKind::Freestanding | FunctionKind::AsyncFreestanding => {
874-
self.push_str(&format!("T::{}", to_rust_ident(func.item_name())));
868+
self.push_str(&format!("T_::{}", to_rust_ident(func.item_name())));
875869
None
876870
}
877871
FunctionKind::Method(_)
878872
| FunctionKind::Static(_)
879873
| FunctionKind::AsyncMethod(_)
880874
| FunctionKind::AsyncStatic(_) => {
881-
self.push_str(&format!("T::{}", to_rust_ident(func.item_name())));
875+
self.push_str(&format!("T_::{}", to_rust_ident(func.item_name())));
882876
None
883877
}
884878
FunctionKind::Constructor(ty) => {
@@ -892,10 +886,10 @@ impl Bindgen for FunctionBindgen<'_, '_> {
892886

893887
match return_type {
894888
ConstructorReturnType::Self_ => {
895-
self.push_str(&format!("{ty}::new(T::new"));
889+
self.push_str(&format!("{ty}::new(T_::new"));
896890
}
897891
ConstructorReturnType::Result { .. } => {
898-
self.push_str(&format!("T::new"));
892+
self.push_str(&format!("T_::new"));
899893
}
900894
}
901895

crates/rust/src/interface.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ unsafe fn call_import(&mut self, _params: Self::ParamsLower, _results: *mut u8)
10771077
"\
10781078
#[doc(hidden)]
10791079
#[allow(non_snake_case, unused_unsafe)]
1080-
pub unsafe fn _export_{name_snake}_cabi<T: {trait_name}>\
1080+
pub unsafe fn _export_{name_snake}_cabi<T_: {trait_name}>\
10811081
",
10821082
);
10831083
let params = self.print_export_sig(func, async_);
@@ -1163,7 +1163,7 @@ unsafe fn call_import(&mut self, _params: Self::ParamsLower, _results: *mut u8)
11631163
"\
11641164
#[doc(hidden)]
11651165
#[allow(non_snake_case)]
1166-
pub unsafe fn __post_return_{name_snake}<T: {trait_name}>\
1166+
pub unsafe fn __post_return_{name_snake}<T_: {trait_name}>\
11671167
"
11681168
);
11691169
let params = self.print_post_return_sig(func);

tests/codegen/issue1466.wit

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package foo:foo;
2+
3+
interface original {
4+
resource extra-options;
5+
record options {
6+
extra: option<borrow<extra-options>>,
7+
}
8+
}
9+
10+
interface adapter {
11+
use original.{options};
12+
variant t { a }
13+
f: func(x: options, t: t);
14+
}
15+
16+
world root {
17+
export original;
18+
export adapter;
19+
}

0 commit comments

Comments
 (0)