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

Const closure weirdness #55272

Open
DutchGhost opened this issue Oct 22, 2018 · 2 comments
Open

Const closure weirdness #55272

DutchGhost opened this issue Oct 22, 2018 · 2 comments
Labels
A-closures Area: closures (`|args| { .. }`) A-const-eval Area: constant evaluation (mir interpretation) A-lifetimes Area: lifetime related

Comments

@DutchGhost
Copy link
Contributor

DutchGhost commented Oct 22, 2018

The following compiles just fine:

#![feature(impl_trait_in_bindings)]
fn main() {
    let func: impl Fn(&str) -> usize = |s| s.parse().unwrap();

}

However this does not:

#![feature(impl_trait_in_bindings)]

fn main() {
    const func: impl Fn(&str) -> usize = |s| s.parse().unwrap();
}

It gives the following error message:

error: non-defining existential type use in defining scope
 --> src/main.rs:4:42
  |
4 |     const func: impl Fn(&str) -> usize = |s| s.parse().unwrap();
  |                                          ^^^^^^^^^^^^^^^^^^^^^^ lifetime `` is part of concrete type but not used in parameter list of existential type

error: aborting due to previous error

error: Could not compile `playground`.

Why is this an error? The closure does not capture anything, so it's lifetime is 'static, and const requires 'static.

@ExpHP
Copy link
Contributor

ExpHP commented Oct 22, 2018

Note also that the following works:

#![feature(impl_trait_in_bindings)]
fn main() {
    const func: impl Fn(String) -> usize = |s| s.parse().unwrap();
}

so it has something to do with the &str type parameter. But the trick that often fixes problems like this in other places in rust does not help here:

#![feature(impl_trait_in_bindings)]
fn main() {
    // explicitly annotate the closure arg as &_.
    // same error as OP
    const func: impl Fn(&str) -> usize = |s: &_| s.parse().unwrap();

    // not even this helps
    const func: impl for<'a> Fn(&'a str) -> usize = |s: &_| s.parse().unwrap();
}

@estebank estebank added A-lifetimes Area: lifetime related A-closures Area: closures (`|args| { .. }`) A-const-eval Area: constant evaluation (mir interpretation) labels Oct 25, 2018
@ilyvion
Copy link

ilyvion commented Oct 1, 2022

Seems this feature doesn't even exist anymore.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-closures Area: closures (`|args| { .. }`) A-const-eval Area: constant evaluation (mir interpretation) A-lifetimes Area: lifetime related
Projects
None yet
Development

No branches or pull requests

4 participants