Skip to content

Commit e10a7db

Browse files
committed
Add another *ExprWithBlock* test for try blocks
1 parent 733108b commit e10a7db

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-fail
2+
//@ edition: 2018
3+
4+
#![feature(try_blocks)]
5+
#![crate_type = "lib"]
6+
7+
// fine because the `;` discards the value
8+
fn foo(a: &str, b: &str) -> i32 {
9+
try {
10+
let foo = std::fs::read_to_string(a)?;
11+
std::fs::write(b, foo);
12+
};
13+
4 + 10
14+
}
15+
16+
// parses without the semicolon, but gives a type error
17+
fn bar(a: &str, b: &str) -> i32 {
18+
try {
19+
let foo = std::fs::read_to_string(a)?;
20+
//~^ ERROR mismatched types
21+
std::fs::write(b, foo);
22+
}
23+
4 + 10
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/try-block-as-statement.rs:19:19
3+
|
4+
LL | let foo = std::fs::read_to_string(a)?;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<_, Error>`
6+
|
7+
= note: expected unit type `()`
8+
found enum `Result<_, std::io::Error>`
9+
help: consider using `Result::expect` to unwrap the `Result<_, std::io::Error>` value, panicking if the value is a `Result::Err`
10+
|
11+
LL | let foo = std::fs::read_to_string(a)?.expect("REASON");
12+
| +++++++++++++++++
13+
14+
error: aborting due to 1 previous error
15+
16+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)