Skip to content

Commit 9aba967

Browse files
committed
Document feature gate checking
1 parent 4bd7280 commit 9aba967

1 file changed

Lines changed: 131 additions & 1 deletion

File tree

src/feature-gate-ck.md

Lines changed: 131 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,133 @@
11
# Feature Gate Checking
22

3-
**TODO**: this chapter [#1158](https://github.com/rust-lang/rustc-dev-guide/issues/1158)
3+
Feature gates prevent usage of unstable language and library features without a
4+
nightly-only `#![feature(...)]` opt-in. This chapter documents the implementation
5+
of feature gating: where gates are defined, how they are enabled, and how usage
6+
is verified.
7+
8+
## Feature Definitions
9+
10+
All feature gate definitions are located in the `rustc_feature` crate:
11+
12+
- **Unstable features** are declared in [`rustc_feature/src/unstable.rs`] via
13+
the `declare_features!` macro. This associates features with issue numbers and
14+
tracking metadata.
15+
- **Accepted features** (stabilized) are listed in [`rustc_feature/src/accepted.rs`].
16+
- **Removed features** (explicitly disallowed) are listed in [`rustc_feature/src/removed.rs`].
17+
- **Gated built-in attributes and cfgs** are declared in [`rustc_feature/src/builtin_attrs.rs`].
18+
19+
The [`rustc_feature::Features`] type represents the **active feature set** for a
20+
crate. Helpers like `enabled`, `incomplete`, and `internal` are used during
21+
compilation to check status.
22+
23+
## Collecting Features
24+
25+
Before AST validation or expansion, `rustc` collects crate-level
26+
`#![feature(...)]` attributes to build the active `Features` set.
27+
28+
- The collection happens in [`rustc_expand/src/config.rs`] in [`features`].
29+
- Each `#![feature]` entry is classified against the `unstable`, `accepted`, and
30+
`removed` tables:
31+
- **Removed** features cause an immediate error.
32+
- **Accepted** features are recorded but do not require nightly. On
33+
stable/beta they trigger the "already stabilized" diagnostic.
34+
- **Unstable** features are recorded as enabled.
35+
- Unknown features are treated as **library features** and validated later.
36+
- With `-Z allow-features=...`, any **unstable** or **unknown** feature
37+
not in the allowlist is rejected.
38+
- [`RUSTC_BOOTSTRAP`] feeds into `UnstableFeatures::from_environment`. This
39+
variable controls whether the compiler is treated as "nightly", allowing
40+
feature gates to be bypassed during bootstrapping or explicitly disabled (`-1`).
41+
42+
## Parser Gating
43+
44+
Some syntax is detected and gated during parsing. The parser records spans for
45+
later checking to keep diagnostics consistent and deferred until after parsing.
46+
47+
- [`rustc_session/src/parse.rs`] defines [`GatedSpans`] and the `gate` method.
48+
- The parser uses it in [`rustc_parse/src/parser/*`] when it encounters
49+
syntax that requires a gate (e.g., `async for`, `yield`, experimental patterns).
50+
51+
## Checking Pass
52+
53+
The central logic lives in [`rustc_ast_passes/src/feature_gate.rs`], primarily
54+
in `check_crate` and its AST visitor.
55+
56+
### `check_crate`
57+
58+
`check_crate` performs high-level validation:
59+
60+
- `maybe_stage_features`: Rejects `#![feature]` on stable/beta.
61+
- `check_incompatible_features`: Ensures incompatible feature combinations
62+
(declared in `rustc_feature::INCOMPATIBLE_FEATURES`) are not used together.
63+
- `check_new_solver_banned_features`: Bans features incompatible with
64+
certain compiler modes (e.g., the next trait solver).
65+
- **Parser-gated spans**: Processes the `GatedSpans` recorded during parsing.
66+
67+
### AST Visitor
68+
69+
A `PostExpansionVisitor` walks the expanded AST to check constructs that are
70+
easier to validate after expansion.
71+
72+
- The visitor uses helper macros (`gate!`, `gate_alt!`, `gate_multi!`) to check:
73+
1. Is the feature enabled?
74+
2. Does `span.allows_unstable` permit it (for internal compiler macros)?
75+
- Examples include `trait_alias`, `decl_macro`, `extern types`, and various
76+
`impl Trait` forms.
77+
78+
### Checking `GatedSpans`
79+
80+
`check_crate` iterates over `sess.psess.gated_spans`:
81+
82+
- The `gate_all!` macro emits diagnostics for each gated span if the feature is
83+
not enabled.
84+
- Some gates have extra logic (e.g., `yield` can be allowed by `coroutines` or
85+
`gen_blocks`).
86+
- Legacy gates (e.g., `box_patterns`, `try_blocks`) may use a separate path that
87+
emits future-incompatibility warnings instead of hard errors.
88+
89+
## Attributes and `cfg`
90+
91+
Beyond syntax, rustc also gates attributes and `cfg` options.
92+
93+
### Built-in attributes
94+
95+
- [`rustc_ast_passes::check_attribute`] inspects attributes against
96+
`BUILTIN_ATTRIBUTE_MAP`.
97+
- If the attribute is `AttributeGate::Gated` and the feature isn’t enabled,
98+
`feature_err` is emitted.
99+
100+
### `cfg` options
101+
102+
- [`rustc_attr_parsing/src/attributes/cfg.rs`] defines
103+
`find_gated_cfg`/`gate_cfg` to reject gated `cfg`s.
104+
- `gate_cfg` respects `Span::allows_unstable`, allowing internal compiler
105+
macros to bypass `cfg` gates when marked with `#[allow_internal_unstable]`.
106+
- The gated cfg list is defined in [`rustc_feature/src/builtin_attrs.rs`].
107+
108+
## Diagnostics
109+
110+
Diagnostic helpers are located in [`rustc_session/src/parse.rs`].
111+
112+
- `feature_err` and `feature_warn` emit standardized diagnostics, attaching the
113+
tracking issue number where possible.
114+
- `Span::allows_unstable` in [`rustc_span/src/lib.rs`] checks if a span originates
115+
from a macro marked with `#[allow_internal_unstable]`. This allows internal
116+
macros to use unstable features on stable channels while enforcing gates for
117+
user code.
118+
119+
[`rustc_feature/src/unstable.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/unstable.rs
120+
[`rustc_feature/src/removed.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/removed.rs
121+
[`rustc_feature/src/accepted.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/accepted.rs
122+
[`rustc_feature/src/builtin_attrs.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_feature/src/builtin_attrs.rs
123+
[`rustc_feature::Features`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_feature/struct.Features.html
124+
[`rustc_expand/src/config.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_expand/src/config.rs
125+
[`features`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_expand/config/fn.features.html
126+
[`RUSTC_BOOTSTRAP`]: https://doc.rust-lang.org/beta/unstable-book/compiler-environment-variables/RUSTC_BOOTSTRAP.html
127+
[`rustc_session/src/parse.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_session/src/parse.rs
128+
[`GatedSpans`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.GatedSpans.html
129+
[`rustc_ast_passes/src/feature_gate.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_ast_passes/src/feature_gate.rs
130+
[`rustc_parse/src/parser/*`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_parse/parser/index.html
131+
[`rustc_ast_passes::check_attribute`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_passes/feature_gate/fn.check_attribute.html
132+
[`rustc_attr_parsing/src/attributes/cfg.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_attr_parsing/src/attributes/cfg.rs
133+
[`rustc_span/src/lib.rs`]: https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc_span/src/lib.rs

0 commit comments

Comments
 (0)