Skip to content

Failure to parse {2} + {2} #54482

Open
Open
@blaztinn

Description

@blaztinn

These examples should compile according to the rust reference:

fn compiles() -> i32 {
    ({2}) + ({2})
}

fn fails() -> i32 {
    {2} + {2}
}

fn compiles2() -> i32 {
    2 + {2}
}

fn fails2() -> i32 {
    {2} + 2
}

(Playground)

Errors:

   Compiling playground v0.0.1 (file:///playground)
error: expected expression, found `+`
 --> src/lib.rs:6:9
  |
6 |     {2} + {2}
  |         ^ expected expression

error: expected expression, found `+`
  --> src/lib.rs:14:9
   |
14 |     {2} + 2
   |         ^ expected expression

error: aborting due to 2 previous errors

error: Could not compile `playground`.

To learn more, run the command again with --verbose.

Rust reference snippet (thanks @Moongoodboy-K for the help here):

Expression : BlockExpression | OperatorExpression | ..
OperatorExpression : ArithmeticOrLogicalExpression | ..
ArithmeticOrLogicalExpression : Expression + Expression | ..
BlockExpression : { InnerAttribute* Statement* Expression? }

cc: @Moongoodboy-K

Activity

added
A-parserArea: The lexing & parsing of Rust source code to an AST
on Sep 22, 2018
nagisa

nagisa commented on Sep 22, 2018

@nagisa
Member

Note: this is not a regression, these fail to parse all the way back to 1.0. I think this is a duplicate of #7909, but since we are now fairly accepting of look-ahead, this seems fairly valid.

estebank

estebank commented on Sep 22, 2018

@estebank
Contributor

but since we are now fairly accepting of look-ahead,

I believe that conversation is still ongoing, we'll probably have to go the RFCS route for that...

scottmcm

scottmcm commented on Sep 23, 2018

@scottmcm
Member

I'm not certain this is actually a bug; it might be worse for + and - to have difference parser behaviour here.

Havvy

Havvy commented on Sep 23, 2018

@Havvy
Contributor

This isn't a bug. It falls out of block expression statements terminating at the }.

Havvy

Havvy commented on Sep 23, 2018

@Havvy
Contributor

The relevant section of the reference:

An expression statement is one that evaluates an expression and ignores its result. As a rule, an expression statement's purpose is to trigger the effects of evaluating its expression.

An expression that consists of only a block expression or control flow expression, if used in a context where a statement is permitted, can omit the trailing semicolon. This can cause an ambiguity between it being parsed as a standalone statement and as a part of another expression; in this case, it is parsed as a statement.

v.pop();          // Ignore the element returned from pop
if v.is_empty() {
    v.push(5);
} else {
    v.remove(0);
}                 // Semicolon can be omitted.
[1];              // Separate expression statement, not an indexing expression.

While we could theoretically make { {2} + {2} } work since { + {2} } is not a valid expression, the same cannot be true of other binary ops. Specifically -. { - {2}; } is valid.

That said, the error for this is terrible. Most likely you want to change your expression statement to be ({2} + {2}); in these cases. Is there a diagnostic issue open for that?

added
A-diagnosticsArea: Messages for errors, warnings, and lints
and removed
C-bugCategory: This is a bug.
on Sep 23, 2018
added a commit that references this issue on Apr 8, 2019

Work around rust-lang/rust#54482 when translating statement expressions

added
A-grammarArea: The grammar of Rust
and removed
A-diagnosticsArea: Messages for errors, warnings, and lints
on May 2, 2019
added a commit that references this issue on May 9, 2019

Rollup merge of rust-lang#60188 - estebank:recover-block, r=varkor

39edc68
estebank

estebank commented on Jul 23, 2019

@estebank
Contributor

The current output for this is:

error: expected expression, found `+`
 --> src/lib.rs:6:9
  |
6 |     {2} + {2}
  |     --- ^ expected expression
  |     |
  |     help: parentheses are required to parse this as an expression: `({2})`

error: expected expression, found `+`
  --> src/lib.rs:14:9
   |
14 |     {2} + 2
   |     --- ^ expected expression
   |     |
   |     help: parentheses are required to parse this as an expression: `({2})`

error[E0308]: mismatched types
 --> src/lib.rs:6:6
  |
6 |     {2} + {2}
  |      ^ expected (), found integer
  |
  = note: expected type `()`
             found type `{integer}`

error[E0308]: mismatched types
  --> src/lib.rs:14:6
   |
14 |     {2} + 2
   |      ^ expected (), found integer
   |
   = note: expected type `()`
              found type `{integer}`

error: aborting due to 4 previous errors
Spoonbender

Spoonbender commented on Mar 22, 2022

@Spoonbender

Triage: no change

added
T-langRelevant to the language team
C-discussionCategory: Discussion or questions that doesn't represent real issues.
on May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-grammarArea: The grammar of RustA-parserArea: The lexing & parsing of Rust source code to an ASTC-discussionCategory: Discussion or questions that doesn't represent real issues.T-langRelevant to the language team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @nagisa@Havvy@blaztinn@Centril@zackmdavis

        Issue actions

          Failure to parse `{2} + {2}` · Issue #54482 · rust-lang/rust