Skip to content

Commit d91c344

Browse files
committed
Rollup merge of rust-lang#30372 - sanxiyn:rustdoc-cfg, r=alexcrichton
Fix rust-lang#30252.
2 parents b20f427 + eb25721 commit d91c344

File tree

4 files changed

+29
-7
lines changed

4 files changed

+29
-7
lines changed

src/librustdoc/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ pub fn main_args(args: &[String]) -> isize {
261261

262262
match (should_test, markdown_input) {
263263
(true, true) => {
264-
return markdown::test(input, libs, externs, test_args)
264+
return markdown::test(input, cfgs, libs, externs, test_args)
265265
}
266266
(true, false) => {
267267
return test::run(input, cfgs, libs, externs, test_args, crate_name)

src/librustdoc/markdown.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,13 @@ pub fn render(input: &str, mut output: PathBuf, matches: &getopts::Matches,
142142
}
143143

144144
/// Run any tests/code examples in the markdown file `input`.
145-
pub fn test(input: &str, libs: SearchPaths, externs: core::Externs,
145+
pub fn test(input: &str, cfgs: Vec<String>, libs: SearchPaths, externs: core::Externs,
146146
mut test_args: Vec<String>) -> isize {
147147
let input_str = load_or_return!(input, 1, 2);
148148

149149
let mut opts = TestOptions::default();
150150
opts.no_crate_inject = true;
151-
let mut collector = Collector::new(input.to_string(), libs, externs,
151+
let mut collector = Collector::new(input.to_string(), cfgs, libs, externs,
152152
true, opts);
153153
find_testable_code(&input_str, &mut collector);
154154
test_args.insert(0, "rustdoctest".to_string());

src/librustdoc/test.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub fn run(input: &str,
8585
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
8686

8787
let mut cfg = config::build_configuration(&sess);
88-
cfg.extend(config::parse_cfgspecs(cfgs));
88+
cfg.extend(config::parse_cfgspecs(cfgs.clone()));
8989
let krate = driver::phase_1_parse_input(&sess, cfg, &input);
9090
let krate = driver::phase_2_configure_and_expand(&sess, &cstore, krate,
9191
"rustdoc-test", None)
@@ -122,6 +122,7 @@ pub fn run(input: &str,
122122
let (krate, _) = passes::unindent_comments(krate);
123123

124124
let mut collector = Collector::new(krate.name.to_string(),
125+
cfgs,
125126
libs,
126127
externs,
127128
false,
@@ -168,7 +169,7 @@ fn scrape_test_config(krate: &::rustc_front::hir::Crate) -> TestOptions {
168169
return opts;
169170
}
170171

171-
fn runtest(test: &str, cratename: &str, libs: SearchPaths,
172+
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
172173
externs: core::Externs,
173174
should_panic: bool, no_run: bool, as_test_harness: bool,
174175
opts: &TestOptions) {
@@ -239,7 +240,8 @@ fn runtest(test: &str, cratename: &str, libs: SearchPaths,
239240

240241
let outdir = TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir");
241242
let out = Some(outdir.path().to_path_buf());
242-
let cfg = config::build_configuration(&sess);
243+
let mut cfg = config::build_configuration(&sess);
244+
cfg.extend(config::parse_cfgspecs(cfgs));
243245
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
244246
let mut control = driver::CompileController::basic();
245247
if no_run {
@@ -349,6 +351,7 @@ fn partition_source(s: &str) -> (String, String) {
349351
pub struct Collector {
350352
pub tests: Vec<testing::TestDescAndFn>,
351353
names: Vec<String>,
354+
cfgs: Vec<String>,
352355
libs: SearchPaths,
353356
externs: core::Externs,
354357
cnt: usize,
@@ -359,11 +362,12 @@ pub struct Collector {
359362
}
360363

361364
impl Collector {
362-
pub fn new(cratename: String, libs: SearchPaths, externs: core::Externs,
365+
pub fn new(cratename: String, cfgs: Vec<String>, libs: SearchPaths, externs: core::Externs,
363366
use_headers: bool, opts: TestOptions) -> Collector {
364367
Collector {
365368
tests: Vec::new(),
366369
names: Vec::new(),
370+
cfgs: cfgs,
367371
libs: libs,
368372
externs: externs,
369373
cnt: 0,
@@ -384,6 +388,7 @@ impl Collector {
384388
format!("{}_{}", self.names.join("::"), self.cnt)
385389
};
386390
self.cnt += 1;
391+
let cfgs = self.cfgs.clone();
387392
let libs = self.libs.clone();
388393
let externs = self.externs.clone();
389394
let cratename = self.cratename.to_string();
@@ -399,6 +404,7 @@ impl Collector {
399404
testfn: testing::DynTestFn(Box::new(move|| {
400405
runtest(&test,
401406
&cratename,
407+
cfgs,
402408
libs,
403409
externs,
404410
should_panic,

src/test/rustdoc/issue-30252.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags:--test --cfg feature="bar"
12+
13+
/// ```rust
14+
/// assert_eq!(cfg!(feature = "bar"), true);
15+
/// ```
16+
pub fn foo() {}

0 commit comments

Comments
 (0)