Skip to content

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

Open
@futile

Description

@futile

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions