2019-11-30 06:43:32 +01:00
|
|
|
//! # Feature gates
|
2013-10-02 18:10:16 -07:00
|
|
|
//!
|
2019-11-30 06:43:32 +01:00
|
|
|
//! This crate declares the set of past and present unstable features in the compiler.
|
2021-04-07 14:47:01 -05:00
|
|
|
//! Feature gate checking itself is done in `rustc_ast_passes/src/feature_gate.rs`
|
2020-01-13 00:51:15 +09:00
|
|
|
//! at the moment.
|
2013-10-02 18:10:16 -07:00
|
|
|
//!
|
|
|
|
//! Features are enabled in programs via the crate-level attributes of
|
2014-06-22 10:29:42 -07:00
|
|
|
//! `#![feature(...)]` with a comma-separated list of features.
|
2015-01-14 19:27:45 -08:00
|
|
|
//!
|
2019-11-30 06:43:32 +01:00
|
|
|
//! For the purpose of future feature-tracking, once a feature gate is added,
|
|
|
|
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
|
|
|
|
//! symbol to the `accepted` or `removed` modules respectively.
|
2015-02-03 23:31:06 +01:00
|
|
|
|
2023-11-13 07:39:17 -05:00
|
|
|
#![allow(internal_features)]
|
2024-01-29 23:59:09 +01:00
|
|
|
#![feature(generic_nonzero)]
|
2023-11-13 07:39:17 -05:00
|
|
|
#![feature(rustdoc_internals)]
|
|
|
|
#![doc(rust_logo)]
|
2022-12-12 00:42:45 -05:00
|
|
|
#![feature(lazy_cell)]
|
2020-09-01 21:42:21 +01:00
|
|
|
|
2019-08-20 18:40:53 +02:00
|
|
|
mod accepted;
|
2019-11-30 02:34:18 +01:00
|
|
|
mod builtin_attrs;
|
2019-12-24 17:44:51 -05:00
|
|
|
mod removed;
|
2023-10-05 19:43:35 +11:00
|
|
|
mod unstable;
|
2019-08-22 23:48:08 +02:00
|
|
|
|
2020-12-12 19:18:44 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2023-12-10 09:34:13 -08:00
|
|
|
use rustc_span::symbol::Symbol;
|
2024-01-29 23:59:09 +01:00
|
|
|
use std::num::NonZero;
|
2019-08-24 17:50:21 +02:00
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Feature {
|
2019-11-30 00:23:38 +01:00
|
|
|
pub name: Symbol,
|
|
|
|
pub since: &'static str,
|
2024-01-29 23:59:09 +01:00
|
|
|
issue: Option<NonZero<u32>>,
|
2019-08-24 17:50:21 +02:00
|
|
|
}
|
|
|
|
|
2019-11-30 00:39:51 +01:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum Stability {
|
|
|
|
Unstable,
|
|
|
|
// First argument is tracking issue link; second argument is an optional
|
|
|
|
// help message, which defaults to "remove this attribute".
|
|
|
|
Deprecated(&'static str, Option<&'static str>),
|
|
|
|
}
|
|
|
|
|
2020-10-10 14:27:52 -04:00
|
|
|
#[derive(Clone, Copy, Debug, Hash)]
|
2019-11-30 02:50:47 +01:00
|
|
|
pub enum UnstableFeatures {
|
2023-10-05 19:43:35 +11:00
|
|
|
/// Disallow use of unstable features, as on beta/stable channels.
|
2019-11-30 02:50:47 +01:00
|
|
|
Disallow,
|
2023-10-05 19:43:35 +11:00
|
|
|
/// Allow use of unstable features, as on nightly.
|
2019-11-30 02:50:47 +01:00
|
|
|
Allow,
|
|
|
|
/// Errors are bypassed for bootstrapping. This is required any time
|
|
|
|
/// during the build that feature-related lints are set to warn or above
|
|
|
|
/// because the build turns on warnings-as-errors and uses lots of unstable
|
|
|
|
/// features. As a result, this is always required for building Rust itself.
|
2019-12-24 17:44:51 -05:00
|
|
|
Cheat,
|
2019-11-30 02:50:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnstableFeatures {
|
2020-10-10 14:27:52 -04:00
|
|
|
/// This takes into account `RUSTC_BOOTSTRAP`.
|
|
|
|
///
|
2023-10-04 14:54:12 +11:00
|
|
|
/// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly
|
|
|
|
/// features. Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
|
2020-10-10 14:27:52 -04:00
|
|
|
pub fn from_environment(krate: Option<&str>) -> Self {
|
2019-11-30 02:50:47 +01:00
|
|
|
// `true` if this is a feature-staged build, i.e., on the beta or stable channel.
|
2023-01-12 05:36:04 -05:00
|
|
|
let disable_unstable_features =
|
2023-05-24 14:33:43 +00:00
|
|
|
option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
|
2020-10-10 14:27:52 -04:00
|
|
|
// Returns whether `krate` should be counted as unstable
|
2023-05-24 14:19:22 +00:00
|
|
|
let is_unstable_crate =
|
|
|
|
|var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
|
2019-11-30 02:50:47 +01:00
|
|
|
// `true` if we should enable unstable features for bootstrapping.
|
2023-05-24 14:19:22 +00:00
|
|
|
let bootstrap =
|
|
|
|
std::env::var("RUSTC_BOOTSTRAP").is_ok_and(|var| var == "1" || is_unstable_crate(&var));
|
2019-11-30 02:50:47 +01:00
|
|
|
match (disable_unstable_features, bootstrap) {
|
|
|
|
(_, true) => UnstableFeatures::Cheat,
|
|
|
|
(true, _) => UnstableFeatures::Disallow,
|
2019-12-24 17:44:51 -05:00
|
|
|
(false, _) => UnstableFeatures::Allow,
|
2019-11-30 02:50:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_nightly_build(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
|
|
|
|
UnstableFeatures::Disallow => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-29 23:59:09 +01:00
|
|
|
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZero<u32>> {
|
2023-10-04 14:54:12 +11:00
|
|
|
// Search in all the feature lists.
|
2023-10-05 19:43:35 +11:00
|
|
|
if let Some(f) = UNSTABLE_FEATURES.iter().find(|f| f.feature.name == feature) {
|
2023-10-05 18:59:01 +11:00
|
|
|
return f.feature.issue;
|
|
|
|
}
|
|
|
|
if let Some(f) = ACCEPTED_FEATURES.iter().find(|f| f.name == feature) {
|
|
|
|
return f.issue;
|
|
|
|
}
|
|
|
|
if let Some(f) = REMOVED_FEATURES.iter().find(|f| f.feature.name == feature) {
|
|
|
|
return f.feature.issue;
|
2019-11-30 07:40:28 +01:00
|
|
|
}
|
2023-10-05 18:59:01 +11:00
|
|
|
panic!("feature `{feature}` is not declared anywhere");
|
2019-11-30 07:40:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-29 23:59:09 +01:00
|
|
|
const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
|
|
|
|
// Can be replaced with `n.and_then(NonZero::new)` if that is ever usable
|
2020-10-01 17:32:23 +02:00
|
|
|
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
|
2020-09-17 21:11:22 +02:00
|
|
|
match n {
|
|
|
|
None => None,
|
2024-01-29 23:59:09 +01:00
|
|
|
Some(n) => NonZero::<u32>::new(n),
|
2020-09-17 21:11:22 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 07:40:28 +01:00
|
|
|
pub enum GateIssue {
|
|
|
|
Language,
|
2024-01-29 23:59:09 +01:00
|
|
|
Library(Option<NonZero<u32>>),
|
2019-11-30 07:40:28 +01:00
|
|
|
}
|
|
|
|
|
2024-01-29 23:59:09 +01:00
|
|
|
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u32>> {
|
2019-11-30 07:40:28 +01:00
|
|
|
match issue {
|
|
|
|
GateIssue::Language => find_lang_feature_issue(feature),
|
|
|
|
GateIssue::Library(lib) => lib,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:23:38 +01:00
|
|
|
pub use accepted::ACCEPTED_FEATURES;
|
2021-09-05 16:30:37 -07:00
|
|
|
pub use builtin_attrs::AttributeDuplicates;
|
2019-11-30 02:34:18 +01:00
|
|
|
pub use builtin_attrs::{
|
2022-04-01 15:04:47 +02:00
|
|
|
deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local,
|
2022-09-06 14:16:54 +08:00
|
|
|
is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute,
|
|
|
|
GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
|
2019-11-30 02:34:18 +01:00
|
|
|
};
|
2023-10-05 11:38:43 +11:00
|
|
|
pub use removed::REMOVED_FEATURES;
|
2023-10-05 19:43:35 +11:00
|
|
|
pub use unstable::{Features, INCOMPATIBLE_FEATURES, UNSTABLE_FEATURES};
|