Skip to content

Commit 09fe75b

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

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/librustdoc/clean/types.rs

+16-1
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;
@@ -2445,6 +2445,15 @@ impl Import {
24452445
pub(crate) fn imported_item_is_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool {
24462446
self.source.did.map_or(false, |did| tcx.is_doc_hidden(did))
24472447
}
2448+
2449+
pub(crate) fn inherits_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool {
2450+
self.imported_item_is_doc_hidden(tcx)
2451+
|| self
2452+
.source
2453+
.did
2454+
.and_then(|did| did.as_local())
2455+
.map_or(false, |did| inherits_doc_hidden(tcx, did, None))
2456+
}
24482457
}
24492458

24502459
#[derive(Clone, Debug)]
@@ -2455,6 +2464,12 @@ pub(crate) enum ImportKind {
24552464
Glob,
24562465
}
24572466

2467+
impl ImportKind {
2468+
pub fn is_glob(&self) -> bool {
2469+
matches!(self, Self::Glob)
2470+
}
2471+
}
2472+
24582473
#[derive(Clone, Debug)]
24592474
pub(crate) struct ImportSource {
24602475
pub(crate) path: Path,

src/librustdoc/json/conversions.rs

+15-11
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;
@@ -760,20 +760,24 @@ impl FromWithTcx<clean::Discriminant> for Discriminant {
760760

761761
impl FromWithTcx<clean::Import> for Import {
762762
fn from_tcx(import: clean::Import, tcx: TyCtxt<'_>) -> Self {
763-
use clean::ImportKind::*;
764-
let (name, glob) = match import.kind {
765-
Simple(s) => (s.to_string(), false),
766-
Glob => (
763+
let (name, glob, id) = match import.kind {
764+
ImportKind::Simple(s) => {
765+
let is_from_doc_hidden =
766+
import.imported_item_is_doc_hidden(tcx) || import.inherits_doc_hidden(tcx);
767+
let id = if is_from_doc_hidden {
768+
None
769+
} else {
770+
import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx))
771+
};
772+
(s.to_string(), false, id)
773+
}
774+
ImportKind::Glob => (
767775
import.source.path.last_opt().unwrap_or_else(|| Symbol::intern("*")).to_string(),
768776
true,
777+
import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)),
769778
),
770779
};
771-
Import {
772-
source: import.source.path.whole_name(),
773-
name,
774-
id: import.source.did.map(ItemId::from).map(|i| id_from_item_default(i, tcx)),
775-
glob,
776-
}
780+
Import { source: import.source.path.whole_name(), name, id, glob }
777781
}
778782
}
779783

src/librustdoc/passes/stripper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ pub(crate) struct ImportStripper<'tcx> {
244244

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

0 commit comments

Comments
 (0)