Rust: When needing type annotation do not suggest using `impl Trait` or closure

Created on 12 Aug 2019  路  2Comments  路  Source: rust-lang/rust

_Subset of https://github.com/rust-lang/rust/issues/63502_

When encountering

#![feature(async_await)]

use std::io::Error;

fn make_unit() -> Result<(), Error> { 
    Ok(())
}

fn main() {
    let fut = async {
        make_unit()?;

        Ok(())
    };
}

Do not suggest "consider giving fut the explicit type impl std::future::Future, with the type parameters specified ", as introduced in https://github.com/rust-lang/rust/pull/61361, when encountering impl Trait:

https://github.com/rust-lang/rust/blob/60960a260f7b5c695fd0717311d72ce62dd4eb43/src/librustc/infer/error_reporting/need_type_info.rs#L140-L169

Also, the case for closures (CC https://github.com/rust-lang/rust/issues/46680):

error[E0282]: type annotations needed for `[closure@src/main.rs:3:13: 6:6]`
 --> src/main.rs:4:9
  |
3 |     let x = || {
  |         - consider giving `x` the explicit type `[closure@src/main.rs:3:13: 6:6]`, with the type parameters specified
4 |         Err(())?;
  |         ^^^^^^^^ cannot infer type
A-diagnostics A-inference A-suggestion-diagnostics C-bug E-easy P-medium T-compiler

Most helpful comment

then it does seem reasonable to suggest it again

Indeed, but suggestions should only be provided when they can be applied, either gated on nightly or not provided at all if depending on nightly features.

All 2 comments

Btw, when we have the ability to ascribe a type let fut: impl Trait = async { ... }; then it does seem reasonable to suggest it again, so we should open an issue for undoing the fix to this issue.

then it does seem reasonable to suggest it again

Indeed, but suggestions should only be provided when they can be applied, either gated on nightly or not provided at all if depending on nightly features.

Was this page helpful?
0 / 5 - 0 ratings