-
Notifications
You must be signed in to change notification settings - Fork 265
Fix C++1 generation of function pointers. #1406
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
threeifbyair
wants to merge
3
commits into
hsutter:main
Choose a base branch
from
threeifbyair:threeifbyair/bug1397
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.
Open
Changes from 2 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
32 changes: 32 additions & 0 deletions
32
regression-tests/pure2-bugfix-for-function-returning-funcptr.cpp2
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,32 @@ | ||
g_inttemplate: std::optional<int> = std::nullopt; | ||
g_template: std::optional<*(_: int) -> void> = std::nullopt; | ||
g_signal_handlers: std::unordered_map<int, *(_: int) -> void> = (); | ||
|
||
intfuncptr: type == *(_: int) -> void; | ||
|
||
set_signal: (signum: int, handler: *(_: int) -> void) -> *(_: int) -> void = { | ||
default_handler := :(foo: int) -> void = { | ||
// Default handler does nothing | ||
_ = foo; | ||
}; | ||
old_handler: intfuncptr = default_handler; | ||
if g_signal_handlers.find(signum) != g_signal_handlers.end() { | ||
old_handler = g_signal_handlers[signum]; | ||
} | ||
g_signal_handlers[signum] = handler; | ||
return old_handler; | ||
} | ||
|
||
g_signal: i64 = 0; | ||
inc_signal: (signum: int) -> void = { | ||
g_signal += signum; | ||
} | ||
dec_signal: (signum: int) -> void = { | ||
g_signal -= signum; | ||
} | ||
|
||
main: () -> int = { | ||
_ = set_signal(1, inc_signal); | ||
cmpx := set_signal(1, dec_signal); | ||
return cmpx != inc_signal; | ||
} |
1 change: 1 addition & 0 deletions
1
...s/test-results/msvc-2022-c++latest/pure2-bugfix-for-function-returning-funcptr.cpp.output
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 @@ | ||
pure2-bugfix-for-function-returning-funcptr.cpp |
72 changes: 72 additions & 0 deletions
72
regression-tests/test-results/pure2-bugfix-for-function-returning-funcptr.cpp
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,72 @@ | ||
|
||
#define CPP2_IMPORT_STD Yes | ||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
#line 1 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
#line 1 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
extern std::optional<int> g_inttemplate; | ||
#line 2 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
extern std::optional<void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> g_template; | ||
extern std::unordered_map<int,void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> g_signal_handlers; | ||
|
||
using intfuncptr = void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1); | ||
|
||
[[nodiscard]] auto set_signal(cpp2::impl::in<int> signum, void(*handler)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)) -> void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1); | ||
|
||
#line 20 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
extern cpp2::i64 g_signal; | ||
auto inc_signal(cpp2::impl::in<int> signum) -> void; | ||
|
||
#line 24 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
auto dec_signal(cpp2::impl::in<int> signum) -> void; | ||
|
||
#line 28 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
[[nodiscard]] auto main() -> int; | ||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
#line 1 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
std::optional<int> g_inttemplate {std::nullopt}; | ||
#line 2 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
std::optional<void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> g_template {std::nullopt}; | ||
std::unordered_map<int,void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)> g_signal_handlers {}; | ||
|
||
#line 7 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
[[nodiscard]] auto set_signal(cpp2::impl::in<int> signum, void(*handler)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1)) -> void(*)([[maybe_unused]] cpp2::impl::in<int> unnamed_param_1){ | ||
auto default_handler {[](cpp2::impl::in<int> foo) -> void{ | ||
// Default handler does nothing | ||
static_cast<void>(foo); | ||
}}; | ||
intfuncptr old_handler {cpp2::move(default_handler)}; | ||
if (CPP2_UFCS(find)(g_signal_handlers, signum) != CPP2_UFCS(end)(g_signal_handlers)) { | ||
old_handler = CPP2_ASSERT_IN_BOUNDS(g_signal_handlers, signum); | ||
} | ||
CPP2_ASSERT_IN_BOUNDS(g_signal_handlers, signum) = handler; | ||
return old_handler; | ||
} | ||
|
||
cpp2::i64 g_signal {0}; | ||
#line 21 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
auto inc_signal(cpp2::impl::in<int> signum) -> void{ | ||
g_signal += signum; | ||
} | ||
#line 24 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
auto dec_signal(cpp2::impl::in<int> signum) -> void{ | ||
g_signal -= signum; | ||
} | ||
|
||
#line 28 "pure2-bugfix-for-function-returning-funcptr.cpp2" | ||
[[nodiscard]] auto main() -> int{ | ||
static_cast<void>(set_signal(1, inc_signal)); | ||
auto cmpx {set_signal(1, dec_signal)}; | ||
return cpp2::move(cmpx) != inc_signal; | ||
} | ||
|
2 changes: 2 additions & 0 deletions
2
regression-tests/test-results/pure2-bugfix-for-function-returning-funcptr.cpp2.output
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,2 @@ | ||
pure2-bugfix-for-function-returning-funcptr.cpp2... ok (all Cpp2, passes safety checks) | ||
|
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
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.