(macros): improve error message for return type mismatch in #[tokio::main]#7856
Merged
taiki-e merged 15 commits intotokio-rs:masterfrom Jan 24, 2026
Merged
(macros): improve error message for return type mismatch in #[tokio::main]#7856taiki-e merged 15 commits intotokio-rs:masterfrom
taiki-e merged 15 commits intotokio-rs:masterfrom
Conversation
Member
|
As said in #6306 (comment) (and #3039 (comment) and #5619 (comment) and #6438 (comment)), this needs to handle cases where the impl trait is included in Output ( (I've mentioned this point repeatedly, so we should probably add a test case like the following.) #[tokio::main]
async fn never() -> ! {
loop {}
}
#[tokio::main]
async fn impl_trait() -> impl Iterator<Item = impl core::fmt::Debug> {
[()].into_iter()
} |
Contributor
Author
taiki-e
requested changes
Jan 18, 2026
Contributor
Author
|
Added another test case where impl trait can also be placed inside other types, so previous check wasn't sufficient. #[tokio::main]
async fn impl_trait2() -> Result<(), impl core::fmt::Debug> {
Err(())
}updated code and test case to cover this |
Contributor
Author
|
any update on this @taiki-e ? |
This was referenced Feb 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #7784
When using
#[tokio::main]with conflicting return statements, the error message was misleading:The compiler would report:
This blames return () (which matches the declared -> ()) instead of return 1.
Solution
Added a check_output identity function that explicitly constrains the async block's output type to match the declared function return type:
This ensures the compiler reports type mismatches against the declared return type rather than an inferred type. The function has zero runtime overhead since it just returns its input unchanged.
After this change, the error is clearer: