Skip to content

Commit 05ff92e

Browse files
authored
Merge pull request #2886 from Herrtian/document-unstable-compiler-flags-972
Document adding unstable compiler flags
2 parents e99720b + b85bee8 commit 05ff92e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

src/implementing-new-features.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,48 @@ The below steps needs to be followed in order to implement a new unstable featur
227227
[here]: ./stabilization-guide.md
228228
[tracking issue]: #tracking-issues
229229
[add-feature-gate]: ./feature-gates.md#adding-a-feature-gate
230+
[guidance for compiler flags]: https://forge.rust-lang.org/compiler/proposals-and-stabilization.html?highlight=unstable%20flag#compiler-flags
231+
[`UnstableOptions`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_session/options/struct.UnstableOptions.html
232+
233+
## Adding unstable compiler flags
234+
235+
For compiler-internal experiments, diagnostics, debugging aids, and temporary implementation
236+
controls, a `-Z` flag can be more appropriate than a language feature gate.
237+
Feature gates should still be preferred for user-facing language features,
238+
especially when crates need to opt in from source code.
239+
240+
Before adding a new flag, check the compiler team's [guidance for compiler flags].
241+
Some flags, especially those intended for direct nightly user workflows or broader
242+
ecosystem use, may need additional sign-off beyond an r+ on the implementation PR.
243+
244+
When adding a new `-Z` flag:
245+
246+
1. Add the option to [`UnstableOptions`] in
247+
`compiler/rustc_session/src/options.rs`.
248+
The option name is written as snake_case in the struct and is exposed as
249+
kebab-case on the command line.
250+
Choose the parser, default value, and dependency tracking marker carefully.
251+
Use `[UNTRACKED]` for flags that only affect diagnostics or debugging output,
252+
and use `[TRACKED]` when changing the flag can change compilation results.
253+
254+
1. Update the option tests in `compiler/rustc_interface/src/tests.rs`.
255+
`options.rs` also has a short checklist near the `UnstableOptions` list for
256+
files that need to stay in sync.
257+
258+
1. Use the option through `sess.opts.unstable_opts.$flag_name` or
259+
`tcx.sess.opts.unstable_opts.$flag_name`.
260+
If bootstrap or rustdoc needs to pass the flag internally,
261+
thread it through the relevant command construction code as part of the same
262+
change.
263+
264+
1. Add focused tests for the behavior controlled by the flag.
265+
For example, UI tests can use `//@ compile-flags: -Zyour-flag`,
266+
while rustdoc behavior usually belongs in `tests/rustdoc`.
267+
268+
1. If the flag is meant for users of nightly rustc or rustdoc,
269+
document it in the unstable book under `src/doc/unstable-book/src/compiler-flags`.
270+
271+
Before opening the PR, run `./x test tidy` and the narrow test suite that exercises the new flag.
230272

231273
## Call for testing
232274

0 commit comments

Comments
 (0)