-
Notifications
You must be signed in to change notification settings - Fork 23
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
Enabling and disabling features #35
Conversation
Hi @Anders429 @cuviper, I'm also looking for a way to test for features and perform additional tests with specific features enabled (ie. features for which an associated path was renamed before being stabilized). But having to remember about disabling the features again looks like a potential footgun to me. I've been playing around a bit with the idea of fixing that and adding a bit of ergonomics. You might want to check out an experimental branch inspired on this one that takes a closure to run probes and then cleans up the enabled features. This plus a couple small changes to return the status of probes when calling emit_*() fns would allow users to write things like: let mut ac = autocfg::new();
// test for reduce
// this wouldn't actually work because we can't `use trait::method`,
// but should be ok for illustration purposes
if !ac.emit_path("core::iter::Iterator::reduce") {
// test for pre-1.51.0 nightly fold_first or reduce
ac.emit_with_features(&["iterator_fold_self"], |featured_ac| {
// both the two below would require `feature(iterator_fold_self)`
// so return true if any one of them succeeds so that
// feature_iterator_fold_self is emitted and user code can do:
// #![cfg_attr(feature_iterator_fold_self, feature(iterator_fold_self)]
featured_ac.emit_path("core::iter::Iterator::reduce")
|| featured_ac.emit_path("core::iter::Iterator::fold_first")
});
}
// iter_fold_self no longer enabled here If there is any interest in going in this direction we can take it from there. |
I think that using a closure would be fine as well. I can't think of any issues with it off the top of my head. It just comes down to which is more user-friendly and more readable, I think. I personally don't think users having to remember to disable features would be a problem, although I can see where issues might arise, since forgetting to disable a feature would result in some As a nitpick, I think having a single method called That leaves me comparing these two APIs: the one in this PR, which is let mut ac = autocfg::new();
ac.enable_feature("step_trait");
ac.emit_has_trait("std::iter::Step")
ac.disable_feature("step_trait); and something like let mut ac = autocfg::new();
ac.with_features(&["step_trait], |ac| {
ac.emit_has_trait("std::iter::Step");
}); The first one looks cleaner to me, IMO, without the indentations. However, the second one doesn't leave any question about which features apply to which probes, and for that reason I kind of lean toward the second one. |
Yes, returning We could track whether we have emitted anything in the closure and if so then emit the feature flag, or just modify
Agreed, that's why I took a stab at it in the first place. To be honest, I've found in my own usage that what I want most of the time, as opposed to using directly something such as
Perhaps a helper that does this, or a change to |
Another way to use it is to simply If we did a |
Second attempt at probing with features. First attempt was #33.
This PR adds two methods to
AutoCfg
:enable_feature()
, which enables a nightly feature to be used in probes, anddisable_feature()
, which disables a nightly feature in probes. Specifically, it adds#![feature(<feature>)]
at the start of subsequent probes.This API change should be sufficient to allow users to probe whether nightly-only features will work as expected (see requests for this kind of stuff in #24 and #28). The following example will be possible with this PR:
Note that when enabling features on
stable
orbeta
channels, any subsequent probes will fail until the feature is disabled.This seems to be the most direct solution. Users who want to probe for the
rustc
channel or the presence of a nightly feature should ascertain that the nightly feature they wish to enable works correctly. See the discussion in #33 for why probing forrustc
channel and probing features directly are not included here.