Skip to content

Commit

Permalink
Rollup merge of rust-lang#94154 - Urgau:rustdoc-check-cfg, r=Guillaum…
Browse files Browse the repository at this point in the history
…eGomez

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).
  • Loading branch information
matthiaskrgr authored Feb 25, 2022
2 parents b889365 + a31ae15 commit a85853a
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 2 deletions.
14 changes: 14 additions & 0 deletions src/doc/rustdoc/src/unstable-features.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,17 @@ crate being documented (`foobar`) and a path to output the calls

To scrape examples from test code, e.g. functions marked `#[test]`, then
add the `--scrape-tests` flag.

### `--check-cfg`: check configuration flags

This flag accepts the same values as `rustc --check-cfg`, and uses it to check configuration flags.

Using this flag looks like this:

```bash
$ rustdoc src/lib.rs -Z unstable-options \
--check-cfg='names()' --check-cfg='values(feature, "foo", "bar")'
```

The example above check every well known names (`target_os`, `doc`, `test`, ... via `names()`)
and check the values of `feature`: `foo` and `bar`.
5 changes: 5 additions & 0 deletions src/librustdoc/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ crate struct Options {
crate extern_strs: Vec<String>,
/// List of `cfg` flags to hand to the compiler. Always includes `rustdoc`.
crate cfgs: Vec<String>,
/// List of check cfg flags to hand to the compiler.
crate check_cfgs: Vec<String>,
/// Codegen options to hand to the compiler.
crate codegen_options: CodegenOptions,
/// Codegen options strings to hand to the compiler.
Expand Down Expand Up @@ -172,6 +174,7 @@ impl fmt::Debug for Options {
.field("libs", &self.libs)
.field("externs", &FmtExterns(&self.externs))
.field("cfgs", &self.cfgs)
.field("check-cfgs", &self.check_cfgs)
.field("codegen_options", &"...")
.field("debugging_options", &"...")
.field("target", &self.target)
Expand Down Expand Up @@ -506,6 +509,7 @@ impl Options {
};

let cfgs = matches.opt_strs("cfg");
let check_cfgs = matches.opt_strs("check-cfg");

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

Expand Down Expand Up @@ -677,6 +681,7 @@ impl Options {
externs,
extern_strs,
cfgs,
check_cfgs,
codegen_options,
codegen_options_strs,
debugging_opts,
Expand Down
4 changes: 3 additions & 1 deletion src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ crate fn create_config(
libs,
externs,
mut cfgs,
check_cfgs,
codegen_options,
debugging_opts,
target,
Expand Down Expand Up @@ -219,6 +220,7 @@ crate fn create_config(
// these are definitely not part of rustdoc, but we want to warn on them anyway.
rustc_lint::builtin::RENAMED_AND_REMOVED_LINTS.name.to_string(),
rustc_lint::builtin::UNKNOWN_LINTS.name.to_string(),
rustc_lint::builtin::UNEXPECTED_CFGS.name.to_string(),
];
lints_to_show.extend(crate::lint::RUSTDOC_LINTS.iter().map(|lint| lint.name.to_string()));

Expand Down Expand Up @@ -253,7 +255,7 @@ crate fn create_config(
interface::Config {
opts: sessopts,
crate_cfg: interface::parse_cfgspecs(cfgs),
crate_check_cfg: interface::parse_check_cfg(vec![]),
crate_check_cfg: interface::parse_check_cfg(check_cfgs),
input,
input_path: cpath,
output_file: None,
Expand Down
8 changes: 7 additions & 1 deletion src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ crate fn run(options: RustdocOptions) -> Result<(), ErrorReported> {
let config = interface::Config {
opts: sessopts,
crate_cfg: interface::parse_cfgspecs(cfgs),
crate_check_cfg: interface::parse_check_cfg(vec![]),
crate_check_cfg: interface::parse_check_cfg(options.check_cfgs.clone()),
input,
input_path: None,
output_file: None,
Expand Down Expand Up @@ -321,6 +321,12 @@ fn run_test(
for cfg in &rustdoc_options.cfgs {
compiler.arg("--cfg").arg(&cfg);
}
if !rustdoc_options.check_cfgs.is_empty() {
compiler.arg("-Z").arg("unstable-options");
for check_cfg in &rustdoc_options.check_cfgs {
compiler.arg("--check-cfg").arg(&check_cfg);
}
}
if let Some(sysroot) = rustdoc_options.maybe_sysroot {
compiler.arg("--sysroot").arg(sysroot);
}
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ fn opts() -> Vec<RustcOptGroup> {
o.optmulti("L", "library-path", "directory to add to crate search path", "DIR")
}),
stable("cfg", |o| o.optmulti("", "cfg", "pass a --cfg to rustc", "")),
unstable("check-cfg", |o| o.optmulti("", "check-cfg", "pass a --check-cfg to rustc", "")),
stable("extern", |o| o.optmulti("", "extern", "pass an --extern to rustc", "NAME[=PATH]")),
unstable("extern-html-root-url", |o| {
o.optmulti(
Expand Down
12 changes: 12 additions & 0 deletions src/test/rustdoc-ui/check-cfg-test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// check-pass
// compile-flags: --test --nocapture --check-cfg=values(feature,"test") -Z unstable-options
// normalize-stderr-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR"
// normalize-stdout-test "finished in \d+\.\d+s" -> "finished in $$TIME"

/// The doctest will produce a warning because feature invalid is unexpected
/// ```
/// #[cfg(feature = "invalid")]
/// assert!(false);
/// ```
pub struct Foo;
11 changes: 11 additions & 0 deletions src/test/rustdoc-ui/check-cfg-test.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
warning: unexpected `cfg` condition value
--> $DIR/check-cfg-test.rs:9:7
|
LL | #[cfg(feature = "invalid")]
| ^^^^^^^^^^^^^^^^^^^
|
= note: `#[warn(unexpected_cfgs)]` on by default
= note: expected values for `feature` are: test

warning: 1 warning emitted

6 changes: 6 additions & 0 deletions src/test/rustdoc-ui/check-cfg-test.stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

running 1 test
test $DIR/check-cfg-test.rs - Foo (line 8) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in $TIME

2 changes: 2 additions & 0 deletions src/test/rustdoc-ui/check-cfg-unstable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// check-fail
// compile-flags: --check-cfg=names()
2 changes: 2 additions & 0 deletions src/test/rustdoc-ui/check-cfg-unstable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
error: the `-Z unstable-options` flag must also be passed to enable the flag `check-cfg`

7 changes: 7 additions & 0 deletions src/test/rustdoc-ui/check-cfg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// check-pass
// compile-flags: --check-cfg=names() -Z unstable-options

/// uniz is nor a builtin nor pass as arguments so is unexpected
#[cfg(uniz)]
//~^ WARNING unexpected `cfg` condition name
pub struct Bar;
10 changes: 10 additions & 0 deletions src/test/rustdoc-ui/check-cfg.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
warning: unexpected `cfg` condition name
--> $DIR/check-cfg.rs:5:7
|
LL | #[cfg(uniz)]
| ^^^^ help: did you mean: `unix`
|
= note: `#[warn(unexpected_cfgs)]` on by default

warning: 1 warning emitted

0 comments on commit a85853a

Please sign in to comment.