Repository navigation
Record auto casts in the IL and generate them as written [GH #576] #579
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
daveedvdv-nvidia
wants to merge
1
commit into
main
Choose a base branch
from
gh-576-closure-braced-cast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+171
−6
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| [GH #576] | ||
| C++-generating back end: auto casts | ||
|
|
||
| A cast written with "auto" (C++23), as in "auto(x)" or "auto{x}", was put | ||
| out as a cast to the deduced type. When that was a closure type, the | ||
| generated code was often ill-formed: if no variable of that type had been | ||
| declared, the type was written as "class __Tn", and if the variable was | ||
| const, the cast yielded a const object. For example, with --c++23: | ||
|
|
||
| int x = auto{[](int a) { return a; }}(2); // "class __T1{...}(2)" | ||
| int f() { | ||
| const auto c = [n = 0]() mutable { return ++n; }; | ||
| return auto{c}(); // Call of a const object. | ||
| } | ||
|
|
||
| The new is_auto_cast field of a dynamic initialization now records such a | ||
| cast, and it is put out as written, with "auto". | ||
|
|
||
| Also, for a variable of closure type declared in the condition of an if, | ||
| switch, or while statement, or in a range-based for statement, a typedef | ||
| was put out inside the declaration, as in | ||
|
|
||
| if (auto m = []{ return 1; } typedef decltype(m) __T2;) ... | ||
|
|
||
| No typedef is put out for such a variable now. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| fe_only -DTEST_NUMBER=1 --c++23 Test_name.c | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| //type:fp | ||
| //options:--c++23 | ||
| //remark:auto casts and closure-type typedefs in generated C++ | ||
| // | ||
| // In generated C++, an auto cast to a closure type for which no typedef had | ||
| // been put out, like auto{[](int a) { return a; }} or auto{m} with m declared | ||
| // in a for-init, was put out with the unnamed closure type written as if it | ||
| // were a class name. An auto cast of a const closure variable used the | ||
| // typedef put out for the variable's type, which is const. Also, a typedef | ||
| // for the closure type of a variable declared in a condition or range-based | ||
| // for statement was put out inside the declaration. | ||
|
|
||
| int i = 3; | ||
| template<class T> constexpr int size(T&&) { return sizeof(T); } | ||
| struct C { C(); C(const C&); int v; }; | ||
| auto g() { return [](int a) { return a; }; } | ||
|
|
||
| static_assert(auto{[](int a) { return a; }}(2) == 2); | ||
| static_assert(auto{auto{[] { return 1; }}}() == 1); | ||
| static_assert(size(auto{[] {}}) > 0); | ||
| int x = auto{[](int a) { return a + i; }}(2); | ||
| int y = sizeof(auto{[] {}}) + size(auto{[] {}}); | ||
| auto h1 = auto{[] { return 1; }}; | ||
| int z = auto{g()}(2) + auto(g())(3); | ||
|
|
||
| int f1() { | ||
| int j = 1; | ||
| return auto{[&] { return i + j; }}() + | ||
| auto{[j]() mutable { return ++j; }}() + | ||
| auto{[](auto a) { return a; }}(j) + | ||
| auto{[c = C()] { return c.v; }}(); | ||
| } | ||
|
|
||
| template<class T> T f2() { return auto{[] { return T(1); }}(); } | ||
| int w = f2<int>(); | ||
|
|
||
| int f3() { | ||
| int n = 0; | ||
| for (auto m = [n = 0]() mutable { return ++n; }; auto{m}() < 3; auto(m)()) { | ||
| n += m(); | ||
| } | ||
| for (const auto c = [k = 0]() mutable { return ++k; }; n < 9; ++n) { | ||
| n += auto{c}(); | ||
| } | ||
| for (auto m = [c = C()] { return c.v; }; n < 12; ++n) { | ||
| n += auto{m}() + auto(m)(); | ||
| } | ||
| for (auto m = g(); n < 15; ++n) { | ||
| n += auto{m}(1); | ||
| } | ||
| return n; | ||
| } | ||
|
|
||
| int f4() { | ||
| return [m = [n = 0]() mutable { return ++n; }]() mutable { | ||
| return auto{m}() + m(); | ||
| }(); | ||
| } | ||
|
|
||
| int f6() { | ||
| const auto c = [n = 0]() mutable { return ++n; }; | ||
| return auto{c}() + auto(c)(); | ||
| } | ||
|
|
||
| struct B { int v; B(int); B(const B&); int operator()(int) const; }; | ||
| B b(1); | ||
| int u = auto{b}(1) + auto(b)(2) + auto{B(i)}(3) + auto(B{i})(4); | ||
|
|
||
| struct R { | ||
| struct It { | ||
| int n; | ||
| auto operator*() { return g(); } | ||
| It& operator++() { ++n; return *this; } | ||
| bool operator!=(It o) const { return n != o.n; } | ||
| }; | ||
| It begin() { return {0}; } | ||
| It end() { return {2}; } | ||
| }; | ||
|
|
||
| int f5() { | ||
| int n = 0; | ||
| if (auto m = [] { return 1; }) { | ||
| n += m() + auto{m}(); | ||
| } | ||
| switch (auto m = [] { return 2; }; m()) { | ||
| default: | ||
| n += m(); | ||
| } | ||
| while (auto m = [] { return 3; }) { | ||
| n += m(); | ||
| break; | ||
| } | ||
| for (auto m : R{}) { | ||
| n += m(1) + auto{m}(2); | ||
| } | ||
| return n; | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a test type that compiles the generated C++ for this regression test.
//type:fprunseccp --cpfe_only. That path writes the.int.cfile but does not compile it. The pass or fail status comes from the driver return code. Malformed closure typedefs or casts in the generated output can leave the return code at 0, so this test can pass with invalid generated C++.Use
//type:cppbe. That type runseccp -c, which compiles the generated C file, so invalid output fails the test.Suggested fix
Re-record the test's recordings after changing the type.