Skip to content

Grammar: Statements. #427

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

Merged
merged 5 commits into from
Oct 20, 2018
Merged

Conversation

ehuss
Copy link
Contributor

@ehuss ehuss commented Sep 23, 2018

Cleans up "Statement" definition.
Adds "let" statement.

@ehuss
Copy link
Contributor Author

ehuss commented Sep 24, 2018

There's an issue with this PR. Some statements allow outer attributes, but that is not indicated in the grammar. I see two ways to fix that:

  1. Just slap OuterAttribute* on all statements, and then explain which ones are restricted in the text.
  2. Carefully annotate which statements accept outer attributes.

Statements that I know of that don't support attributes:

  • ;
  • if and if let
  • Compound expressions like most Operator Expressions and Range Expressions (requires stmt_expr_attributes feature), presumably this is due to precedence.

I lean towards 1, but please let me know which you think would be better.

@matthewjasper
Copy link
Contributor

I lean towards 1, but please let me know which you think would be better.

I think that's probably the right choice.

@ehuss ehuss force-pushed the grammar-statements branch from 20c0b33 to f30c45d Compare October 8, 2018 06:39
@ehuss
Copy link
Contributor Author

ehuss commented Oct 8, 2018

I decided to document attributes in a slightly different way. It was more complex than I first thought, as it was accidentally stabilized for expressions in some contexts.

I am a little uneasy about the way statements are defined. Technically the ; is not part of the statement. Trying to define it without made it needlessly more complicated, though.

@Havvy
Copy link
Contributor

Havvy commented Oct 8, 2018

What I see LGTM. What I don't see, doesn't. attributes.md should list all places attributes are allowed, and you've shown a bunch of expression locations that I've missed. Can you add them to the list please?

@ehuss
Copy link
Contributor Author

ehuss commented Oct 8, 2018

attributes.md should list all places attributes are allowed

I added to the list, though in a brief form. Hopefully that's ok, it seemed more natural that way instead of stuffing all the rules in one place.

@@ -4,19 +4,11 @@
> _BlockExpression_ :\
>    `{`\
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
> &nbsp;&nbsp; &nbsp;&nbsp; _Statements_<sup>\*</sup>\
> &nbsp;&nbsp; &nbsp;&nbsp; [_Statement_]<sup>\*</sup>\
Copy link
Contributor

Choose a reason for hiding this comment

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

There should be a note that * is lazy 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.

Just to be clear, are you saying that because block-expressions can be confused as to whether it is a statement or a tail expression?

This kinda gets back to my uneasiness with how statements are defined. Technically they do not include the trailing semicolon, they are separated by semicolons. I'm thinking about rewriting the grammar to express it that way. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes { { 2 } } has a tail expression, not a tail-statement.

If you can get statements without ; to work well then I would prefer that but I couldn't work out how to do it myself.

@ehuss ehuss force-pushed the grammar-statements branch from 94a8929 to f428fa2 Compare October 19, 2018 21:55
@ehuss ehuss force-pushed the grammar-statements branch from f428fa2 to 49f5c9a Compare October 19, 2018 22:01
@ehuss
Copy link
Contributor Author

ehuss commented Oct 19, 2018

Ok, I have pushed an update to rewrite statements in such a way that I think is more correct (less wrong?). I also came across some related attributes that were missing.

Please let me know if there's anything I can do to make it easier to review these things. Sample code, links to rustc source, whatever.

Copy link
Contributor

@matthewjasper matthewjasper left a comment

Choose a reason for hiding this comment

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

I think this is good now. One thing that is maybe worth changing.

> &nbsp;&nbsp; &nbsp;&nbsp; `;`\
> &nbsp;&nbsp; | [_Item_]\
> &nbsp;&nbsp; | [_LetStatement_]\
> &nbsp;&nbsp; | [_ExpressionWithoutBlock_][expression] `;`\
Copy link
Contributor

Choose a reason for hiding this comment

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

You could move these down to Expression statements, also this arguably should be Expression since { { 1 } let x = 0; } does not type check while { { 1 }; let x = 0; } does.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, moved it down.

It can't be just Expression because { { () } let x = 0; } is valid. That's why I put the sentence about blocks must have the unit type. Generally I don't think the grammar should include type checking rules.

@matthewjasper matthewjasper merged commit 457b072 into rust-lang:master Oct 20, 2018
@matthewjasper
Copy link
Contributor

Thanks for this!

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.

3 participants