Open
Description
Version
Cargo.toml: tokio = { version = "1", features = ["full"] }
% cargo tree | grep tokio
└── tokio v1.35.1
└── tokio-macros v2.2.0 (proc-macro)
Platform
% uname -a
Darwin (my hostname) 23.2.0 Darwin Kernel Version 23.2.0: Wed Nov 15 21:53:18 PST 2023; root:xnu-10002.61.3~2/RELEASE_ARM64_T6000 arm64
% rustc --version
rustc 1.75.0 (82e1608df 2023-12-21)
Description
[short summary of the bug]
Compiler error message for an extra semicolon with ?
(try operator) is confusing.
[code sample that causes the bug]
#[tokio::main]
async fn extra_semicolon_with_try_operator() -> Result<(), ()> {
Ok(())?;
Ok(());
}
fn main() {}
I expected to see this compiler error message with or without #[tokio::main]
attribute:
error[E0308]: mismatched types
--> hello-world/src/main.rs:2:70
|
2 | async fn extra_semicolon_with_try_operator_tokio() -> Result<(), ()> {
| ______________________________________________________________________^
3 | | Ok(())?;
4 | | Ok(());
| | - help: remove this semicolon to return this value
5 | | }
| |_^ expected `Result<(), ()>`, found `()`
|
= note: expected enum `Result<(), ()>`
found unit type `()`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `hello-world` (bin "hello-world") due to previous error
The compiler error message clearly states that line number 4's extra semicolon should be fixed, and there's no error at line 3.
Instead, this happened:
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
--> hello-world/src/main.rs:3:11
|
1 | #[tokio::main]
| -------------- this function should return `Result` or `Option` to accept `?`
2 | async fn extra_semicolon_with_try_operator_tokio() -> Result<(), ()> {
3 | Ok(())?;
| ^ cannot use the `?` operator in an async block that returns `()`
|
= help: the trait `FromResidual<Result<Infallible, _>>` is not implemented for `()`
error[E0308]: mismatched types
--> hello-world/src/main.rs:4:5
|
2 | async fn extra_semicolon_with_try_operator_tokio() -> Result<(), ()> {
| -------------- expected `Result<(), ()>` because of return type
3 | Ok(())?;
4 | Ok(());
| ^^^^^^^ expected `Result<(), ()>`, found `()`
|
= note: expected enum `Result<(), ()>`
found unit type `()`
help: try adding an expression at the end of the block
|
4 ~ Ok(());;
5 + Ok(())
|
Some errors have detailed explanations: E0277, E0308.
For more information about an error, try `rustc --explain E0277`.
error: could not compile `hello-world` (bin "hello-world") due to 2 previous errors
The compiler incorrectly states that line number 3 is problematic, and the compiler error at line number 4 is slightly different.