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

Improve expression & attribute parsing #69760

Merged
merged 19 commits into from Mar 11, 2020
Merged

Conversation

@Centril
Copy link
Member

Centril commented Mar 6, 2020

This PR includes misc improvements to expression and attribute parsing.

  1. Some code simplifications
  2. Better recovery for various block forms, e.g. loop statements } (missing { after loop). (See e.g., block-no-opening-brace.rs among others for examples.)
  3. Added recovery for e.g., unsafe $b where $b refers to a block macro fragment. (See bad-interpolated-block.rs for examples.)
  4. ^--- These are done so that code sharing in block parsing is increased.
  5. Added recovery for e.g., 'label: loop { ... } (See labeled-no-colon-expr.rs.)
  6. Added recovery for e.g., &'lifetime expr (See regions-out-of-scope-slice.rs.)
  7. Added recovery for e.g., fn foo() = expr; (See fn-body-eq-expr-semi.rs.)
  8. Simplified attribute parsing code & slightly improved diagnostics.
  9. Added recovery for e.g., Box<('a) + Trait>.
  10. Added recovery for e.g, if true #[attr] {} else #[attr] {} else #[attr] if true {}.

r? @estebank

src/librustc_parse/parser/item.rs Show resolved Hide resolved
@petrochenkov petrochenkov removed their assignment Mar 6, 2020
@Centril

This comment was marked as resolved.

Copy link
Member Author

Centril commented Mar 6, 2020

@petrochenkov Not sure if you meant "r=me" by that approval. :D

@petrochenkov

This comment was marked as resolved.

Copy link
Contributor

petrochenkov commented Mar 6, 2020

No, leaving this to estebank, I didn't look at the diagnostic parts carefully.

error: labeled expression must be followed by `:`
--> $DIR/labeled-no-colon-expr.rs:8:9
|
LL | 'l4 0;
| ----^
| | |
| | help: add `:` after the label
| the label
Comment on lines +43 to +58

This comment has been minimized.

Copy link
@estebank

estebank Mar 6, 2020

Contributor

Should we only suggest this for loops or blocks?

This comment has been minimized.

Copy link
@Centril

Centril Mar 6, 2020

Author Member

Imo that unnecessarily complicates the diagnostic logic. I mostly included this for completeness, but getting both the colon and the block wrong (e.g. 'lab if cond { ... }) seems rather unlikely.

LL | extern {
| ------ `extern` blocks define existing foreign functions and functions inside of them cannot have a body
LL | fn foo() = 42;
| ^^^ ----- help: remove the invalid body: `;`

This comment has been minimized.

Copy link
@estebank

estebank Mar 6, 2020

Contributor

Consider using span_suggestion_short so that this doesn't include the : ';' part, but I leave it to you.

This comment has been minimized.

Copy link
@Centril

Centril Mar 6, 2020

Author Member

This is pre-existing logic in AST validation. I would prefer not adjusting that in this PR.

let span = eq_sp.to(self.prev_token.span);
self.struct_span_err(span, "function body cannot be `= expression;`")
.multipart_suggestion(
"surround the expression with `{` and `}` instead of `=` and `;`",

This comment has been minimized.

Copy link
@estebank

estebank Mar 6, 2020

Contributor

What about "give the function's expression a body" or similar? The suggestion text itself will imply the mechanical change, while the message should introduce concepts/terminology we use elsewhere.

This comment has been minimized.

Copy link
@Centril

Centril Mar 6, 2020

Author Member

I've used the current wording because it highlights that the users was thinking correctly, but used the wrong syntax. I think if we say "give the function's expression a body", then a user can reasonably react: "but I have given the function a body?!?" and "body" doesn't necessarily refer to { ... }, as the "body" of a const uses = expr ;.

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Mar 6, 2020

LGTM, but left some nitpicks. Feel free to resolve them as you prefer.

@Centril

This comment has been minimized.

Copy link
Member Author

Centril commented Mar 7, 2020

@estebank Pushed some changes to address the review comments; please take a look. :)

error: expected outer doc comment
--> $DIR/doc-comment-in-if-statement.rs:2:13
|
LL | if true /*!*/ {}
| -- ^^^^^ expected `{`
| |
| this `if` expression has a condition, but no block
| ^^^^^
|
= note: inner doc comments like this (starting with `//!` or `/*!`) can only appear before items
Comment on lines +1 to +7

This comment has been minimized.

Copy link
@estebank

estebank Mar 7, 2020

Contributor

I feel the message is a bit misleading, as it should be closer to "found invalid inner doc comment"/"inner doc comments like this (...) can only appear at the mod level before all items", or something like that.

This comment has been minimized.

Copy link
@estebank

estebank Mar 7, 2020

Contributor

Other than this nitpick, r=me

This comment has been minimized.

Copy link
@Centril

Centril Mar 8, 2020

Author Member

I believe the message is actually accurate, because trait Foo { /*! foo */ } is legal. In other words, this applies to item containers in general (but there are other contexts where inner attributes are allowed as well, e.g., Foo { /*! foo */ };).

Anyways, this is a pre-existing wording, and I would prefer not to inflate the PR more... :)

error: expected expression, found reserved keyword `try`
--> $DIR/block-no-opening-brace.rs:24:5
|
LL | try
| ^^^ expected expression

error: expected one of `move`, `|`, or `||`, found keyword `let`
--> $DIR/block-no-opening-brace.rs:30:9
|
LL | async
| - expected one of `move`, `|`, or `||`
LL | let x = 0;
| ^^^ unexpected token

error[E0658]: async closures are unstable
--> $DIR/block-no-opening-brace.rs:29:5
|
LL | async
| ^^^^^
|
= note: see issue #62290 <https://github.com/rust-lang/rust/issues/62290> for more information
= help: add `#![feature(async_closure)]` to the crate attributes to enable
Comment on lines +28 to +49

This comment has been minimized.

Copy link
@estebank

estebank Mar 7, 2020

Contributor

:-/

This comment has been minimized.

Copy link
@Centril

Centril Mar 8, 2020

Author Member

(This happens because async block/closure parsing routes to a block if and only if there's a { ahead, and otherwise parses a closure. I want to fix this, but some other time. try blocks have a similar issue.)

This comment has been minimized.

Copy link
@estebank

estebank Mar 10, 2020

Contributor

At the very least a small improvement would be to add a check call for { so that the error is

expected one of `move`, `|`, `||` or `{`, found keyword `let`
@bors

This comment was marked as resolved.

Copy link
Contributor

bors commented Mar 8, 2020

☔️ The latest upstream changes (presumably #69804) made this pull request unmergeable. Please resolve the merge conflicts.

@Centril Centril force-pushed the Centril:parse-expr-improve branch from f30ff8f to 458383d Mar 10, 2020
@Centril

This comment has been minimized.

Copy link
Member Author

Centril commented Mar 10, 2020

@bors r=estebank

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Mar 10, 2020

📌 Commit 458383d has been approved by estebank

@bors

This comment has been minimized.

Copy link
Contributor

bors commented Mar 10, 2020

🌲 The tree is currently closed for pull requests below priority 1000, this pull request will be tested once the tree is reopened

Centril added a commit to Centril/rust that referenced this pull request Mar 10, 2020
Improve expression & attribute parsing

This PR includes misc improvements to expression and attribute parsing.

1. Some code simplifications
2. Better recovery for various block forms, e.g. `loop statements }` (missing `{` after `loop`). (See e.g., `block-no-opening-brace.rs` among others for examples.)
3. Added recovery for e.g., `unsafe $b` where `$b` refers to a `block` macro fragment. (See `bad-interpolated-block.rs` for examples.)
4. ^--- These are done so that code sharing in block parsing is increased.
5. Added recovery for e.g., `'label: loop { ... }` (See `labeled-no-colon-expr.rs`.)
6. Added recovery for e.g., `&'lifetime expr` (See `regions-out-of-scope-slice.rs`.)
7. Added recovery for e.g., `fn foo() = expr;` (See `fn-body-eq-expr-semi.rs`.)
8. Simplified attribute parsing code & slightly improved diagnostics.
9. Added recovery for e.g., `Box<('a) + Trait>`.
10. Added recovery for e.g, `if true #[attr] {} else #[attr] {} else #[attr] if true {}`.

r? @estebank
Centril added a commit to Centril/rust that referenced this pull request Mar 10, 2020
Improve expression & attribute parsing

This PR includes misc improvements to expression and attribute parsing.

1. Some code simplifications
2. Better recovery for various block forms, e.g. `loop statements }` (missing `{` after `loop`). (See e.g., `block-no-opening-brace.rs` among others for examples.)
3. Added recovery for e.g., `unsafe $b` where `$b` refers to a `block` macro fragment. (See `bad-interpolated-block.rs` for examples.)
4. ^--- These are done so that code sharing in block parsing is increased.
5. Added recovery for e.g., `'label: loop { ... }` (See `labeled-no-colon-expr.rs`.)
6. Added recovery for e.g., `&'lifetime expr` (See `regions-out-of-scope-slice.rs`.)
7. Added recovery for e.g., `fn foo() = expr;` (See `fn-body-eq-expr-semi.rs`.)
8. Simplified attribute parsing code & slightly improved diagnostics.
9. Added recovery for e.g., `Box<('a) + Trait>`.
10. Added recovery for e.g, `if true #[attr] {} else #[attr] {} else #[attr] if true {}`.

r? @estebank
bors added a commit that referenced this pull request Mar 10, 2020
Rollup of 6 pull requests

Successful merges:

 - #69122 (Backtrace Debug tweaks)
 - #69591 (Use TypeRelating for instantiating query responses)
 - #69760 (Improve expression & attribute parsing)
 - #69837 (Use smaller discriminants for generators)
 - #69838 (Expansion-driven outline module parsing)
 - #69859 (fix #62456)

Failed merges:

r? @ghost
bors added a commit that referenced this pull request Mar 10, 2020
Rollup of 6 pull requests

Successful merges:

 - #69122 (Backtrace Debug tweaks)
 - #69591 (Use TypeRelating for instantiating query responses)
 - #69760 (Improve expression & attribute parsing)
 - #69837 (Use smaller discriminants for generators)
 - #69838 (Expansion-driven outline module parsing)
 - #69859 (fix #62456)

Failed merges:

r? @ghost
bors added a commit that referenced this pull request Mar 11, 2020
Rollup of 8 pull requests

Successful merges:

 - #66472 (--show-coverage json)
 - #69603 (tidy: replace `make check` with `./x.py test` in documentation)
 - #69760 (Improve expression & attribute parsing)
 - #69828 (fix memory leak when vec::IntoIter panics during drop)
 - #69850 (panic_bounds_check: use caller_location, like PanicFnLangItem)
 - #69876 (Add long error explanation for E0739)
 - #69888 ([Miri] Use a session variable instead of checking for an env var always)
 - #69893 (librustc_codegen_llvm: Use slices instead of 0-terminated strings)

Failed merges:

r? @ghost
@bors bors merged commit 9674c09 into rust-lang:master Mar 11, 2020
4 checks passed
4 checks passed
pr Build #20200310.21 succeeded
Details
pr (Linux mingw-check) Linux mingw-check succeeded
Details
pr (Linux x86_64-gnu-llvm-7) Linux x86_64-gnu-llvm-7 succeeded
Details
pr (Linux x86_64-gnu-tools) Linux x86_64-gnu-tools succeeded
Details
@Centril Centril deleted the Centril:parse-expr-improve branch Mar 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

5 participants
You can’t perform that action at this time.