3 //! This crate declares the set of past and present unstable features in the compiler.
4 //! Feature gate checking itself is done in `librustc_ast_passes/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.
19 use rustc_span::{edition::Edition, symbol::Symbol, Span};
21 use std::num::NonZeroU32;
23 #[derive(Clone, Copy)]
26 Active { set: fn(&mut Features, Span) },
27 Removed { reason: Option<&'static str> },
28 Stabilized { reason: Option<&'static str> },
31 impl fmt::Debug for State {
32 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 State::Accepted { .. } => write!(f, "accepted"),
35 State::Active { .. } => write!(f, "active"),
36 State::Removed { .. } => write!(f, "removed"),
37 State::Stabilized { .. } => write!(f, "stabilized"),
42 #[derive(Debug, Clone)]
46 pub since: &'static str,
47 issue: Option<u32>, // FIXME: once #58732 is done make this an Option<NonZeroU32>
48 pub edition: Option<Edition>,
49 description: &'static str,
53 fn issue(&self) -> Option<NonZeroU32> {
54 self.issue.and_then(NonZeroU32::new)
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, 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 pub fn from_environment() -> UnstableFeatures {
81 // `true` if this is a feature-staged build, i.e., on the beta or stable channel.
82 let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
83 // `true` if we should enable unstable features for bootstrapping.
84 let bootstrap = std::env::var("RUSTC_BOOTSTRAP").is_ok();
85 match (disable_unstable_features, bootstrap) {
86 (_, true) => UnstableFeatures::Cheat,
87 (true, _) => UnstableFeatures::Disallow,
88 (false, _) => UnstableFeatures::Allow,
92 pub fn is_nightly_build(&self) -> bool {
94 UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
95 UnstableFeatures::Disallow => false,
100 fn find_lang_feature_issue(feature: Symbol) -> Option<NonZeroU32> {
101 if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.name == feature) {
102 // FIXME (#28244): enforce that active features have issue numbers
103 // assert!(info.issue().is_some())
106 // search in Accepted, Removed, or Stable Removed features
107 let found = ACCEPTED_FEATURES
109 .chain(REMOVED_FEATURES)
110 .chain(STABLE_REMOVED_FEATURES)
111 .find(|t| t.name == feature);
113 Some(found) => found.issue(),
114 None => panic!("feature `{}` is not declared anywhere", feature),
121 Library(Option<NonZeroU32>),
124 pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZeroU32> {
126 GateIssue::Language => find_lang_feature_issue(feature),
127 GateIssue::Library(lib) => lib,
131 pub use accepted::ACCEPTED_FEATURES;
132 pub use active::{Features, ACTIVE_FEATURES, INCOMPLETE_FEATURES};
133 pub use builtin_attrs::{
134 deprecated_attributes, find_gated_cfg, is_builtin_attr_name, AttributeGate, AttributeTemplate,
135 AttributeType, BuiltinAttribute, GatedCfg, BUILTIN_ATTRIBUTES, BUILTIN_ATTRIBUTE_MAP,
137 pub use removed::{REMOVED_FEATURES, STABLE_REMOVED_FEATURES};