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

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

Open
jonas-schievink opened this issue Jan 24, 2020 · 1 comment
Open

Comments

@jonas-schievink
Copy link
Member

@jonas-schievink jonas-schievink commented Jan 24, 2020

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")
}

@jonas-schievink
Copy link
Member Author

@jonas-schievink jonas-schievink commented Jan 24, 2020

Similarly this code also doesn't seem to be run, even though it should:

// Watch out for various cases of cyclic types and try to explain.
if ty.is_closure() || ty.is_generator() {
db.note(
"closures cannot capture themselves or take themselves as argument;\n\
this error may be the result of a recent compiler bug-fix,\n\
see https://github.com/rust-lang/rust/issues/46062 for more details",
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.