Open
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.
Metadata
Metadata
Assignees
Labels
Area: Async & AwaitArea: Messages for errors, warnings, and lintsArea: Type inferenceArea: Suggestions generated by the compiler applied by `cargo fix`Async-await issues that have been triaged during a working group meeting.Diagnostics: An error or lint that needs small tweaks.Diagnostics: An error or lint that doesn't give enough information about the problem at hand.Relevant to the compiler team, which will review and decide on the PR/issue.
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
impl Trait
or closure #63504estebank 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, becausefut
doesn't explicitly constraint what the expected type is,rustc
doesn'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…
21 remaining items