Skip to content

Commit bb98f3a

Browse files
author
Lukas Markeffsky
committed
fix doc links on extern crate items
1 parent 9ebd809 commit bb98f3a

File tree

4 files changed

+28
-18
lines changed

4 files changed

+28
-18
lines changed

src/librustdoc/clean/mod.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -2643,15 +2643,12 @@ fn clean_extern_crate<'tcx>(
26432643
}
26442644
}
26452645

2646-
// FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason
2647-
vec![Item {
2648-
name: Some(name),
2649-
attrs: Box::new(Attributes::from_ast(attrs)),
2650-
item_id: crate_def_id.into(),
2651-
kind: Box::new(ExternCrateItem { src: orig_name }),
2652-
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
2653-
inline_stmt_id: Some(krate_owner_def_id),
2654-
}]
2646+
vec![Item::from_def_id_and_parts(
2647+
krate_owner_def_id,
2648+
Some(name),
2649+
ExternCrateItem { src: orig_name },
2650+
cx,
2651+
)]
26552652
}
26562653

26572654
fn clean_use_statement<'tcx>(

src/librustdoc/clean/utils.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate {
3838
for it in &module.items {
3939
// `compiler_builtins` should be masked too, but we can't apply
4040
// `#[doc(masked)]` to the injected `extern crate` because it's unstable.
41-
if it.is_extern_crate()
42-
&& (it.attrs.has_doc_flag(sym::masked)
43-
|| cx.tcx.is_compiler_builtins(it.item_id.krate()))
44-
{
41+
if cx.tcx.is_compiler_builtins(it.item_id.krate()) {
4542
cx.cache.masked_crates.insert(it.item_id.krate());
43+
} else if it.is_extern_crate()
44+
&& it.attrs.has_doc_flag(sym::masked)
45+
&& let Some(def_id) = it.item_id.as_def_id()
46+
&& let Some(local_def_id) = def_id.as_local()
47+
&& let Some(cnum) = cx.tcx.extern_mod_stmt_cnum(local_def_id)
48+
{
49+
cx.cache.masked_crates.insert(cnum);
4650
}
4751
}
4852
}

src/librustdoc/html/format.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::captures::Captures;
1818
use rustc_data_structures::fx::FxHashSet;
1919
use rustc_hir as hir;
2020
use rustc_hir::def::DefKind;
21-
use rustc_hir::def_id::DefId;
21+
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
2222
use rustc_metadata::creader::{CStore, LoadedMacro};
2323
use rustc_middle::ty;
2424
use rustc_middle::ty::TyCtxt;
@@ -662,6 +662,14 @@ pub(crate) fn href_with_root_path(
662662
// documented on their parent's page
663663
tcx.parent(did)
664664
}
665+
DefKind::ExternCrate => {
666+
// Link to the crate itself, not the `extern crate` item.
667+
if let Some(local_did) = did.as_local() {
668+
tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id()
669+
} else {
670+
did
671+
}
672+
}
665673
_ => did,
666674
};
667675
let cache = cx.cache();

tests/rustdoc/issue-33178.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
// ignore-cross-compile
55

66
// @has issue_33178/index.html
7-
// @has - //a/@title empty
8-
// @has - //a/@href ../empty/index.html
7+
// @has - '//a[@title="mod empty"][@href="../empty/index.html"]' empty
98
pub extern crate empty;
109

11-
// @has - //a/@title variant_struct
12-
// @has - //a/@href ../variant_struct/index.html
10+
// @has - '//a[@title="mod variant_struct"][@href="../variant_struct/index.html"]' variant_struct
1311
pub extern crate variant_struct as foo;
12+
13+
// @has - '//a[@title="mod issue_33178"][@href="index.html"]' self
14+
pub extern crate self as bar;

0 commit comments

Comments
 (0)