2019-11-29 23:43:32 -06:00
|
|
|
//! # Feature gates
|
2013-10-02 20:10:16 -05:00
|
|
|
//!
|
2019-11-29 23:43:32 -06: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-12 09:51:15 -06:00
|
|
|
//! at the moment.
|
2013-10-02 20:10:16 -05:00
|
|
|
//!
|
|
|
|
//! Features are enabled in programs via the crate-level attributes of
|
2014-06-22 12:29:42 -05:00
|
|
|
//! `#![feature(...)]` with a comma-separated list of features.
|
2015-01-14 21:27:45 -06:00
|
|
|
//!
|
2019-11-29 23:43:32 -06: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 16:31:06 -06:00
|
|
|
|
2020-09-01 15:42:21 -05:00
|
|
|
#![feature(once_cell)]
|
|
|
|
|
2019-08-20 11:40:53 -05:00
|
|
|
mod accepted;
|
2019-08-20 11:50:33 -05:00
|
|
|
mod active;
|
2019-11-29 19:34:18 -06:00
|
|
|
mod builtin_attrs;
|
2019-12-24 16:44:51 -06:00
|
|
|
mod removed;
|
2019-08-22 16:48:08 -05:00
|
|
|
|
2020-12-12 10:18:44 -06:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests;
|
|
|
|
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::{edition::Edition, symbol::Symbol, Span};
|
2019-11-11 11:33:30 -06:00
|
|
|
use std::fmt;
|
|
|
|
use std::num::NonZeroU32;
|
2019-08-24 10:50:21 -05:00
|
|
|
|
|
|
|
#[derive(Clone, Copy)]
|
|
|
|
pub enum State {
|
|
|
|
Accepted,
|
|
|
|
Active { set: fn(&mut Features, Span) },
|
|
|
|
Removed { reason: Option<&'static str> },
|
|
|
|
Stabilized { reason: Option<&'static str> },
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for State {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
match self {
|
|
|
|
State::Accepted { .. } => write!(f, "accepted"),
|
|
|
|
State::Active { .. } => write!(f, "active"),
|
|
|
|
State::Removed { .. } => write!(f, "removed"),
|
|
|
|
State::Stabilized { .. } => write!(f, "stabilized"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Feature {
|
2019-11-29 17:23:38 -06:00
|
|
|
pub state: State,
|
|
|
|
pub name: Symbol,
|
|
|
|
pub since: &'static str,
|
2020-09-17 14:11:22 -05:00
|
|
|
issue: Option<NonZeroU32>,
|
2019-11-29 17:23:38 -06:00
|
|
|
pub edition: Option<Edition>,
|
2019-08-24 10:50:21 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 17:39:51 -06: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 13:27:52 -05:00
|
|
|
#[derive(Clone, Copy, Debug, Hash)]
|
2019-11-29 19:50:47 -06:00
|
|
|
pub enum UnstableFeatures {
|
|
|
|
/// Hard errors for unstable features are active, as on beta/stable channels.
|
|
|
|
Disallow,
|
|
|
|
/// Allow features to be activated, as on nightly.
|
|
|
|
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 16:44:51 -06:00
|
|
|
Cheat,
|
2019-11-29 19:50:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl UnstableFeatures {
|
2020-10-10 13:27:52 -05:00
|
|
|
/// This takes into account `RUSTC_BOOTSTRAP`.
|
|
|
|
///
|
|
|
|
/// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly features.
|
|
|
|
/// Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
|
|
|
|
pub fn from_environment(krate: Option<&str>) -> Self {
|
2019-11-29 19:50:47 -06:00
|
|
|
// `true` if this is a feature-staged build, i.e., on the beta or stable channel.
|
|
|
|
let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
|
2020-10-10 13:27:52 -05:00
|
|
|
// Returns whether `krate` should be counted as unstable
|
|
|
|
let is_unstable_crate = |var: &str| {
|
|
|
|
krate.map_or(false, |name| var.split(',').any(|new_krate| new_krate == name))
|
|
|
|
};
|
2019-11-29 19:50:47 -06:00
|
|
|
// `true` if we should enable unstable features for bootstrapping.
|
2020-10-10 13:27:52 -05:00
|
|
|
let bootstrap = std::env::var("RUSTC_BOOTSTRAP")
|
|
|
|
.map_or(false, |var| var == "1" || is_unstable_crate(&var));
|
2019-11-29 19:50:47 -06:00
|
|
|
match (disable_unstable_features, bootstrap) {
|
|
|
|
(_, true) => UnstableFeatures::Cheat,
|
|
|
|
(true, _) => UnstableFeatures::Disallow,
|
2019-12-24 16:44:51 -06:00
|
|
|
(false, _) => UnstableFeatures::Allow,
|
2019-11-29 19:50:47 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_nightly_build(&self) -> bool {
|
|
|
|
match *self {
|
|
|
|
UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
|
|
|
|
UnstableFeatures::Disallow => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:40:28 -06:00
|
|
|
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
|
|
|
|
if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) {
|
|
|
|
// FIXME (#28244): enforce that active features have issue numbers
|
2020-09-17 14:11:22 -05:00
|
|
|
// assert!(info.issue.is_some())
|
|
|
|
info.issue
|
2019-11-30 00:40:28 -06:00
|
|
|
} else {
|
|
|
|
// search in Accepted, Removed, or Stable Removed features
|
|
|
|
let found = ACCEPTED_FEATURES
|
|
|
|
.iter()
|
|
|
|
.chain(REMOVED_FEATURES)
|
|
|
|
.chain(STABLE_REMOVED_FEATURES)
|
|
|
|
.find(|t| t.name == feature);
|
|
|
|
match found {
|
2020-09-17 14:11:22 -05:00
|
|
|
Some(found) => found.issue,
|
2019-11-30 00:40:28 -06:00
|
|
|
None => panic!("feature `{}` is not declared anywhere", feature),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:11:22 -05:00
|
|
|
const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
|
2020-10-01 10:32:23 -05:00
|
|
|
// Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable
|
|
|
|
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
|
2020-09-17 14:11:22 -05:00
|
|
|
match n {
|
|
|
|
None => None,
|
|
|
|
Some(n) => NonZeroU32::new(n),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-30 00:40:28 -06:00
|
|
|
pub enum GateIssue {
|
|
|
|
Language,
|
2019-12-24 16:44:51 -06:00
|
|
|
Library(Option<NonZeroU32>),
|
2019-11-30 00:40:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
|
|
|
|
match issue {
|
|
|
|
GateIssue::Language => find_lang_feature_issue(feature),
|
|
|
|
GateIssue::Library(lib) => lib,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-29 17:23:38 -06:00
|
|
|
pub use accepted::ACCEPTED_FEATURES;
|
2021-06-28 13:39:20 -05:00
|
|
|
pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
|
2019-11-29 19:34:18 -06:00
|
|
|
pub use builtin_attrs::{
|
2019-12-24 16:44:51 -06:00
|
|
|
deprecated_attributes, find_gated_cfg, is_builtin_attr_name, AttributeGate, AttributeTemplate,
|
|
|
|
AttributeType, BuiltinAttribute, GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
|
2019-11-29 19:34:18 -06:00
|
|
|
};
|
2019-12-24 16:44:51 -06:00
|
|
|
pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};
|