-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inconsistent Error Handling Behavior with ?
operator in Marco #[tokio::main]
#6930
Comments
I guess the issue is that we're not properly informing the closure about the return type... |
Thank you for the clarification! It makes sense regarding the closure and return type. I’ll take a closer look and experiment with it a bit more to deepen my understanding of tokio. Appreciate the insights—this is a great learning opportunity for me! |
If we can fix this in a non-intrusive way, then I'm happy to see a PR for that. But changes to this part of the macro have proven tricky in the past. |
Thanks for pointing that out! I fully understand that changes in this area can be tricky. If I come up with any ideas or plans for a potential PR, I’ll be sure to discuss them with you and the other maintainers before making any code changes or submitting anything. I really appreciate your guidance and openness to contributions! |
You don't have to ask first before creating a PR. Just be prepared that it may (or may not!) be difficult to fix this. We have previously closed several PRs in this part of the codebase because they were incorrect and we couldn't figure out how to fix them. |
Haha, thanks for the heads-up! I’ll keep that in mind and do my best. |
Hi, apologies for the delayed response; I was a bit occupied recently. I did some digging into the root cause of this Code 1 expands into the following:
Code 2 expands as follows:
The inconsistency in using the
In the expanded Code 1, since the async block’s body must match the In Code 2, the error is returned directly from @Darksonn Hi, could you help check if this explanation aligns with the intended behavior of the |
Unfortunately, You are out of luck. this can't be fixed with the current implementation due to the use of An attempt (#6882) was made in the past to address this using Drop order for async is tricky:
Perhaps we can use |
Version
Tokio version:
Rust version:
Reqwest version:
Platform
Description
When using the
#[tokio::main]
macro to define an asynchronousmain()
function, I've encountered inconsistent behavior regarding error handling when returningstd::io::Error
as part of aResult
that hasBox<dyn Error>
as its error type.Specifically, in the
main()
function, it seems necessary to use the?
operator before returning it, otherwise it fails to compile. However, in other async functions,Box::new(std::io::Error)
can be returned directly without the?
operator conversion.For example, in the
main()
function, the following code is required:While in other asynchronous functions, the following code works without the need for
?
:Minimal, Complete, and Verifiable Example (MCVE)
To reproduce the issue, you can use the following minimal example:
Code1:
Code2:
Notice that in Code1, within the
main()
function, the error must use the?
operator, while in Code2, the test function, the error can be returned directly without operator?
.Expected Behavior
I expect both functions to require the same treatment for returning errors. Consistent error handling across all async functions, including
main()
, would make the code more intuitive and easier to maintain.Possible Solutions
-Documentation: Clarify in the documentation whether this is expected behavior and why.
-Bug Fix: If it's a bug, fix the behavior so that all async functions, including
main
, handle return errors consistently.This issue was also discussed in the community: https://users.rust-lang.org/t/why-must-i-convert-std-error-to-box-dyn-error-in-one-function-but-not-in-another/120141/9
Thank you for looking into this!
The text was updated successfully, but these errors were encountered: