-
Notifications
You must be signed in to change notification settings - Fork 242
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
Allow inspect
statements
#738
Open
ntrel
wants to merge
3
commits into
hsutter:main
Choose a base branch
from
ntrel:inspect-statement
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 all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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,18 @@ | ||
v : std::any = (); | ||
|
||
f: () -> int = | ||
inspect v { | ||
is 1 = return 1; | ||
is int = { return 2; } | ||
is _ = return 3; | ||
} | ||
|
||
main: () = | ||
{ | ||
v = 1; | ||
[[assert: f() == 1]] | ||
v = 10; | ||
[[assert: f() == 2]] | ||
v = true; | ||
[[assert: f() == 3]] | ||
} |
This file contains 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,41 @@ | ||
|
||
|
||
//=== Cpp2 type declarations ==================================================== | ||
|
||
|
||
#include "cpp2util.h" | ||
|
||
|
||
|
||
//=== Cpp2 type definitions and function declarations =========================== | ||
|
||
extern std::any v; | ||
|
||
[[nodiscard]] auto f() -> int; | ||
|
||
|
||
#line 10 "pure2-inspect-statement.cpp2" | ||
auto main() -> int; | ||
|
||
|
||
//=== Cpp2 function definitions ================================================= | ||
|
||
std::any v {}; | ||
|
||
[[nodiscard]] auto f() -> int | ||
{ auto&& _expr = v; | ||
if (cpp2::is(_expr, 1)) return 1; | ||
else if (cpp2::is<int>(_expr)) {return 2; } | ||
else return 3; | ||
} | ||
|
||
auto main() -> int | ||
{ | ||
v = 1; | ||
cpp2::Default.expects(f() == 1, ""); | ||
v = 10; | ||
cpp2::Default.expects(f() == 2, ""); | ||
v = true; | ||
cpp2::Default.expects(f() == 3, ""); | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
regression-tests/test-results/pure2-inspect-statement.cpp2.output
This file contains 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-inspect-statement.cpp2... ok (all Cpp2, passes safety checks) |
This file contains 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 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.
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.
Is this the correct implementation?
I don't think there's code to lower inspect statements.
What happens with this code?
In the test case, the
inspect
is actually the expression of an expression-statement.Could it be that
is_expression
should have beentrue
in this case?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.
Oh that doesn't work. So yes it seems the test case is doingreturn inspect v {
.It does work actually!
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.
There is, in
emit(inspect_expression_node, bool)
. Ifis_expression
is false, the lambda parameters and inline call are not emitted, but the braces still are emitted. So it just generates a block. You can see it in the diff here.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.
While your example does work, the following does not:
The Cpp1 code has a semi-colon before the
else
clause, causing an error. I'll try to fix it.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.
Maybe you should scope your PR
to treating
inspect
as an expression rather than as a statementwhen it is a declaration's statement.
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.
Now pushed a simple fix.