summaryrefslogtreecommitdiffstats
path: root/tests/ui/expr/malformed_closure
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-17 12:19:13 +0000
commit218caa410aa38c29984be31a5229b9fa717560ee (patch)
treec54bd55eeb6e4c508940a30e94c0032fbd45d677 /tests/ui/expr/malformed_closure
parentReleasing progress-linux version 1.67.1+dfsg1-1~progress7.99u1. (diff)
downloadrustc-218caa410aa38c29984be31a5229b9fa717560ee.tar.xz
rustc-218caa410aa38c29984be31a5229b9fa717560ee.zip
Merging upstream version 1.68.2+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tests/ui/expr/malformed_closure')
-rw-r--r--tests/ui/expr/malformed_closure/missing_braces_around_block.fixed19
-rw-r--r--tests/ui/expr/malformed_closure/missing_braces_around_block.rs19
-rw-r--r--tests/ui/expr/malformed_closure/missing_braces_around_block.stderr38
-rw-r--r--tests/ui/expr/malformed_closure/ruby_style_closure.rs15
-rw-r--r--tests/ui/expr/malformed_closure/ruby_style_closure.stderr9
5 files changed, 100 insertions, 0 deletions
diff --git a/tests/ui/expr/malformed_closure/missing_braces_around_block.fixed b/tests/ui/expr/malformed_closure/missing_braces_around_block.fixed
new file mode 100644
index 000000000..c50b9a12b
--- /dev/null
+++ b/tests/ui/expr/malformed_closure/missing_braces_around_block.fixed
@@ -0,0 +1,19 @@
+// This snippet ensures that no attempt to recover on a semicolon instead of
+// comma is made next to a closure body.
+//
+// If this recovery happens, then plenty of errors are emitted. Here, we expect
+// only one error.
+//
+// This is part of issue #88065:
+// https://github.com/rust-lang/rust/issues/88065
+
+// run-rustfix
+
+fn main() {
+ let num = 5;
+ (1..num).reduce(|a, b| {
+ //~^ ERROR: closure bodies that contain statements must be surrounded by braces
+ println!("{}", a);
+ a * b
+ }).unwrap();
+}
diff --git a/tests/ui/expr/malformed_closure/missing_braces_around_block.rs b/tests/ui/expr/malformed_closure/missing_braces_around_block.rs
new file mode 100644
index 000000000..58c81f3a6
--- /dev/null
+++ b/tests/ui/expr/malformed_closure/missing_braces_around_block.rs
@@ -0,0 +1,19 @@
+// This snippet ensures that no attempt to recover on a semicolon instead of
+// comma is made next to a closure body.
+//
+// If this recovery happens, then plenty of errors are emitted. Here, we expect
+// only one error.
+//
+// This is part of issue #88065:
+// https://github.com/rust-lang/rust/issues/88065
+
+// run-rustfix
+
+fn main() {
+ let num = 5;
+ (1..num).reduce(|a, b|
+ //~^ ERROR: closure bodies that contain statements must be surrounded by braces
+ println!("{}", a);
+ a * b
+ ).unwrap();
+}
diff --git a/tests/ui/expr/malformed_closure/missing_braces_around_block.stderr b/tests/ui/expr/malformed_closure/missing_braces_around_block.stderr
new file mode 100644
index 000000000..dac9a8cfc
--- /dev/null
+++ b/tests/ui/expr/malformed_closure/missing_braces_around_block.stderr
@@ -0,0 +1,38 @@
+error: closure bodies that contain statements must be surrounded by braces
+ --> $DIR/missing_braces_around_block.rs:14:26
+ |
+LL | (1..num).reduce(|a, b|
+ | ^
+...
+LL | ).unwrap();
+ | ^
+ |
+note: statement found outside of a block
+ --> $DIR/missing_braces_around_block.rs:16:26
+ |
+LL | println!("{}", a);
+ | -----------------^ this `;` turns the preceding closure into a statement
+ | |
+ | this expression is a statement because of the trailing semicolon
+note: the closure body may be incorrectly delimited
+ --> $DIR/missing_braces_around_block.rs:14:21
+ |
+LL | (1..num).reduce(|a, b|
+ | _____________________^
+LL | |
+LL | | println!("{}", a);
+ | |_________________________^ this is the parsed closure...
+LL | a * b
+LL | ).unwrap();
+ | - ...but likely you meant the closure to end here
+help: try adding braces
+ |
+LL ~ (1..num).reduce(|a, b| {
+LL |
+LL | println!("{}", a);
+LL | a * b
+LL ~ }).unwrap();
+ |
+
+error: aborting due to previous error
+
diff --git a/tests/ui/expr/malformed_closure/ruby_style_closure.rs b/tests/ui/expr/malformed_closure/ruby_style_closure.rs
new file mode 100644
index 000000000..fdec072b8
--- /dev/null
+++ b/tests/ui/expr/malformed_closure/ruby_style_closure.rs
@@ -0,0 +1,15 @@
+// Part of issue #27300.
+// The problem here is that ruby-style closures are parsed as blocks whose
+// first statement is a closure. See the issue for more details:
+// https://github.com/rust-lang/rust/issues/27300
+
+// Note: this test represents what the compiler currently emits. The error
+// message will be improved later.
+
+fn main() {
+ let p = Some(45).and_then({
+ |x| println!("doubling {}", x);
+ Some(x * 2)
+ //~^ ERROR: cannot find value `x` in this scope
+ });
+}
diff --git a/tests/ui/expr/malformed_closure/ruby_style_closure.stderr b/tests/ui/expr/malformed_closure/ruby_style_closure.stderr
new file mode 100644
index 000000000..e8b34121b
--- /dev/null
+++ b/tests/ui/expr/malformed_closure/ruby_style_closure.stderr
@@ -0,0 +1,9 @@
+error[E0425]: cannot find value `x` in this scope
+ --> $DIR/ruby_style_closure.rs:12:14
+ |
+LL | Some(x * 2)
+ | ^ not found in this scope
+
+error: aborting due to previous error
+
+For more information about this error, try `rustc --explain E0425`.