Skip to content

Commit ab125a2

Browse files
committed
Revert "rustdoc: factor all-impls-for-item out into its own method"
This reverts commit c3e5ad4.
1 parent aa76a59 commit ab125a2

File tree

3 files changed

+78
-55
lines changed

3 files changed

+78
-55
lines changed

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@ 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};
1110
use rustc_middle::ty::TyCtxt;
1211
use rustc_session::Session;
13-
use rustc_span::def_id::DefId;
1412
use rustc_span::edition::Edition;
1513
use rustc_span::source_map::FileName;
1614
use rustc_span::{sym, Symbol};
@@ -25,13 +23,13 @@ use super::{
2523
AllTypes, LinkFromSrc, StylePath,
2624
};
2725
use crate::clean::utils::has_doc_flag;
28-
use crate::clean::{self, types::ExternalLocation, ExternalCrate, TypeAliasItem};
26+
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
2927
use crate::config::{ModuleSorting, RenderOptions};
3028
use crate::docfs::{DocFS, PathError};
3129
use crate::error::Error;
3230
use crate::formats::cache::Cache;
3331
use crate::formats::item_type::ItemType;
34-
use crate::formats::{self, FormatRenderer};
32+
use crate::formats::FormatRenderer;
3533
use crate::html::escape::Escape;
3634
use crate::html::format::{join_with_double_colon, Buffer};
3735
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
@@ -150,47 +148,6 @@ impl SharedContext<'_> {
150148
pub(crate) fn edition(&self) -> Edition {
151149
self.tcx.sess.edition()
152150
}
153-
154-
/// Returns a list of impls on the given type, and, if it's a type alias,
155-
/// other types that it aliases.
156-
pub(crate) fn all_impls_for_item<'a>(
157-
&'a self,
158-
it: &clean::Item,
159-
did: DefId,
160-
) -> Vec<&'a formats::Impl> {
161-
let tcx = self.tcx;
162-
let cache = &self.cache;
163-
let mut v: Vec<&formats::Impl> =
164-
cache.impls.get(&did).map(Vec::as_slice).unwrap_or(&[]).iter().collect();
165-
if let TypeAliasItem(ait) = &*it.kind &&
166-
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
167-
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
168-
let Some(av) = cache.impls.get(&aliased_type_defid) &&
169-
let Some(alias_def_id) = it.item_id.as_def_id()
170-
{
171-
// This branch of the compiler compares types structually, but does
172-
// not check trait bounds. That's probably fine, since type aliases
173-
// don't normally constrain on them anyway.
174-
// https://github.com/rust-lang/rust/issues/21903
175-
//
176-
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
177-
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
178-
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
179-
let reject_cx = DeepRejectCtxt {
180-
treat_obligation_params: TreatParams::AsCandidateKey,
181-
};
182-
v.extend(av.iter().filter(|impl_| {
183-
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
184-
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
185-
} else {
186-
false
187-
}
188-
}));
189-
}
190-
let mut saw_impls = FxHashSet::default();
191-
v.retain(|i| saw_impls.insert(i.def_id()));
192-
v
193-
}
194151
}
195152

196153
impl<'tcx> Context<'tcx> {

src/librustdoc/html/render/mod.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ use rustc_hir::def_id::{DefId, DefIdSet};
5555
use rustc_hir::Mutability;
5656
use rustc_middle::middle::stability;
5757
use rustc_middle::ty::{self, TyCtxt};
58+
use rustc_middle::ty::fast_reject::{DeepRejectCtxt, TreatParams};
5859
use rustc_span::{
5960
symbol::{sym, Symbol},
6061
BytePos, FileName, RealFileName,
6162
};
6263
use serde::ser::{SerializeMap, SerializeSeq};
6364
use serde::{Serialize, Serializer};
6465

66+
use crate::clean::types::TypeAliasItem;
6567
use crate::clean::{self, ItemId, RenderedLink, SelfTy};
6668
use crate::error::Error;
6769
use crate::formats::cache::Cache;
@@ -1132,13 +1134,13 @@ pub(crate) fn render_all_impls(
11321134
fn render_assoc_items<'a, 'cx: 'a>(
11331135
cx: &'a mut Context<'cx>,
11341136
containing_item: &'a clean::Item,
1135-
did: DefId,
1137+
it: DefId,
11361138
what: AssocItemRender<'a>,
11371139
) -> impl fmt::Display + 'a + Captures<'cx> {
11381140
let mut derefs = DefIdSet::default();
1139-
derefs.insert(did);
1141+
derefs.insert(it);
11401142
display_fn(move |f| {
1141-
render_assoc_items_inner(f, cx, containing_item, did, what, &mut derefs);
1143+
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
11421144
Ok(())
11431145
})
11441146
}
@@ -1147,16 +1149,46 @@ fn render_assoc_items_inner(
11471149
mut w: &mut dyn fmt::Write,
11481150
cx: &mut Context<'_>,
11491151
containing_item: &clean::Item,
1150-
did: DefId,
1152+
it: DefId,
11511153
what: AssocItemRender<'_>,
11521154
derefs: &mut DefIdSet,
11531155
) {
11541156
info!("Documenting associated items of {:?}", containing_item.name);
11551157
let shared = Rc::clone(&cx.shared);
1156-
let v = shared.all_impls_for_item(containing_item, did);
1157-
let v = v.as_slice();
1158-
let (non_trait, traits): (Vec<&Impl>, _) =
1159-
v.iter().partition(|i| i.inner_impl().trait_.is_none());
1158+
let cache = &shared.cache;
1159+
let tcx = cx.tcx();
1160+
let av = if let TypeAliasItem(ait) = &*containing_item.kind &&
1161+
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
1162+
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
1163+
let Some(mut av) = cache.impls.get(&aliased_type_defid).cloned() &&
1164+
let Some(alias_def_id) = containing_item.item_id.as_def_id()
1165+
{
1166+
// This branch of the compiler compares types structually, but does
1167+
// not check trait bounds. That's probably fine, since type aliases
1168+
// don't normally constrain on them anyway.
1169+
// https://github.com/rust-lang/rust/issues/21903
1170+
//
1171+
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
1172+
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
1173+
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
1174+
let reject_cx = DeepRejectCtxt {
1175+
treat_obligation_params: TreatParams::AsCandidateKey,
1176+
};
1177+
av.retain(|impl_| {
1178+
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
1179+
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
1180+
} else {
1181+
false
1182+
}
1183+
});
1184+
av
1185+
} else {
1186+
Vec::new()
1187+
};
1188+
let blank = Vec::new();
1189+
let v = cache.impls.get(&it).unwrap_or(&blank);
1190+
let (non_trait, traits): (Vec<_>, _) =
1191+
v.iter().chain(&av[..]).partition(|i| i.inner_impl().trait_.is_none());
11601192
let mut saw_impls = FxHashSet::default();
11611193
if !non_trait.is_empty() {
11621194
let mut tmp_buf = Buffer::html();

src/librustdoc/html/render/sidebar.rs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ 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};
67
use rustc_middle::ty::{self, TyCtxt};
78

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

293327
let mut assoc_consts = Vec::new();
294328
let mut methods = Vec::new();

0 commit comments

Comments
 (0)