about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-08-13 11:03:09 +0200
committerGitHub <noreply@github.com>2017-08-13 11:03:09 +0200
commit7ebd81377db0e24f57be9c4f7aa84d2556931f1a (patch)
tree20f8c61828289792c264a66dbfc9fdaf7c0cf2ba
parentadbce60d6f131e5b3789f01417dedb05e4489898 (diff)
parent6d736df76bb37e019728cea50a4902c23fb09e1b (diff)
downloadrust-7ebd81377db0e24f57be9c4f7aa84d2556931f1a.tar.gz
rust-7ebd81377db0e24f57be9c4f7aa84d2556931f1a.zip
Rollup merge of #43782 - nrc:include, r=GuillaumeGomez
Fix include! in doc tests

By making the path relative to the current file.

Fixes #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.
-rw-r--r--src/librustdoc/html/markdown.rs8
-rw-r--r--src/librustdoc/test.rs46
-rw-r--r--src/libsyntax/ext/source_util.rs11
-rw-r--r--src/test/rustdoc/issue-43153.rs20
4 files changed, 55 insertions, 30 deletions
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 735c9d8af7a..82c252ae4d7 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -190,8 +190,8 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
                     .map(|l| map_line(l).for_code())
                     .collect::<Vec<&str>>().join("\n");
                 let krate = krate.as_ref().map(|s| &**s);
-                let test = test::maketest(&test, krate, false,
-                                        &Default::default());
+                let test = test::make_test(&test, krate, false,
+                                           &Default::default());
                 let channel = if test.contains("#![feature(") {
                     "&amp;version=nightly"
                 } else {
@@ -584,8 +584,8 @@ pub fn render(w: &mut fmt::Formatter,
                         .map(|l| map_line(l).for_code())
                         .collect::<Vec<&str>>().join("\n");
                     let krate = krate.as_ref().map(|s| &**s);
-                    let test = test::maketest(&test, krate, false,
-                                              &Default::default());
+                    let test = test::make_test(&test, krate, false,
+                                               &Default::default());
                     let channel = if test.contains("#![feature(") {
                         "&amp;version=nightly"
                     } else {
diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs
index fff047c99c0..b1fb343b8bb 100644
--- a/src/librustdoc/test.rs
+++ b/src/librustdoc/test.rs
@@ -174,16 +174,16 @@ fn scrape_test_config(krate: &::rustc::hir::Crate) -> TestOptions {
     opts
 }
 
-fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
-           externs: Externs,
-           should_panic: bool, no_run: bool, as_test_harness: bool,
-           compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
-           maybe_sysroot: Option<PathBuf>) {
+fn run_test(test: &str, cratename: &str, filename: &str, cfgs: Vec<String>, libs: SearchPaths,
+            externs: Externs,
+            should_panic: bool, no_run: bool, as_test_harness: bool,
+            compile_fail: bool, mut error_codes: Vec<String>, opts: &TestOptions,
+            maybe_sysroot: Option<PathBuf>) {
     // the test harness wants its own `main` & top level functions, so
     // never wrap the test in `fn main() { ... }`
-    let test = maketest(test, Some(cratename), as_test_harness, opts);
+    let test = make_test(test, Some(cratename), as_test_harness, opts);
     let input = config::Input::Str {
-        name: driver::anon_src(),
+        name: filename.to_owned(),
         input: test.to_owned(),
     };
     let outputs = OutputTypes::new(&[(OutputType::Exe, None)]);
@@ -320,8 +320,11 @@ fn runtest(test: &str, cratename: &str, cfgs: Vec<String>, libs: SearchPaths,
     }
 }
 
-pub fn maketest(s: &str, cratename: Option<&str>, dont_insert_main: bool,
-                opts: &TestOptions) -> String {
+pub fn make_test(s: &str,
+                 cratename: Option<&str>,
+                 dont_insert_main: bool,
+                 opts: &TestOptions)
+                 -> String {
     let (crate_attrs, everything_else) = partition_source(s);
 
     let mut prog = String::new();
@@ -505,18 +508,19 @@ impl Collector {
                     rustc_driver::in_rustc_thread(move || {
                         io::set_panic(panic);
                         io::set_print(print);
-                        runtest(&test,
-                                &cratename,
-                                cfgs,
-                                libs,
-                                externs,
-                                should_panic,
-                                no_run,
-                                as_test_harness,
-                                compile_fail,
-                                error_codes,
-                                &opts,
-                                maybe_sysroot)
+                        run_test(&test,
+                                 &cratename,
+                                 &filename,
+                                 cfgs,
+                                 libs,
+                                 externs,
+                                 should_panic,
+                                 no_run,
+                                 as_test_harness,
+                                 compile_fail,
+                                 error_codes,
+                                 &opts,
+                                 maybe_sysroot)
                     })
                 } {
                     Ok(()) => (),
diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs
index b293aa8de38..95fe41be122 100644
--- a/src/libsyntax/ext/source_util.rs
+++ b/src/libsyntax/ext/source_util.rs
@@ -193,13 +193,14 @@ pub fn expand_include_bytes(cx: &mut ExtCtxt, sp: Span, tts: &[tokenstream::Toke
 // resolve a file-system path to an absolute file-system path (if it
 // isn't already)
 fn res_rel_file(cx: &mut ExtCtxt, sp: syntax_pos::Span, arg: &Path) -> PathBuf {
-    // NB: relative paths are resolved relative to the compilation unit
+    // Relative paths are resolved relative to the file in which they are found
+    // after macro expansion (that is, they are unhygienic).
     if !arg.is_absolute() {
         let callsite = sp.source_callsite();
-        let mut cu = PathBuf::from(&cx.codemap().span_to_filename(callsite));
-        cu.pop();
-        cu.push(arg);
-        cu
+        let mut path = PathBuf::from(&cx.codemap().span_to_filename(callsite));
+        path.pop();
+        path.push(arg);
+        path
     } else {
         arg.to_path_buf()
     }
diff --git a/src/test/rustdoc/issue-43153.rs b/src/test/rustdoc/issue-43153.rs
new file mode 100644
index 00000000000..6ac341d8b02
--- /dev/null
+++ b/src/test/rustdoc/issue-43153.rs
@@ -0,0 +1,20 @@
+// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Test that `include!` in a doc test searches relative to the directory in
+// which the test is declared.
+
+// compile-flags:--test
+
+/// ```rust
+/// include!("auxiliary/empty.rs");
+/// fn main() {}
+/// ```
+pub struct Foo;