rustc_feature/
lib.rs

1//! # Feature gates
2//!
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`
5//! at the moment.
6//!
7//! Features are enabled in programs via the crate-level attributes of
8//! `#![feature(...)]` with a comma-separated list of features.
9//!
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.
13
14// tidy-alphabetical-start
15#![allow(internal_features)]
16#![doc(rust_logo)]
17#![feature(rustdoc_internals)]
18#![warn(unreachable_pub)]
19// tidy-alphabetical-end
20
21mod accepted;
22mod builtin_attrs;
23mod removed;
24mod unstable;
25
26#[cfg(test)]
27mod tests;
28
29use std::num::NonZero;
30
31use rustc_span::Symbol;
32
33#[derive(Debug, Clone)]
34pub struct Feature {
35    pub name: Symbol,
36    /// For unstable features: the version the feature was added in.
37    /// For accepted features: the version the feature got stabilized in.
38    /// For removed features we are inconsistent; sometimes this is the
39    /// version it got added, sometimes the version it got removed.
40    pub since: &'static str,
41    issue: Option<NonZero<u32>>,
42}
43
44#[derive(Copy, Clone, Debug)]
45pub enum Stability {
46    Unstable,
47    // First argument is tracking issue link; second argument is an optional
48    // help message, which defaults to "remove this attribute".
49    Deprecated(&'static str, Option<&'static str>),
50}
51
52#[derive(Clone, Copy, Debug, Hash)]
53pub enum UnstableFeatures {
54    /// Disallow use of unstable features, as on beta/stable channels.
55    Disallow,
56    /// Allow use of unstable features, as on nightly.
57    Allow,
58    /// Errors are bypassed for bootstrapping. This is required any time
59    /// during the build that feature-related lints are set to warn or above
60    /// because the build turns on warnings-as-errors and uses lots of unstable
61    /// features. As a result, this is always required for building Rust itself.
62    Cheat,
63}
64
65impl UnstableFeatures {
66    /// This takes into account `RUSTC_BOOTSTRAP`.
67    ///
68    /// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly
69    /// features. Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
70    pub fn from_environment(krate: Option<&str>) -> Self {
71        Self::from_environment_value(krate, std::env::var("RUSTC_BOOTSTRAP"))
72    }
73
74    /// Avoid unsafe `std::env::set_var()` by allowing tests to inject
75    /// `std::env::var("RUSTC_BOOTSTRAP")` with the `env_var_rustc_bootstrap`
76    /// arg.
77    fn from_environment_value(
78        krate: Option<&str>,
79        env_var_rustc_bootstrap: Result<String, std::env::VarError>,
80    ) -> Self {
81        // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
82        let disable_unstable_features =
83            option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
84        // Returns whether `krate` should be counted as unstable
85        let is_unstable_crate =
86            |var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
87
88        let bootstrap = env_var_rustc_bootstrap.ok();
89        if let Some(val) = bootstrap.as_deref() {
90            match val {
91                val if val == "1" || is_unstable_crate(val) => return UnstableFeatures::Cheat,
92                // Hypnotize ourselves so that we think we are a stable compiler and thus don't
93                // allow any unstable features.
94                "-1" => return UnstableFeatures::Disallow,
95                _ => {}
96            }
97        }
98
99        if disable_unstable_features { UnstableFeatures::Disallow } else { UnstableFeatures::Allow }
100    }
101
102    pub fn is_nightly_build(&self) -> bool {
103        match *self {
104            UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
105            UnstableFeatures::Disallow => false,
106        }
107    }
108}
109
110fn find_lang_feature_issue(feature: Symbol) -> Option<NonZero<u32>> {
111    // Search in all the feature lists.
112    if let Some(f) = UNSTABLE_LANG_FEATURES.iter().find(|f| f.name == feature) {
113        return f.issue;
114    }
115    if let Some(f) = ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature) {
116        return f.issue;
117    }
118    if let Some(f) = REMOVED_LANG_FEATURES.iter().find(|f| f.feature.name == feature) {
119        return f.feature.issue;
120    }
121    panic!("feature `{feature}` is not declared anywhere");
122}
123
124const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
125    // Can be replaced with `n.and_then(NonZero::new)` if that is ever usable
126    // in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
127    match n {
128        None => None,
129        Some(n) => NonZero::new(n),
130    }
131}
132
133pub enum GateIssue {
134    Language,
135    Library(Option<NonZero<u32>>),
136}
137
138pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u32>> {
139    match issue {
140        GateIssue::Language => find_lang_feature_issue(feature),
141        GateIssue::Library(lib) => lib,
142    }
143}
144
145pub use accepted::ACCEPTED_LANG_FEATURES;
146pub use builtin_attrs::{
147    AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate, AttributeType,
148    BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg, deprecated_attributes,
149    encode_cross_crate, find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute,
150    is_valid_for_get_attr,
151};
152pub use removed::REMOVED_LANG_FEATURES;
153pub use unstable::{
154    EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES, UNSTABLE_LANG_FEATURES,
155};