Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a83112f

Browse files
committed
Remove temporary GetDefId impl for Path
1 parent 4128a3d commit a83112f

File tree

8 files changed

+63
-93
lines changed

8 files changed

+63
-93
lines changed

src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ crate fn build_impl(
447447
};
448448
let polarity = tcx.impl_polarity(did);
449449
let trait_ = associated_trait.clean(cx);
450-
if trait_.def_id() == tcx.lang_items().deref_trait() {
450+
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
451451
super::build_deref_target_impls(cx, &trait_items, ret);
452452
}
453453

@@ -481,7 +481,7 @@ crate fn build_impl(
481481
let (merged_attrs, cfg) = merge_attrs(cx, parent_module.into(), load_attrs(cx, did), attrs);
482482
trace!("merged_attrs={:?}", merged_attrs);
483483

484-
trace!("build_impl: impl {:?} for {:?}", trait_.def_id(), for_.def_id());
484+
trace!("build_impl: impl {:?} for {:?}", trait_.as_ref().map(|t| t.def_id()), for_.def_id());
485485
ret.push(clean::Item::from_def_id_and_attrs_and_parts(
486486
did,
487487
None,

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1883,7 +1883,7 @@ fn clean_impl(impl_: &hir::Impl<'_>, hir_id: hir::HirId, cx: &mut DocContext<'_>
18831883

18841884
// If this impl block is an implementation of the Deref trait, then we
18851885
// need to try inlining the target's inherent impl blocks as well.
1886-
if trait_.def_id() == tcx.lang_items().deref_trait() {
1886+
if trait_.as_ref().map(|t| t.def_id()) == tcx.lang_items().deref_trait() {
18871887
build_deref_target_impls(cx, &items, &mut ret);
18881888
}
18891889

src/librustdoc/clean/types.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ impl GenericBound {
11221122
crate fn is_sized_bound(&self, cx: &DocContext<'_>) -> bool {
11231123
use rustc_hir::TraitBoundModifier as TBM;
11241124
if let GenericBound::TraitBound(PolyTrait { ref trait_, .. }, TBM::None) = *self {
1125-
if trait_.def_id() == cx.tcx.lang_items().sized_trait() {
1125+
if Some(trait_.def_id()) == cx.tcx.lang_items().sized_trait() {
11261126
return true;
11271127
}
11281128
}
@@ -1942,6 +1942,10 @@ crate struct Path {
19421942
}
19431943

19441944
impl Path {
1945+
crate fn def_id(&self) -> DefId {
1946+
self.res.def_id()
1947+
}
1948+
19451949
crate fn last(&self) -> Symbol {
19461950
self.segments.last().expect("segments were empty").name
19471951
}
@@ -1992,17 +1996,6 @@ impl Path {
19921996
}
19931997
}
19941998

1995-
// FIXME: this is temporary
1996-
impl GetDefId for Path {
1997-
fn def_id(&self) -> Option<DefId> {
1998-
Some(self.res.def_id())
1999-
}
2000-
2001-
fn def_id_full(&self, _: &Cache) -> Option<DefId> {
2002-
self.def_id()
2003-
}
2004-
}
2005-
20061999
#[derive(Clone, PartialEq, Eq, Debug, Hash)]
20072000
crate enum GenericArg {
20082001
Lifetime(Lifetime),
@@ -2155,7 +2148,8 @@ crate struct Impl {
21552148
impl Impl {
21562149
crate fn provided_trait_methods(&self, tcx: TyCtxt<'_>) -> FxHashSet<Symbol> {
21572150
self.trait_
2158-
.def_id()
2151+
.as_ref()
2152+
.map(|t| t.def_id())
21592153
.map(|did| tcx.provided_trait_methods(did).map(|meth| meth.ident.name).collect())
21602154
.unwrap_or_default()
21612155
}

src/librustdoc/formats/cache.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
201201
// masked crate then remove it completely.
202202
if let clean::ImplItem(ref i) = *item.kind {
203203
if self.cache.masked_crates.contains(&item.def_id.krate())
204-
|| i.trait_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
204+
|| i.trait_
205+
.as_ref()
206+
.map_or(false, |t| self.cache.masked_crates.contains(&t.def_id().krate))
205207
|| i.for_.def_id().map_or(false, |d| self.cache.masked_crates.contains(&d.krate))
206208
{
207209
return None;
@@ -221,11 +223,11 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
221223

222224
// Collect all the implementors of traits.
223225
if let clean::ImplItem(ref i) = *item.kind {
224-
if let Some(did) = i.trait_.def_id() {
226+
if let Some(trait_) = &i.trait_ {
225227
if i.blanket_impl.is_none() {
226228
self.cache
227229
.implementors
228-
.entry(did)
230+
.entry(trait_.def_id())
229231
.or_default()
230232
.push(Impl { impl_item: item.clone() });
231233
}
@@ -400,12 +402,8 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
400402
}
401403
clean::DynTrait(ref bounds, _)
402404
| clean::BorrowedRef { type_: box clean::DynTrait(ref bounds, _), .. } => {
403-
if let Some(did) = bounds[0].trait_.def_id() {
404-
self.cache.parent_stack.push(did);
405-
true
406-
} else {
407-
false
408-
}
405+
self.cache.parent_stack.push(bounds[0].trait_.def_id());
406+
true
409407
}
410408
ref t => {
411409
let prim_did = t
@@ -439,9 +437,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
439437
}
440438
clean::DynTrait(ref bounds, _)
441439
| clean::BorrowedRef { type_: box clean::DynTrait(ref bounds, _), .. } => {
442-
if let Some(did) = bounds[0].trait_.def_id() {
443-
dids.insert(did);
444-
}
440+
dids.insert(bounds[0].trait_.def_id());
445441
}
446442
ref t => {
447443
let did = t

src/librustdoc/formats/mod.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use rustc_hir::def_id::DefId;
77
crate use renderer::{run_format, FormatRenderer};
88

99
use crate::clean;
10-
use crate::clean::types::GetDefId;
11-
use crate::formats::cache::Cache;
1210

1311
/// Specifies whether rendering directly implemented trait items or ones from a certain Deref
1412
/// impl.
@@ -40,10 +38,6 @@ impl Impl {
4038
}
4139

4240
crate fn trait_did(&self) -> Option<DefId> {
43-
self.inner_impl().trait_.def_id()
44-
}
45-
46-
crate fn trait_did_full(&self, cache: &Cache) -> Option<DefId> {
47-
self.inner_impl().trait_.def_id_full(cache)
41+
self.inner_impl().trait_.as_ref().map(|t| t.def_id())
4842
}
4943
}

src/librustdoc/html/render/mod.rs

Lines changed: 40 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -687,18 +687,12 @@ fn short_item_info(
687687

688688
// Render the list of items inside one of the sections "Trait Implementations",
689689
// "Auto Trait Implementations," "Blanket Trait Implementations" (on struct/enum pages).
690-
fn render_impls(
691-
cx: &Context<'_>,
692-
w: &mut Buffer,
693-
traits: &[&&Impl],
694-
containing_item: &clean::Item,
695-
) {
696-
let cache = cx.cache();
690+
fn render_impls(cx: &Context<'_>, w: &mut Buffer, impls: &[&&Impl], containing_item: &clean::Item) {
697691
let tcx = cx.tcx();
698-
let mut impls = traits
692+
let mut rendered_impls = impls
699693
.iter()
700694
.map(|i| {
701-
let did = i.trait_did_full(cache).unwrap();
695+
let did = i.trait_did().unwrap();
702696
let provided_trait_methods = i.inner_impl().provided_trait_methods(tcx);
703697
let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_trait_methods);
704698
let mut buffer = if w.is_for_html() { Buffer::html() } else { Buffer::new() };
@@ -722,8 +716,8 @@ fn render_impls(
722716
buffer.into_inner()
723717
})
724718
.collect::<Vec<_>>();
725-
impls.sort();
726-
w.write_str(&impls.join(""));
719+
rendered_impls.sort();
720+
w.write_str(&rendered_impls.join(""));
727721
}
728722

729723
fn naive_assoc_href(it: &clean::Item, link: AssocItemLink<'_>, cx: &Context<'_>) -> String {
@@ -1068,13 +1062,11 @@ fn render_assoc_items(
10681062
return;
10691063
}
10701064
if !traits.is_empty() {
1071-
let deref_impl = traits.iter().find(|t| {
1072-
t.inner_impl().trait_.def_id_full(cache) == cx.tcx().lang_items().deref_trait()
1073-
});
1065+
let deref_impl =
1066+
traits.iter().find(|t| t.trait_did() == cx.tcx().lang_items().deref_trait());
10741067
if let Some(impl_) = deref_impl {
1075-
let has_deref_mut = traits.iter().any(|t| {
1076-
t.inner_impl().trait_.def_id_full(cache) == cx.tcx().lang_items().deref_mut_trait()
1077-
});
1068+
let has_deref_mut =
1069+
traits.iter().any(|t| t.trait_did() == cx.tcx().lang_items().deref_mut_trait());
10781070
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
10791071
}
10801072
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
@@ -1192,45 +1184,39 @@ fn should_render_item(item: &clean::Item, deref_mut_: bool, tcx: TyCtxt<'_>) ->
11921184

11931185
fn notable_traits_decl(decl: &clean::FnDecl, cx: &Context<'_>) -> String {
11941186
let mut out = Buffer::html();
1195-
let mut trait_ = String::new();
11961187

11971188
if let Some(did) = decl.output.def_id_full(cx.cache()) {
11981189
if let Some(impls) = cx.cache().impls.get(&did) {
11991190
for i in impls {
12001191
let impl_ = i.inner_impl();
1201-
if impl_.trait_.def_id().map_or(false, |d| {
1202-
cx.cache().traits.get(&d).map(|t| t.is_notable).unwrap_or(false)
1203-
}) {
1204-
if out.is_empty() {
1205-
write!(
1206-
&mut out,
1207-
"<div class=\"notable\">Notable traits for {}</div>\
1208-
<code class=\"content\">",
1209-
impl_.for_.print(cx)
1210-
);
1211-
trait_.push_str(&impl_.for_.print(cx).to_string());
1212-
}
1192+
if let Some(trait_) = &impl_.trait_ {
1193+
let trait_did = trait_.def_id();
12131194

1214-
//use the "where" class here to make it small
1215-
write!(
1216-
&mut out,
1217-
"<span class=\"where fmt-newline\">{}</span>",
1218-
impl_.print(false, cx)
1219-
);
1220-
let t_did = impl_.trait_.def_id_full(cx.cache()).unwrap();
1221-
for it in &impl_.items {
1222-
if let clean::TypedefItem(ref tydef, _) = *it.kind {
1223-
out.push_str("<span class=\"where fmt-newline\"> ");
1224-
assoc_type(
1195+
if cx.cache().traits.get(&trait_did).map_or(false, |t| t.is_notable) {
1196+
if out.is_empty() {
1197+
write!(
12251198
&mut out,
1226-
it,
1227-
&[],
1228-
Some(&tydef.type_),
1229-
AssocItemLink::GotoSource(t_did.into(), &FxHashSet::default()),
1230-
"",
1231-
cx,
1199+
"<div class=\"notable\">Notable traits for {}</div>\
1200+
<code class=\"content\">",
1201+
impl_.for_.print(cx)
12321202
);
1233-
out.push_str(";</span>");
1203+
}
1204+
1205+
//use the "where" class here to make it small
1206+
write!(
1207+
&mut out,
1208+
"<span class=\"where fmt-newline\">{}</span>",
1209+
impl_.print(false, cx)
1210+
);
1211+
for it in &impl_.items {
1212+
if let clean::TypedefItem(ref tydef, _) = *it.kind {
1213+
out.push_str("<span class=\"where fmt-newline\"> ");
1214+
let empty_set = FxHashSet::default();
1215+
let src_link =
1216+
AssocItemLink::GotoSource(trait_did.into(), &empty_set);
1217+
assoc_type(&mut out, it, &[], Some(&tydef.type_), src_link, "", cx);
1218+
out.push_str(";</span>");
1219+
}
12341220
}
12351221
}
12361222
}
@@ -1273,7 +1259,7 @@ fn render_impl(
12731259
) {
12741260
let cache = cx.cache();
12751261
let traits = &cache.traits;
1276-
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
1262+
let trait_ = i.trait_did().map(|did| &traits[&did]);
12771263
let mut close_tags = String::new();
12781264

12791265
// For trait implementations, the `interesting` output contains all methods that have doc
@@ -1503,7 +1489,7 @@ fn render_impl(
15031489
if i.items.iter().any(|m| m.name == n) {
15041490
continue;
15051491
}
1506-
let did = i.trait_.as_ref().unwrap().def_id_full(cx.cache()).unwrap();
1492+
let did = i.trait_.as_ref().unwrap().def_id();
15071493
let provided_methods = i.provided_trait_methods(cx.tcx());
15081494
let assoc_link = AssocItemLink::GotoSource(did.into(), &provided_methods);
15091495

@@ -1887,9 +1873,9 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
18871873
}
18881874

18891875
if v.iter().any(|i| i.inner_impl().trait_.is_some()) {
1890-
if let Some(impl_) = v.iter().filter(|i| i.inner_impl().trait_.is_some()).find(|i| {
1891-
i.inner_impl().trait_.def_id_full(cache) == cx.tcx().lang_items().deref_trait()
1892-
}) {
1876+
if let Some(impl_) =
1877+
v.iter().find(|i| i.trait_did() == cx.tcx().lang_items().deref_trait())
1878+
{
18931879
sidebar_deref_methods(cx, out, impl_, v);
18941880
}
18951881

@@ -1987,9 +1973,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
19871973
}
19881974
}
19891975
}
1990-
let deref_mut = v.iter().filter(|i| i.inner_impl().trait_.is_some()).any(|i| {
1991-
i.inner_impl().trait_.def_id_full(c) == cx.tcx().lang_items().deref_mut_trait()
1992-
});
1976+
let deref_mut = v.iter().any(|i| i.trait_did() == cx.tcx().lang_items().deref_mut_trait());
19931977
let inner_impl = target
19941978
.def_id_full(c)
19951979
.or_else(|| {

src/librustdoc/passes/collect_trait_impls.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ crate fn collect_trait_impls(krate: Crate, cx: &mut DocContext<'_>) -> Crate {
5757
// scan through included items ahead of time to splice in Deref targets to the "valid" sets
5858
for it in &new_items {
5959
if let ImplItem(Impl { ref for_, ref trait_, ref items, .. }) = *it.kind {
60-
if cleaner.keep_impl(for_) && trait_.def_id() == cx.tcx.lang_items().deref_trait() {
60+
if cleaner.keep_impl(for_)
61+
&& trait_.as_ref().map(|t| t.def_id()) == cx.tcx.lang_items().deref_trait()
62+
{
6163
let target = items
6264
.iter()
6365
.find_map(|item| match *item.kind {

src/librustdoc/passes/stripper.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ impl<'a> DocFolder for ImplStripper<'a> {
134134
return None;
135135
}
136136
}
137-
if let Some(did) = imp.trait_.def_id() {
137+
if let Some(did) = imp.trait_.as_ref().map(|t| t.def_id()) {
138138
if did.is_local() && !self.retained.contains(&did.into()) {
139139
debug!("ImplStripper: impl item for stripped trait; removing");
140140
return None;

0 commit comments

Comments
 (0)