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 #34255.
This commit is contained in:
parent
deec530523
commit
e5f83bcd04
7 changed files with 183 additions and 17 deletions
15
src/test/ui/parser/bare-struct-body.rs
Normal file
15
src/test/ui/parser/bare-struct-body.rs
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
struct Foo {
|
||||
val: (),
|
||||
}
|
||||
|
||||
fn foo() -> Foo { //~ ERROR struct literal body without path
|
||||
val: (),
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = foo();
|
||||
x.val == 42; //~ ERROR mismatched types
|
||||
let x = { //~ ERROR struct literal body without path
|
||||
val: (),
|
||||
};
|
||||
}
|
||||
41
src/test/ui/parser/bare-struct-body.stderr
Normal file
41
src/test/ui/parser/bare-struct-body.stderr
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
error: struct literal body without path
|
||||
--> $DIR/bare-struct-body.rs:5:17
|
||||
|
|
||||
LL | fn foo() -> Foo {
|
||||
| _________________^
|
||||
LL | | val: (),
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
help: you might have forgotten to add the struct literal inside the block
|
||||
|
|
||||
LL | fn foo() -> Foo { SomeStruct {
|
||||
LL | val: (),
|
||||
LL | } }
|
||||
|
|
||||
|
||||
error: struct literal body without path
|
||||
--> $DIR/bare-struct-body.rs:12:13
|
||||
|
|
||||
LL | let x = {
|
||||
| _____________^
|
||||
LL | | val: (),
|
||||
LL | | };
|
||||
| |_____^
|
||||
|
|
||||
help: you might have forgotten to add the struct literal inside the block
|
||||
|
|
||||
LL | let x = { SomeStruct {
|
||||
LL | val: (),
|
||||
LL | } };
|
||||
|
|
||||
|
||||
error[E0308]: mismatched types
|
||||
--> $DIR/bare-struct-body.rs:11:14
|
||||
|
|
||||
LL | x.val == 42;
|
||||
| ^^ expected `()`, found integer
|
||||
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0308`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue