You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
One possible extension we might want to do in the future is to allow users to
782
-
check whether a certain `rustc` feature gate is enabled or not.
781
+
One possible extension we might want to do in the future is to allow users
782
+
to check whether a certain `rustc` feature gate is enabled or not.
783
783
For example, we might write `#[cfg(rustc_feature(generic_associated_types))]`
784
784
to check whether the [GAT] feature is supported in the compiler or not.
785
785
@@ -803,6 +803,35 @@ However, there are some drawbacks as well:
803
803
their names. Being more principled could potentially add an undue burden
804
804
on the library and compiler teams.
805
805
806
+
## `#[cfg(has_attr($attribute))]`
807
+
808
+
One possible extension would be to introduce a `has_attr(..)` feature.
809
+
`has_attr` would check if the specified attribute would be usable on the
810
+
item the `cfg` (or `cfg_attr`) directive is attached to. For instance:
811
+
812
+
```rust
813
+
#[cfg_attr(have_attr(must_use), must_use)]
814
+
fndouble(x:i32) ->i32 {
815
+
2*x
816
+
}
817
+
```
818
+
819
+
This would allow code to detect the availability of an attribute before using it,
820
+
while not failing if the attribute did not exist.
821
+
822
+
Using `has_attr` in a `cfg` block may be useful for conditionally compiling
823
+
code that only makes sense if a given attribute exists (e.g. `global_allocator`),
824
+
while using `has_attr` in a `cfg_attr` block may be useful for adding an attribute to an item if supported but still support compilers that don't
825
+
support that attribute.
826
+
827
+
As previously discussed, currently, the names of feature gates do not tend to
828
+
appear in code targeting stable versions of Rust. Allowing code to detect the
829
+
availability of specified feature gates by name would require committing to stable names for these features, and would require that those names refer to
830
+
a fixed set of functionality. This would require additional curation.
831
+
However, as attribute names already have to be standardized,
832
+
`has_attr(..)` would not suffer the same problems wherefore
833
+
it may be the better solution.
834
+
806
835
## `#[cfg(nightly)]`
807
836
808
837
In a previous iteration of this RFC, a `#[cfg(nightly)]` flag was included.
@@ -811,14 +840,14 @@ We may still add such a feature in the future if we wish.
811
840
Therefore, we have outlined what `nightly` would have meant
812
841
and some upsides and drawbacks to adding it.
813
842
814
-
## Technical specification
843
+
###Technical specification
815
844
816
845
To the `cfg` attribute, a `nightly` flag is added.
817
846
818
847
If and only if a Rust compiler permits a user to specify `#![feature(..)]`
819
848
will the `nightly` flag be considered active.
820
849
821
-
## Drawbacks: Combining `nightly` and `accessible(..)`
850
+
###Drawbacks: Combining `nightly` and `accessible(..)`
822
851
823
852
Consider that a popular library writes:
824
853
@@ -864,7 +893,7 @@ but this would be anti-modular).
864
893
This is the fundamental reason that for the time being,
865
894
we have not included `nightly` in the proposal.
866
895
867
-
## Upsides
896
+
###Upsides
868
897
869
898
[dbg]: https://crates.io/crates/dbg
870
899
@@ -873,7 +902,7 @@ own to conditionally compile based on nightly/not as opposed to providing
873
902
an `unstable` feature in `Cargo.toml`. An example of this is provided by the
874
903
[dbg] crate which currently uses [version_check] to provide this automation.
875
904
876
-
## Alternative `#![if_possible_feature(<feature>)]`
0 commit comments