Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When needing type annotation do not suggest using impl Trait or closure #63504

Closed
estebank opened this issue Aug 12, 2019 · 2 comments · Fixed by #63507
Closed

When needing type annotation do not suggest using impl Trait or closure #63504

estebank opened this issue Aug 12, 2019 · 2 comments · Fixed by #63507
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Aug 12, 2019

Subset of #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 #61361, when encountering impl Trait:

// When `name` corresponds to a type argument, show the path of the full type we're
// trying to infer. In the following example, `ty_msg` contains
// " in `std::result::Result<i32, E>`":
// ```
// error[E0282]: type annotations needed for `std::result::Result<i32, E>`
// --> file.rs:L:CC
// |
// L | let b = Ok(4);
// | - ^^ cannot infer type for `E` in `std::result::Result<i32, E>`
// | |
// | consider giving `b` the explicit type `std::result::Result<i32, E>`, where
// | the type parameter `E` is specified
// ```
let (ty_msg, suffix) = match &local_visitor.found_ty {
Some(ty) if &ty.to_string() != "_" && name == "_" => {
let ty = ty_to_string(ty);
(format!(" for `{}`", ty),
format!("the explicit type `{}`, with the type parameters specified", ty))
}
Some(ty) if &ty.to_string() != "_" && ty.to_string() != name => {
let ty = ty_to_string(ty);
(format!(" for `{}`", ty),
format!(
"the explicit type `{}`, where the type parameter `{}` is specified",
ty,
name,
))
}
_ => (String::new(), "a type".to_owned()),
};

Also, the case for closures (CC #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
@estebank estebank added E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. A-diagnostics Area: Messages for errors, warnings, and lints P-medium Medium priority A-inference Area: Type inference C-bug Category: This is a bug. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Aug 12, 2019
@Centril
Copy link
Contributor

Centril commented Aug 12, 2019

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.

@Centril Centril added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 12, 2019
@estebank estebank changed the title When needing type annotation do not suggest using impl Trait When needing type annotation do not suggest using impl Trait or closure Aug 12, 2019
@estebank
Copy link
Contributor Author

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.

Centril added a commit to Centril/rust that referenced this issue Aug 14, 2019
…ntril

When needing type annotations in local bindings, account for impl Trait and closures

Fix rust-lang#46680, fix rust-lang#63504, fix rust-lang#63506, fix rust-lang#40014, cc rust-lang#63502.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants