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

Conversation

ntrel
Copy link
Contributor

@ntrel ntrel commented Oct 9, 2023

I'm not sure why they were disabled, the test below works.

Update: A small fix was needed to avoid emitting }; else Cpp1 when statement is a compound statement.

@ntrel ntrel changed the title Allow inspect statements Allow inspect statements Oct 9, 2023
Copy link
Contributor

@JohelEGP JohelEGP left a comment

Choose a reason for hiding this comment

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

Does this also resolve #723?
If so, add "Closes #723" to the opening comment.

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

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.

@ntrel ntrel marked this pull request as draft October 9, 2023 11:28
@ntrel
Copy link
Contributor Author

ntrel commented Oct 9, 2023

Does this also resolve #723?

No, it gives:

    s := inspect v -> std::string {

inspect.cpp2(13,20): error: an inspect statement cannot have an explicit return type (whereas an inspect expression must have one) (at '->')

@ntrel ntrel marked this pull request as ready for review October 9, 2023 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants