cuviper / autocfg

Automatic cfg for Rust compiler features
Apache License 2.0
95 stars 24 forks source link

Can I probe for const generics support? #31

Closed cole-miller closed 3 years ago

cole-miller commented 3 years ago

I'd like to probe support for (minimal) const generics by checking this code:

type A<const N: usize> = [(); N];

But this can't be done with probe_type and I'm not sure how else to do it (other than maybe probe_rustc_version which seems kind of awkward). Any workarounds?

cole-miller commented 3 years ago

This seems to work:

fn main() {
    let ac = autocfg::new();
    ac.emit_expression_cfg(
        r#"{ struct A<const N: usize>([(); N]); A([]) }"#,
        "have_const_generics",
    );
}

But if there's a nicer way I'd love to know about it! (Otherwise feel free to close this.)

cuviper commented 3 years ago

There's no direct support for item definitions, which is what you'd need to test this, but I think a block expression is reasonable. You could also do that with emit_constant_cfg, but the constraints there are really about constant evaluation.

Did you add A([]) to avoid unused warning spew? I might change it to start hiding output by default for #30.

cole-miller commented 3 years ago

Did you add A([]) to avoid unused warning spew?

No, it was just a reflex :P (I wasn't thinking about implicit () return). The unused warning doesn't bother me especially since it only shows up with -vv and this is the only thing in my build.rs currently.