]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_feature/src/lib.rs
Rollup merge of #99712 - davidtwco:translation-migrate-passes-2, r=compiler-errors
[rust.git] / compiler / rustc_feature / src / 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 #![feature(once_cell)]
15
16 mod accepted;
17 mod active;
18 mod builtin_attrs;
19 mod removed;
20
21 #[cfg(test)]
22 mod tests;
23
24 use rustc_span::{edition::Edition, symbol::Symbol, Span};
25 use std::fmt;
26 use std::num::NonZeroU32;
27
28 #[derive(Clone, Copy)]
29 pub enum State {
30     Accepted,
31     Active { set: fn(&mut Features, Span) },
32     Removed { reason: Option<&'static str> },
33     Stabilized { reason: Option<&'static str> },
34 }
35
36 impl fmt::Debug for State {
37     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
38         match self {
39             State::Accepted { .. } => write!(f, "accepted"),
40             State::Active { .. } => write!(f, "active"),
41             State::Removed { .. } => write!(f, "removed"),
42             State::Stabilized { .. } => write!(f, "stabilized"),
43         }
44     }
45 }
46
47 #[derive(Debug, Clone)]
48 pub struct Feature {
49     pub state: State,
50     pub name: Symbol,
51     pub since: &'static str,
52     issue: Option<NonZeroU32>,
53     pub edition: Option<Edition>,
54 }
55
56 #[derive(Copy, Clone, Debug)]
57 pub enum Stability {
58     Unstable,
59     // First argument is tracking issue link; second argument is an optional
60     // help message, which defaults to "remove this attribute".
61     Deprecated(&'static str, Option<&'static str>),
62 }
63
64 #[derive(Clone, Copy, Debug, Hash)]
65 pub enum UnstableFeatures {
66     /// Hard errors for unstable features are active, as on beta/stable channels.
67     Disallow,
68     /// Allow features to be activated, as on nightly.
69     Allow,
70     /// Errors are bypassed for bootstrapping. This is required any time
71     /// during the build that feature-related lints are set to warn or above
72     /// because the build turns on warnings-as-errors and uses lots of unstable
73     /// features. As a result, this is always required for building Rust itself.
74     Cheat,
75 }
76
77 impl UnstableFeatures {
78     /// This takes into account `RUSTC_BOOTSTRAP`.
79     ///
80     /// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly features.
81     /// Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
82     pub fn from_environment(krate: Option<&str>) -> Self {
83         // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
84         let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
85         // Returns whether `krate` should be counted as unstable
86         let is_unstable_crate = |var: &str| {
87             krate.map_or(false, |name| var.split(',').any(|new_krate| new_krate == name))
88         };
89         // `true` if we should enable unstable features for bootstrapping.
90         let bootstrap = std::env::var("RUSTC_BOOTSTRAP")
91             .map_or(false, |var| var == "1" || is_unstable_crate(&var));
92         match (disable_unstable_features, bootstrap) {
93             (_, true) => UnstableFeatures::Cheat,
94             (true, _) => UnstableFeatures::Disallow,
95             (false, _) => UnstableFeatures::Allow,
96         }
97     }
98
99     pub fn is_nightly_build(&self) -> bool {
100         match *self {
101             UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
102             UnstableFeatures::Disallow => false,
103         }
104     }
105 }
106
107 fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
108     if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) {
109         // FIXME (#28244): enforce that active features have issue numbers
110         // assert!(info.issue.is_some())
111         info.issue
112     } else {
113         // search in Accepted, Removed, or Stable Removed features
114         let found = ACCEPTED_FEATURES
115             .iter()
116             .chain(REMOVED_FEATURES)
117             .chain(STABLE_REMOVED_FEATURES)
118             .find(|t| t.name == feature);
119         match found {
120             Some(found) => found.issue,
121             None => panic!("feature `{}` is not declared anywhere", feature),
122         }
123     }
124 }
125
126 const fn to_nonzero(n: Option<u32>) -> Option<NonZeroU32> {
127     // Can be replaced with `n.and_then(NonZeroU32::new)` if that is ever usable
128     // in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
129     match n {
130         None => None,
131         Some(n) => NonZeroU32::new(n),
132     }
133 }
134
135 pub enum GateIssue {
136     Language,
137     Library(Option<NonZeroU32>),
138 }
139
140 pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
141     match issue {
142         GateIssue::Language => find_lang_feature_issue(feature),
143         GateIssue::Library(lib) => lib,
144     }
145 }
146
147 pub use accepted::ACCEPTED_FEATURES;
148 pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
149 pub use builtin_attrs::AttributeDuplicates;
150 pub use builtin_attrs::{
151     deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local,
152     AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute, GatedCfg,
153     BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
154 };
155 pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};