diff --git a/src/librustdoc/config.rs b/src/librustdoc/config.rs index 19ea781430041..fe4e2bd091519 100644 --- a/src/librustdoc/config.rs +++ b/src/librustdoc/config.rs @@ -344,7 +344,6 @@ impl Options { .map(|s| PathBuf::from(&s)) .unwrap_or_else(|| PathBuf::from("doc")); let mut cfgs = matches.opt_strs("cfg"); - cfgs.push("rustdoc".to_string()); if should_test { cfgs.push("doctest".to_string()); } diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index 010e4cf6cd0d5..00265caa965ea 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -234,7 +234,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt error_format, libs, externs, - cfgs, + mut cfgs, codegen_options, debugging_options, target, @@ -250,6 +250,9 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt .. } = options; + // Add the rustdoc cfg into the doc build. + cfgs.push("rustdoc".to_string()); + let cpath = Some(input.clone()); let input = Input::File(input); diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 6576165b6ce74..3e77ca47e8a6a 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -62,9 +62,11 @@ pub fn run(options: Options) -> i32 { ..config::Options::default() }; + let mut cfgs = options.cfgs.clone(); + cfgs.push("rustdoc".to_owned()); let config = interface::Config { opts: sessopts, - crate_cfg: config::parse_cfgspecs(options.cfgs.clone()), + crate_cfg: config::parse_cfgspecs(cfgs), input, input_path: None, output_file: None, diff --git a/src/test/rustdoc-ui/doc-test-rustdoc-feature.rs b/src/test/rustdoc-ui/doc-test-rustdoc-feature.rs new file mode 100644 index 0000000000000..d0ead4136575f --- /dev/null +++ b/src/test/rustdoc-ui/doc-test-rustdoc-feature.rs @@ -0,0 +1,14 @@ +// build-pass +// compile-flags:--test +// normalize-stdout-test: "src/test/rustdoc-ui" -> "$$DIR" + +#![feature(doc_cfg)] + +// Make sure `cfg(rustdoc)` is set when finding doctests but not inside the doctests. + +/// ``` +/// #![feature(doc_cfg)] +/// assert!(!cfg!(rustdoc)); +/// ``` +#[cfg(rustdoc)] +pub struct Foo; diff --git a/src/test/rustdoc-ui/doc-test-rustdoc-feature.stdout b/src/test/rustdoc-ui/doc-test-rustdoc-feature.stdout new file mode 100644 index 0000000000000..f2525c2dbec21 --- /dev/null +++ b/src/test/rustdoc-ui/doc-test-rustdoc-feature.stdout @@ -0,0 +1,6 @@ + +running 1 test +test $DIR/doc-test-rustdoc-feature.rs - Foo (line 9) ... ok + +test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out +