rust/compiler/rustc_feature/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
5.5 KiB
Rust
Raw Normal View History

2019-11-30 05:43:32 +00:00
//! # Feature gates
//!
2019-11-30 05:43:32 +00:00
//! This crate declares the set of past and present unstable features in the compiler.
//! Feature gate checking itself is done in `rustc_ast_passes/src/feature_gate.rs`
2020-01-12 15:51:15 +00:00
//! at the moment.
//!
//! Features are enabled in programs via the crate-level attributes of
//! `#![feature(...)]` with a comma-separated list of features.
//!
2019-11-30 05:43:32 +00:00
//! For the purpose of future feature-tracking, once a feature gate is added,
//! even if it is stabilized or removed, *do not remove it*. Instead, move the
//! symbol to the `accepted` or `removed` modules respectively.
2015-02-03 22:31:06 +00:00
// tidy-alphabetical-start
2023-11-13 12:39:17 +00:00
#![allow(internal_features)]
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
// tidy-alphabetical-end
mod accepted;
2019-11-30 01:34:18 +00:00
mod builtin_attrs;
mod removed;
mod unstable;
2019-08-22 21:48:08 +00:00
#[cfg(test)]
mod tests;
2024-01-29 22:59:09 +00:00
use std::num::NonZero;
use rustc_span::Symbol;
#[derive(Debug, Clone)]
pub struct Feature {
pub name: Symbol,
/// For unstable features: the version the feature was added in.
/// For accepted features: the version the feature got stabilized in.
/// For removed features we are inconsistent; sometimes this is the
/// version it got added, sometimes the version it got removed.
pub since: &'static str,
2024-01-29 22:59:09 +00:00
issue: Option<NonZero<u32>>,
}
2019-11-29 23:39:51 +00:00
#[derive(Copy, Clone, Debug)]
pub enum Stability {
Unstable,
// First argument is tracking issue link; second argument is an optional
// help message, which defaults to "remove this attribute".
Deprecated(&'static str, Option<&'static str>),
}
2020-10-10 18:27:52 +00:00
#[derive(Clone, Copy, Debug, Hash)]
2019-11-30 01:50:47 +00:00
pub enum UnstableFeatures {
/// Disallow use of unstable features, as on beta/stable channels.
2019-11-30 01:50:47 +00:00
Disallow,
/// Allow use of unstable features, as on nightly.
2019-11-30 01:50:47 +00:00
Allow,
/// Errors are bypassed for bootstrapping. This is required any time
/// during the build that feature-related lints are set to warn or above
/// because the build turns on warnings-as-errors and uses lots of unstable
/// features. As a result, this is always required for building Rust itself.
Cheat,
2019-11-30 01:50:47 +00:00
}
impl UnstableFeatures {
2020-10-10 18:27:52 +00:00
/// This takes into account `RUSTC_BOOTSTRAP`.
///
/// If `krate` is [`Some`], then setting `RUSTC_BOOTSTRAP=krate` will enable the nightly
/// features. Otherwise, only `RUSTC_BOOTSTRAP=1` will work.
2020-10-10 18:27:52 +00:00
pub fn from_environment(krate: Option<&str>) -> Self {
Self::from_environment_value(krate, std::env::var("RUSTC_BOOTSTRAP"))
}
/// Avoid unsafe `std::env::set_var()` by allowing tests to inject
/// `std::env::var("RUSTC_BOOTSTRAP")` with the `env_var_rustc_bootstrap`
/// arg.
fn from_environment_value(
krate: Option<&str>,
env_var_rustc_bootstrap: Result<String, std::env::VarError>,
) -> Self {
2019-11-30 01:50:47 +00:00
// `true` if this is a feature-staged build, i.e., on the beta or stable channel.
let disable_unstable_features =
option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some_and(|s| s != "0");
2020-10-10 18:27:52 +00:00
// Returns whether `krate` should be counted as unstable
let is_unstable_crate =
|var: &str| krate.is_some_and(|name| var.split(',').any(|new_krate| new_krate == name));
let bootstrap = env_var_rustc_bootstrap.ok();
if let Some(val) = bootstrap.as_deref() {
match val {
val if val == "1" || is_unstable_crate(val) => return UnstableFeatures::Cheat,
// Hypnotize ourselves so that we think we are a stable compiler and thus don't
// allow any unstable features.
"-1" => return UnstableFeatures::Disallow,
_ => {}
}
2019-11-30 01:50:47 +00:00
}
if disable_unstable_features { UnstableFeatures::Disallow } else { UnstableFeatures::Allow }
2019-11-30 01:50:47 +00:00
}
pub fn is_nightly_build(&self) -> bool {
match *self {
UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
UnstableFeatures::Disallow => false,
}
}
}
2024-01-29 22:59:09 +00:00
fn find_lang_feature_issue(feature: Symbol) -> Option<NonZero<u32>> {
// Search in all the feature lists.
if let Some(f) = UNSTABLE_LANG_FEATURES.iter().find(|f| f.name == feature) {
return f.issue;
}
if let Some(f) = ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature) {
return f.issue;
}
if let Some(f) = REMOVED_LANG_FEATURES.iter().find(|f| f.feature.name == feature) {
return f.feature.issue;
}
panic!("feature `{feature}` is not declared anywhere");
}
2024-01-29 22:59:09 +00:00
const fn to_nonzero(n: Option<u32>) -> Option<NonZero<u32>> {
// Can be replaced with `n.and_then(NonZero::new)` if that is ever usable
// in const context. Requires https://github.com/rust-lang/rfcs/pull/2632.
match n {
None => None,
Some(n) => NonZero::new(n),
}
}
pub enum GateIssue {
Language,
2024-01-29 22:59:09 +00:00
Library(Option<NonZero<u32>>),
}
2024-01-29 22:59:09 +00:00
pub fn find_feature_issue(feature: Symbol, issue: GateIssue) -> Option<NonZero<u32>> {
match issue {
GateIssue::Language => find_lang_feature_issue(feature),
GateIssue::Library(lib) => lib,
}
}
pub use accepted::ACCEPTED_LANG_FEATURES;
2019-11-30 01:34:18 +00:00
pub use builtin_attrs::{
AttributeDuplicates, AttributeGate, AttributeSafety, AttributeTemplate, AttributeType,
BUILTIN_ATTRIBUTE_MAP, BUILTIN_ATTRIBUTES, BuiltinAttribute, GatedCfg, deprecated_attributes,
encode_cross_crate, find_gated_cfg, is_builtin_attr_name, is_stable_diagnostic_attribute,
is_valid_for_get_attr,
2019-11-30 01:34:18 +00:00
};
pub use removed::REMOVED_LANG_FEATURES;
pub use unstable::{
EnabledLangFeature, EnabledLibFeature, Features, INCOMPATIBLE_FEATURES, UNSTABLE_LANG_FEATURES,
};