Skip to content

Commit a5b4dc5

Browse files
committed
Auto merge of #68911 - jonas-schievink:inherent-overlap, r=<try>
Speed up the inherent impl overlap check This gives a ~7% improvement in compile times for the stm32f0(x2) crate. Also addresses @eddyb's comment in #68837 (comment).
2 parents 442ae7f + 000243c commit a5b4dc5

File tree

26 files changed

+118
-80
lines changed

26 files changed

+118
-80
lines changed

src/librustc/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ rustc_queries! {
311311
query associated_item(_: DefId) -> ty::AssocItem {}
312312

313313
/// Collects the associated items defined on a trait or impl.
314-
query associated_items(key: DefId) -> ty::AssocItemsIterator<'tcx> {
314+
query associated_items(key: DefId) -> &'tcx [ty::AssocItem] {
315315
desc { |tcx| "collecting associated items of {}", tcx.def_path_str(key) }
316316
}
317317

src/librustc/traits/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,7 @@ fn vtable_methods<'tcx>(
517517
tcx.arena.alloc_from_iter(supertraits(tcx, trait_ref).flat_map(move |trait_ref| {
518518
let trait_methods = tcx
519519
.associated_items(trait_ref.def_id())
520+
.iter()
520521
.filter(|item| item.kind == ty::AssocKind::Method);
521522

522523
// Now list each method's DefId and InternalSubsts (for within its trait).

src/librustc/traits/object_safety.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ fn object_safety_violations_for_trait(
212212
// Check methods for violations.
213213
let mut violations: Vec<_> = tcx
214214
.associated_items(trait_def_id)
215+
.iter()
215216
.filter(|item| item.kind == ty::AssocKind::Method)
216217
.filter_map(|item| {
217218
object_safety_violation_for_method(tcx, trait_def_id, &item)
@@ -277,6 +278,7 @@ fn object_safety_violations_for_trait(
277278

278279
violations.extend(
279280
tcx.associated_items(trait_def_id)
281+
.iter()
280282
.filter(|item| item.kind == ty::AssocKind::Const)
281283
.map(|item| ObjectSafetyViolation::AssocConst(item.ident.name, item.ident.span)),
282284
);
@@ -632,7 +634,9 @@ fn object_ty_for_trait<'tcx>(
632634

633635
let mut associated_types = traits::supertraits(tcx, ty::Binder::dummy(trait_ref))
634636
.flat_map(|super_trait_ref| {
635-
tcx.associated_items(super_trait_ref.def_id()).map(move |item| (super_trait_ref, item))
637+
tcx.associated_items(super_trait_ref.def_id())
638+
.iter()
639+
.map(move |item| (super_trait_ref, item))
636640
})
637641
.filter(|(_, item)| item.kind == ty::AssocKind::Type)
638642
.collect::<Vec<_>>();

src/librustc/traits/project.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1436,7 +1436,7 @@ fn assoc_ty_def(
14361436
{
14371437
return specialization_graph::NodeItem {
14381438
node: specialization_graph::Node::Impl(impl_def_id),
1439-
item,
1439+
item: *item,
14401440
};
14411441
}
14421442
}

src/librustc/traits/types/specialization_graph.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl<'tcx> Node {
8181
}
8282

8383
/// Iterate over the items defined directly by the given (impl or trait) node.
84-
pub fn items(&self, tcx: TyCtxt<'tcx>) -> ty::AssocItemsIterator<'tcx> {
84+
pub fn items(&self, tcx: TyCtxt<'tcx>) -> &'tcx [ty::AssocItem] {
8585
tcx.associated_items(self.def_id())
8686
}
8787

@@ -98,8 +98,10 @@ impl<'tcx> Node {
9898
) -> Option<ty::AssocItem> {
9999
use crate::ty::AssocKind::*;
100100

101-
tcx.associated_items(self.def_id()).find(move |impl_item| {
102-
match (trait_item_kind, impl_item.kind) {
101+
tcx.associated_items(self.def_id())
102+
.iter()
103+
.find(move |impl_item| {
104+
match (trait_item_kind, impl_item.kind) {
103105
| (Const, Const)
104106
| (Method, Method)
105107
| (Type, Type)
@@ -112,7 +114,8 @@ impl<'tcx> Node {
112114
| (OpaqueTy, _)
113115
=> false,
114116
}
115-
})
117+
})
118+
.copied()
116119
}
117120

118121
pub fn def_id(&self) -> DefId {

src/librustc/traits/wf.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
166166
let extend_cause_with_original_assoc_item_obligation =
167167
|cause: &mut traits::ObligationCause<'_>,
168168
pred: &ty::Predicate<'_>,
169-
trait_assoc_items: ty::AssocItemsIterator<'_>| {
169+
trait_assoc_items: &[ty::AssocItem]| {
170170
let trait_item = tcx
171171
.hir()
172172
.as_local_hir_id(trait_ref.def_id)
@@ -283,6 +283,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
283283
) = (&proj.skip_binder().self_ty().kind, item.map(|i| &i.kind))
284284
{
285285
if let Some((impl_item, trait_assoc_item)) = trait_assoc_items
286+
.iter()
286287
.filter(|i| i.def_id == *item_def_id)
287288
.next()
288289
.and_then(|trait_assoc_item| {
@@ -325,7 +326,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
325326
extend_cause_with_original_assoc_item_obligation(
326327
&mut cause,
327328
&pred,
328-
trait_assoc_items.clone(),
329+
trait_assoc_items,
329330
);
330331
traits::Obligation::new(cause, param_env, pred)
331332
});

src/librustc/ty/adjustment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ impl<'tcx> OverloadedDeref<'tcx> {
122122
};
123123
let method_def_id = tcx
124124
.associated_items(trait_def_id.unwrap())
125+
.iter()
125126
.find(|m| m.kind == ty::AssocKind::Method)
126127
.unwrap()
127128
.def_id;

src/librustc/ty/instance.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ impl<'tcx> Instance<'tcx> {
376376
let fn_once = tcx.lang_items().fn_once_trait().unwrap();
377377
let call_once = tcx
378378
.associated_items(fn_once)
379+
.iter()
379380
.find(|it| it.kind == ty::AssocKind::Method)
380381
.unwrap()
381382
.def_id;

src/librustc/ty/mod.rs

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2705,14 +2705,15 @@ impl<'tcx> TyCtxt<'tcx> {
27052705
.for_each(|&body_id| f(self.hir().body_owner_def_id(body_id)));
27062706
}
27072707

2708-
pub fn provided_trait_methods(self, id: DefId) -> Vec<AssocItem> {
2708+
pub fn provided_trait_methods(self, id: DefId) -> Vec<&'tcx AssocItem> {
27092709
self.associated_items(id)
2710+
.iter()
27102711
.filter(|item| item.kind == AssocKind::Method && item.defaultness.has_value())
27112712
.collect()
27122713
}
27132714

27142715
pub fn trait_relevant_for_never(self, did: DefId) -> bool {
2715-
self.associated_items(did).any(|item| item.relevant_for_never())
2716+
self.associated_items(did).iter().any(|item| item.relevant_for_never())
27162717
}
27172718

27182719
pub fn opt_item_name(self, def_id: DefId) -> Option<Ident> {
@@ -2974,25 +2975,6 @@ impl<'tcx> TyCtxt<'tcx> {
29742975
}
29752976
}
29762977

2977-
#[derive(Copy, Clone, HashStable)]
2978-
pub struct AssocItemsIterator<'tcx> {
2979-
pub items: &'tcx [AssocItem],
2980-
}
2981-
2982-
impl<'tcx> Iterator for AssocItemsIterator<'tcx> {
2983-
type Item = AssocItem;
2984-
2985-
#[inline]
2986-
fn next(&mut self) -> Option<AssocItem> {
2987-
if let Some((first, rest)) = self.items.split_first() {
2988-
self.items = rest;
2989-
Some(*first)
2990-
} else {
2991-
None
2992-
}
2993-
}
2994-
}
2995-
29962978
#[derive(Clone, HashStable)]
29972979
pub struct AdtSizedConstraint<'tcx>(pub &'tcx [Ty<'tcx>]);
29982980

src/librustc/ty/sty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,7 @@ impl<'tcx> ProjectionTy<'tcx> {
10471047
) -> ProjectionTy<'tcx> {
10481048
let item_def_id = tcx
10491049
.associated_items(trait_ref.def_id)
1050+
.iter()
10501051
.find(|item| {
10511052
item.kind == ty::AssocKind::Type
10521053
&& tcx.hygienic_eq(item_name, item.ident, trait_ref.def_id)

0 commit comments

Comments
 (0)