Skip to content

Commit b64b5de

Browse files
committed
doc tests: use the filename from the source file for doc test programs, rather than a dummy name
1 parent 60e6354 commit b64b5de

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

src/librustdoc/html/markdown.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
191191
.map(|l| map_line(l).for_code())
192192
.collect::<Vec<&str>>().join("\n");
193193
let krate = krate.as_ref().map(|s| &**s);
194-
let test = test::maketest(&test, krate, false,
195-
&Default::default());
194+
let test = test::make_test(&test, krate, false,
195+
&Default::default());
196196
let channel = if test.contains("#![feature(") {
197197
"&amp;version=nightly"
198198
} else {
@@ -585,8 +585,8 @@ pub fn render(w: &mut fmt::Formatter,
585585
.map(|l| map_line(l).for_code())
586586
.collect::<Vec<&str>>().join("\n");
587587
let krate = krate.as_ref().map(|s| &**s);
588-
let test = test::maketest(&test, krate, false,
589-
&Default::default());
588+
let test = test::make_test(&test, krate, false,
589+
&Default::default());
590590
let channel = if test.contains("#![feature(") {
591591
"&amp;version=nightly"
592592
} else {

src/librustdoc/test.rs

+25-21
Original file line numberDiff line numberDiff line change
@@ -167,16 +167,16 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
167167
opts
168168
}
169169

170-
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
171-
externs: Externs,
172-
should_panic: bool, no_run: bool, as_test_harness: bool,
173-
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
174-
maybe_sysroot: Option<PathBuf>) {
170+
fn run_test(test: &str, cratename: &str, filename: &str, cfgs: Vec<String>, libs: SearchPaths,
171+
externs: Externs,
172+
should_panic: bool, no_run: bool, as_test_harness: bool,
173+
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
174+
maybe_sysroot: Option<PathBuf>) {
175175
// the test harness wants its own `main` & top level functions, so
176176
// never wrap the test in `fn main() { ... }`
177-
let test = maketest(test, Some(cratename), as_test_harness, opts);
177+
let test = make_test(test, Some(cratename), as_test_harness, opts);
178178
let input = config::Input::Str {
179-
name: driver::anon_src(),
179+
name: filename.to_owned(),
180180
input: test.to_owned(),
181181
};
182182
let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
@@ -313,8 +313,11 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
313313
}
314314
}
315315

316-
pub fn maketest(s: &str, cratename: Option<&str>, dont_insert_main: bool,
317-
opts: &TestOptions) -> String {
316+
pub fn make_test(s: &str,
317+
cratename: Option<&str>,
318+
dont_insert_main: bool,
319+
opts: &TestOptions)
320+
-> String {
318321
let (crate_attrs, everything_else) = partition_source(s);
319322

320323
let mut prog = String::new();
@@ -498,18 +501,19 @@ impl Collector {
498501
rustc_driver::in_rustc_thread(move || {
499502
io::set_panic(panic);
500503
io::set_print(print);
501-
runtest(&test,
502-
&cratename,
503-
cfgs,
504-
libs,
505-
externs,
506-
should_panic,
507-
no_run,
508-
as_test_harness,
509-
compile_fail,
510-
error_codes,
511-
&opts,
512-
maybe_sysroot)
504+
run_test(&test,
505+
&cratename,
506+
&filename,
507+
cfgs,
508+
libs,
509+
externs,
510+
should_panic,
511+
no_run,
512+
as_test_harness,
513+
compile_fail,
514+
error_codes,
515+
&opts,
516+
maybe_sysroot)
513517
})
514518
} {
515519
Ok(()) => (),

src/test/rustdoc/issue-43153.rs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2017 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+
// Test that `include!` in a doc test searches relative to the directory in
12+
// which the test is declared.
13+
14+
// compile-flags:--test
15+
16+
/// ```rust
17+
/// include!("auxiliary/empty.rs");
18+
/// fn main() {}
19+
/// ```
20+
pub struct Foo;

0 commit comments

Comments
 (0)