@@ -4,9 +4,9 @@ For the how-to steps to add, remove, rename, or stabilize feature gates,
44see [ Feature gates] [ feature-gates ] .
55
66Feature gates prevent usage of unstable language and library features without a
7- nightly-only ` #![feature(...)] ` opt-in. This chapter documents the implementation
8- of feature gating: where gates are defined, how they are enabled, and how usage
9- is verified.
7+ nightly-only ` #![feature(...)] ` opt-in.
8+ This chapter documents the implementation
9+ of feature gating: where gates are defined, how they are enabled, and how usage is verified.
1010
1111<!-- data-check: Feb 2026 -->
1212
@@ -15,41 +15,40 @@ is verified.
1515All feature gate definitions are located in the ` rustc_feature ` crate:
1616
1717- ** Unstable features** are declared in [ ` rustc_feature/src/unstable.rs ` ] via
18- the ` declare_features! ` macro. This associates features with issue numbers and
19- tracking metadata.
18+ the ` declare_features! ` macro.
19+ This associates features with issue numbers and tracking metadata.
2020- ** Accepted features** (stabilized) are listed in [ ` rustc_feature/src/accepted.rs ` ] .
2121- ** Removed features** (explicitly disallowed) are listed in [ ` rustc_feature/src/removed.rs ` ] .
2222- ** Gated built-in attributes and cfgs** are declared in [ ` rustc_feature/src/builtin_attrs.rs ` ] .
2323
24- The [ ` rustc_feature::Features ` ] type represents the ** active feature set** for a
25- crate. Helpers like ` enabled ` , ` incomplete ` , and ` internal ` are used during
26- compilation to check status.
24+ The [ ` rustc_feature::Features ` ] type represents the ** active feature set** for a crate.
25+ Helpers like ` enabled ` , ` incomplete ` , and ` internal ` are used during compilation to check status.
2726
2827## Collecting Features
2928
3029Before AST validation or expansion, ` rustc ` collects crate-level
3130` #![feature(...)] ` attributes to build the active ` Features ` set.
3231
3332- The collection happens in [ ` rustc_expand/src/config.rs ` ] in [ ` features ` ] .
34- - Each ` #![feature] ` entry is classified against the ` unstable ` , ` accepted ` , and
35- ` removed ` tables:
33+ - Each ` #![feature] ` entry is classified against the ` unstable ` , ` accepted ` , and ` removed ` tables:
3634 - ** Removed** features cause an immediate error.
37- - ** Accepted** features are recorded but do not require nightly. On
38- stable/beta, ` maybe_stage_features ` in
35+ - ** Accepted** features are recorded but do not require nightly.
36+ On stable/beta, ` maybe_stage_features ` in
3937 [ ` rustc_ast_passes/src/feature_gate.rs ` ] emits the non-nightly
4038 diagnostic and lists stable features, which is where the "already
4139 stabilized" messaging comes from.
4240 - ** Unstable** features are recorded as enabled.
4341 - Unknown features are treated as ** library features** and validated later.
4442- With ` -Z allow-features=... ` , any ** unstable** or ** unknown** feature
4543 not in the allowlist is rejected.
46- - [ ` RUSTC_BOOTSTRAP ` ] feeds into ` UnstableFeatures::from_environment ` . This
47- variable controls whether the compiler is treated as "nightly", allowing
44+ - [ ` RUSTC_BOOTSTRAP ` ] feeds into ` UnstableFeatures::from_environment ` .
45+ This variable controls whether the compiler is treated as "nightly", allowing
4846 feature gates to be bypassed during bootstrapping or explicitly disabled (` -1 ` ).
4947
5048## Parser Gating
5149
52- Some syntax is detected and gated during parsing. The parser records spans for
50+ Some syntax is detected and gated during parsing.
51+ The parser records spans for
5352later checking to keep diagnostics consistent and deferred until after parsing.
5453
5554- [ ` rustc_session/src/parse.rs ` ] defines [ ` GatedSpans ` ] and the ` gate ` method.
@@ -77,8 +76,7 @@ in `check_crate` and its AST visitor.
7776
7877` check_crate ` iterates over ` sess.psess.gated_spans ` :
7978
80- - The ` gate_all! ` macro emits diagnostics for each gated span if the feature is
81- not enabled.
79+ - The ` gate_all! ` macro emits diagnostics for each gated span if the feature is not enabled.
8280- Some gates have extra logic (e.g., ` yield ` can be allowed by ` coroutines ` or
8381 ` gen_blocks ` ).
8482- Legacy gates (e.g., ` box_patterns ` , ` try_blocks ` ) may use a separate path that
@@ -92,17 +90,15 @@ easier to validate after expansion.
9290- The visitor uses helper macros (` gate! ` , ` gate_alt! ` , ` gate_multi! ` ) to check:
9391 1 . Is the feature enabled?
9492 2 . Does ` span.allows_unstable ` permit it (for internal compiler macros)?
95- - Examples include ` trait_alias ` , ` decl_macro ` , ` extern types ` , and various
96- ` impl Trait ` forms.
93+ - Examples include ` trait_alias ` , ` decl_macro ` , ` extern types ` , and various ` impl Trait ` forms.
9794
9895## Attributes and ` cfg `
9996
10097Beyond syntax, rustc also gates attributes and ` cfg ` options.
10198
10299### Built-in attributes
103100
104- - [ ` rustc_ast_passes::check_attribute ` ] inspects attributes against
105- ` BUILTIN_ATTRIBUTE_MAP ` .
101+ - [ ` rustc_ast_passes::check_attribute ` ] inspects attributes against ` BUILTIN_ATTRIBUTE_MAP ` .
106102- If the attribute is ` AttributeGate::Gated ` and the feature isn’t enabled,
107103 ` feature_err ` is emitted.
108104
@@ -121,9 +117,9 @@ Diagnostic helpers are located in [`rustc_session/src/parse.rs`].
121117- ` feature_err ` and ` feature_warn ` emit standardized diagnostics, attaching the
122118 tracking issue number where possible.
123119- ` Span::allows_unstable ` in [ ` rustc_span/src/lib.rs ` ] checks if a span originates
124- from a macro marked with ` #[allow_internal_unstable] ` . This allows internal
125- macros to use unstable features on stable channels while enforcing gates for
126- user code.
120+ from a macro marked with ` #[allow_internal_unstable] ` .
121+ This allows internal
122+ macros to use unstable features on stable channels while enforcing gates for user code.
127123
128124[ `rustc_feature/src/unstable.rs` ] : https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/unstable.rs
129125[ `rustc_feature/src/removed.rs` ] : https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/removed.rs
0 commit comments