charon icon indicating copy to clipboard operation
charon copied to clipboard

Supporting vtables for closures

Open ssyram opened this issue 2 months ago • 4 comments

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:

  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.

ssyram avatar Oct 27 '25 02:10 ssyram

@protz Could you please help us approving the CI procedure?

ssyram avatar Oct 27 '25 04:10 ssyram

@sonmarcho @Nadrieril would you mind approving the CI for us?

ssyram avatar Nov 04 '25 10:11 ssyram

Done

R1kM avatar Nov 04 '25 10:11 R1kM

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.

Nadrieril avatar Nov 04 '25 12:11 Nadrieril