Skip to content

Code only compiles with try!, not with question mark syntax #36244

New issue

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

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

Already on GitHub? # to your account

Open
futile opened this issue Sep 3, 2016 · 6 comments
Open

Code only compiles with try!, not with question mark syntax #36244

futile opened this issue Sep 3, 2016 · 6 comments
Labels
A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@futile
Copy link
Contributor

futile commented Sep 3, 2016

The following code only compiles with try!, not when using the ?-syntax instead:

#![feature(question_mark)]

fn foo() -> Result<Vec<u32>, std::num::ParseIntError> {
    let s = &["42", "1337"];

    let parsed: Vec<u32> = try!(s.iter().map(|str| {
        let val = str.parse::<u32>()?;
        Ok(val + 1)
    }).collect());

    Ok(parsed)
}

fn main() {
    println!("{:?}", foo());
}

Replacing the single try! in function foo with its ? equivalent produces the following error:

error[E0284]: type annotations required: cannot resolve `<_ as std::ops::Carrier>::Success == _`
 --> <anon>:6:28
  |
6 |     let parsed: Vec<u32> = s.iter().map(|str| {
  |                            ^

error: aborting due to previous error

Example on playground: https://is.gd/CPUV0f (replace try! with ? to see the error)

Rustc version: rustc 1.13.0-nightly (acd3f79 2016-08-28)

Note: @KiChjang mentioned on IRC that using turbofish on the collect call, .collect::<Result<_,_>>() , makes the ? version compile again.

@Stebalien
Copy link
Contributor

This is a known issue and has to to do with how the Carrier trait currently works. There's an old discussion at #35056 which should be continued at rust-lang/rfcs#1718.

@futile
Copy link
Contributor Author

futile commented Sep 4, 2016

@Stebalien thanks! Wasn't aware of the already existing discussion.

@Mark-Simulacrum
Copy link
Member

This compiles today; closing.

@compressed
Copy link

compressed commented Mar 11, 2018

I think this issue may need to be re-opened. I ran into it today (rustc 1.26.0-nightly (9cb18a92a 2018-03-02)). The example doesn't compile anymore without the type annotation.

https://play.rust-lang.org/?gist=f12d5c5166d268479961bcbb2eccbd50&version=nightly

   Compiling playground v0.0.1 (file:///playground)
error[E0284]: type annotations required: cannot resolve `<_ as std::ops::Try>::Ok == _`
 --> src/main.rs:6:28
  |
6 |       let parsed: Vec<u32> = s.iter().map(|str| {
  |  ____________________________^
7 | |         let val = str.parse::<u32>()?;
8 | |         Ok(val + 1)
9 | |     }).collect()?;
  | |_________________^

error: aborting due to previous error

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference labels Mar 13, 2018
@estebank
Copy link
Contributor

Current output for the last comment:

error[E0284]: type annotations needed
 --> src/main.rs:9:8
  |
9 |     }).collect()?;
  |        ^^^^^^^
  |        |
  |        cannot infer type
  |        help: consider specifying the type argument in the method call: `collect::<B>`
  |
  = note: cannot resolve `<_ as std::ops::Try>::Ok == _`

It should work without annotations, but at least we lead people in the right direction :-/

@futile
Copy link
Contributor Author

futile commented May 16, 2021

I think this issue may need to be re-opened. I ran into it today (rustc 1.26.0-nightly (9cb18a92a 2018-03-02)). The example doesn't compile anymore without the type annotation.

https://play.rust-lang.org/?gist=f12d5c5166d268479961bcbb2eccbd50&version=nightly

Current output of that link:

error[E0283]: type annotations needed
 --> src/main.rs:9:8
  |
9 |     }).collect()?;
  |        ^^^^^^^ cannot infer type
  |
  = note: cannot satisfy `_: Try`
  = note: required by `into_result`
help: consider specifying the type argument in the method call
  |
9 |     }).collect::<B>()?;
  |               ^^^^^

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants