Skip to content

Commit c3e5ad4

Browse files
committed
rustdoc: factor all-impls-for-item out into its own method
1 parent d882b21 commit c3e5ad4

File tree

3 files changed

+55
-78
lines changed

3 files changed

+55
-78
lines changed

src/librustdoc/html/render/context.rs

+45-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ use std::sync::mpsc::{channel, Receiver};
77

88
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_hir::def_id::{DefIdMap, LOCAL_CRATE};
10+
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
1011
use rustc_middle::ty::TyCtxt;
1112
use rustc_session::Session;
13+
use rustc_span::def_id::DefId;
1214
use rustc_span::edition::Edition;
1315
use rustc_span::source_map::FileName;
1416
use rustc_span::{sym, Symbol};
@@ -22,13 +24,13 @@ use super::{
2224
sidebar::{sidebar_module_like, Sidebar},
2325
AllTypes, LinkFromSrc, StylePath,
2426
};
25-
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
27+
use crate::clean::{self, types::ExternalLocation, ExternalCrate, TypeAliasItem};
2628
use crate::config::{ModuleSorting, RenderOptions};
2729
use crate::docfs::{DocFS, PathError};
2830
use crate::error::Error;
2931
use crate::formats::cache::Cache;
3032
use crate::formats::item_type::ItemType;
31-
use crate::formats::FormatRenderer;
33+
use crate::formats::{self, FormatRenderer};
3234
use crate::html::escape::Escape;
3335
use crate::html::format::{join_with_double_colon, Buffer};
3436
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
@@ -147,6 +149,47 @@ impl SharedContext<'_> {
147149
pub(crate) fn edition(&self) -> Edition {
148150
self.tcx.sess.edition()
149151
}
152+
153+
/// Returns a list of impls on the given type, and, if it's a type alias,
154+
/// other types that it aliases.
155+
pub(crate) fn all_impls_for_item<'a>(
156+
&'a self,
157+
it: &clean::Item,
158+
did: DefId,
159+
) -> Vec<&'a formats::Impl> {
160+
let tcx = self.tcx;
161+
let cache = &self.cache;
162+
let mut v: Vec<&formats::Impl> =
163+
cache.impls.get(&did).map(Vec::as_slice).unwrap_or(&[]).iter().collect();
164+
if let TypeAliasItem(ait) = &*it.kind &&
165+
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
166+
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
167+
let Some(av) = cache.impls.get(&aliased_type_defid) &&
168+
let Some(alias_def_id) = it.item_id.as_def_id()
169+
{
170+
// This branch of the compiler compares types structually, but does
171+
// not check trait bounds. That's probably fine, since type aliases
172+
// don't normally constrain on them anyway.
173+
// https://github.com/rust-lang/rust/issues/21903
174+
//
175+
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
176+
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
177+
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
178+
let reject_cx = DeepRejectCtxt {
179+
treat_obligation_params: TreatParams::AsCandidateKey,
180+
};
181+
v.extend(av.iter().filter(|impl_| {
182+
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
183+
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
184+
} else {
185+
false
186+
}
187+
}));
188+
}
189+
let mut saw_impls = FxHashSet::default();
190+
v.retain(|i| saw_impls.insert(i.def_id()));
191+
v
192+
}
150193
}
151194

