about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-08-12 15:39:15 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-10-07 13:40:52 -0700
commite5f83bcd04e85b8ae5f04d2d95dd9af774d422e2 (patch)
tree91a55ddfa401d3de3caa3930c232dcf43aa3c6c8 /src/test
parentdeec53052312ac709f6a37110b59ada486bea0bd (diff)
downloadrust-e5f83bcd04e85b8ae5f04d2d95dd9af774d422e2.tar.gz
rust-e5f83bcd04e85b8ae5f04d2d95dd9af774d422e2.zip
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.
Diffstat (limited to 'src/test')
-rw-r--r--src/test/ui/parser/bare-struct-body.rs15
-rw-r--r--src/test/ui/parser/bare-struct-body.stderr41
2 files changed, 56 insertions, 0 deletions
diff --git a/src/test/ui/parser/bare-struct-body.rs b/src/test/ui/parser/bare-struct-body.rs
new file mode 100644
index 00000000000..a557e861dee
--- /dev/null
+++ b/src/test/ui/parser/bare-struct-body.rs
@@ -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: (),
+    };
+}
diff --git a/src/test/ui/parser/bare-struct-body.stderr b/src/test/ui/parser/bare-struct-body.stderr
new file mode 100644
index 00000000000..df10b0ec2d2
--- /dev/null
+++ b/src/test/ui/parser/bare-struct-body.stderr
@@ -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`.