diff options
| author | Lukas Markeffsky <@> | 2023-07-21 23:10:07 +0200 |
|---|---|---|
| committer | Lukas Markeffsky <@> | 2023-07-22 12:27:25 +0200 |
| commit | bb98f3ad4d67db68292724d767a4fd41386c8d20 (patch) | |
| tree | 0d40e8285a3817ed7679d115763da74292fad957 | |
| parent | 9ebd8095fad4ab1bc23717139402e6b3a834020a (diff) | |
| download | rust-bb98f3ad4d67db68292724d767a4fd41386c8d20.tar.gz rust-bb98f3ad4d67db68292724d767a4fd41386c8d20.zip | |
fix doc links on `extern crate` items
| -rw-r--r-- | src/librustdoc/clean/mod.rs | 15 | ||||
| -rw-r--r-- | src/librustdoc/clean/utils.rs | 12 | ||||
| -rw-r--r-- | src/librustdoc/html/format.rs | 10 | ||||
| -rw-r--r-- | tests/rustdoc/issue-33178.rs | 9 |
4 files changed, 28 insertions, 18 deletions
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d14953f1bb7..440874df14c 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2643,15 +2643,12 @@ fn clean_extern_crate<'tcx>( } } - // FIXME: using `from_def_id_and_kind` breaks `rustdoc/masked` for some reason - vec![Item { - name: Some(name), - attrs: Box::new(Attributes::from_ast(attrs)), - item_id: crate_def_id.into(), - kind: Box::new(ExternCrateItem { src: orig_name }), - cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg), - inline_stmt_id: Some(krate_owner_def_id), - }] + vec![Item::from_def_id_and_parts( + krate_owner_def_id, + Some(name), + ExternCrateItem { src: orig_name }, + cx, + )] } fn clean_use_statement<'tcx>( diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index df9da9e7c7f..5c8db3b8774 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -38,11 +38,15 @@ pub(crate) fn krate(cx: &mut DocContext<'_>) -> Crate { for it in &module.items { // `compiler_builtins` should be masked too, but we can't apply // `#[doc(masked)]` to the injected `extern crate` because it's unstable. - if it.is_extern_crate() - && (it.attrs.has_doc_flag(sym::masked) - || cx.tcx.is_compiler_builtins(it.item_id.krate())) - { + if cx.tcx.is_compiler_builtins(it.item_id.krate()) { cx.cache.masked_crates.insert(it.item_id.krate()); + } else if it.is_extern_crate() + && it.attrs.has_doc_flag(sym::masked) + && let Some(def_id) = it.item_id.as_def_id() + && let Some(local_def_id) = def_id.as_local() + && let Some(cnum) = cx.tcx.extern_mod_stmt_cnum(local_def_id) + { + cx.cache.masked_crates.insert(cnum); } } } diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 1099c68b004..f60f40267d6 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -18,7 +18,7 @@ use rustc_data_structures::captures::Captures; use rustc_data_structures::fx::FxHashSet; use rustc_hir as hir; use rustc_hir::def::DefKind; -use rustc_hir::def_id::DefId; +use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_metadata::creader::{CStore, LoadedMacro}; use rustc_middle::ty; use rustc_middle::ty::TyCtxt; @@ -662,6 +662,14 @@ pub(crate) fn href_with_root_path( // documented on their parent's page tcx.parent(did) } + DefKind::ExternCrate => { + // Link to the crate itself, not the `extern crate` item. + if let Some(local_did) = did.as_local() { + tcx.extern_mod_stmt_cnum(local_did).unwrap_or(LOCAL_CRATE).as_def_id() + } else { + did + } + } _ => did, }; let cache = cx.cache(); diff --git a/tests/rustdoc/issue-33178.rs b/tests/rustdoc/issue-33178.rs index 1f45fe72391..ed643f5ae11 100644 --- a/tests/rustdoc/issue-33178.rs +++ b/tests/rustdoc/issue-33178.rs @@ -4,10 +4,11 @@ // ignore-cross-compile // @has issue_33178/index.html -// @has - //a/@title empty -// @has - //a/@href ../empty/index.html +// @has - '//a[@title="mod empty"][@href="../empty/index.html"]' empty pub extern crate empty; -// @has - //a/@title variant_struct -// @has - //a/@href ../variant_struct/index.html +// @has - '//a[@title="mod variant_struct"][@href="../variant_struct/index.html"]' variant_struct pub extern crate variant_struct as foo; + +// @has - '//a[@title="mod issue_33178"][@href="index.html"]' self +pub extern crate self as bar; |
