Skip to content

Closure capturing itself results in unclear "mismatched types" error #68520

@jonas-schievink

Description

@jonas-schievink

Code:

fn main() {
    let mut r = None;
    
    let c = move || {
        let _r = r;
    };
    
    r = Some(c);
}

Results in this error:

error[E0308]: mismatched types
 --> src/main.rs:4:13
  |
4 |       let c = move || {
  |  _____________^
5 | |         let _r = r;
6 | |     };
  | |_____^ cyclic type of infinite size

This seems a bit unclear to me. What types don't match here?

There is special code in the compiler that is supposed to give a better error message, but for some reason it's not happening in this case:

_ => match terr {
TypeError::CyclicTy(ty) if ty.is_closure() || ty.is_generator() => {
Error0644("closure/generator type that references itself")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-closuresArea: Closures (`|…| { … }`)A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions