Skip to content

Commit 7ebd813

Browse files
Rollup merge of rust-lang#43782 - nrc:include, r=GuillaumeGomez
Fix include! in doc tests By making the path relative to the current file. Fixes rust-lang#43153 [breaking-change] - if you use `include!` inside a doc test, you'll need to change the path to be relative to the current file rather than relative to the working directory.
2 parents adbce60 + 6d736df commit 7ebd813

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

src/librustdoc/html/markdown.rs

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

src/librustdoc/test.rs

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -174,16 +174,16 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
174174
opts
175175
}
176176

177-
fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
178-
externs: Externs,
179-
should_panic: bool, no_run: bool, as_test_harness: bool,
180-
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
181-
maybe_sysroot: Option<PathBuf>) {
177+
fn run_test(test: &str, cratename: &str, filename: &str, cfgs: Vec<String>, libs: SearchPaths,
178+
externs: Externs,
179+
should_panic: bool, no_run: bool, as_test_harness: bool,
180+
compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
181+
maybe_sysroot: Option<PathBuf>) {
182182
// the test harness wants its own `main` & top level functions, so
183183
// never wrap the test in `fn main() { ... }`
184-
let test = maketest(test, Some(cratename), as_test_harness, opts);
184+
let test = make_test(test, Some(cratename), as_test_harness, opts);
185185
let input = config::Input::Str {
186-
name: driver::anon_src(),
186+
name: filename.to_owned(),
187187
input: test.to_owned(),
188188
};
189189
let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
@@ -320,8 +320,11 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
320320
}
321321
}
322322

323-
pub fn maketest(s: &str, cratename: Option<&str>, dont_insert_main: bool,
324-
opts: &TestOptions) -> String {
323+
pub fn make_test(s: &str,
324+
cratename: Option<&str>,
325+
dont_insert_main: bool,
326+
opts: &TestOptions)
327+
-> String {
325328
let (crate_attrs, everything_else) = partition_source(s);
326329

327330
let mut prog = String::new();
@@ -505,18 +508,19 @@ impl Collector {
505508
rustc_driver::in_rustc_thread(move || {
506509
io::set_panic(panic);
507510
io::set_print(print);
508-
runtest(&test,
509-
&cratename,
510-
cfgs,
511-
libs,
512-
externs,
513-
should_panic,
514-
no_run,
515-
as_test_harness,
516-
compile_fail,
517-
error_codes,
518-
&opts,
519-
maybe_sysroot)
511+
run_test(&test,
512+
&cratename,
513+
&filename,
514+
cfgs,
515+
libs,
516+
externs,
517+
should_panic,
518+
no_run,
519+
as_test_harness,
520+
compile_fail,
521+
error_codes,
522+
&opts,
523+
maybe_sysroot)
520524
})
521525
} {
522526
Ok(()) => (),

src/libsyntax/ext/source_util.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,14 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Toke
193193
// resolve a file-system path to an absolute file-system path (if it
194194
// isn't already)
195195
fn res_rel_file(cx: &mut ExtCtxt, sp: syntax_pos::Span, arg: &Path) -> PathBuf {
196-
// NB: relative paths are resolved relative to the compilation unit
196+
// Relative paths are resolved relative to the file in which they are found
197+
// after macro expansion (that is, they are unhygienic).
197198
if !arg.is_absolute() {
198199
let callsite = sp.source_callsite();
199-
let mut cu = PathBuf::from(&cx.codemap().span_to_filename(callsite));
200-
cu.pop();
201-
cu.push(arg);
202-
cu
200+
let mut path = PathBuf::from(&cx.codemap().span_to_filename(callsite));
201+
path.pop();
202+
path.push(arg);
203+
path
203204
} else {
204205
arg.to_path_buf()
205206
}

src/test/rustdoc/issue-43153.rs

Lines changed: 20 additions & 0 deletions
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)