Skip to content

Commit acd4dc2

Browse files
committed
rustdoc: Rename expect_real to expect_def_id, remove Item::is_fake
1 parent 43e1cdb commit acd4dc2

13 files changed

+103
-105
lines changed

src/librustdoc/clean/types.rs

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,22 @@ impl ItemId {
6666
#[inline]
6767
crate fn is_local(self) -> bool {
6868
match self {
69-
ItemId::DefId(id) => id.is_local(),
70-
_ => false,
69+
ItemId::Auto { for_: id, .. }
70+
| ItemId::Blanket { for_: id, .. }
71+
| ItemId::DefId(id) => id.is_local(),
72+
ItemId::Primitive(krate) => krate == LOCAL_CRATE,
7173
}
7274
}
7375

7476
#[inline]
7577
#[track_caller]
76-
crate fn expect_real(self) -> rustc_hir::def_id::DefId {
77-
self.as_real()
78-
.unwrap_or_else(|| panic!("ItemId::expect_real: `{:?}` isn't a real ItemId", self))
78+
crate fn expect_def_id(self) -> DefId {
79+
self.as_def_id()
80+
.unwrap_or_else(|| panic!("ItemId::expect_def_id: `{:?}` isn't a DefId", self))
7981
}
8082

8183
#[inline]
82-
crate fn as_real(self) -> Option<DefId> {
84+
crate fn as_def_id(self) -> Option<DefId> {
8385
match self {
8486
ItemId::DefId(id) => Some(id),
8587
_ => None,
@@ -89,9 +91,9 @@ impl ItemId {
8991
#[inline]
9092
crate fn krate(self) -> CrateNum {
9193
match self {
92-
ItemId::DefId(id) => id.krate,
93-
ItemId::Auto { trait_, .. } => trait_.krate,
94-
ItemId::Blanket { trait_, .. } => trait_.krate,
94+
ItemId::Auto { for_: id, .. }
95+
| ItemId::Blanket { for_: id, .. }
96+
| ItemId::DefId(id) => id.krate,
9597
ItemId::Primitive(krate) => krate,
9698
}
9799
}
@@ -100,9 +102,7 @@ impl ItemId {
100102
crate fn index(self) -> Option<DefIndex> {
101103
match self {
102104
ItemId::DefId(id) => Some(id.index),
103-
ItemId::Auto { trait_, .. } => Some(trait_.index),
104-
ItemId::Blanket { trait_, .. } => Some(trait_.index),
105-
ItemId::Primitive(..) => None,
105+
_ => None,
106106
}
107107
}
108108
}
@@ -354,19 +354,19 @@ crate fn rustc_span(def_id: DefId, tcx: TyCtxt<'_>) -> Span {
354354

355355
impl Item {
356356
crate fn stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx Stability> {
357-
if self.is_fake() { None } else { tcx.lookup_stability(self.def_id.expect_real()) }
357+
self.def_id.as_def_id().and_then(|did| tcx.lookup_stability(did))
358358
}
359359

360360
crate fn const_stability<'tcx>(&self, tcx: TyCtxt<'tcx>) -> Option<&'tcx ConstStability> {
361-
if self.is_fake() { None } else { tcx.lookup_const_stability(self.def_id.expect_real()) }
361+
self.def_id.as_def_id().and_then(|did| tcx.lookup_const_stability(did))
362362
}
363363

364364
crate fn deprecation(&self, tcx: TyCtxt<'_>) -> Option<Deprecation> {
365-
if self.is_fake() { None } else { tcx.lookup_deprecation(self.def_id.expect_real()) }
365+
self.def_id.as_def_id().and_then(|did| tcx.lookup_deprecation(did))
366366
}
367367

368368
crate fn inner_docs(&self, tcx: TyCtxt<'_>) -> bool {
369-
if self.is_fake() { false } else { tcx.get_attrs(self.def_id.expect_real()).inner_docs() }
369+
self.def_id.as_def_id().map(|did| tcx.get_attrs(did).inner_docs()).unwrap_or(false)
370370
}
371371

372372
crate fn span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -378,10 +378,8 @@ impl Item {
378378
kind
379379
{
380380
*span
381-
} else if self.is_fake() {
382-
Span::dummy()
383381
} else {
384-
rustc_span(self.def_id.expect_real(), tcx)
382+
self.def_id.as_def_id().map(|did| rustc_span(did, tcx)).unwrap_or_else(|| Span::dummy())
385383
}
386384
}
387385

@@ -546,7 +544,7 @@ impl Item {
546544
}
547545

548546
crate fn is_crate(&self) -> bool {
549-
self.is_mod() && self.def_id.as_real().map_or(false, |did| did.index == CRATE_DEF_INDEX)
547+
self.is_mod() && self.def_id.as_def_id().map_or(false, |did| did.index == CRATE_DEF_INDEX)
550548
}
551549
crate fn is_mod(&self) -> bool {
552550
self.type_() == ItemType::Module
@@ -657,11 +655,6 @@ impl Item {
657655
_ => false,
658656
}
659657
}
660-
661-
crate fn is_fake(&self) -> bool {
662-
// FIXME: Find a better way to handle this
663-
!matches!(self.def_id, ItemId::DefId(..))
664-
}
665658
}
666659

667660
#[derive(Clone, Debug)]

src/librustdoc/formats/cache.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
215215
// Propagate a trait method's documentation to all implementors of the
216216
// trait.
217217
if let clean::TraitItem(ref t) = *item.kind {
218-
self.cache.traits.entry(item.def_id.expect_real()).or_insert_with(|| {
218+
self.cache.traits.entry(item.def_id.expect_def_id()).or_insert_with(|| {
219219
clean::TraitWithExtraInfo {
220220
trait_: t.clone(),
221221
is_notable: item.attrs.has_doc_flag(sym::notable_trait),
@@ -348,11 +348,11 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
348348
// `public_items` map, so we can skip inserting into the
349349
// paths map if there was already an entry present and we're
350350
// not a public item.
351-
if !self.cache.paths.contains_key(&item.def_id.expect_real())
352-
|| self.cache.access_levels.is_public(item.def_id.expect_real())
351+
if !self.cache.paths.contains_key(&item.def_id.expect_def_id())
352+
|| self.cache.access_levels.is_public(item.def_id.expect_def_id())
353353
{
354354
self.cache.paths.insert(
355-
item.def_id.expect_real(),
355+
item.def_id.expect_def_id(),
356356
(self.cache.stack.clone(), item.type_()),
357357
);
358358
}
@@ -361,7 +361,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
361361
clean::PrimitiveItem(..) => {
362362
self.cache
363363
.paths
364-
.insert(item.def_id.expect_real(), (self.cache.stack.clone(), item.type_()));
364+
.insert(item.def_id.expect_def_id(), (self.cache.stack.clone(), item.type_()));
365365
}
366366

367367
clean::ExternCrateItem { .. }
@@ -391,7 +391,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
391391
| clean::StructItem(..)
392392
| clean::UnionItem(..)
393393
| clean::VariantItem(..) => {
394-
self.cache.parent_stack.push(item.def_id.expect_real());
394+
self.cache.parent_stack.push(item.def_id.expect_def_id());
395395
self.cache.parent_is_trait_impl = false;
396396
true
397397
}

src/librustdoc/html/format.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,7 @@ impl clean::Visibility {
11911191
// FIXME(camelid): This may not work correctly if `item_did` is a module.
11921192
// However, rustdoc currently never displays a module's
11931193
// visibility, so it shouldn't matter.
1194-
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_real());
1194+
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
11951195

11961196
if vis_did.index == CRATE_DEF_INDEX {
11971197
"pub(crate) ".to_owned()

src/librustdoc/html/render/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'tcx> Context<'tcx> {
230230
&self.shared.style_files,
231231
)
232232
} else {
233-
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_real()) {
233+
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_def_id()) {
234234
let mut path = String::new();
235235
for name in &names[..names.len() - 1] {
236236
path.push_str(name);

src/librustdoc/html/render/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -735,7 +735,7 @@ fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>)
735735
AssocItemLink::Anchor(Some(ref id)) => format!("#{}", id),
736736
AssocItemLink::Anchor(None) => anchor,
737737
AssocItemLink::GotoSource(did, _) => {
738-
href(did.expect_real(), cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
738+
href(did.expect_def_id(), cx).map(|p| format!("{}{}", p.0, anchor)).unwrap_or(anchor)
739739
}
740740
}
741741
}
@@ -867,7 +867,7 @@ fn render_assoc_item(
867867
ItemType::TyMethod
868868
};
869869

870-
href(did.expect_real(), cx)
870+
href(did.expect_def_id(), cx)
871871
.map(|p| format!("{}#{}.{}", p.0, ty, name))
872872
.unwrap_or_else(|| format!("#{}.{}", ty, name))
873873
}
@@ -1819,7 +1819,7 @@ fn small_url_encode(s: String) -> String {
18191819
}
18201820

18211821
fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
1822-
let did = it.def_id.expect_real();
1822+
let did = it.def_id.expect_def_id();
18231823
if let Some(v) = cx.cache.impls.get(&did) {
18241824
let mut used_links = FxHashSet::default();
18251825
let cache = cx.cache();
@@ -2109,7 +2109,7 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
21092109
"</div>",
21102110
);
21112111

2112-
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
2112+
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) {
21132113
let cache = cx.cache();
21142114
let mut res = implementors
21152115
.iter()

src/librustdoc/html/render/print_item.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
289289
w,
290290
"<div class=\"item-left\"><code>{}extern crate {} as {};",
291291
myitem.visibility.print_with_space(myitem.def_id, cx),
292-
anchor(myitem.def_id.expect_real(), &*src.as_str(), cx),
292+
anchor(myitem.def_id.expect_def_id(), &*src.as_str(), cx),
293293
myitem.name.as_ref().unwrap(),
294294
),
295295
None => write!(
296296
w,
297297
"<div class=\"item-left\"><code>{}extern crate {};",
298298
myitem.visibility.print_with_space(myitem.def_id, cx),
299299
anchor(
300-
myitem.def_id.expect_real(),
300+
myitem.def_id.expect_def_id(),
301301
&*myitem.name.as_ref().unwrap().as_str(),
302302
cx
303303
),
@@ -669,9 +669,9 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
669669
}
670670

671671
// If there are methods directly on this trait object, render them here.
672-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All);
672+
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All);
673673

674-
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_real()) {
674+
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) {
675675
// The DefId is for the first Type found with that name. The bool is
676676
// if any Types with the same name but different DefId have been found.
677677
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
@@ -787,7 +787,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
787787
path = if it.def_id.is_local() {
788788
cx.current.join("/")
789789
} else {
790-
let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_real()];
790+
let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_def_id()];
791791
path[..path.len() - 1].join("/")
792792
},
793793
ty = it.type_(),
@@ -813,7 +813,7 @@ fn item_trait_alias(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clea
813813
// won't be visible anywhere in the docs. It would be nice to also show
814814
// associated items from the aliased type (see discussion in #32077), but
815815
// we need #14072 to make sense of the generics.
816-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
816+
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All)
817817
}
818818

819819
fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::OpaqueTy) {
@@ -834,7 +834,7 @@ fn item_opaque_ty(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean:
834834
// won't be visible anywhere in the docs. It would be nice to also show
835835
// associated items from the aliased type (see discussion in #32077), but
836836
// we need #14072 to make sense of the generics.
837-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
837+
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All)
838838
}
839839

840840
fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
@@ -851,7 +851,7 @@ fn item_typedef(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::T
851851

852852
document(w, cx, it, None);
853853

854-
let def_id = it.def_id.expect_real();
854+
let def_id = it.def_id.expect_def_id();
855855
// Render any items associated directly to this alias, as otherwise they
856856
// won't be visible anywhere in the docs. It would be nice to also show
857857
// associated items from the aliased type (see discussion in #32077), but
@@ -903,7 +903,7 @@ fn item_union(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::Uni
903903
document(w, cx, field, Some(it));
904904
}
905905
}
906-
let def_id = it.def_id.expect_real();
906+
let def_id = it.def_id.expect_def_id();
907907
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
908908
document_type_layout(w, cx, def_id);
909909
}
@@ -1041,7 +1041,7 @@ fn item_enum(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, e: &clean::Enum
10411041
}
10421042
}
10431043
}
1044-
let def_id = it.def_id.expect_real();
1044+
let def_id = it.def_id.expect_def_id();
10451045
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
10461046
document_type_layout(w, cx, def_id);
10471047
}
@@ -1093,7 +1093,7 @@ fn item_proc_macro(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, m: &clean
10931093

10941094
fn item_primitive(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
10951095
document(w, cx, it, None);
1096-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
1096+
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All)
10971097
}
10981098

