Skip to content
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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions regression-tests/pure2-inspect-statement.cpp2
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]]
}
41 changes: 41 additions & 0 deletions regression-tests/test-results/pure2-inspect-statement.cpp
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, "");
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pure2-inspect-statement.cpp2... ok (all Cpp2, passes safety checks)
17 changes: 8 additions & 9 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1943,15 +1943,15 @@ class cppfront
printer.emit_to_string(&statement);
emit(*alt->statement);
printer.emit_to_string();
// ... and jettison the final ; for an expression-statement
while (
!statement.empty()
&& (
statement.back() == ';'
|| isspace(statement.back())
)
)
// ... and jettison any final ; for an expression-statement
auto return_suffix = std::string{};
while (!statement.empty())
{
if (statement.back() == ';') {
return_suffix = ';'; // tack the ; back on in the alternative body
} else if (!isspace(statement.back())) {
break;
}
statement.pop_back();
}

Expand All @@ -1961,7 +1961,6 @@ class cppfront
// in an 'if constexpr' so that its type is ignored for mismatches with
// the inspect-expression's type
auto return_prefix = std::string{};
auto return_suffix = std::string{";"}; // use this to tack the ; back on in the alternative body
if (is_expression) {
return_prefix = "{ if constexpr( requires{" + statement + ";} ) if constexpr( std::is_convertible_v<CPP2_TYPEOF((" + statement + "))," + result_type + "> ) return ";
return_suffix += " }";
Expand Down
8 changes: 0 additions & 8 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -6770,14 +6770,6 @@ class parser
return {};
}

if (!is_expression) {
errors.emplace_back(
curr().position(),
"(temporary alpha limitation) cppfront is still learning 'inspect' - only inspect expressions are currently supported"
);
return {};
}

Comment on lines -6773 to -6780
Copy link
Contributor

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?

main: () = {
  inspect 0 {
    is 0 = std::cout << "0\n";
    is _ = std::cout << "?\n";
  }
}

In the test case, the inspect is actually the expression of an expression-statement.
Could it be that is_expression should have been true in this case?

Copy link
Contributor Author

@ntrel ntrel Oct 9, 2023

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 doing return inspect v {.

It does work actually!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there's code to lower inspect statements.

There is, in emit(inspect_expression_node, bool). If is_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.

Copy link
Contributor Author

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:

  inspect 0 {
    is 0 = { std::cout << "0\n"; }
    is _ = std::cout << "?\n";
  }

The Cpp1 code has a semi-colon before the else clause, causing an error. I'll try to fix it.

Copy link
Contributor

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 statement
when it is a declaration's statement.

Copy link
Contributor Author

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.

auto n = std::make_unique<inspect_expression_node>();
n->identifier = &curr();
next();
Expand Down