Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tests/ui/try-block/try-block-as-statement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//@ check-fail
//@ edition: 2018
Copy link
Member

@Kivooeo Kivooeo Nov 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit wonder about why edition 2018

(I know that most/all try block tests are using this edition, but I'm unsure, can we maybe update edition?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because try isn't a keyword in 2015.

(I didn't actually think about which edition, just copied an existing try block test.)


#![feature(try_blocks)]
#![crate_type = "lib"]

// fine because the `;` discards the value
fn foo(a: &str, b: &str) -> i32 {
try {
let foo = std::fs::read_to_string(a)?;
std::fs::write(b, foo);
};
4 + 10
}

// parses without the semicolon, but gives a type error
fn bar(a: &str, b: &str) -> i32 {
try {
let foo = std::fs::read_to_string(a)?;
//~^ ERROR mismatched types
std::fs::write(b, foo);
}
4 + 10
}
16 changes: 16 additions & 0 deletions tests/ui/try-block/try-block-as-statement.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error[E0308]: mismatched types
--> $DIR/try-block-as-statement.rs:19:19
|
LL | let foo = std::fs::read_to_string(a)?;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `Result<_, Error>`
|
= note: expected unit type `()`
found enum `Result<_, std::io::Error>`
help: consider using `Result::expect` to unwrap the `Result<_, std::io::Error>` value, panicking if the value is a `Result::Err`
|
LL | let foo = std::fs::read_to_string(a)?.expect("REASON");
| +++++++++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Loading