10991099
fn item_constant(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, c: &clean::Constant) {
@@ -1182,7 +1182,7 @@ fn item_struct(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, s: &clean::St
11821182
}
11831183
}
11841184
}
1185-
let def_id = it.def_id.expect_real();
1185+
let def_id = it.def_id.expect_def_id();
11861186
render_assoc_items(w, cx, it, def_id, AssocItemRender::All);
11871187
document_type_layout(w, cx, def_id);
11881188
}
@@ -1213,7 +1213,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {
12131213

12141214
document(w, cx, it, None);
12151215

1216-
render_assoc_items(w, cx, it, it.def_id.expect_real(), AssocItemRender::All)
1216+
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All)
12171217
}
12181218

12191219
fn item_keyword(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item) {

src/librustdoc/json/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,11 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
164164
let id = item.def_id;
165165
if let Some(mut new_item) = self.convert_item(item) {
166166
if let types::ItemEnum::Trait(ref mut t) = new_item.inner {
167-
t.implementors = self.get_trait_implementors(id.expect_real())
167+
t.implementors = self.get_trait_implementors(id.expect_def_id())
168168
} else if let types::ItemEnum::Struct(ref mut s) = new_item.inner {
169-
s.impls = self.get_impls(id.expect_real())
169+
s.impls = self.get_impls(id.expect_def_id())
170170
} else if let types::ItemEnum::Enum(ref mut e) = new_item.inner {
171-
e.impls = self.get_impls(id.expect_real())
171+
e.impls = self.get_impls(id.expect_def_id())
172172
}
173173
let removed = self.index.borrow_mut().insert(from_item_id(id), new_item.clone());
174174

src/librustdoc/passes/calculate_doc_coverage.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,13 +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-
// The `expect_real()` should be okay because `local_def_id_to_hir_id`
216+
// The `expect_def_id()` should be okay because `local_def_id_to_hir_id`
217217
// would presumably panic if a fake `DefIndex` were passed.
218218
let hir_id = self
219219
.ctx
220220
.tcx
221221
.hir()
222-
.local_def_id_to_hir_id(i.def_id.expect_real().expect_local());
222+
.local_def_id_to_hir_id(i.def_id.expect_def_id().expect_local());
223223
let (level, source) = self.ctx.tcx.lint_level_at_node(MISSING_DOCS, hir_id);
224224
// `missing_docs` is allow-by-default, so don't treat this as ignoring the item
225225
// unless the user had an explicit `allow`

src/librustdoc/passes/check_code_block_syntax.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
5353
return;
5454
}
5555

56-
let local_id = match item.def_id.as_real().and_then(|x| x.as_local()) {
56+
let local_id = match item.def_id.as_def_id().and_then(|x| x.as_local()) {
5757
Some(id) => id,
5858
// We don't need to check the syntax for other crates so returning
5959
// without doing anything should not be a problem.
@@ -137,7 +137,7 @@ impl<'a, 'tcx> DocFolder for SyntaxChecker<'a, 'tcx> {
137137
let sp = item.attr_span(self.cx.tcx);
138138
let extra = crate::html::markdown::ExtraInfo::new_did(
139139
self.cx.tcx,
140-
item.def_id.expect_real(),
140+
item.def_id.expect_def_id(),
141141
sp,
142142
);
143143
for code_block in markdown::rust_code_blocks(&dox, &extra) {

0 commit comments

Comments
 (0)