cuviper / autocfg

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

Expose `AutoCfg::probe` #24

Closed jhpratt closed 4 months ago

jhpratt commented 4 years ago

Is there any particular reason why this is private?

Right now, I'm doing

cfg.emit_expression_cfg(r#"{
    #[non_exhaustive]
    struct Foo;
    Foo
}"#, "supports_non_exhaustive");

It would be nice to be able to check if #[non_exhaustive] struct Foo is supported without having to return a zero-sized type to make an expression.

jhpratt commented 4 years ago

Another use case is checking if the user is able to supply Rust feature gates (like #![feature(…)]).

cuviper commented 4 years ago

Is there any particular reason why this is private?

It's a conservative API choice, not having to worry about how that function looks. Right now it takes T: AsRef<[u8]> and returns Result, which isn't necessarily something I'd want to lock into the API, but it doesn't matter when that's private. There's also a little bit of implicit "magic" for #![no_std] that might have to be explained (and committed to) if we allow sending code directly to this context.

It would be nice to be able to check if #[non_exhaustive] struct Foo is supported without having to return a zero-sized type to make an expression.

You don't have to return Foo -- a block expression works just as well returning the unit () implicitly:

ac.emit_expression_cfg("{ #[non_exhaustive] struct Foo; }", "supports_non_exhaustive");

Rustc will warn of dead code, that Foo is never constructed, but all output is captured from build scripts anyway.

Another use case is checking if the user is able to supply Rust feature gates (like #![feature(…)]).

I think this might deserve a dedicated probe_feature, but nobody has asked for it yet. :slightly_smiling_face:

RalfJung commented 2 years ago

Something like what anyhow does would probably also be best exposed via custom probes.

autocfg gets a lot of details right regarding how to invoke rustc (propagating rustc flags and --target), but its fairly restricted API means there are many re-implementations of that logic throughout the ecosystem -- and most of them are buggy.

RalfJung commented 2 years ago

Another issue that could have been prevented if other crates could use autocfg for their probes: https://github.com/yaahc/eyre/issues/84

RalfJung commented 1 year ago

Judging from https://github.com/rust-lang/cargo/issues/11138, doing build probes correctly looks like it is actually going to become even more subtle than it already is. It would be really useful if that functionality could be exposed so it doesn't need to be duplicated everywhere.

cuviper commented 5 months ago

Please take a look at #59!

RalfJung commented 5 months ago

Thanks a lot. :-)