3 //! This crate declares the set of past and present unstable features in the compiler.
4 //! Feature gate checking itself is done in `rustc_ast_passes/src/feature_gate.rs`
7 //! Features are enabled in programs via the crate-level attributes of
8 //! `#![feature(...)]` with a comma-separated list of features.
10 //! For the purpose of future feature-tracking, once a feature gate is added,
11 //! even if it is stabilized or removed, *do not remove it*. Instead, move the
12 //! symbol to the `accepted` or `removed` modules respectively.
14 #![feature(once_cell)]
15 #![deny(rustc::untranslatable_diagnostic)]
16 #![deny(rustc::diagnostic_outside_of_impl)]
26 use rustc_span::{edition::Edition, symbol::Symbol, Span};
28 use std::num::NonZeroU32;
30 #[derive(Clone, Copy)]
33 Active { set: fn(&mut Features, Span) },
34 Removed { reason: Option<&'static str> },
35 Stabilized { reason: Option<&'static str> },
38 impl fmt::Debug for State {
39 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41 State::Accepted { .. } => write!(f, "accepted"),
42 State::Active { .. } => write!(f, "active"),
43 State::Removed { .. } => write!(f, "removed"),
44 State::Stabilized { .. } => write!(f, "stabilized"),
49 #[derive(Debug, Clone)]
53 pub since: &'static str,
54 issue: Option<NonZeroU32>,
55 pub edition: Option<Edition>,
58 #[derive(Copy, Clone, Debug)]
61 // First argument is tracking issue link; second argument is an optional
62 // help message, which defaults to "remove this attribute".
63 Deprecated(&'static str, Option<&'static str>),
66 #[derive(Clone, Copy, Debug, Hash)]
67 pub enum UnstableFeatures {
68 /// Hard errors for unstable features are active, as on beta/stable channels.
70 /// Allow features to be activated, as on nightly.
72 /// Errors are bypassed for bootstrapping. This is required any time
73 /// during the build that feature-related lints are set to warn or above
74 /// because the build turns on warnings-as-errors and uses lots of unstable
75 /// features. As a result, this is always required for building Rust itself.
79 impl UnstableFeatures {
80 /// This takes into account `RUSTC_BOOTSTRAP`.
82 /// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly features.
83 /// Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
84 pub fn from_environment(krate: Option<&str>) -> Self {
85 // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
86 let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
87 // Returns whether `krate` should be counted as unstable
88 let is_unstable_crate = |var: &str| {
89 krate.map_or(false, |name| var.split(',').any(|new_krate| new_krate == name))
91 // `true` if we should enable unstable features for bootstrapping.
92 let bootstrap = std::env::var("RUSTC_BOOTSTRAP")
93 .map_or(false, |var| var == "1" || is_unstable_crate(&var));
94 match (disable_unstable_features, bootstrap) {
95 (_, true) => UnstableFeatures::Cheat,
96 (true, _) => UnstableFeatures::Disallow,
97 (false, _) => UnstableFeatures::Allow,
101 pub fn is_nightly_build(&self) -> bool {
103 UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
104 UnstableFeatures::Disallow => false,
109 fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
110 if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) {
111 // FIXME (#28244): enforce that active features have issue numbers
112 // assert!(info.issue.is_some())
115 // search in Accepted, Removed, or Stable Removed features
116 let found = ACCEPTED_FEATURES
118 .chain(REMOVED_FEATURES)
119 .chain(STABLE_REMOVED_FEATURES)
120 .find(|t| t.name == feature);
122 Some(found) => found.issue,
123 None => panic!("feature `{}` is not declared anywhere", feature),
128 const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
129 // Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable
130 // in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
133 Some(n) => NonZeroU32::new(n),
139 Library(Option<NonZeroU32>),
142 pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
144 GateIssue::Language => find_lang_feature_issue(feature),
145 GateIssue::Library(lib) => lib,
149 pub use accepted::ACCEPTED_FEATURES;
150 pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
151 pub use builtin_attrs::AttributeDuplicates;
152 pub use builtin_attrs::{
153 deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local,
154 is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute,
155 GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
157 pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};