You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Generate casts to unnamed closure types and closure-type condition variables correctly [GH #576] - #579
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).
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.
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.
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: 4
Check name
Status
Explanation
Title check
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
[#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
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
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #576.
The C++-generating back end can name a closure type only through the
typedef decltype(v) __Tnthatgen_variable_declputs out after the declaration of a variablevof that type. This PR fixes two problems with that.Casts to a closure type with no typedef. In C++23,
auto{x}andauto(x)can cast to a closure type that the source can't name otherwise. Examples areauto{[](int a) { return a; }},auto{g()}withgreturning a closure object, andauto{m}withmdeclared in aforinit (no typedef is put out there).gen_dynamic_initput these out as functional-notation casts withgen_type_reference, which writesclass __Tnfor a closure type without a typedef. That is ill-formed.Such a cast is now put out as written, with
auto. The new helperis_single_operand_cast_to_unnamed_closurechecks 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_copyordik_constructor, for a copy of an lvalue.Then
gen_paren_or_brace_dynamic_initwrites the operand as before, in braces or parentheses as in the source. Writingautokeeps 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 isconst. An unnameable closure type can come only fromauto{x}orauto(x), since casts throughdecltype, 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 anif,switch, orwhile, or in a range-basedfor, the typedef was put out inside the declaration, for exampleif (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 aforinit. A cast of such a variable then takes theautopath above.Not covered. For a
constclosure variable, the typedef that stands for the unqualified closure type isdecltype(c), which isconst. Soauto{c}becomes__T2{c}, aconstobject, and calling amutablelambda through it fails. This is unchanged. Removing that typedef would break other uses of it, such asnew auto(c).This came up while working on #575.
Summary by CodeRabbit
Bug Fixes
auto.Tests
Documentation