Repository navigation
Fix reconstruction of reference casts involving unnamed lambdas [GH #586] #587
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
jakub-labenski
wants to merge
1
commit into
edgcpp:main
Choose a base branch
from
jakub-labenski:unnamed-lambda-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.
+119
−3
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| [GH #586] | ||
| C++-generating back end: reference casts involving unnamed lambda types | ||
|
|
||
| The changes for EDGcpfe/26294 in version 6.8 could cause the C++-generating | ||
| back end to emit an unnameable lambda closure type as a template argument | ||
| in the destination type of a reference cast. For example, with --c++17: | ||
|
|
||
| template<class T> T&& declval(); | ||
| template<class F> using value_t = decltype(declval<F>()()); | ||
| template<class T> struct R { T value; }; | ||
| template<class F> R<value_t<F>> make(F fn) { return {fn()}; } | ||
|
|
||
| int f() { | ||
| auto&& r = make([] { return 1; }); | ||
| return static_cast<decltype(r)&&>(r).value; | ||
| } | ||
|
|
||
| Previously, the generated cast used value_t with a synthetic name for the | ||
| lambda's closure type, which made the generated code uncompilable. The | ||
| back end now emits R<int>&& as the destination type in this example. |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.unnamed-lambda-cast.rto/default.1.1.txt
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 @@ | ||
| cpfe -DTEST_NUMBER=1 --c++17 Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.unnamed-lambda-cast.rto/default.2.1.txt
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 @@ | ||
| cpfe -DTEST_NUMBER=2 --c++17 --gnu_version 150200 Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.unnamed-lambda-cast.rto/default.3.1.txt
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 @@ | ||
| cpfe -DTEST_NUMBER=3 --c++17 --clang_version 230100 Test_name.c |
1 change: 1 addition & 0 deletions
1
tests/tests/regressions/.unnamed-lambda-cast.rto/default.4.1.txt
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 @@ | ||
| cpfe -DTEST_NUMBER=4 --ms_c++17 --microsoft_version 1950 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,48 @@ | ||
| //type:cp | ||
| //options:--c++17:--c++17 --gnu_version 150200:--c++17 --clang_version 230100:--ms_c++17 --microsoft_version 1950 | ||
| //remark:Reconstruct a cast whose deduced type involves an unnamed lambda | ||
|
|
||
| template<typename T> T&& declval(); | ||
|
|
||
| template<typename T> struct identity { | ||
| using type = T; | ||
| }; | ||
|
|
||
| template<typename F> struct invoke_result { | ||
| // Expanding invoke_result_t<F> can expose this decltype. After | ||
| // substitution, G is the closure type, so emitting declval<G>() would | ||
| // still require a name for it. The back end must use the resolved int. | ||
| template<typename G> | ||
| static identity<decltype(declval<G>()(0))> test(int); | ||
| using type = typename decltype(test<F>(0))::type; | ||
| }; | ||
|
|
||
| template<typename F> using invoke_result_t = typename invoke_result<F>::type; | ||
|
|
||
| template<typename T> struct result { | ||
| T value; | ||
| }; | ||
|
|
||
| template<typename F> result<invoke_result_t<F>> transform(int input, F&& f) { | ||
| return {f(input)}; | ||
| } | ||
|
|
||
| int f(int input) { | ||
| // Passing the lambda directly leaves its closure type without a name in | ||
| // the generated C++. When reconstructing the cast, the back end must | ||
| // expand invoke_result_t<F>, whose template argument is that closure type. | ||
| auto&& r = transform(input, [](auto x) { return x + 1; }); | ||
| // The reconstructed destination type is result<int>&&. | ||
| return static_cast<decltype(r)&&>(r).value; | ||
| } | ||
|
|
||
| auto make_callable() { | ||
| struct local {}; | ||
| return [](auto) { return local{}; }; | ||
| } | ||
|
|
||
| auto callable = make_callable(); | ||
| // Checking the inner decltype must preserve the member-access exemption | ||
| // for the call. The outer decltype is usable through callable, and its | ||
| // resolved type local cannot be named here. | ||
| decltype(callable.operator()<decltype(0)>(0)) value; |
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.
Uh oh!
There was an error while loading. Please reload this page.