Open TomMonkeyMan opened 1 week ago
I guess the issue is that we're not properly informing the closure about the return type...
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.
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 themain()
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!