Skip to content

Fully translate closures, also handle closure casts #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

N1ark
Copy link
Contributor

@N1ark N1ark commented Jun 4, 2025

  • Add TypeDecl.src: ItemKind, with the type's source (either TopLevel or Closure)
  • Add fn_once_impl: TraitImplRef, fn_mut_impl: Option<TraitImplRef>, fn_impl: Option<TraitImplRef> to ClosureInfo
  • If a closure type is cast to a function pointer, generate a new function that doesn't take in a state (ie. that matches the target function pointer type), to avoid having to special case closures in those casts.

This also means that closure's functions/traits are translated even when they're not used, however that's necessary to properly translate e.g. closure-casts.

For instance for the following code, if we don't translate the closure's function then it is never translated in the (U)LLBC, as it is never referred to directly (instead, only the ADT was translated).

fn main() {
    let f: fn() = || {
        let _ = 1 / 0;
    };
    f();
}

Now instead we generate all three Fn[Once,Mut] trait impls, functions, as well as an additional function that looks like

fn main::closure::as_fn(args: ()) {
  let closure_st = main::closure {};
  {impl FnOnce<()> for main::closure}::call_once(closure_st, args)
}

@N1ark N1ark force-pushed the type-decl-kinds branch 3 times, most recently from ea6cacb to 9e972ff Compare June 7, 2025 11:16
@N1ark N1ark changed the title TypeDecl.kind->type_kind, add kind: ItemKind Fully translate closures, also handle closure casts Jun 7, 2025
@N1ark N1ark force-pushed the type-decl-kinds branch from 7e5f594 to 4b5cfcb Compare June 7, 2025 11:35
@N1ark
Copy link
Contributor Author

N1ark commented Jun 7, 2025

Reworked a bit according to what you said @Nadrieril , getting some errors with generics (missing regions in ClosureInfo), should maybefn_mut_impl and fn_impl be behind a region binder?... or maybe we skip ClosureInfos when checking for generics because really it doesn't matter? idk

@Nadrieril
Copy link
Member

Reworked a bit according to what you said @Nadrieril , getting some errors with generics (missing regions in ClosureInfo), should maybefn_mut_impl and fn_impl be behind a region binder?... or maybe we skip ClosureInfos when checking for generics because really it doesn't matter? idk

All three of them should be behind a region binder indeed, that binders the same regions as the PolyFnSig that corresponds to the closure.

@N1ark N1ark force-pushed the type-decl-kinds branch from 4b5cfcb to 4158ecf Compare June 11, 2025 09:21
@N1ark N1ark force-pushed the type-decl-kinds branch from 4158ecf to 34610f3 Compare June 11, 2025 09:21
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