Skip to content

Commit 6f96dc2

Browse files
committed
rustdoc: Don't try to load source files from external crates
Local items defined in external macros shouldn't generate rendered source files and should link to the external crate's docs instead.
1 parent 607b858 commit 6f96dc2

File tree

8 files changed

+47
-7
lines changed

8 files changed

+47
-7
lines changed

src/librustdoc/clean/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1937,6 +1937,7 @@ impl Clean<Span> for rustc_span::Span {
19371937
let hi = sm.lookup_char_pos(self.hi());
19381938
Span {
19391939
filename,
1940+
cnum: lo.file.cnum,
19401941
loline: lo.line,
19411942
locol: lo.col.to_usize(),
19421943
hiline: hi.line,

src/librustdoc/clean/types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_ast::util::comments::strip_doc_comment_decoration;
1414
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1515
use rustc_hir as hir;
1616
use rustc_hir::def::Res;
17-
use rustc_hir::def_id::{CrateNum, DefId};
17+
use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
1818
use rustc_hir::lang_items;
1919
use rustc_hir::Mutability;
2020
use rustc_index::vec::IndexVec;
@@ -1357,6 +1357,7 @@ pub enum VariantKind {
13571357
#[derive(Clone, Debug)]
13581358
pub struct Span {
13591359
pub filename: FileName,
1360+
pub cnum: CrateNum,
13601361
pub loline: usize,
13611362
pub locol: usize,
13621363
pub hiline: usize,
@@ -1368,6 +1369,7 @@ impl Span {
13681369
pub fn empty() -> Span {
13691370
Span {
13701371
filename: FileName::Anon(0),
1372+
cnum: LOCAL_CRATE,
13711373
loline: 0,
13721374
locol: 0,
13731375
hiline: 0,

src/librustdoc/html/render.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use rustc_data_structures::flock;
4747
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4848
use rustc_feature::UnstableFeatures;
4949
use rustc_hir as hir;
50-
use rustc_hir::def_id::DefId;
50+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
5151
use rustc_hir::Mutability;
5252
use rustc_middle::middle::privacy::AccessLevels;
5353
use rustc_middle::middle::stability;
@@ -1623,14 +1623,14 @@ impl Context {
16231623
_ => return None,
16241624
};
16251625

1626-
let (krate, path) = if item.def_id.is_local() {
1626+
let (krate, path) = if item.source.cnum == LOCAL_CRATE {
16271627
if let Some(path) = self.shared.local_sources.get(file) {
16281628
(&self.shared.layout.krate, path)
16291629
} else {
16301630
return None;
16311631
}
16321632
} else {
1633-
let (krate, src_root) = match *self.cache.extern_locations.get(&item.def_id.krate)? {
1633+
let (krate, src_root) = match *self.cache.extern_locations.get(&item.source.cnum)? {
16341634
(ref name, ref src, Local) => (name, src),
16351635
(ref name, ref src, Remote(ref s)) => {
16361636
root = s.to_string();

src/librustdoc/html/sources.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::html::format::Buffer;
55
use crate::html::highlight;
66
use crate::html::layout;
77
use crate::html::render::{Error, SharedContext, BASIC_KEYWORDS};
8+
use rustc_hir::def_id::LOCAL_CRATE;
89
use rustc_span::source_map::FileName;
910
use std::ffi::OsStr;
1011
use std::fs;
@@ -37,8 +38,8 @@ impl<'a> DocFolder for SourceCollector<'a> {
3738
if self.scx.include_sources
3839
// skip all synthetic "files"
3940
&& item.source.filename.is_real()
40-
// skip non-local items
41-
&& item.def_id.is_local()
41+
// skip non-local files
42+
&& item.source.cnum == LOCAL_CRATE
4243
{
4344
// If it turns out that we couldn't read this file, then we probably
4445
// can't read any of the files (generating html output from json or
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// compile-flags:--remap-path-prefix={{src-base}}=/does-not-exist
2+
3+
#![doc(html_root_url = "https://example.com/")]
4+
5+
#[macro_export]
6+
macro_rules! make_foo {
7+
() => {
8+
pub struct Foo;
9+
impl Foo {
10+
pub fn new() -> Foo {
11+
Foo
12+
}
13+
}
14+
}
15+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// aux-build:external-macro-src.rs
2+
// ignore-tidy-linelength
3+
4+
#![crate_name = "foo"]
5+
6+
#[macro_use]
7+
extern crate external_macro_src;
8+
9+
// @has foo/index.html '//a[@href="../src/foo/external-macro-src.rs.html#4-15"]' '[src]'
10+
11+
// @has foo/struct.Foo.html
12+
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#8"]' '[src]'
13+
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#9-13"]' '[src]'
14+
// @has - '//a[@href="https://example.com/src/external_macro_src/external-macro-src.rs.html#10-12"]' '[src]'
15+
make_foo!();

src/test/rustdoc/issue-26606.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
extern crate issue_26606_macro;
88

99
// @has issue_26606/constant.FOO.html
10-
// @has - '//a/@href' '../src/issue_26606/auxiliary/issue-26606-macro.rs.html#3'
10+
// @has - '//a/@href' '../src/issue_26606_macro/issue-26606-macro.rs.html#3'
1111
make_item!(FOO);

src/test/rustdoc/thread-local-src.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_name = "foo"]
2+
3+
// @has foo/index.html '//a[@href="../src/foo/thread-local-src.rs.html#1-6"]' '[src]'
4+
5+
// @has foo/constant.FOO.html '//a/@href' 'https://doc.rust-lang.org/nightly/src/std/'
6+
thread_local!(pub static FOO: bool = false);

0 commit comments

Comments
 (0)