Skip to content

Commit a85853a

Browse files
authored
Rollup merge of rust-lang#94154 - Urgau:rustdoc-check-cfg, r=GuillaumeGomez
Wire up unstable rustc --check-cfg to rustdoc This pull-request wire up the new unstable `--check-cfg` option from `rustc` to `rustdoc` as [requested](rust-lang#93915 (comment)) in the [pull-request](rust-lang#93915) that introduce `--check-cfg`. The motivation was describe in the original PR by `@jyn514` who wrote rust-lang#89346 (comment): > > add plumbing to pass --check-cfg from rustdoc (do we want this one?) > > It would be useful, I think, it catches issues like cfg(doctst) or something (and in general I would like expansion to match rustc as closely as possible).
2 parents b889365 + a31ae15 commit a85853a

12 files changed

+80
-2
lines changed

src/doc/rustdoc/src/unstable-features.md

+14
Original file line numberDiff line numberDiff line change
@@ -512,3 +512,17 @@ crate being documented (`foobar`) and a path to output the calls
512512

513513
To scrape examples from test code, e.g. functions marked `#[test]`, then
514514
add the `--scrape-tests` flag.
515+
516+
### `--check-cfg`: check configuration flags
517+
518+
This flag accepts the same values as `rustc --check-cfg`, and uses it to check configuration flags.
519+
520+
Using this flag looks like this:
521+
522+
```bash
523+
$ rustdoc src/lib.rs -Z unstable-options \
524+
--check-cfg='names()' --check-cfg='values(feature, "foo", "bar")'
525+
```
526+
527+
The example above check every well known names (`target_os`, `doc`, `test`, ... via `names()`)
528+
and check the values of `feature`: `foo` and `bar`.

src/librustdoc/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ crate struct Options {
8080
crate extern_strs: Vec<String>,
8181
/// List of `cfg` flags to hand to the compiler. Always includes `rustdoc`.
8282
crate cfgs: Vec<String>,
83+
/// List of check cfg flags to hand to the compiler.
84+
crate check_cfgs: Vec<String>,
8385
/// Codegen options to hand to the compiler.
8486
crate codegen_options: CodegenOptions,
8587
/// Codegen options strings to hand to the compiler.
@@ -172,6 +174,7 @@ impl fmt::Debug for Options {
172174
.field("libs", &self.libs)
173175
.field("externs", &FmtExterns(&self.externs))
174176
.field("cfgs", &self.cfgs)
177+
.field("check-cfgs", &self.check_cfgs)
175178
.field("codegen_options", &"...")
176179
.field("debugging_options", &"...")
177180
.field("target", &self.target)
@@ -506,6 +509,7 @@ impl Options {
506509
};
507510

508511
let cfgs = matches.opt_strs("cfg");
512+
let check_cfgs = matches.opt_strs("check-cfg");
509513

510514
let extension_css = matches.opt_str("e").map(|s| PathBuf::from(&s));
511515

@@ -677,6 +681,7 @@ impl Options {
677681
externs,
678682
extern_strs,
679683
cfgs,
684+
check_cfgs,
680685
codegen_options,
681686
codegen_options_strs,
682687
debugging_opts,

src/librustdoc/core.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ crate fn create_config(
192192
libs,
193193
externs,
194194
mut cfgs,
195+
check_cfgs,
195196
codegen_options,
196197
debugging_opts,
197198
target,
@@ -219,6 +220,7 @@ crate fn create_config(
219220
// these are definitely not part of rustdoc, but we want to warn on them anyway.
220221
rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(),
221222
rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(),
223+
rustc_lint::builtin::UNEXPECTED_CFGS.name.to_string(),
222224
];
223225
lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));
224226

@@ -253,7 +255,7 @@ crate fn create_config(
253255
interface::Config {
254256
opts: sessopts,
255257
crate_cfg: interface::parse_cfgspecs(cfgs),
256-
crate_check_cfg: interface::parse_check_cfg(vec![]),
258+
crate_check_cfg: interface::parse_check_cfg(check_cfgs),
257259
input,
258260
input_path: cpath,
259261
output_file: None,

src/librustdoc/doctest.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ crate fn run(options: RustdocOptions) -> Result<(), ErrorReported> {
9191
let config = interface::Config {
9292
opts: sessopts,
9393
crate_cfg: interface::parse_cfgspecs(cfgs),
94-
crate_check_cfg: interface::parse_check_cfg(vec![]),
94+
crate_check_cfg: interface::parse_check_cfg(options.check_cfgs.clone()),
9595
input,
9696
input_path: None,
9797
output_file: None,
@@ -321,6 +321,12 @@ fn run_test(
321321
for cfg in &rustdoc_options.cfgs {
322322
compiler.arg("--cfg").arg(&cfg);
323323
}
324+
if !rustdoc_options.check_cfgs.is_empty() {
325+
compiler.arg("-Z").arg("unstable-options");
326+
for check_cfg in &rustdoc_options.check_cfgs {
327+
compiler.arg("--check-cfg").arg(&check_cfg);
328+
}
329+
}
324330
if let Some(sysroot) = rustdoc_options.maybe_sysroot {
325331
compiler.arg("--sysroot").arg(sysroot);
326332
}

src/librustdoc/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ fn opts() -> Vec<RustcOptGroup> {
259259
o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
260260
}),
261261
stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
262+
unstable("check-cfg", |o| o.optmulti("", "check-cfg", "pass a --check-cfg to rustc", "")),
262263
stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
263264
unstable("extern-html-root-url", |o| {
264265
o.optmulti(

src/test/rustdoc-ui/check-cfg-test.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// check-pass
2+
// compile-flags: --test --nocapture --check-cfg=values(feature,"test") -Z unstable-options
3+
// normalize-stderr-test: "src/test/rustdoc-ui" -> "$$DIR"
4+
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
5+
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"
6+
7+
/// The doctest will produce a warning because feature invalid is unexpected
8+
/// ```
9+
/// #[cfg(feature = "invalid")]
10+
/// assert!(false);
11+
/// ```
12+
pub struct Foo;
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
warning: unexpected `cfg` condition value
2+
--> $DIR/check-cfg-test.rs:9:7
3+
|
4+
LL | #[cfg(feature = "invalid")]
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(unexpected_cfgs)]` on by default
8+
= note: expected values for `feature` are: test
9+
10+
warning: 1 warning emitted
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
running 1 test
3+
test $DIR/check-cfg-test.rs - Foo (line 8) ... ok
4+
5+
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME
6+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// check-fail
2+
// compile-flags: --check-cfg=names()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
error: the `-Z unstable-options` flag must also be passed to enable the flag `check-cfg`
2+

src/test/rustdoc-ui/check-cfg.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
// compile-flags: --check-cfg=names() -Z unstable-options
3+
4+
/// uniz is nor a builtin nor pass as arguments so is unexpected
5+
#[cfg(uniz)]
6+
//~^ WARNING unexpected `cfg` condition name
7+
pub struct Bar;

src/test/rustdoc-ui/check-cfg.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
warning: unexpected `cfg` condition name
2+
--> $DIR/check-cfg.rs:5:7
3+
|
4+
LL | #[cfg(uniz)]
5+
| ^^^^ help: did you mean: `unix`
6+
|
7+
= note: `#[warn(unexpected_cfgs)]` on by default
8+
9+
warning: 1 warning emitted
10+

0 commit comments

Comments
 (0)