|
| 1 | +- Feature Name: N/A |
| 2 | +- Start Date: 2016-04-21 |
| 3 | +- RFC PR: (leave this empty) |
| 4 | +- Rust Issue: N/A |
| 5 | + |
| 6 | + |
| 7 | +# Summary |
| 8 | +[summary]: #summary |
| 9 | + |
| 10 | +This RFC proposes a process for deciding detailed guidelines for code |
| 11 | +formatting, and default settings for Rustfmt. The outcome of the process should |
| 12 | +be an approved formatting style defined by a style guide and enforced by |
| 13 | +Rustfmt. |
| 14 | + |
| 15 | +This RFC proposes creating a new repository under the [rust-lang](https://github.com/rust-lang) |
| 16 | +organisation called fmt-rfcs. It will be operated in a similar manner to the |
| 17 | +[RFCs repository](https://github.com/rust-lang/rfcs), but restricted to |
| 18 | +formatting issues. A new [sub-team](https://github.com/rust-lang/rfcs/blob/master/text/1068-rust-governance.md#subteams) |
| 19 | +will be created to deal with those RFCs. Both the team and repository are |
| 20 | +expected to be temporary. Once the style guide is complete, the team can be |
| 21 | +disbanded and the repository frozen. |
| 22 | + |
| 23 | + |
| 24 | +# Motivation |
| 25 | +[motivation]: #motivation |
| 26 | + |
| 27 | +There is a need to decide on detailed guidelines for the format of Rust code. A |
| 28 | +uniform, language-wide formatting style makes comprehending new code-bases |
| 29 | +easier and forestalls bikeshedding arguments in teams of Rust users. The utility |
| 30 | +of such guidelines has been proven by Go, amongst other languages. |
| 31 | + |
| 32 | +The [Rustfmt](https://github.com/rust-lang-nursery/rustfmt) tool is |
| 33 | +[reaching maturity](https://users.rust-lang.org/t/please-help-test-rustfmt/5386) |
| 34 | +and currently enforces a somewhat arbitrary, lightly discussed style, with many |
| 35 | +configurable options. |
| 36 | + |
| 37 | +If Rustfmt is to become a widely accepted tool, there needs to be a process for |
| 38 | +the Rust community to decide on the default style, and how configurable that |
| 39 | +style should be. |
| 40 | + |
| 41 | +These discussions should happen in the open and be highly visible. It is |
| 42 | +important that the Rust community has significant input to the process. The RFC |
| 43 | +repository would be an ideal place to have this discussion because it exists to |
| 44 | +satisfy these goals, and is tried and tested. However, the discussion is likely |
| 45 | +to be a high-bandwidth one (code style is a contentious and often subjective |
| 46 | +topic, and syntactic RFCs tend to be the highest traffic ones). Therefore, |
| 47 | +having the discussion on the RFCs repository could easily overwhelm it and make |
| 48 | +it less useful for other important discussions. |
| 49 | + |
| 50 | +There currently exists a [style guide](https://github.com/rust-lang/rust/tree/master/src/doc/style) |
| 51 | +as part of the Rust documentation. This is far more wide-reaching than just |
| 52 | +formatting style, but also not detailed enough to specify Rustfmt. This was |
| 53 | +originally developed in its [own repository](https://github.com/rust-lang/rust-guidelines), |
| 54 | +but is now part of the main Rust repository. That seems like a poor venue for |
| 55 | +discussion of these guidelines due to visibility. |
| 56 | + |
| 57 | + |
| 58 | +# Detailed design |
| 59 | +[design]: #detailed-design |
| 60 | + |
| 61 | +## Process |
| 62 | + |
| 63 | +The process for style RFCs will mostly follow the [process for other RFCs](https://github.com/rust-lang/rfcs). |
| 64 | +Anyone may submit an RFC. An overview of the process is: |
| 65 | + |
| 66 | +* If there is no single, obvious style, then open a GitHub issue on the |
| 67 | + fmt-rfcs repo for initial discussion. This initial discussion should identify |
| 68 | + which Rustfmt options are required to enforce the guideline. |
| 69 | +* Implement the style in rustfmt (behind an option if it is not the current |
| 70 | + default). In exceptional circumstances (such as where the implementation would |
| 71 | + require very deep changes to rustfmt), this step may be skipped. |
| 72 | +* Write an RFC formalising the formatting convention and referencing the |
| 73 | + implementation, submit as a PR to fmt-rfcs. The RFC should include the default |
| 74 | + values for options to enforce the guideline and which non-default options |
| 75 | + should be kept. |
| 76 | +* The RFC PR will be triaged by the style team and either assigned to a team |
| 77 | + member for [shepherding](https://github.com/rust-lang/rfcs#the-role-of-the-shepherd), |
| 78 | + or closed. |
| 79 | +* When discussion has reached a fixed point, the RFC PR will be put into a final |
| 80 | + comment period (FCP). |
| 81 | +* After FCP, the RFC will either be accepted and merged or closed. |
| 82 | +* Implementation in Rustfmt can then be finished (including any changes due to |
| 83 | + discussion of the RFC), and defaults are set. |
| 84 | + |
| 85 | + |
| 86 | +### Scope of the process |
| 87 | + |
| 88 | +This process is specifically limited to formatting style guidelines which can be |
| 89 | +enforced by Rustfmt with its current architecture. Guidelines that cannot be |
| 90 | +enforced by Rustfmt without a large amount of work are out of scope, even if |
| 91 | +they only pertain to formatting. |
| 92 | + |
| 93 | +Note whether Rustfmt should be configurable at all, and if so how configurable |
| 94 | +is a decision that should be dealt with using the formatting RFC process. That |
| 95 | +will be a rather exceptional RFC. |
| 96 | + |
| 97 | +### Size of RFCs |
| 98 | + |
| 99 | +RFCs should be self-contained and coherent, whilst being as small as possible to |
| 100 | +keep discussion focused. For example, an RFC on 'arithmetic and logic |
| 101 | +expressions' is about the right size; 'expressions' would be too big, and |
| 102 | +'addition' would be too small. |
| 103 | + |
| 104 | + |
| 105 | +### When is a guideline ready for RFC? |
| 106 | + |
| 107 | +The purpose of the style RFC process is to foster an open discussion about style |
| 108 | +guidelines. Therefore, RFC PRs should be made early rather than late. It is |
| 109 | +expected that there may be more discussion and changes to style RFCs than is |
| 110 | +typical for Rust RFCs. However, at submission, RFC PRs should be completely |
| 111 | +developed and explained to the level where they can be used as a specification. |
| 112 | + |
| 113 | +A guideline should usually be implemented in Rustfmt **before** an RFC PR is |
| 114 | +submitted. The RFC should be used to select an option to be the default |
| 115 | +behaviour, rather than to identify a range of options. An RFC can propose a |
| 116 | +combination of options (rather than a single one) as default behaviour. An RFC |
| 117 | +may propose some reorganisation of options. |
| 118 | + |
| 119 | +Usually a style should be widely used in the community before it is submitted as |
| 120 | +an RFC. Where multiple styles are used, they should be covered as alternatives |
| 121 | +in the RFC, rather than being submitted as multiple RFCs. In some cases, a style |
| 122 | +may be proposed without wide use (we don't want to discourage innovation), |
| 123 | +however, it should have been used in *some* real code, rather than just being |
| 124 | +sketched out. |
| 125 | + |
| 126 | + |
| 127 | +### Triage |
| 128 | + |
| 129 | +RFC PRs are triaged by the style team. An RFC may be closed during triage (with |
| 130 | +feedback for the author) if the style team think it is not specified in enough |
| 131 | +detail, has too narrow or broad scope, or is not appropriate in some way (e.g., |
| 132 | +applies to more than just formatting). Otherwise, the PR will be assigned a |
| 133 | +shepherd as for other RFCs. |
| 134 | + |
| 135 | + |
| 136 | +### FCP |
| 137 | + |
| 138 | +FCP will last for two weeks (assuming the team decide to meet every two weeks) |
| 139 | +and will be announced in the style team sub-team report. |
| 140 | + |
| 141 | + |
| 142 | +### Decision and post-decision process |
| 143 | + |
| 144 | +The style team will make the ultimate decision on accepting or closing a style |
| 145 | +RFC PR. Decisions should be by consensus. Most discussion should take place on |
| 146 | +the PR comment thread, a decision should ideally be made when consensus is |
| 147 | +reached on the thread. Any additional discussion amongst the style team will be |
| 148 | +summarised on the thread. |
| 149 | + |
| 150 | +If an RFC PR is accepted, it will be merged. An issue for implementation will be |
| 151 | +filed in the appropriate place (usually the Rustfmt repository) referencing the |
| 152 | +RFC. If the style guide needs to be updated, then an issue for that should be |
| 153 | +filed on the Rust repository. |
| 154 | + |
| 155 | +The author of an RFC is not required to implement the guideline. If you are |
| 156 | +interested in working on the implementation for an 'active' RFC, but cannot |
| 157 | +determine if someone else is already working on it, feel free to ask (e.g. by |
| 158 | +leaving a comment on the associated issue). |
| 159 | + |
| 160 | + |
| 161 | +## The fmt-rfcs repository |
| 162 | + |
| 163 | +The form of the fmt-rfcs repository will follow the rfcs repository. Accepted |
| 164 | +RFCs will live in a `text` directory, the `README.md` will include information |
| 165 | +taken from this RFC, there will be an RFC template in the root of the |
| 166 | +repository. Issues on the repository can be used for placeholders for future |
| 167 | +RFCs and for preliminary discussion. |
| 168 | + |
| 169 | +The RFC format will be illustrated by the RFC template. It will have the |
| 170 | +following sections: |
| 171 | + |
| 172 | +* summary |
| 173 | +* details |
| 174 | +* implementation |
| 175 | +* rationale |
| 176 | +* alternatives |
| 177 | +* unresolved questions |
| 178 | + |
| 179 | +The 'details' section should contain examples of both what should and shouldn't |
| 180 | +be done, cover simple and complex cases, and the interaction with other style |
| 181 | +guidelines. |
| 182 | + |
| 183 | +The 'implementation' section should specify how options must be set to enforce |
| 184 | +the guideline, and what further changes (including additional options) are |
| 185 | +required. It should specify any renaming, reorganisation, or removal of options. |
| 186 | + |
| 187 | +The 'rationale' section should motivate the choices behind the RFC. It should |
| 188 | +reference existing code bases which use the proposed style. 'Alternatives' |
| 189 | +should cover alternative possible guidelines, if appropriate. |
| 190 | + |
| 191 | +Guidelines may include more than one acceptable rule, but should offer |
| 192 | +guidance for when to use each rule (which should be formal enough to be used by |
| 193 | +a tool). |
| 194 | + |
| 195 | +For example: "a struct literal must be formatted either on a single line (with |
| 196 | +spaces after the opening brace and before the closing brace, and with fields |
| 197 | +separated by commas and spaces), or on multiple lines (with one field per line |
| 198 | +and newlines after the opening brace and before the closing brace). The former |
| 199 | +approach should be used for short struct literals, the latter for longer struct |
| 200 | +literals. For tools, the first approach should be used when the width of the |
| 201 | +fields (excluding commas and braces) is 16 characters. E.g., |
| 202 | + |
| 203 | +``` |
| 204 | +let x = Foo { a: 42, b: 34 }; |
| 205 | +let y = Foo { |
| 206 | + a: 42, |
| 207 | + b: 34, |
| 208 | + c: 1000 |
| 209 | +}; |
| 210 | +``` |
| 211 | +" |
| 212 | + |
| 213 | +(Note this is just an example, not a proposed guideline). |
| 214 | + |
| 215 | +The repository in embryonic form lives at [nrc/fmt-rfcs](https://github.com/nrc/fmt-rfcs). |
| 216 | +It illustrates what [issues](https://github.com/nrc/fmt-rfcs/issues/1) and |
| 217 | +[PRs](https://github.com/nrc/fmt-rfcs/pull/2) might look like, as well as |
| 218 | +including the RFC template. Note that typically there should be more discussion |
| 219 | +on an issue before submitting an RFC PR. |
| 220 | + |
| 221 | +The repository should be updated as this RFC develops, and moved to the rust-lang |
| 222 | +GitHub organisation if this RFC is accepted. |
| 223 | + |
| 224 | + |
| 225 | +## The style team |
| 226 | + |
| 227 | +The style [sub-team](https://github.com/rust-lang/rfcs/blob/master/text/1068-rust-governance.md#subteams) |
| 228 | +will be responsible for handling style RFCs and making decisions related to |
| 229 | +code style and formatting. |
| 230 | + |
| 231 | +Per the [governance RFC](https://github.com/rust-lang/rfcs/blob/master/text/1068-rust-governance.md), |
| 232 | +the core team would pick a leader who would then pick the rest of the team. I |
| 233 | +propose that the team should include members representative of the following |
| 234 | +areas: |
| 235 | + |
| 236 | +* Rustfmt, |
| 237 | +* the language, tools, and libraries sub-teams (since each has a stake in code style), |
| 238 | +* large Rust projects. |
| 239 | + |
| 240 | +Because activity such as this hasn't been done before in the RUst community, it |
| 241 | +is hard to identify suitable candidates for the team ahead of time. The team |
| 242 | +will probably start small and consist of core members of the Rust community. I |
| 243 | +expect that once the process gets underway the team can be rapidly expanded with |
| 244 | +community members who are active in the fmt-rfcs repository (i.e., submitting |
| 245 | +and constructively commenting on RFCs). |
| 246 | + |
| 247 | +There will be a dedicated irc channel for discussion on formatting issues: |
| 248 | +`#rust-style`. |
| 249 | + |
| 250 | + |
| 251 | +## Style guide |
| 252 | + |
| 253 | +The [existing style guide](https://github.com/rust-lang/rust/tree/master/src/doc/style) |
| 254 | +will be split into two guides: one dealing with API design and similar issues |
| 255 | +which will be managed by the libs team, and one dealing with formatting issues |
| 256 | +which will be managed by the style team. Note that the formatting part of the |
| 257 | +guide may include guidelines which are not enforced by Rustfmt. Those are outside |
| 258 | +the scope of the process defined in this RFC, but still belong in that part of |
| 259 | +the style guide. |
| 260 | + |
| 261 | +When RFCs are accepted the style guide may need to be updated. Towards the end |
| 262 | +of the process, the style team should audit and edit the guide to ensure it is a |
| 263 | +coherent document. |
| 264 | + |
| 265 | + |
| 266 | +## Material goals |
| 267 | + |
| 268 | +Hopefully, the style guideline process will have limited duration, one year |
| 269 | +seems reasonable. After that time, style guidelines for new syntax could be |
| 270 | +included with regular RFCs, or the fmt-rfcs repository could be maintained in a |
| 271 | +less active fashion. |
| 272 | + |
| 273 | +At the end of the process, the fmt-rfcs repository should be a fairly complete |
| 274 | +guide for formatting Rust code, and useful as a specification for Rustfmt and |
| 275 | +tools with similar goals, such as IDEs. In particular, there should be a |
| 276 | +decision made on how configurable Rustfmt should be, and an agreed set of |
| 277 | +default options. The formatting style guide in the Rust repository should be a |
| 278 | +more human-friendly source of formatting guidelines, and should be in sync with |
| 279 | +the fmt-rfcs repo. |
| 280 | + |
| 281 | + |
| 282 | +# Drawbacks |
| 283 | +[drawbacks]: #drawbacks |
| 284 | + |
| 285 | +This RFC introduces more process and bureaucracy, and requires more meetings for |
| 286 | +some core Rust contributors. Precious time and energy will need to be devoted to |
| 287 | +discussions. |
| 288 | + |
| 289 | + |
| 290 | +# Alternatives |
| 291 | +[alternatives]: #alternatives |
| 292 | + |
| 293 | +Benevolent dictator - a single person dictates style rules which will be |
| 294 | +followed without question by the community. This seems to work for Go, I suspect |
| 295 | +it will not work for Rust. |
| 296 | + |
| 297 | +Parliamentary 'democracy' - the community 'elects' a style team (via the usual |
| 298 | +RFC consensus process, rather than actual voting). The style team decides on |
| 299 | +style issues without an open process. This would be more efficient, but doesn't |
| 300 | +fit very well with the open ethos of the Rust community. |
| 301 | + |
| 302 | +Use the RFCs repo, rather than a new repo. This would have the benefit that |
| 303 | +style RFCs would get more visibility, and it is one less place to keep track of |
| 304 | +for Rust community members. However, it risks overwhelming the RFC repo with |
| 305 | +style debate. |
| 306 | + |
| 307 | +Use issues on Rustfmt. I feel that the discussions would not have enough |
| 308 | +visibility in this fashion, but perhaps that can be addressed by wide and |
| 309 | +regular announcement. |
| 310 | + |
| 311 | +Use a book format for the style repo, rather than a collection of RFCs. This |
| 312 | +would make it easier to see how the 'final product' style guide would look. |
| 313 | +However, I expect there will be many issues that are important to be aware of |
| 314 | +while discussing an RFC, that are not important to include in a final guide. |
| 315 | + |
| 316 | +Have an existing team handle the process, rather than create a new style team. |
| 317 | +Saves on a little bureaucracy. Candidate teams would be language and tools. |
| 318 | +However, the language team has very little free bandwidth, and the tools team is |
| 319 | +probably not broad enough to effectively handle the style decisions. |
| 320 | + |
| 321 | + |
| 322 | +# Unresolved questions |
| 323 | +[unresolved]: #unresolved-questions |
0 commit comments