Skip to content

Allow attributes on expression - #980

Closed
pcpthm wants to merge 2 commits into
rust-lang:masterfrom
pcpthm:allow-attributes-on-expr
Closed

pcpthm wants to merge 2 commits into
rust-lang:masterfrom
pcpthm:allow-attributes-on-expr

Conversation

@pcpthm

@pcpthm pcpthm commented Mar 16, 2019 •

Copy link
Copy Markdown
Contributor

This PR adds a new AST node AttrExpr. This node is used when outer attributes are applied to an expression that is not an expression statement.

fn foo() {
    bar(#[attr] 1); // CallExpr(.. ArgList(.. AttrExpr(..)))
    #[attr] func();   // ExprStmt(Attr(..), CallExpr(..))
    #[attr] func()    // AttrExpr(Attr(..), CallExpr(..)) last expression of a block is not an ExprStmt
}

It seems like stable rustc allows attributes on certain expressions when operator precedence is not confusing despite the unstable stmt_expr_attributes feature.
This PR currently isn't trying to produce parse errors and instead allowing attributes on arbitrary expression with the same precedence as lambda (this is suggested in the stmt_expr_attributes tracking issue). Probably errors can be reported on a validation pass.

This PR addresses #919's second and third case.

@pcpthm pcpthm changed the title Allow attributes on expression and struct field WIP: Allow attributes on expression and struct field Mar 17, 2019
// }
IDENT | POUND => {
let m = p.start();
attributes::outer_attributes(p);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Let's maybe send this change as a separate PR, which can be r+ed immediately?

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

Hm, this expr_block_contents looks way to complicated. Let me see if I can simplify it a bit...

@pcpthm

pcpthm commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

I did that refactoring. Will update PR.

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

@pcpthm would you mind rebasing this on a copule of cosmetic changes I am about to push?

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

(basically, flattening this all a bit)

bors Bot added a commit that referenced this pull request Mar 17, 2019
984: Allow attribute on struct literal field r=matklad a=pcpthm

Cherrypicked from #980

Co-authored-by: pcpthm <pcpthm@gmail.com>
@pcpthm
pcpthm force-pushed the allow-attributes-on-expr branch from 18978df to ef4dd4d Compare March 17, 2019 10:17
@pcpthm pcpthm changed the title WIP: Allow attributes on expression and struct field Allow attributes on expression Mar 17, 2019
@pcpthm
pcpthm force-pushed the allow-attributes-on-expr branch from ef4dd4d to 646fa06 Compare March 17, 2019 10:52
@pcpthm

pcpthm commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

@matklad I rebased your commit. This PR is updated to include bigger change than first (a new AST syntax kind) to address issue on attributes on non-ExprStmt expression leaking to outer block. Do you think introducing a new AST node right approach?

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

Do you think introducing a new AST node right approach?

I don't think so, treating attrs as children of expressions seems better. Ie, we don't create "decorator" nodes, for things like modifiers or visibilty, we store them directly as children and introduce traits like "visibility owner".

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

Whet we need here I think is an Option<Marker> arguemnt to expr_stmt. See, for example, how block_expr works

@pcpthm

pcpthm commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

Actually, I couldn't come up with how to implement parsing without introducing an another node because each expression type start an own node and I can not inject children to such node. If there is a way to "meld" two nodes after parsing, it can be much simpler to implement, I think.

@pcpthm

pcpthm commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

@matklad The problem is Option<Marker> needs to be passed to not expr_stmt, every expression types possible (tuple_expr, array_expr, ... etc. ) if I am correct. That is a modularity hazard and want to avoid.

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

. If there is a way to "meld" two nodes after parsing, it can be much simpler to implement, I think.

We can do this (CompletedMarker::precede achieves something similar). I am not sure it would be the definitive win, we need to try things out and make sure we use the single approach everywhere.

Currently, at least two approaches are used simulateneosly:

  • don't call start/finish at all inside parsing function, let the caller take care of this (maybe_item works like this)
  • instead of calling start, accept a Marker argument call finish explicitelly (lifetime_param does this)

And there's a third one:

  • call both start/finish, but allow caller to prepend values to the nodes (this'll require new API)

My gut feeling is that the Marker approach, although a bit boilerplatey, is the most robust one.

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

Another thing to keep in mind is that if we want to support parsing attributes "inside" expressions, like

1 + #[foo] 2

We'll need t teach atom to parse attrs anyway

@killercup

Copy link
Copy Markdown
Contributor

Am I right to assume this also addresses #759?

@pcpthm

pcpthm commented Mar 17, 2019 •

Copy link
Copy Markdown
Contributor Author

@killercup
Attributes on match arms are already handled (no error) but parse tree seems incorrect -> opened #986.

@killercup

Copy link
Copy Markdown
Contributor

Ah, cool! Updated the issue to show the second case, attributes on blocks, don't work yet :)

@matklad

matklad commented Mar 17, 2019

Copy link
Copy Markdown
Contributor

So, how about this plan:

  • first, we refactor maybe_item to also accept Marker as an argument: the current approach is confusing, and doing this refactor should give us a rough feeling about how it'd work for expressions
  • if it works ok for items, we could try it for expressions as well

I wouldn't worry too much about modularity here parser is nicely isolated from the rest of the analyzer, and this is what is most important.

@pcpthm

pcpthm commented Mar 17, 2019

Copy link
Copy Markdown
Contributor Author

Okay, let me try that plan. My word about modularity was too much and actually I'm thinking that approach is not bad after thinking.

bors Bot added a commit that referenced this pull request Mar 18, 2019
987: Refactor maybe_item to use Marker argument r=pcpthm a=pcpthm

As suggested at <#980 (comment)>.
For expression paring functions, changing signature
- from `fn(&mut Parser) -> Option<CompletedMarker>` to `fn(&mut Parser, Marker) -> Result<CompletedMarker, Marker>`
- from `fn(&mut Parser) -> CompletedMarker` to `fn(&mut Parser, Marker) -> CompletedMarker`
is my plan.

Co-authored-by: pcpthm <pcpthm@gmail.com>
@pcpthm

pcpthm commented Mar 18, 2019

Copy link
Copy Markdown
Contributor Author

Another PR with suggested approach incoming.

@pcpthm pcpthm closed this Mar 18, 2019
@Boscop

Boscop commented Feb 11, 2020 •

Copy link
Copy Markdown

I just installed and enabled it, but it gives me "false" errors like this:

image

In main.rs, I have stmt_expr_attributes and type_ascription enabled (among many other features). How can I tell rust-analyzer to take my enabled features into account? :)

Btw, renaming and finding all symbol references doesn't seem to work (the dialog for renaming opens but when i hit enter, it doesn't get renamed). I guess it's because it thinks that the source has syntax errors?

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.

4 participants