Skip to content

Commit 4a31165

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

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};
@@ -24,13 +22,13 @@ use super::{
2422
sidebar::{sidebar_module_like, Sidebar},
2523
AllTypes, LinkFromSrc, StylePath,
2624
};
27-
use crate::clean::{self, types::ExternalLocation, ExternalCrate, TypeAliasItem};
25+
use crate::clean::{self, types::ExternalLocation, ExternalCrate};
2826
use crate::config::{ModuleSorting, RenderOptions};
2927
use crate::docfs::{DocFS, PathError};
3028
use crate::error::Error;
3129
use crate::formats::cache::Cache;
3230
use crate::formats::item_type::ItemType;
33-
use crate::formats::{self, FormatRenderer};
31+
use crate::formats::FormatRenderer;
3432
use crate::html::escape::Escape;
3533
use crate::html::format::{join_with_double_colon, Buffer};
3634
use crate::html::markdown::{self, plain_text_summary, ErrorCodes, IdMap};
@@ -149,47 +147,6 @@ impl SharedContext<'_> {
149147
pub(crate) fn edition(&self) -> Edition {
150148
self.tcx.sess.edition()
151149
}
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-
}
193150
}
194151

195152
impl<'tcx> Context<'tcx> {

src/librustdoc/html/render/mod.rs

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ 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};
5758
use rustc_middle::ty::TyCtxt;
5859
use rustc_span::{
5960
symbol::{sym, Symbol},
@@ -62,6 +63,7 @@ use rustc_span::{
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;
@@ -1131,13 +1133,13 @@ pub(crate) fn render_all_impls(
11311133
fn render_assoc_items<'a, 'cx: 'a>(
11321134
cx: &'a mut Context<'cx>,
11331135
containing_item: &'a clean::Item,
1134-
did: DefId,
1136+
it: DefId,
11351137
what: AssocItemRender<'a>,
11361138
) -> impl fmt::Display + 'a + Captures<'cx> {
11371139
let mut derefs = DefIdSet::default();
1138-
derefs.insert(did);
1140+
derefs.insert(it);
11391141
display_fn(move |f| {
1140-
render_assoc_items_inner(f, cx, containing_item, did, what, &mut derefs);
1142+
render_assoc_items_inner(f, cx, containing_item, it, what, &mut derefs);
11411143
Ok(())
11421144
})
11431145
}
@@ -1146,16 +1148,46 @@ fn render_assoc_items_inner(
11461148
mut w: &mut dyn fmt::Write,
11471149
cx: &mut Context<'_>,
11481150
containing_item: &clean::Item,
1149-
did: DefId,
1151+
it: DefId,
11501152
what: AssocItemRender<'_>,
11511153
derefs: &mut DefIdSet,
11521154
) {
11531155
info!("Documenting associated items of {:?}", containing_item.name);
11541156
let shared = Rc::clone(&cx.shared);
1155-
let v = shared.all_impls_for_item(containing_item, did);
1156-
let v = v.as_slice();
1157-
let (non_trait, traits): (Vec<&Impl>, _) =
1158-
v.iter().partition(|i| i.inner_impl().trait_.is_none());
1157+
let cache = &shared.cache;
1158+
let tcx = cx.tcx();
1159+
let av = if let TypeAliasItem(ait) = &*containing_item.kind &&
1160+
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
1161+
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
1162+
let Some(mut av) = cache.impls.get(&aliased_type_defid).cloned() &&
1163+
let Some(alias_def_id) = containing_item.item_id.as_def_id()
1164+
{
1165+
// This branch of the compiler compares types structually, but does
1166+
// not check trait bounds. That's probably fine, since type aliases
1167+
// don't normally constrain on them anyway.
1168+
// https://github.com/rust-lang/rust/issues/21903
1169+
//
1170+
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
1171+
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
1172+
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
1173+
let reject_cx = DeepRejectCtxt {
1174+
treat_obligation_params: TreatParams::AsCandidateKey,
1175+
};
1176+
av.retain(|impl_| {
1177+
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
1178+
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
1179+
} else {
1180+
false
1181+
}
1182+
});
1183+
av
1184+
} else {
1185+
Vec::new()
1186+
};
1187+
let blank = Vec::new();
1188+
let v = cache.impls.get(&it).unwrap_or(&blank);
1189+
let (non_trait, traits): (Vec<_>, _) =
1190+
v.iter().chain(&av[..]).partition(|i| i.inner_impl().trait_.is_none());
11591191
let mut saw_impls = FxHashSet::default();
11601192
if !non_trait.is_empty() {
11611193
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
};
@@ -279,8 +281,40 @@ fn sidebar_assoc_items<'a>(
279281
links: &mut Vec<LinkBlock<'a>>,
280282
) {
281283
let did = it.item_id.expect_def_id();
282-
let v = cx.shared.all_impls_for_item(it, it.item_id.expect_def_id());
283-
let v = v.as_slice();
284+
let cache = cx.cache();
285+
let tcx = cx.tcx();
286+
let mut v: Vec<&Impl> =
287+
cache.impls.get(&did).map(Vec::as_slice).unwrap_or(&[]).iter().collect();
288+
if let TypeAliasItem(ait) = &*it.kind &&
289+
let aliased_clean_type = ait.item_type.as_ref().unwrap_or(&ait.type_) &&
290+
let Some(aliased_type_defid) = aliased_clean_type.def_id(cache) &&
291+
let Some(av) = cache.impls.get(&aliased_type_defid) &&
292+
let Some(alias_def_id) = it.item_id.as_def_id()
293+
{
294+
// This branch of the compiler compares types structually, but does
295+
// not check trait bounds. That's probably fine, since type aliases
296+
// don't normally constrain on them anyway.
297+
// https://github.com/rust-lang/rust/issues/21903
298+
//
299+
// FIXME(lazy_type_alias): Once the feature is complete or stable, rewrite this to use type unification.
300+
// Be aware of `tests/rustdoc/issue-112515-impl-ty-alias.rs` which might regress.
301+
let aliased_ty = tcx.type_of(alias_def_id).skip_binder();
302+
let reject_cx = DeepRejectCtxt {
303+
treat_obligation_params: TreatParams::AsCandidateKey,
304+
};
305+
v.extend(av.iter().filter(|impl_| {
306+
if let Some(impl_def_id) = impl_.impl_item.item_id.as_def_id() {
307+
reject_cx.types_may_unify(aliased_ty, tcx.type_of(impl_def_id).skip_binder())
308+
} else {
309+
false
310+
}
311+
}));
312+
}
313+
let v = {
314+
let mut saw_impls = FxHashSet::default();
315+
v.retain(|i| saw_impls.insert(i.def_id()));
316+
v.as_slice()
317+
};
284318

285319
let mut assoc_consts = Vec::new();
286320
let mut methods = Vec::new();

0 commit comments

Comments
 (0)