From 70ddde76dfbb03909c565b329f86e49de1f767cc Mon Sep 17 00:00:00 2001 From: León Orell Valerian Liehr Date: Fri, 6 Jan 2023 17:47:21 +0100 Subject: parser: recover from where clauses placed before tuple struct bodies --- tests/ui/parser/issues/issue-17904.rs | 4 ++- tests/ui/parser/issues/issue-17904.stderr | 15 ++++++-- ...r-where-clause-before-tuple-struct-body-0.fixed | 15 ++++++++ ...over-where-clause-before-tuple-struct-body-0.rs | 17 +++++++++ ...-where-clause-before-tuple-struct-body-0.stderr | 40 ++++++++++++++++++++++ ...over-where-clause-before-tuple-struct-body-1.rs | 7 ++++ ...-where-clause-before-tuple-struct-body-1.stderr | 23 +++++++++++++ 7 files changed, 117 insertions(+), 4 deletions(-) create mode 100644 tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed create mode 100644 tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs create mode 100644 tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr create mode 100644 tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs create mode 100644 tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr (limited to 'tests/ui/parser') diff --git a/tests/ui/parser/issues/issue-17904.rs b/tests/ui/parser/issues/issue-17904.rs index 7d6a54f4be1..020fb41c227 100644 --- a/tests/ui/parser/issues/issue-17904.rs +++ b/tests/ui/parser/issues/issue-17904.rs @@ -1,6 +1,8 @@ +// compile-flags: -Zparse-only + struct Baz where U: Eq(U); //This is parsed as the new Fn* style parenthesis syntax. struct Baz where U: Eq(U) -> R; // Notice this parses as well. struct Baz(U) where U: Eq; // This rightfully signals no error as well. -struct Foo where T: Copy, (T); //~ ERROR expected one of `:`, `==`, or `=`, found `;` +struct Foo where T: Copy, (T); //~ ERROR where clauses are not allowed before tuple struct bodies fn main() {} diff --git a/tests/ui/parser/issues/issue-17904.stderr b/tests/ui/parser/issues/issue-17904.stderr index a3cac676189..aa343975dca 100644 --- a/tests/ui/parser/issues/issue-17904.stderr +++ b/tests/ui/parser/issues/issue-17904.stderr @@ -1,8 +1,17 @@ -error: expected one of `:`, `==`, or `=`, found `;` - --> $DIR/issue-17904.rs:4:33 +error: where clauses are not allowed before tuple struct bodies + --> $DIR/issue-17904.rs:6:15 | LL | struct Foo where T: Copy, (T); - | ^ expected one of `:`, `==`, or `=` + | --- ^^^^^^^^^^^^^^ --- the struct body + | | | + | | unexpected where clause + | while parsing this tuple struct + | +help: move the body before the where clause + | +LL - struct Foo where T: Copy, (T); +LL + struct Foo(T) where T: Copy; + | error: aborting due to previous error diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed new file mode 100644 index 00000000000..227c40e97c0 --- /dev/null +++ b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.fixed @@ -0,0 +1,15 @@ +// Regression test for issues #100790 and #106439. +// run-rustfix + +pub struct Example(usize) +where + (): Sized; +//~^^^ ERROR where clauses are not allowed before tuple struct bodies + +struct _Demo(pub usize, usize) +where + (): Sized, + String: Clone; +//~^^^^ ERROR where clauses are not allowed before tuple struct bodies + +fn main() {} diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs new file mode 100644 index 00000000000..3699e6fe572 --- /dev/null +++ b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.rs @@ -0,0 +1,17 @@ +// Regression test for issues #100790 and #106439. +// run-rustfix + +pub struct Example +where + (): Sized, +(usize); +//~^^^ ERROR where clauses are not allowed before tuple struct bodies + +struct _Demo +where + (): Sized, + String: Clone, +(pub usize, usize); +//~^^^^ ERROR where clauses are not allowed before tuple struct bodies + +fn main() {} diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr new file mode 100644 index 00000000000..18aa5fadb6b --- /dev/null +++ b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-0.stderr @@ -0,0 +1,40 @@ +error: where clauses are not allowed before tuple struct bodies + --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:5:1 + | +LL | pub struct Example + | ------- while parsing this tuple struct +LL | / where +LL | | (): Sized, + | |______________^ unexpected where clause +LL | (usize); + | ------- the struct body + | +help: move the body before the where clause + | +LL ~ pub struct Example(usize) +LL | where +LL ~ (): Sized; + | + +error: where clauses are not allowed before tuple struct bodies + --> $DIR/recover-where-clause-before-tuple-struct-body-0.rs:11:1 + | +LL | struct _Demo + | ----- while parsing this tuple struct +LL | / where +LL | | (): Sized, +LL | | String: Clone, + | |__________________^ unexpected where clause +LL | (pub usize, usize); + | ------------------ the struct body + | +help: move the body before the where clause + | +LL ~ struct _Demo(pub usize, usize) +LL | where +LL | (): Sized, +LL ~ String: Clone; + | + +error: aborting due to 2 previous errors + diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs new file mode 100644 index 00000000000..f515ae81e51 --- /dev/null +++ b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.rs @@ -0,0 +1,7 @@ +// Regression test for issues #100790 and #106439. + +// Make sure that we still show a helpful error message even if the trailing semicolon is missing. + +struct Foo where T: MyTrait, (T) +//~^ ERROR where clauses are not allowed before tuple struct bodies +//~| ERROR expected `;`, found `` diff --git a/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr new file mode 100644 index 00000000000..2219c2a7316 --- /dev/null +++ b/tests/ui/parser/recover-where-clause-before-tuple-struct-body-1.stderr @@ -0,0 +1,23 @@ +error: where clauses are not allowed before tuple struct bodies + --> $DIR/recover-where-clause-before-tuple-struct-body-1.rs:5:15 + | +LL | struct Foo where T: MyTrait, (T) + | --- ^^^^^^^^^^^^^^^^^ --- the struct body + | | | + | | unexpected where clause + | while parsing this tuple struct + | +help: move the body before the where clause + | +LL - struct Foo where T: MyTrait, (T) +LL + struct Foo(T) where T: MyTrait + | + +error: expected `;`, found `` + --> $DIR/recover-where-clause-before-tuple-struct-body-1.rs:5:35 + | +LL | struct Foo where T: MyTrait, (T) + | ^ expected `;` + +error: aborting due to 2 previous errors + -- cgit 1.4.1-3-g733a5