]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_feature/src/lib.rs
New upstream version 1.71.1+dfsg1
[rustc.git] / compiler / rustc_feature / src / lib.rs
CommitLineData
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
353b0b11 14#![feature(lazy_cell)]
f2b60f7d
FG
15#![deny(rustc::untranslatable_diagnostic)]
16#![deny(rustc::diagnostic_outside_of_impl)]
1b1a35ee 17
60c5eb7d 18mod accepted;
60c5eb7d
XL
19mod active;
20mod builtin_attrs;
dfeec247 21mod removed;
60c5eb7d 22
fc512014
XL
23#[cfg(test)]
24mod tests;
25
dfeec247 26use rustc_span::{edition::Edition, symbol::Symbol, Span};
60c5eb7d
XL
27use std::fmt;
28use std::num::NonZeroU32;
60c5eb7d
XL
29
30#[derive(Clone, Copy)]
31pub 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
38impl 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)]
50pub 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)]
59pub 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
67pub 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
79impl 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 85 // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
9ffffee4 86 let disable_unstable_features =
49aad941 87 option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
fc512014 88 // Returns whether `krate` should be counted as unstable
49aad941
FG
89 let is_unstable_crate =
90 |var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
60c5eb7d 91 // `true` if we should enable unstable features for bootstrapping.
49aad941
FG
92 let bootstrap =
93 std::env::var("RUSTC_BOOTSTRAP").is_ok_and(|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
109fn 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,
9c376795 123 None => panic!("feature `{feature}` is not declared anywhere"),
60c5eb7d
XL
124 }
125 }
126}
127
1b1a35ee
XL
128const 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
137pub enum GateIssue {
138 Language,
dfeec247 139 Library(Option<NonZeroU32>),
60c5eb7d
XL
140}
141
142pub 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
149pub use accepted::ACCEPTED_FEATURES;
136023e0 150pub use active::{Features, ACTIVE_FEATURES, INCOMPATIBLE_FEATURES};
3c0e092e 151pub use builtin_attrs::AttributeDuplicates;
60c5eb7d 152pub 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 157pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};