|
| 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). |
| 24 | + |
| 25 | +Again, the pFCP process is only needed if you need consensus - if you |
| 26 | +don't think anyone would have a problem with your change, it's ok to |
| 27 | +get by with only an r+. For example, it is OK to add or modify |
| 28 | +unstable command-line flags or attributes without an pFCP for |
| 29 | +compiler development or standard library use, as long as you don't |
| 30 | +expect them to be in wide use in the nightly ecosystem. |
| 31 | + |
| 32 | +You don't need to have the implementation fully ready for r+ to ask |
| 33 | +for a pFCP, but it is generally a good idea to have at least a proof |
| 34 | +of concept so that people can see what you are talking about. |
| 35 | + |
| 36 | +That starts a "proposed final comment period" (pFCP), which requires |
| 37 | +all members of the team to sign off the FCP. After they all do so, |
| 38 | +there's a week long "final comment period" where everybody can comment, |
| 39 | +and if no new concerns are raised, the PR/issue gets FCP approval. |
| 40 | + |
| 41 | +## The logistics of writing features |
| 42 | + |
| 43 | +There are a few "logistic" hoops you might need to go through in |
| 44 | +order to implement a feature in a working way. |
| 45 | + |
| 46 | +### Warning Cycles |
| 47 | + |
| 48 | +In some cases, a feature or bugfix might break some existing programs |
| 49 | +in some edge cases. In that case, you might want to do a crater run |
| 50 | +to assess the impact and possibly add a future-compatibility lint, |
| 51 | +similar to those used for |
| 52 | +[edition-gated lints](./diag.md#edition-gated-lints). |
| 53 | + |
| 54 | +### Stability |
| 55 | + |
| 56 | +We [value the stability of Rust]. Code that works and runs on stable |
| 57 | +should (mostly) not break. Because of that, we don't want to release |
| 58 | +a feature to the world with only team consensus and code review - |
| 59 | +we want to gain real-world experience on using that feature on nightly, |
| 60 | +and we might want to change the feature based on that experience. |
| 61 | + |
| 62 | +To allow for that, we must make sure users don't accidentally depend |
| 63 | +on that new feature - otherwise, especially if experimentation takes |
| 64 | +time or is delayed and the feature takes the trains to stable, |
| 65 | +it would end up de facto stable and we'll not be able to make changes |
| 66 | +in it without breaking people's code. |
| 67 | + |
| 68 | +The way we do that is that we make sure all new features are feature |
| 69 | +gated - they can't be used without a enabling a feature gate |
| 70 | +`(#[feature(foo)])`, which can't be done in a stable/beta compiler. |
| 71 | +See the [stability in code] section for the technical details. |
| 72 | + |
| 73 | +Eventually, after we gain enough experience using the feature, |
| 74 | +make the necessary changes, and are satisfied, we expose it to |
| 75 | +the world using the stabilization process described [here]. |
| 76 | +Until then, the feature is not set in stone: every part of the |
| 77 | +feature can be changed, or the feature might be completely |
| 78 | +rewritten or removed. Features are not supposed to gain tenure |
| 79 | +by being unstable and unchanged for a year. |
| 80 | + |
| 81 | +### Tracking Issues |
| 82 | + |
| 83 | +To keep track of the status of an unstable feature, the |
| 84 | +experience we get while using it on nightly, and of the |
| 85 | +concerns that block its stabilization, every feature-gate |
| 86 | +needs a tracking issue. |
| 87 | + |
| 88 | +General discussions about the feature should be done on |
| 89 | +the tracking issue. |
| 90 | + |
| 91 | +For features that have an RFC, you should use the RFC's |
| 92 | +tracking issue for the feature. |
| 93 | + |
| 94 | +For other features, you'll have to make a tracking issue |
| 95 | +for that feature. The issue title should be "Tracking issue |
| 96 | +for YOUR FEATURE". |
| 97 | + |
| 98 | +For tracking issues for features (as opposed to future-compat |
| 99 | +warnings), I don't think the description has to contain |
| 100 | +anything specific. Generally we put the list of items required |
| 101 | +for stabilization using a github list, e.g. |
| 102 | + |
| 103 | +```txt |
| 104 | + **Steps:** |
| 105 | +
|
| 106 | + - [ ] Implement the RFC (cc @rust-lang/compiler -- can anyone write up mentoring instructions?) |
| 107 | + - [ ] Adjust documentation ([see instructions on forge][doc-guide]) |
| 108 | + - Note: no stabilization step here. |
| 109 | +``` |
| 110 | + |
| 111 | +## Stability in code |
| 112 | + |
| 113 | +The below steps needs to be followed in order to implement |
| 114 | +a new unstable feature: |
| 115 | + |
| 116 | +1. Open a [tracking issue] - if you have an RFC, you can use |
| 117 | + the tracking issue for the RFC. |
| 118 | + |
| 119 | +2. Pick a name for the feature gate (for RFCs, use the name |
| 120 | + in the RFC). |
| 121 | + |
| 122 | +3. Add a feature gate declaration to `libsyntax/feature_gate.rs` |
| 123 | + in the active `declare_features` block: |
| 124 | + |
| 125 | +```rust,ignore |
| 126 | + // description of feature |
| 127 | + (active, $feature_name, "$current_nightly_version", Some($tracking_issue_number), $edition) |
| 128 | +``` |
| 129 | + |
| 130 | +where `$edition` has the type `Option<Edition>`, and is typically |
| 131 | +just `None`. |
| 132 | + |
| 133 | +For example: |
| 134 | + |
| 135 | +```rust,ignore |
| 136 | + // allow '|' at beginning of match arms (RFC 1925) |
| 137 | +( active, match_beginning_vert, "1.21.0", Some(44101), None), |
| 138 | +``` |
| 139 | + |
| 140 | +The current version is not actually important – the important |
| 141 | +version is when you are stabilizing a feature. |
| 142 | + |
| 143 | +4. Prevent usage of the new feature unless the feature gate is set. |
| 144 | + You can check it in most places in the compiler using the |
| 145 | + expression `tcx.features().$feature_name` (or |
| 146 | + `sess.features_untracked().borrow().$feature_name` if the |
| 147 | + tcx is unavailable) |
| 148 | + |
| 149 | + If the feature gate is not set, you should either maintain |
| 150 | + the pre-feature behavior or raise an error, depending on |
| 151 | + what makes sense. |
| 152 | + |
| 153 | +5. Add a test to ensure the feature cannot be used without |
| 154 | + a feature gate, by creating `feature-gate-$feature_name.rs` |
| 155 | + and `feature-gate-$feature_name.stderr` files under the |
| 156 | + `src/test/ui/feature-gates` directory. |
| 157 | + |
| 158 | +6. Add a section to the unstable book, in |
| 159 | + `src/doc/unstable-book/src/language-features/$feature_name.md`. |
| 160 | + |
| 161 | +7. Write a lots of tests for the new feature. |
| 162 | + PRs without tests will not be accepted! |
| 163 | + |
| 164 | +8. Get your PR reviewed and land it. You have now successfully |
| 165 | + implemented a feature in Rust! |
| 166 | + |
| 167 | +[value the stability of Rust]: https://github.com/rust-lang/rfcs/blob/master/text/1122-language-semver.md |
| 168 | +[stability in code]: |
| 169 | +[here]: https://rust-lang.github.io/rustc-guide/stabilization_guide.html |
| 170 | +[tracking issue]: |
0 commit comments