Skip to content

Generate casts to unnamed closure types and closure-type condition variables correctly [GH #576] - #579

Open
daveedvdv-nvidia wants to merge 1 commit into
mainfrom
gh-576-closure-braced-cast
Open

daveedvdv-nvidia wants to merge 1 commit into
mainfrom
gh-576-closure-braced-cast

Conversation

@daveedvdv-nvidia

@daveedvdv-nvidia daveedvdv-nvidia commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #576.

The C++-generating back end can name a closure type only through the typedef decltype(v) __Tn that gen_variable_decl puts out after the declaration of a variable v of that type. This PR fixes two problems with that.

Casts to a closure type with no typedef. In C++23, auto{x} and auto(x) can cast to a closure type that the source can't name otherwise. Examples are auto{[](int a) { return a; }}, auto{g()} with g returning a closure object, and auto{m} with m declared in a for init (no typedef is put out there). gen_dynamic_init put these out as functional-notation casts with gen_type_reference, which writes class __Tn for a closure type without a typedef. That is ill-formed.

Such a cast is now put out as written, with auto. The new helper is_single_operand_cast_to_unnamed_closure checks for a closure type that hasn't been declared (no typedef) and a single operand, in any of the forms the front end uses for it:

  • dik_lambda, when the operand is a lambda-expression with captures.
  • dik_expression, for a lambda-expression without captures or for a prvalue call.
  • dik_bitwise_copy or dik_constructor, for a copy of an lvalue.

Then gen_paren_or_brace_dynamic_init writes the operand as before, in braces or parentheses as in the source. Writing auto keeps the meaning of the source. A glvalue operand is still copied, a prvalue is still not, and the cast yields the unqualified type even when the operand is const. An unnameable closure type can come only from auto{x} or auto(x), since casts through decltype, typedefs, and template parameters keep their names in the IL.

Typedefs in conditions and range-based for. For a variable of closure type declared in the condition of an if, switch, or while, or in a range-based for, the typedef was put out inside the declaration, for example if (auto m = []{ return 1; } typedef decltype(m) __T1;). This is ill-formed, and it doesn't need C++23. Such variables now get no typedef, as was already the case in a for init. A cast of such a variable then takes the auto path above.

Not covered. For a const closure variable, the typedef that stands for the unqualified closure type is decltype(c), which is const. So auto{c} becomes __T2{c}, a const object, and calling a mutable lambda through it fails. This is unchanged. Removing that typedef would break other uses of it, such as new auto(c).

This came up while working on #575.

Summary by CodeRabbit

  • Bug Fixes

    • Improved generated C++ for casts to unnamed lambda closure types, including cases where the cast can be expressed using auto.
    • Corrected generated declarations for closure-type variables in control-flow statements.
  • Tests

    • Added C++23 coverage for closure casts and variables across initializers, functions, and control-flow statements.
  • Documentation

    • Documented the updated generated-code behavior.

@daveedvdv-nvidia
daveedvdv-nvidia requested a review from a team November 20, 2026 16:39
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The C++-generating back end now emits certain casts to undeclared closure types using auto functional notation. It also excludes condition and iterator declarations from generated closure-type typedefs. New C++23 tests cover casts and closure variables in expressions, conditions, and loops.

Changes: Closure code generation

Layer / File(s) Summary
Casts to unnamed closure types
src/cp_gen_be.c, tests/tests/changes/GH576.sft.cpp, tests/tests/changes/.GH576.rto/default.1.1.txt
The back end recognizes supported single-operand casts to undeclared closure types and emits functional notation with auto. The C++23 test fixture covers closure expressions, variables, captures, and template cases.
Closure declaration typedefs
src/cp_gen_be.c, tests/tests/changes/GH576.sft.cpp, src/pending_changes/gh-576-closure-braced-cast.txt
The back end excludes condition and iterator declarations from generated closure-type typedefs. Tests cover if, switch, while, and range-based for declarations. The pending-change note describes both back-end changes.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: daveedvdv, nv-cmeerw, darkarc

Merge Risk: Minimal · up to adfa5

The change makes the C++-generating back end emit valid code for casts to unnamed closure types and for closure variables declared in conditions and range-based for statements. No concrete defect was established. One question about older-standard output for decltype-based casts could not be resolved from the available source. It is not evidence of a problem.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two main changes in the PR: casts to unnamed closure types and closure-type condition variables in generated C++. The raw summary, the PR objectives, and the linked issue #576 all …
Linked Issues check Passed [#576] Requirement 1: casts to an unnamed closure type must use the C++23 auto form. The change adds is_single_operand_cast_to_unnamed_closure in src/cp_gen_be.c, and gen_dynamic_init uses it to emit …
Out of Scope Changes check Passed All changed paths relate to issue #576: src/cp_gen_be.c (the new helper and the typedef-skip logic), src/pending_changes/gh-576-closure-braced-cast.txt (change note for this fix), tests/tests/changes/…
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Full details: Title check

Explanation

The title names the two main changes in the PR: casts to unnamed closure types and closure-type condition variables in generated C++. The raw summary, the PR objectives, and the linked issue #576 all describe these same two fixes. The title is specific and matches the changeset. It is 96 characters long, which is longer than ideal, but it stays readable and on topic.

Full details: Linked Issues check

Explanation

[#576] Requirement 1: casts to an unnamed closure type must use the C++23 auto form. The change adds is_single_operand_cast_to_unnamed_closure in src/cp_gen_be.c, and gen_dynamic_init uses it to emit auto{x} or auto(x) instead of class __Tn{...}. The check covers the dik_lambda, dik_expression, dik_bitwise_copy, and dik_constructor forms described in the PR and summary, which match the issue's reproducer cases 1 to 5. Requirement 2: no typedef is put out for closure variables in if, switch, while conditions, or range-based for. The summary states that the typedef is skipped for for-init variables, conditions, and iterators, which matches issue case 6 and the range-based for form. The test fixture tests/tests/changes/GH576.sft.cpp (with its .rto file) covers these forms under --c++23. Limitation: the source diff for src/cp_gen_be.c could not be read (changed blobs missing from checkout). This verdict relies on the whole-PR summary and the PR description, not on direct line-level review. The const-closure case (auto{c} producing __T2{c}) is not in the issue's reproducer list and is documented as not covered, so it does not fail this check.

Full details: Out of Scope Changes check

Explanation

All changed paths relate to issue #576: src/cp_gen_be.c (the new helper and the typedef-skip logic), src/pending_changes/gh-576-closure-braced-cast.txt (change note for this fix), tests/tests/changes/GH576.sft.cpp and tests/tests/changes/.GH576.rto/default.1.1.txt (regression test for the issue cases). No unrelated functional changes appear in the summary. The source diff could not be read directly, so this verdict is based on the summary and PR description.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 69d1f4f and adfa5d9.

Files selected for processing: 4

  • src/cp_gen_be.c
  • src/pending_changes/gh-576-closure-braced-cast.txt
  • tests/tests/changes/.GH576.rto/default.1.1.txt
  • tests/tests/changes/GH576.sft.cpp

Included review availability: This review used your included allowance. Your plan provides up to 4 included reviews per hour; 3 remain after this review.

This branch has not been deployed

No deployments
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.

Generated C++ for casts to unnamed closure types and for closure-type condition variables is ill-formed

1 participant