Skip to content

Conversation

@Sam-Ni
Copy link

@Sam-Ni Sam-Ni commented Oct 24, 2025

This PR adds support for vtable instantiation for closures.
The implementation involves two main steps:

  1. Matching a closure implementation

  2. 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.

  1. translate_body.rs
  • translate_rvalue
    Supports registering closure trait implementations. For example, in the test file dyn-fn.rs, the closure |counter| { ... } is matched in the hax::ImplExprAtom::Builtin case and registered as a VTableInstance.
  1. translate_trait_objects.rs
  • add_method_to_vtable_def
    Adds additional checks for FnOnce when constructing the method field in the FnOnce vtable, due to the incompatibility of FnOnce::call_once with dyn-compatibility.

  • get_vtable_instance_info
    Supports 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_value
    Supports filling vtable fields with built-in supertraits. For closures supertraits, they are translated as VTableInstance with TraitImplSource::Closure(kind), generating a vtable according to the closure kind. For other built-in supertraits, they are translated as VTableInstance with TraitImplSource::Normal, whose trait reference itself is used as the impl reference (e.g. @2 := &core::marker::MetaSized::{vtable}).

  • gen_vtable_instance_init_body
    Supports generating the vtable body for closures. Closure implementations are treated as trait implementations with no associated items and a single specialized closure method (call or call_mut). call_once is ignored due to the incompatibility mentioned above. Based on this design, the function retrieves (trait_pred, items) for closures and uses mk_field to add the specialized closure method.

  • generate_closure_method_shim_ref
    Implements the generation of the closure method shim reference used in the mk_field in gen_vtable_instance_init_body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants