]>
Commit | Line | Data |
---|---|---|
60c5eb7d XL |
1 | //! # Feature gates |
2 | //! | |
3 | //! This crate declares the set of past and present unstable features in the compiler. | |
cdc7bbd5 | 4 | //! Feature gate checking itself is done in `rustc_ast_passes/src/feature_gate.rs` |
dfeec247 | 5 | //! at the moment. |
60c5eb7d XL |
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 | ||
1b1a35ee | 14 | #![feature(once_cell)] |
f2b60f7d FG |
15 | #![deny(rustc::untranslatable_diagnostic)] |
16 | #![deny(rustc::diagnostic_outside_of_impl)] | |
1b1a35ee | 17 | |
60c5eb7d | 18 | mod accepted; |
60c5eb7d XL |
19 | mod active; |
20 | mod builtin_attrs; | |
dfeec247 | 21 | mod removed; |
60c5eb7d | 22 | |
fc512014 XL |
23 | #[cfg(test)] |
24 | mod tests; | |
25 | ||
dfeec247 | 26 | use rustc_span::{edition::Edition, symbol::Symbol, Span}; |
60c5eb7d XL |
27 | use std::fmt; |
28 | use std::num::NonZeroU32; | |
60c5eb7d XL |
29 | |
30 | #[derive(Clone, Copy)] | |
31 | pub enum State { | |
32 | Accepted, | |
33 | Active { set: fn(&mut Features, Span) }, | |
34 | Removed { reason: Option<&'static str> }, | |
35 | Stabilized { reason: Option<&'static str> }, | |
36 | } | |
37 | ||
38 | impl fmt::Debug for State { | |
39 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
40 | match self { | |
41 | State::Accepted { .. } => write!(f, "accepted"), | |
42 | State::Active { .. } => write!(f, "active"), | |
43 | State::Removed { .. } => write!(f, "removed"), | |
44 | State::Stabilized { .. } => write!(f, "stabilized"), | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | #[derive(Debug, Clone)] | |
50 | pub struct Feature { | |
51 | pub state: State, | |
52 | pub name: Symbol, | |
53 | pub since: &'static str, | |
1b1a35ee | 54 | issue: Option<NonZeroU32>, |
60c5eb7d | 55 | pub edition: Option<Edition>, |
60c5eb7d XL |
56 | } |
57 | ||
60c5eb7d XL |
58 | #[derive(Copy, Clone, Debug)] |
59 | pub enum Stability { | |
60 | Unstable, | |
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>), | |
64 | } | |
65 | ||
fc512014 | 66 | #[derive(Clone, Copy, Debug, Hash)] |
60c5eb7d XL |
67 | pub enum UnstableFeatures { |
68 | /// Hard errors for unstable features are active, as on beta/stable channels. | |
69 | Disallow, | |
70 | /// Allow features to be activated, as on nightly. | |
71 | Allow, | |
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. | |
dfeec247 | 76 | Cheat, |
60c5eb7d XL |
77 | } |
78 | ||
79 | impl UnstableFeatures { | |
fc512014 XL |
80 | /// This takes into account `RUSTC_BOOTSTRAP`. |
81 | /// | |
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 { | |
60c5eb7d XL |
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(); | |
fc512014 XL |
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)) | |
90 | }; | |
60c5eb7d | 91 | // `true` if we should enable unstable features for bootstrapping. |
fc512014 XL |
92 | let bootstrap = std::env::var("RUSTC_BOOTSTRAP") |
93 | .map_or(false, |var| var == "1" || is_unstable_crate(&var)); | |
60c5eb7d XL |
94 | match (disable_unstable_features, bootstrap) { |
95 | (_, true) => UnstableFeatures::Cheat, | |
96 | (true, _) => UnstableFeatures::Disallow, | |
dfeec247 | 97 | (false, _) => UnstableFeatures::Allow, |
60c5eb7d XL |
98 | } |
99 | } | |
100 | ||
101 | pub fn is_nightly_build(&self) -> bool { | |
102 | match *self { | |
103 | UnstableFeatures::Allow | UnstableFeatures::Cheat => true, | |
104 | UnstableFeatures::Disallow => false, | |
105 | } | |
106 | } | |
107 | } | |
108 | ||
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 | |
1b1a35ee XL |
112 | // assert!(info.issue.is_some()) |
113 | info.issue | |
60c5eb7d XL |
114 | } else { |
115 | // search in Accepted, Removed, or Stable Removed features | |
116 | let found = ACCEPTED_FEATURES | |
117 | .iter() | |
118 | .chain(REMOVED_FEATURES) | |
119 | .chain(STABLE_REMOVED_FEATURES) | |
120 | .find(|t| t.name == feature); | |
121 | match found { | |
1b1a35ee | 122 | Some(found) => found.issue, |
f25598a0 | 123 | None => panic!("feature `{feature}` is not declared anywhere"), |
60c5eb7d XL |
124 | } |
125 | } | |
126 | } | |
127 | ||
1b1a35ee XL |
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. | |
131 | match n { | |
132 | None => None, | |
133 | Some(n) => NonZeroU32::new(n), | |
134 | } | |
135 | } | |
136 | ||
60c5eb7d XL |
137 | pub enum GateIssue { |
138 | Language, | |
dfeec247 | 139 | Library(Option<NonZeroU32>), |
60c5eb7d XL |
140 | } |
141 | ||
142 | pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> { | |
143 | match issue { | |
144 | GateIssue::Language => find_lang_feature_issue(feature), | |
145 | GateIssue::Library(lib) => lib, | |
146 | } | |
147 | } | |
148 | ||
149 | pub use accepted::ACCEPTED_FEATURES; | |
136023e0 | 150 | pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES}; |
3c0e092e | 151 | pub use builtin_attrs::AttributeDuplicates; |
60c5eb7d | 152 | pub use builtin_attrs::{ |
04454e1e | 153 | deprecated_attributes, find_gated_cfg, is_builtin_attr_name, is_builtin_only_local, |
f2b60f7d FG |
154 | is_valid_for_get_attr, AttributeGate, AttributeTemplate, AttributeType, BuiltinAttribute, |
155 | GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP, | |
60c5eb7d | 156 | }; |
dfeec247 | 157 | pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES}; |