Skip to content

Commit 19dae7b

Browse files
committed
Auto merge of #85093 - camelid:remove-fake-expect_local, r=GuillaumeGomez
Remove `FakeDefId::expect_local()` This function returned a fake `DefIndex`, with no indication that it was fake, when it was provided with a `FakeDefId::Fake`. Every use of the function uses the returned `DefIndex` in a call to `tcx.local_def_id_to_hir_id()`, which I'm pretty sure would panic if it were given a fake `DefIndex`. I removed the function and replaced all calls to it with a call to `expect_real()` followed by `DefId::expect_local()` (that's a function on the *real* `DefId`).
2 parents d6d0283 + 4b7c8b0 commit 19dae7b

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

src/librustdoc/clean/types.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
1818
use rustc_data_structures::thin_vec::ThinVec;
1919
use rustc_hir as hir;
2020
use rustc_hir::def::{CtorKind, DefKind, Res};
21-
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
21+
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
2222
use rustc_hir::lang_items::LangItem;
2323
use rustc_hir::{BodyId, Mutability};
2424
use rustc_index::vec::IndexVec;
@@ -86,22 +86,7 @@ impl FakeDefId {
8686
}
8787

8888
#[inline]
89-
crate fn as_local(self) -> Option<LocalDefId> {
90-
match self {
91-
FakeDefId::Real(id) => id.as_local(),
92-
FakeDefId::Fake(idx, krate) => {
93-
(krate == LOCAL_CRATE).then(|| LocalDefId { local_def_index: idx })
94-
}
95-
}
96-
}
97-
98-
#[inline]
99-
crate fn expect_local(self) -> LocalDefId {
100-
self.as_local()
101-
.unwrap_or_else(|| panic!("FakeDefId::expect_local: `{:?}` isn't local", self))
102-
}
103-
104-
#[inline]
89+
#[track_caller]
10590
crate fn expect_real(self) -> rustc_hir::def_id::DefId {
10691
self.as_real().unwrap_or_else(|| panic!("FakeDefId::expect_real: `{:?}` isn't real", self))
10792
}

src/librustdoc/passes/calculate_doc_coverage.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,13 @@ impl<'a, 'b> fold::DocFolder for CoverageCalculator<'a, 'b> {
213213

214214
let filename = i.span(self.ctx.tcx).filename(self.ctx.sess());
215215
let has_doc_example = tests.found_tests != 0;
216-
let hir_id = self.ctx.tcx.hir().local_def_id_to_hir_id(i.def_id.expect_local());
216+
// The `expect_real()` should be okay because `local_def_id_to_hir_id`
217+
// would presumably panic if a fake `DefIndex` were passed.
218+
let hir_id = self
219+
.ctx
220+
.tcx
221+
.hir()
222+
.local_def_id_to_hir_id(i.def_id.expect_real().expect_local());
217223
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
218224
// `missing_docs` is allow-by-default, so don't treat this as ignoring the item
219225
// unless the user had an explicit `allow`

src/librustdoc/passes/collect_intra_doc_links.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,11 @@ impl LinkCollector<'_, '_> {
12071207
// item can be non-local e.g. when using #[doc(primitive = "pointer")]
12081208
if let Some((src_id, dst_id)) = id
12091209
.as_local()
1210-
.and_then(|dst_id| item.def_id.as_local().map(|src_id| (src_id, dst_id)))
1210+
// The `expect_real()` should be okay because `local_def_id_to_hir_id`
1211+
// would presumably panic if a fake `DefIndex` were passed.
1212+
.and_then(|dst_id| {
1213+
item.def_id.expect_real().as_local().map(|src_id| (src_id, dst_id))
1214+
})
12111215
{
12121216
use rustc_hir::def_id::LOCAL_CRATE;
12131217

src/librustdoc/passes/doc_test_lints.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ crate fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -> boo
7171
{
7272
return false;
7373
}
74-
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_local());
74+
// The `expect_real()` should be okay because `local_def_id_to_hir_id`
75+
// would presumably panic if a fake `DefIndex` were passed.
76+
let hir_id = cx.tcx.hir().local_def_id_to_hir_id(item.def_id.expect_real().expect_local());
7577
if cx.tcx.hir().attrs(hir_id).lists(sym::doc).has_word(sym::hidden)
7678
|| inherits_doc_hidden(cx.tcx, hir_id)
7779
{

0 commit comments

Comments
 (0)