152195
impl<'tcx> Context<'tcx> {

src/librustdoc/html/render/mod.rs

+8-40
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5454
use rustc_hir::def_id::{DefId, DefIdSet};
5555
use rustc_hir::Mutability;
5656
use rustc_middle::middle::stability;
57-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
5857
use rustc_middle::ty::TyCtxt;
5958
use rustc_span::{
6059
symbol::{sym, Symbol},
@@ -63,7 +62,6 @@ use rustc_span::{
6362
use serde::ser::{SerializeMap, SerializeSeq};
6463
use serde::{Serialize, Serializer};
6564

66-
use crate::clean::types::TypeAliasItem;
6765
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
6866
use crate::error::Error;
6967
use crate::formats::cache::Cache;
@@ -1119,13 +1117,13 @@ pub(crate) fn render_all_impls(
11191117
fn render_assoc_items<'a, 'cx: 'a>(
11201118
cx: &'a mut Context<'cx>,
11211119
containing_item: &'a clean::Item,
1122-
it: DefId,
1120+
did: DefId,
11231121
what: AssocItemRender<'a>,
11241122
) -> impl fmt::Display + 'a + Captures<'cx> {
11251123
let mut derefs = DefIdSet::default();
1126-
derefs.insert(it);
1124+
derefs.insert(did);
11271125
display_fn(move |f| {
1128-
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
1126+
render_assoc_items_inner(f, cx, containing_item, did, what, &mut derefs);
11291127
Ok(())
11301128
})
11311129
}
@@ -1134,46 +1132,16 @@ fn render_assoc_items_inner(
11341132
mut w: &mut dyn fmt::Write,
11351133
cx: &mut Context<'_>,
11361134
containing_item: &clean::Item,
1137-
it: DefId,
1135+
did: DefId,
11381136
what: AssocItemRender<'_>,
11391137
derefs: &mut DefIdSet,
11401138
) {
11411139
info!("Documenting associated items of {:?}", containing_item.name);
11421140
let shared = Rc::clone(&cx.shared);
1143-
let cache = &shared.cache;
1144-
let tcx = cx.tcx();
1145-
let av = if let TypeAliasItem(ait) = &*containing_item.kind &&
1146-
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
1147-
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
1148-
let Some(mut av) = cache.impls.get(&aliased_type_defid).cloned() &&
1149-
let Some(alias_def_id) = containing_item.item_id.as_def_id()
1150-
{
1151-
// This branch of the compiler compares types structually, but does
1152-
// not check trait bounds. That's probably fine, since type aliases
1153-
// don't normally constrain on them anyway.
1154-
// https://github.com/rust-lang/rust/issues/21903
1155-
//
1156-
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
1157-
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
1158-
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
1159-
let reject_cx = DeepRejectCtxt {
1160-
treat_obligation_params: TreatParams::AsCandidateKey,
1161-
};
1162-
av.retain(|impl_| {
1163-
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
1164-
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
1165-
} else {
1166-
false
1167-
}
1168-
});
1169-
av
1170-
} else {
1171-
Vec::new()
1172-
};
1173-
let blank = Vec::new();
1174-
let v = cache.impls.get(&it).unwrap_or(&blank);
1175-
let (non_trait, traits): (Vec<_>, _) =
1176-
v.iter().chain(&av[..]).partition(|i| i.inner_impl().trait_.is_none());
1141+
let v = shared.all_impls_for_item(containing_item, did);
1142+
let v = v.as_slice();
1143+
let (non_trait, traits): (Vec<&Impl>, _) =
1144+
v.iter().partition(|i| i.inner_impl().trait_.is_none());
11771145
let mut saw_impls = FxHashSet::default();
11781146
if !non_trait.is_empty() {
11791147
let mut tmp_buf = Buffer::html();

src/librustdoc/html/render/sidebar.rs

+2-36
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ use std::{borrow::Cow, rc::Rc};
33
use askama::Template;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_hir::{def::CtorKind, def_id::DefIdSet};
6-
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
76
use rustc_middle::ty::{self, TyCtxt};
87

98
use crate::{
109
clean,
11-
clean::types::TypeAliasItem,
1210
formats::{item_type::ItemType, Impl},
1311
html::{format::Buffer, markdown::IdMap},
1412
};
@@ -280,40 +278,8 @@ fn sidebar_assoc_items<'a>(
280278
links: &mut Vec<LinkBlock<'a>>,
281279
) {
282280
let did = it.item_id.expect_def_id();
283-
let cache = cx.cache();
284-
let tcx = cx.tcx();
285-
let mut v: Vec<&Impl> =
286-
cache.impls.get(&did).map(Vec::as_slice).unwrap_or(&[]).iter().collect();
287-
if let TypeAliasItem(ait) = &*it.kind &&
288-
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
289-
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
290-
let Some(av) = cache.impls.get(&aliased_type_defid) &&
291-
let Some(alias_def_id) = it.item_id.as_def_id()
292-
{
293-
// This branch of the compiler compares types structually, but does
294-
// not check trait bounds. That's probably fine, since type aliases
295-
// don't normally constrain on them anyway.
296-
// https://github.com/rust-lang/rust/issues/21903
297-
//
298-
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
299-
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
300-
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
301-
let reject_cx = DeepRejectCtxt {
302-
treat_obligation_params: TreatParams::AsCandidateKey,
303-
};
304-
v.extend(av.iter().filter(|impl_| {
305-
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
306-
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
307-
} else {
308-
false
309-
}
310-
}));
311-
}
312-
let v = {
313-
let mut saw_impls = FxHashSet::default();
314-
v.retain(|i| saw_impls.insert(i.def_id()));
315-
v.as_slice()
316-
};
281+
let v = cx.shared.all_impls_for_item(it, it.item_id.expect_def_id());
282+
let v = v.as_slice();
317283

318284
let mut assoc_consts = Vec::new();
319285
let mut methods = Vec::new();

0 commit comments

Comments
 (0)