Skip to content

Commit f9d124f

Browse files
committed
fix: handle empty namespace paths in component codegen
When a WIT interface has no namespace path (e.g. no package prefix), the generated Rust code would produce invalid double-colon (::) paths. Add guards to emit correct paths when the namespace is empty. Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent 4d5bf98 commit f9d124f

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

src/hyperlight_component_util/src/host.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,13 @@ fn emit_export_instance<'a, 'b, 'c>(s: &'c mut State<'a, 'b>, wn: WitName, it: &
135135
let (root_ns, root_base_name) = s.root_component_name.unwrap();
136136
let wrapper_name = kebab_to_wrapper_name(root_base_name);
137137
let imports_name = kebab_to_imports_name(root_base_name);
138+
let trait_path = if ns.is_empty() {
139+
quote! { #trait_name }
140+
} else {
141+
quote! { #ns::#trait_name }
142+
};
138143
s.root_mod.items.extend(quote! {
139-
impl<I: #root_ns::#imports_name, S: ::hyperlight_host::sandbox::Callable> #ns::#trait_name <#(#tvs),*> for #wrapper_name<I, S> {
144+
impl<I: #root_ns::#imports_name, S: ::hyperlight_host::sandbox::Callable> #trait_path <#(#tvs),*> for #wrapper_name<I, S> {
140145
#(#exports)*
141146
}
142147
});

src/hyperlight_component_util/src/rtypes.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ fn emit_resource_ref(s: &mut State, n: u32, path: Vec<ImportExport>) -> TokenStr
120120
trait_path.push(rtrait.clone());
121121
let t = s.resolve_trait_immut(true, &trait_path);
122122
let tvis = emit_tvis(s, t.tv_idxs());
123-
quote! { <#sv::#extras #instance_type as #rp #tns::#instance_mod::#rtrait #tvis>::T }
123+
let trait_ref = if tns.is_empty() {
124+
quote! { #rp #instance_mod::#rtrait }
125+
} else {
126+
quote! { #rp #tns::#instance_mod::#rtrait }
127+
};
128+
quote! { <#sv::#extras #instance_type as #trait_ref #tvis>::T }
124129
}
125130

126131
/// Try to find a way to refer to the given type variable from the
@@ -174,7 +179,11 @@ fn try_find_local_var_id(
174179
let rp = s.root_path();
175180
let tns = wn.namespace_path();
176181
let helper = kebab_to_namespace(wn.name);
177-
Some(quote! { #rp #tns::#helper::#name })
182+
if tns.is_empty() {
183+
Some(quote! { #rp #helper::#name })
184+
} else {
185+
Some(quote! { #rp #tns::#helper::#name })
186+
}
178187
} else {
179188
let hp = s.helper_path();
180189
Some(quote! { #hp #name })
@@ -735,8 +744,13 @@ fn emit_extern_decl<'a, 'b, 'c>(
735744
let rp = s.root_path();
736745
let tns = wn.namespace_path();
737746
let tn = kebab_to_type(wn.name);
747+
let trait_bound = if tns.is_empty() {
748+
quote! { #rp #tn }
749+
} else {
750+
quote! { #rp #tns::#tn }
751+
};
738752
quote! {
739-
type #tn: #rp #tns::#tn #vs;
753+
type #tn: #trait_bound #vs;
740754
fn #getter(&mut self) -> impl ::core::borrow::BorrowMut<Self::#tn>;
741755
}
742756
}

0 commit comments

Comments
 (0)