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

Expose AutoCfg::probe #24

Closed
jhpratt opened this issue Jul 4, 2020 · 7 comments · Fixed by #59
Closed

Expose AutoCfg::probe #24

jhpratt opened this issue Jul 4, 2020 · 7 comments · Fixed by #59

Comments

@jhpratt
Copy link

jhpratt commented Jul 4, 2020

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
Copy link
Author

jhpratt commented Jul 4, 2020

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

@cuviper
Copy link
Owner

cuviper commented Jul 5, 2020

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. 🙂

@RalfJung
Copy link

RalfJung commented Aug 2, 2022

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
Copy link

RalfJung commented Aug 5, 2022

Another issue that could have been prevented if other crates could use autocfg for their probes: eyre-rs/eyre#84

@RalfJung
Copy link

Judging from rust-lang/cargo#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
Copy link
Owner

cuviper commented Mar 28, 2024

Please take a look at #59!

@RalfJung
Copy link

Thanks a lot. :-)

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

Successfully merging a pull request may close this issue.

3 participants