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

Detect blocks that could be struct expr bodies #75470

Merged
merged 1 commit into from
Oct 8, 2020

Commits on Oct 7, 2020

  1. Detect blocks that could be struct expr bodies

    This approach lives exclusively in the parser, so struct expr bodies
    that are syntactically correct on their own but are otherwise incorrect
    will still emit confusing errors, like in the following case:
    
    ```rust
    fn foo() -> Foo {
        bar: Vec::new()
    }
    ```
    
    ```
    error[E0425]: cannot find value `bar` in this scope
     --> src/file.rs:5:5
      |
    5 |     bar: Vec::new()
      |     ^^^ expecting a type here because of type ascription
    
    error[E0214]: parenthesized type parameters may only be used with a `Fn` trait
     --> src/file.rs:5:15
      |
    5 |     bar: Vec::new()
      |               ^^^^^ only `Fn` traits may use parentheses
    
    error[E0107]: wrong number of type arguments: expected 1, found 0
     --> src/file.rs:5:10
      |
    5 |     bar: Vec::new()
      |          ^^^^^^^^^^ expected 1 type argument
      ```
    
    If that field had a trailing comma, that would be a parse error and it
    would trigger the new, more targetted, error:
    
    ```
    error: struct literal body without path
     --> file.rs:4:17
      |
    4 |   fn foo() -> Foo {
      |  _________________^
    5 | |     bar: Vec::new(),
    6 | | }
      | |_^
      |
    help: you might have forgotten to add the struct literal inside the block
      |
    4 | fn foo() -> Foo { Path {
    5 |     bar: Vec::new(),
    6 | } }
      |
    ```
    
    Partially address last part of rust-lang#34255.
    estebank committed Oct 7, 2020
    Configuration menu
    Copy the full SHA
    e5f83bc View commit details
    Browse the repository at this point in the history