Skip to content

Commit 16ef0b2

Browse files
committed
rfc, cfg-path-version: discuss has_attr as future work.
1 parent 0b12c54 commit 16ef0b2

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

text/0000-cfg-path-version.md

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -778,8 +778,8 @@ The ability to have optional cargo dependencies is out of scope for this RFC.
778778

779779
[GAT]: https://github.com/rust-lang/rust/issues/44265
780780

781-
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.
783783
For example, we might write `#[cfg(rustc_feature(generic_associated_types))]`
784784
to check whether the [GAT] feature is supported in the compiler or not.
785785

@@ -803,6 +803,35 @@ However, there are some drawbacks as well:
803803
their names. Being more principled could potentially add an undue burden
804804
on the library and compiler teams.
805805

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+
fn double(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+
806835
## `#[cfg(nightly)]`
807836

808837
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.
811840
Therefore, we have outlined what `nightly` would have meant
812841
and some upsides and drawbacks to adding it.
813842

814-
## Technical specification
843+
### Technical specification
815844

816845
To the `cfg` attribute, a `nightly` flag is added.
817846

818847
If and only if a Rust compiler permits a user to specify `#![feature(..)]`
819848
will the `nightly` flag be considered active.
820849

821-
## Drawbacks: Combining `nightly` and `accessible(..)`
850+
### Drawbacks: Combining `nightly` and `accessible(..)`
822851

823852
Consider that a popular library writes:
824853

@@ -864,7 +893,7 @@ but this would be anti-modular).
864893
This is the fundamental reason that for the time being,
865894
we have not included `nightly` in the proposal.
866895

867-
## Upsides
896+
### Upsides
868897

869898
[dbg]: https://crates.io/crates/dbg
870899

@@ -873,7 +902,7 @@ own to conditionally compile based on nightly/not as opposed to providing
873902
an `unstable` feature in `Cargo.toml`. An example of this is provided by the
874903
[dbg] crate which currently uses [version_check] to provide this automation.
875904

876-
## Alternative `#![if_possible_feature(<feature>)]`
905+
### Alternative `#![if_possible_feature(<feature>)]`
877906

878907
As an alternative to `#[cfg_attr(nightly, feature(<feature>))]`
879908
we could permit the user to write `#![if_possible_feature(<feature>)]`.
@@ -884,7 +913,7 @@ using `any`, `not`, and `all`.
884913

885914
This alternative also suffers from the problems previously noted.
886915

887-
## Naming of the attribute
916+
### Naming of the attribute
888917

889918
If this flag were to be proposed again, it would probably be proposed under
890919
a different name than `nightly`. Instead, a more apt name with respect to intent

0 commit comments

Comments
 (0)