Skip to content

Incorrect type mismatch resolving when using return in closure #39502

Open
@seppo0010

Description

@seppo0010

The following code is failing to compile in stable and nightly

fn main() {
    let a = |hello| { return hello + 1; };
    let _ = a(2);
}

with the following message:

rustc 1.15.0 (10893a9a3 2017-01-19)
error[E0271]: type mismatch resolving `<i32 as std::ops::Add>::Output == ()`
 --> <anon>:2:30
  |
2 |     let a = |hello| { return hello + 1; };
  |                              ^^^^^^^^^ expected i32, found ()
  |
  = note: expected type `i32`
  = note:    found type `()`

error: aborting due to previous error

If I remove the return and the semicolon, it works as expected.

Adding the type i32 to _ or adding a type to the parameter (e.g.: 2u32) fixes the problem.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-inferenceArea: Type inferenceC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions