Supporting vtables for closures
This PR is a re-submission of #871 , as @Sam-Ni does not have the permission of running the CI, I resubmit for him to view the CI results.
This PR adds support for vtable instantiation for closures. The implementation involves two main steps:
-
Matching a closure implementation
-
Instantiating the corresponding vtable
Step 1 is implemented in translate_body.rs, while Step 2 is implemented in translate_trait_objects.rs.
The modifications in each file are described below.
- translate_body.rs
translate_rvalueSupports registering closure trait implementations. For example, in the test filedyn-fn.rs, the closure|counter| { ... }is matched in thehax::ImplExprAtom::Builtincase and registered as aVTableInstance.
- translate_trait_objects.rs
-
add_method_to_vtable_defAdds additional checks forFnOncewhen constructing the method field in theFnOncevtable, due to the incompatibility ofFnOnce::call_oncewith dyn-compatibility. -
get_vtable_instance_infoSupports retrieving the vtable struct reference and the corresponding trait declaration reference for closures, based on their kind (Fn, FnMut, or FnOnce). -
add_supertraits_to_vtable_valueSupports filling vtable fields with built-in supertraits. For closures supertraits, they are translated as VTableInstance withTraitImplSource::Closure(kind), generating a vtable according to the closurekind. For other built-in supertraits, they are translated as VTableInstance withTraitImplSource::Normal, whose trait reference itself is used as the impl reference (e.g.@2 := &core::marker::MetaSized::{vtable}). -
gen_vtable_instance_init_bodySupports generating the vtable body for closures. Closure implementations are treated as trait implementations with no associated items and a single specialized closure method (callorcall_mut).call_onceis ignored due to the incompatibility mentioned above. Based on this design, the function retrieves(trait_pred, items)for closures and usesmk_fieldto add the specialized closure method. -
generate_closure_method_shim_refImplements the generation of the closure method shim reference used in themk_fieldingen_vtable_instance_init_body.
@protz Could you please help us approving the CI procedure?
@sonmarcho @Nadrieril would you mind approving the CI for us?
Done
This overall looks good, the one issue is the new panic. In the future please don't submit PRs with such regressions without explaining them.
Also just so you know the PR description was not useful information: you don't need to say what you did, I will read that in the code; instead a PR description is useful for context and for why you did things a certain way. FYI LLMs are terrible at generating useful PR descriptions as far as I've seen, please avoid that in the future.