]> git.lizzy.rs Git - rust.git/commit
Auto merge of #75470 - estebank:bare-type-expr, r=davidtwco
authorbors <bors@rust-lang.org>
Thu, 8 Oct 2020 01:37:27 +0000 (01:37 +0000)
committerbors <bors@rust-lang.org>
Thu, 8 Oct 2020 01:37:27 +0000 (01:37 +0000)
commitd9985fc1081849532546d74c35a276694833c09d
tree66cd5655e123d69172cf9638cef30a541a4ff008
parente055f87cdfcac1f4da6c518a547dee459de0aa26
parente5f83bcd04e85b8ae5f04d2d95dd9af774d422e2
Auto merge of #75470 - estebank:bare-type-expr, r=davidtwco

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 remaining part of #34255.