Skip to content

Commit 98643de

Browse files
Fix dangling ID when pub useing item which is Doc(hidden) or inherits it in rustdoc JSON output
1 parent cf8d812 commit 98643de

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

src/librustdoc/clean/types.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use rustc_target::spec::abi::Abi;
3838
use crate::clean::cfg::Cfg;
3939
use crate::clean::external_path;
4040
use crate::clean::inline::{self, print_inlined_const};
41-
use crate::clean::utils::{is_literal_expr, print_evaluated_const};
41+
use crate::clean::utils::{inherits_doc_hidden, is_literal_expr, print_evaluated_const};
4242
use crate::core::DocContext;
4343
use crate::formats::cache::Cache;
4444
use crate::formats::item_type::ItemType;
@@ -2489,6 +2489,15 @@ impl Import {
24892489
pub(crate) fn imported_item_is_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool {
24902490
self.source.did.is_some_and(|did| tcx.is_doc_hidden(did))
24912491
}
2492+
2493+
pub(crate) fn inherits_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool {
2494+
self.imported_item_is_doc_hidden(tcx)
2495+
|| self
2496+
.source
2497+
.did
2498+
.and_then(|did| did.as_local())
2499+
.map_or(false, |did| inherits_doc_hidden(tcx, did, None))
2500+
}
24922501
}
24932502

24942503
#[derive(Clone, Debug)]
@@ -2499,6 +2508,12 @@ pub(crate) enum ImportKind {
24992508
Glob,
25002509
}
25012510

2511+
impl ImportKind {
2512+
pub fn is_glob(&self) -> bool {
2513+
matches!(self, Self::Glob)
2514+
}
2515+
}
2516+
25022517
#[derive(Clone, Debug)]
25032518
pub(crate) struct ImportSource {
25042519
pub(crate) path: Path,

src/librustdoc/json/conversions.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_target::spec::abi::Abi as RustcAbi;
1717

1818
use rustdoc_json_types::*;
1919

20-
use crate::clean::{self, ItemId};
20+
use crate::clean::{self, ImportKind, ItemId};
2121
use crate::formats::item_type::ItemType;
2222
use crate::formats::FormatRenderer;
2323
use crate::json::JsonRenderer;
@@ -761,20 +761,22 @@ impl FromWithTcx<clean::Discriminant> for Discriminant {
761761

762762
impl FromWithTcx<clean::Import> for Import {
763763
fn from_tcx(import: clean::Import, tcx: TyCtxt<'_>) -> Self {
764-
use clean::ImportKind::*;
765-
let (name, glob) = match import.kind {
766-
Simple(s) => (s.to_string(), false),
767-
Glob => (
764+
let (name, glob, id) = match import.kind {
765+
ImportKind::Simple(s) => {
766+
let id = if import.inherits_doc_hidden(tcx) {
767+
None
768+
} else {
769+
import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx))
770+
};
771+
(s.to_string(), false, id)
772+
}
773+
ImportKind::Glob => (
768774
import.source.path.last_opt().unwrap_or_else(|| Symbol::intern("*")).to_string(),
769775
true,
776+
import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)),
770777
),
771778
};
772-
Import {
773-
source: import.source.path.whole_name(),
774-
name,
775-
id: import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)),
776-
glob,
777-
}
779+
Import { source: import.source.path.whole_name(), name, id, glob }
778780
}
779781
}
780782

src/librustdoc/passes/stripper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub(crate) struct ImportStripper<'tcx> {
245245

246246
impl<'tcx> ImportStripper<'tcx> {
247247
fn import_should_be_hidden(&self, i: &Item, imp: &clean::Import) -> bool {
248-
if self.is_json_output {
248+
if self.is_json_output && imp.kind.is_glob() {
249249
// FIXME: This should be handled the same way as for HTML output.
250250
imp.imported_item_is_doc_hidden(self.tcx)
251251
} else {

0 commit comments

Comments
 (0)