]> git.proxmox.com Git - rustc.git/blob - src/doc/rustc-dev-guide/src/implementing_new_features.md
New upstream version 1.52.0~beta.3+dfsg1
[rustc.git] / src / doc / rustc-dev-guide / src / implementing_new_features.md
1 # Implement New Feature
2
3 When you want to implement a new significant feature in the compiler,
4 you need to go through this process to make sure everything goes
5 smoothly.
6
7 ## The @rfcbot (p)FCP process
8
9 When the change is small and uncontroversial, then it can be done
10 with just writing a PR and getting r+ from someone who knows that
11 part of the code. However, if the change is potentially controversial,
12 it would be a bad idea to push it without consensus from the rest
13 of the team (both in the "distributed system" sense to make sure
14 you don't break anything you don't know about, and in the social
15 sense to avoid PR fights).
16
17 If such a change seems to be too small to require a full formal RFC
18 process (e.g. a big refactoring of the code, or a
19 "technically-breaking" change, or a "big bugfix" that basically
20 amounts to a small feature) but is still too controversial or
21 big to get by with a single r+, you can start a pFCP (or, if you
22 don't have r+ rights, ask someone who has them to start one - and
23 unless they have a concern themselves, they should). pFCP stands for
24 "proposed final comment period".
25
26 Again, the pFCP process is only needed if you need consensus - if you
27 don't think anyone would have a problem with your change, it's ok to
28 get by with only an r+. For example, it is OK to add or modify
29 unstable command-line flags or attributes without a pFCP for
30 compiler development or standard library use, as long as you don't
31 expect them to be in wide use in the nightly ecosystem.
32
33 You don't need to have the implementation fully ready for r+ to ask
34 for a pFCP, but it is generally a good idea to have at least a proof
35 of concept so that people can see what you are talking about.
36
37 When a pFCP is started, it requires all members of the team to sign off
38 the FCP. After they all do so, there's a 10 day long "final comment
39 period" where everybody can comment, and if no new concerns are raised,
40 the PR/issue gets FCP approval.
41
42 ## The logistics of writing features
43
44 There are a few "logistic" hoops you might need to go through in
45 order to implement a feature in a working way.
46
47 ### Warning Cycles
48
49 In some cases, a feature or bugfix might break some existing programs
50 in some edge cases. In that case, you might want to do a crater run
51 to assess the impact and possibly add a future-compatibility lint,
52 similar to those used for
53 [edition-gated lints](diagnostics.md#edition-gated-lints).
54
55 ### Stability
56
57 We [value the stability of Rust]. Code that works and runs on stable
58 should (mostly) not break. Because of that, we don't want to release
59 a feature to the world with only team consensus and code review -
60 we want to gain real-world experience on using that feature on nightly,
61 and we might want to change the feature based on that experience.
62
63 To allow for that, we must make sure users don't accidentally depend
64 on that new feature - otherwise, especially if experimentation takes
65 time or is delayed and the feature takes the trains to stable,
66 it would end up de facto stable and we'll not be able to make changes
67 in it without breaking people's code.
68
69 The way we do that is that we make sure all new features are feature
70 gated - they can't be used without enabling a feature gate
71 (`#[feature(foo)]`), which can't be done in a stable/beta compiler.
72 See the [stability in code] section for the technical details.
73
74 Eventually, after we gain enough experience using the feature,
75 make the necessary changes, and are satisfied, we expose it to
76 the world using the stabilization process described [here].
77 Until then, the feature is not set in stone: every part of the
78 feature can be changed, or the feature might be completely
79 rewritten or removed. Features are not supposed to gain tenure
80 by being unstable and unchanged for a year.
81
82 <a name = "tracking-issue"></a>
83 ### Tracking Issues
84
85 To keep track of the status of an unstable feature, the
86 experience we get while using it on nightly, and of the
87 concerns that block its stabilization, every feature-gate
88 needs a tracking issue.
89
90 General discussions about the feature should be done on
91 the tracking issue.
92
93 For features that have an RFC, you should use the RFC's
94 tracking issue for the feature.
95
96 For other features, you'll have to make a tracking issue
97 for that feature. The issue title should be "Tracking issue
98 for YOUR FEATURE".
99
100 For tracking issues for features (as opposed to future-compat
101 warnings), I don't think the description has to contain
102 anything specific. Generally we put the list of items required
103 for stabilization in a checklist, e.g.,
104
105 ```txt
106 **Steps:**
107
108 - [ ] Implement the RFC. (CC @rust-lang/compiler -- can anyone write
109 up mentoring instructions?)
110 - [ ] Adjust the documentation. ([See instructions on rustc-dev-guide.](https://rustc-dev-guide.rust-lang.org/stabilization_guide.html#documentation-prs))
111 - [ ] Stabilize the feature. ([See instructions on rustc-dev-guide.](https://rustc-dev-guide.rust-lang.org/stabilization_guide.html#stabilization-pr))
112 ```
113
114 <a name="stability-in-code"></a>
115 ## Stability in code
116
117 The below steps needs to be followed in order to implement
118 a new unstable feature:
119
120 1. Open a [tracking issue] -
121 if you have an RFC, you can use the tracking issue for the RFC.
122
123 The tracking issue should be labeled with at least `C-tracking-issue`.
124 For a language feature, a label `F-feature_name` should be added as well.
125
126 2. Pick a name for the feature gate (for RFCs, use the name
127 in the RFC).
128
129 3. Add a feature gate declaration to `rustc_feature/src/active.rs`
130 in the active `declare_features` block. See [here][add-feature-gate] for
131 detailed instructions.
132
133 4. Prevent usage of the new feature unless the feature gate is set.
134 You can check it in most places in the compiler using the
135 expression `tcx.features().$feature_name` (or
136 `sess.features_untracked().$feature_name` if the
137 tcx is unavailable)
138
139 If the feature gate is not set, you should either maintain
140 the pre-feature behavior or raise an error, depending on
141 what makes sense. Errors should generally use [`rustc_session::parse::feature_err`].
142 For an example of adding an error, see [#81015].
143
144 For features introducing new syntax, pre-expansion gating should be used instead.
145 To do so, extend the [`GatedSpans`] struct, add spans to it during parsing,
146 and then finally feature-gate all the spans in
147 [`rustc_ast_passes::feature_gate::check_crate`].
148
149 5. Add a test to ensure the feature cannot be used without
150 a feature gate, by creating `feature-gate-$feature_name.rs`
151 and `feature-gate-$feature_name.stderr` files under the
152 directory where the other tests for your feature reside.
153
154 6. Add a section to the unstable book, in
155 `src/doc/unstable-book/src/language-features/$feature_name.md`.
156
157 7. Write a lot of tests for the new feature.
158 PRs without tests will not be accepted!
159
160 8. Get your PR reviewed and land it. You have now successfully
161 implemented a feature in Rust!
162
163 [`GatedSpans`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/struct.GatedSpans.html
164 [#81015]: https://github.com/rust-lang/rust/pull/81015
165 [`rustc_session::parse::feature_err`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/parse/fn.feature_err.html
166 [`rustc_ast_passes::feature_gate::check_crate`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_passes/feature_gate/fn.check_crate.html
167 [value the stability of Rust]: https://github.com/rust-lang/rfcs/blob/master/text/1122-language-semver.md
168 [stability in code]: #stability-in-code
169 [here]: ./stabilization_guide.md
170 [tracking issue]: #tracking-issue
171 [add-feature-gate]: ./feature-gates.md#adding-a-feature-gate