]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Don't try to load source files from external crates
authorOliver Middleton <olliemail27@gmail.com>
Sun, 5 Apr 2020 21:37:06 +0000 (22:37 +0100)
committerOliver Middleton <olliemail27@gmail.com>
Wed, 8 Apr 2020 17:35:17 +0000 (18:35 +0100)
Local items defined in external macros shouldn't generate rendered source files and should link to the external crate's docs instead.

src/librustdoc/clean/mod.rs
src/librustdoc/clean/types.rs
src/librustdoc/html/render.rs
src/librustdoc/html/sources.rs
src/test/rustdoc/auxiliary/external-macro-src.rs [new file with mode: 0644]
src/test/rustdoc/external-macro-src.rs [new file with mode: 0644]
src/test/rustdoc/issue-26606.rs
src/test/rustdoc/thread-local-src.rs [new file with mode: 0644]

index e027db8b56c003f8b6302b195ace534670c94561..4ab2f7f6970c8cba94cdcdb2773f931f63f06d48 100644 (file)
@@ -1937,6 +1937,7 @@ fn clean(&self, cx: &DocContext<'_>) -> Span {
         let hi = sm.lookup_char_pos(self.hi());
         Span {
             filename,
+            cnum: lo.file.cnum,
             loline: lo.line,
             locol: lo.col.to_usize(),
             hiline: hi.line,
index 2e5ecacee12dfa5fce9d5a39bf67fdab701fec43..0a682857b18259fee5ff7398681b35c1c4100fff 100644 (file)
@@ -14,7 +14,7 @@
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_hir as hir;
 use rustc_hir::def::Res;
-use rustc_hir::def_id::{CrateNum, DefId};
+use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
 use rustc_hir::lang_items;
 use rustc_hir::Mutability;
 use rustc_index::vec::IndexVec;
@@ -1357,6 +1357,7 @@ pub enum VariantKind {
 #[derive(Clone, Debug)]
 pub struct Span {
     pub filename: FileName,
+    pub cnum: CrateNum,
     pub loline: usize,
     pub locol: usize,
     pub hiline: usize,
@@ -1368,6 +1369,7 @@ impl Span {
     pub fn empty() -> Span {
         Span {
             filename: FileName::Anon(0),
+            cnum: LOCAL_CRATE,
             loline: 0,
             locol: 0,
             hiline: 0,
index f51f47a8d3387702a9b1292edab2a687663e027e..da020b85ed499cc1be7c48dad8a82968c6b15b77 100644 (file)
@@ -47,7 +47,7 @@
 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
 use rustc_feature::UnstableFeatures;
 use rustc_hir as hir;
-use rustc_hir::def_id::DefId;
+use rustc_hir::def_id::{DefId, LOCAL_CRATE};
 use rustc_hir::Mutability;
 use rustc_middle::middle::privacy::AccessLevels;
 use rustc_middle::middle::stability;
@@ -1623,14 +1623,14 @@ fn src_href(&self, item: &clean::Item) -> Option<String> {
             _ => return None,
         };
 
-        let (krate, path) = if item.def_id.is_local() {
+        let (krate, path) = if item.source.cnum == LOCAL_CRATE {
             if let Some(path) = self.shared.local_sources.get(file) {
                 (&self.shared.layout.krate, path)
             } else {
                 return None;
             }
         } else {
-            let (krate, src_root) = match *self.cache.extern_locations.get(&item.def_id.krate)? {
+            let (krate, src_root) = match *self.cache.extern_locations.get(&item.source.cnum)? {
                 (ref name, ref src, Local) => (name, src),
                 (ref name, ref src, Remote(ref s)) => {
                     root = s.to_string();
index 86f46b2d7e15422214dfc8ca029583447eec6ecd..c5f44baced2e47c3e8f6f9e7f1fc6d98d6a3be05 100644 (file)
@@ -5,6 +5,7 @@
 use crate::html::highlight;
 use crate::html::layout;
 use crate::html::render::{Error, SharedContext, BASIC_KEYWORDS};
+use rustc_hir::def_id::LOCAL_CRATE;
 use rustc_span::source_map::FileName;
 use std::ffi::OsStr;
 use std::fs;
@@ -37,8 +38,8 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
         if self.scx.include_sources
             // skip all synthetic "files"
             && item.source.filename.is_real()
-            // skip non-local items
-            && item.def_id.is_local()
+            // skip non-local files
+            && item.source.cnum == LOCAL_CRATE
         {
             // If it turns out that we couldn't read this file, then we probably
             // can't read any of the files (generating html output from json or
diff --git a/src/test/rustdoc/auxiliary/external-macro-src.rs b/src/test/rustdoc/auxiliary/external-macro-src.rs
new file mode 100644 (file)
index 0000000..ce20ca5
--- /dev/null
@@ -0,0 +1,15 @@
+// compile-flags:--remap-path-prefix={{src-base}}=/does-not-exist
+
+#![doc(html_root_url = "https://example.com/")]
+
+#[macro_export]
+macro_rules! make_foo {
+    () => {
+        pub struct Foo;
+        impl Foo {
+            pub fn new() -> Foo {
+                Foo
+            }
+        }
+    }
+}
diff --git a/src/test/rustdoc/external-macro-src.rs b/src/test/rustdoc/external-macro-src.rs
new file mode 100644 (file)
index 0000000..4394415
--- /dev/null
@@ -0,0 +1,15 @@
+// aux-build:external-macro-src.rs
+// ignore-tidy-linelength
+
+#![crate_name = "foo"]
+
+#[macro_use]
+extern crate external_macro_src;
+
+// @has foo/index.html '//a[@href="../src/foo/external-macro-src.rs.html#4-15"]' '[src]'
+
+// @has foo/struct.Foo.html
+// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#8"]' '[src]'
+// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#9-13"]' '[src]'
+// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#10-12"]' '[src]'
+make_foo!();
index d2aa4bbbd12511982843df7cd720c9c4f4d7c9c3..c8e9a63ea9f771e2a648ac8afe2c93ffa406e5ce 100644 (file)
@@ -7,5 +7,5 @@
 extern crate issue_26606_macro;
 
 // @has issue_26606/constant.FOO.html
-// @has - '//a/@href' '../src/issue_26606/auxiliary/issue-26606-macro.rs.html#3'
+// @has - '//a/@href' '../src/issue_26606_macro/issue-26606-macro.rs.html#3'
 make_item!(FOO);
diff --git a/src/test/rustdoc/thread-local-src.rs b/src/test/rustdoc/thread-local-src.rs
new file mode 100644 (file)
index 0000000..022d81a
--- /dev/null
@@ -0,0 +1,6 @@
+#![crate_name = "foo"]
+
+// @has foo/index.html '//a[@href="../src/foo/thread-local-src.rs.html#1-6"]' '[src]'
+
+// @has foo/constant.FOO.html '//a/@href' 'https://doc.rust-lang.org/nightly/src/std/'
+thread_local!(pub static FOO: bool = false);