How about allowing Rust source code to query whether a language feature is implemented or not in the current compiler, and choose different implementations based on the information?
Let me give an example where this feature will be useful. Recently (at Rust 1.20) MauallyDrop is stabilized under the feature name “manually_drop”. Before that, when you don’t want to automatically drop an object, the state-of-the-art way to do is using the nodrop crate.
As a library author, one may want to use ManuallyDrop if the feature named “manually_drop” is implemented in the underlying compiler, and nodrop::NoDrop otherwise. This is for supporting an old version of Rust and using the currently-introduced feature at the same time. In order to do so, I’d like to write something like:
#[cfg(feature = "manually_drop")]
mod nodrop {
pub use std::mem::ManuallyDrop as NoDrop;
}
#[cfg(not(feature = "manually_drop"))]
extern crate nodrop;
... // use nodrop::NoDrop
An alternative could be using library-provided features declared in Cargo.toml. However, in that case, the users of a library should specify which features to use in Cargo.toml, which can be cumbersome and easy to forget. The ability to directly query the underlying compiler will be much more convenient.
Another alternative could be querying about version numbers. However, version numbers itself doesn’t contain any semantic information, and alternative Rust compilers may have different version policies or different priority on implementing features. On the other hand, querying about feature names is more semantic, and can be used for other Rust compilers.
A drawback is the feature names can clash: Both the Rust compiler and Cargo have their own namespace for features, and e.g. “manually_drop” can be interpreted as both the Rust language feature and a library feature declared in Cargo.toml. There should be a way to distinguish the two. I’d prefer “lang/manually_drop” for the language feature, and “manually_drop” for the library feature.