forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
7 changed files
with
183 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: (), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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`. |