Repository navigation
Open
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
This seems related to #42424, except I don't see an obvious workaround like in #42424.
#![feature(async_await)]
use std::io::Error;
fn make_unit() -> Result<(), Error> {
Ok(())
}
fn main() {
let fut = async {
make_unit()?;
Ok(())
};
}Fails with the error
error[E0282]: type annotations needed for `impl std::future::Future`
--> src/main.rs:11:9
|
10 | let fut = async {
| --- consider giving `fut` the explicit type `impl std::future::Future`, with the type parameters specified
11 | make_unit()?;
| ^^^^^^^^^^^^ cannot infer type
First of all, it seems like this shouldn't be an error--it should be able to deduce that the return type is Result<(), io::Error>, but also the diagnostic does not work. If you try to add impl std::future::Future<Output=Result<(), Error>> as a type annotation, it also fails to compile because impl Trait is only allowed as return types and argument types.
Reacted by evandrocoan, Hugues Morisset, CxdInitial, Yagiz Degirmenci, stanly-johnson, Elichai Turkel and Michal Srb
Metadata
Metadata
Assignees
Labels
A-async-awaitArea: Async & AwaitArea: Async & AwaitA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceArea: Type inferenceA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`AsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.D-papercutDiagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that needs small tweaks.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
estebank commentedon Aug 12, 2019
Opened #63504 for the incorrect suggestion. For the main issue,
?interacts in non-obvious ways with type inference because it does implicit conversion. In this case, becausefutdoesn't explicitly constraint what the expected type is,rustcdoesn't know whatResult<(), std::io:Error>should be converted to.There are other similar tickets filed about these kind of type inference issues: #49391, #38508, #46333, #46680, #48089, #61152, #58517 and #63082.
(larger work around
?tracked in #31436)cramertj commentedon Aug 13, 2019
The workaround is this:
Rollup merge of rust-lang#63507 - estebank:type-inference-error, r=Ce…
nikomatsakis commentedon Aug 28, 2019
Discussing as part of a "triage effort". A few notes:
Errorvalue can beInto'd into.We lack a syntax. So maybe we can give a better diagnostic that at least hints at the solution. Or maybe an extended error code description.
estebank commentedon Aug 28, 2019
For this case, we probably could suggest a temporary local binding with an explicit type:
As for the syntax, we could borrow the closure syntax and allow something like
async -> Result<(), Error> {}orasync::<Result<(), Error> {}, but those are uuuugly.CC #62570, as it is not the same error, but it is similar enough code that these two errors will likely happen in short temporal distance of each other.
cramertj commentedon Aug 28, 2019
@estebank did you mean to include the
?after thexin your example above?2 remaining items
estebank commentedon Aug 29, 2019
You're absolutely right. Need more coffee.
programmerjake commentedon Aug 29, 2019
doesn't
?still useFromrather thanInto, or did I miss some happy RFC?Auto merge of #65951 - estebank:type-inference-error, r=<try>
Auto merge of #65951 - estebank:type-inference-error, r=nikomatsakis
Dylan-DPC commentedon Mar 24, 2023
Current output:
estebank commentedon Mar 25, 2023
The only thing left is for inference to detect that
Ecould have beenErrorand suggest that.KalyanKadiyala commentedon May 6, 2025
Not a question, but a comment with something that I tried to resolve annoyance at call-site. The
?-operator is used to extract value from a Result. Wanted to check, if there's been any progress on the topic (less async itself). The coherence rules prevent adding implicit coercion support for concrete types in my case. This can be the case with any other remotely defined Traits.Briefly,
Though a deviation from the standard approach of using the
?operator, I've added an implementation support that wraps ? operator usage with expected concrete types on the Result type using a separate extract method. Here's the impl:And here's the call-site, where the assigned value from the closure gets resolved (by inference) to expected Result's error value type:
Like to share this and seek any comments/suggestions. I'm using Rust 1.86 stable build.