Skip to content

Commit 6359780

Browse files
committed
Compute all_traits_impls during resolution.
1 parent 26eeec0 commit 6359780

File tree

8 files changed

+17
-18
lines changed

8 files changed

+17
-18
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
383383
this.lower_trait_ref(trait_ref, ImplTraitContext::disallowed())
384384
});
385385

386-
if let Some(ref trait_ref) = trait_ref {
387-
if let Res::Def(DefKind::Trait, def_id) = trait_ref.path.res {
388-
this.trait_impls
389-
.entry(def_id)
390-
.or_default()
391-
.push(lowered_trait_def_id);
392-
}
393-
}
394-
395386
let lowered_ty = this.lower_ty(ty, ImplTraitContext::disallowed());
396387

397388
(trait_ref, lowered_ty)

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,6 @@ struct LoweringContext<'a, 'hir: 'a> {
104104
owners: IndexVec<LocalDefId, Option<hir::OwnerNode<'hir>>>,
105105
bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
106106

107-
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
108-
109107
modules: BTreeMap<LocalDefId, hir::ModuleItems>,
110108

111109
generator_kind: Option<hir::GeneratorKind>,
@@ -324,7 +322,6 @@ pub fn lower_crate<'a, 'hir>(
324322
arena,
325323
owners: IndexVec::default(),
326324
bodies: BTreeMap::new(),
327-
trait_impls: BTreeMap::new(),
328325
modules: BTreeMap::new(),
329326
attrs: BTreeMap::default(),
330327
catch_scopes: Vec::new(),
@@ -512,7 +509,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
512509
let krate = hir::Crate {
513510
owners: self.owners,
514511
bodies: self.bodies,
515-
trait_impls: self.trait_impls,
516512
modules: self.modules,
517513
proc_macros,
518514
trait_map,

compiler/rustc_hir/src/hir.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,6 @@ pub struct ModuleItems {
672672
pub struct Crate<'hir> {
673673
pub owners: IndexVec<LocalDefId, Option<OwnerNode<'hir>>>,
674674
pub bodies: BTreeMap<BodyId, Body<'hir>>,
675-
pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
676675

677676
/// A list of modules written out in the order in which they
678677
/// appear in the crate. This includes the main crate module.

compiler/rustc_middle/src/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn provide(providers: &mut Providers) {
170170
}
171171
};
172172
providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
173-
providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
173+
providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
174174
providers.expn_that_defined = |tcx, id| {
175175
let id = id.expect_local();
176176
tcx.resolutions(()).definitions.expansion_that_defined(id)

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use rustc_span::Span;
4444
use rustc_target::abi::Align;
4545

4646
use std::cmp::Ordering;
47+
use std::collections::BTreeMap;
4748
use std::hash::{Hash, Hasher};
4849
use std::ops::ControlFlow;
4950
use std::{fmt, ptr, str};
@@ -132,6 +133,7 @@ pub struct ResolverOutputs {
132133
/// via `extern crate` item and not `--extern` option or compiler built-in.
133134
pub extern_prelude: FxHashMap<Symbol, bool>,
134135
pub main_def: Option<MainDefinition>,
136+
pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
135137
}
136138

137139
#[derive(Clone, Copy, Debug)]

compiler/rustc_resolve/src/late.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1281,7 +1281,14 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
12811281
this.with_self_rib(Res::SelfTy(None, None), |this| {
12821282
// Resolve the trait reference, if necessary.
12831283
this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
1284-
let item_def_id = this.r.local_def_id(item_id).to_def_id();
1284+
let item_def_id = this.r.local_def_id(item_id);
1285+
1286+
// Register the trait definitions from here.
1287+
if let Some(trait_id) = trait_id {
1288+
this.r.trait_impls.entry(trait_id).or_default().push(item_def_id);
1289+
}
1290+
1291+
let item_def_id = item_def_id.to_def_id();
12851292
this.with_self_rib(Res::SelfTy(trait_id, Some((item_def_id, false))), |this| {
12861293
if let Some(trait_ref) = opt_trait_reference.as_ref() {
12871294
// Resolve type arguments in the trait path.

compiler/rustc_resolve/src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use rustc_span::{Span, DUMMY_SP};
6060

6161
use smallvec::{smallvec, SmallVec};
6262
use std::cell::{Cell, RefCell};
63-
use std::collections::BTreeSet;
63+
use std::collections::{BTreeMap, BTreeSet};
6464
use std::ops::ControlFlow;
6565
use std::{cmp, fmt, iter, ptr};
6666
use tracing::debug;
@@ -1034,6 +1034,7 @@ pub struct Resolver<'a> {
10341034
item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
10351035

10361036
main_def: Option<MainDefinition>,
1037+
trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
10371038
}
10381039

10391040
/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1398,6 +1399,7 @@ impl<'a> Resolver<'a> {
13981399
legacy_const_generic_args: Default::default(),
13991400
item_generics_num_lifetimes: Default::default(),
14001401
main_def: Default::default(),
1402+
trait_impls: Default::default(),
14011403
};
14021404

14031405
let root_parent_scope = ParentScope::module(graph_root, &resolver);
@@ -1455,6 +1457,7 @@ impl<'a> Resolver<'a> {
14551457
.map(|(ident, entry)| (ident.name, entry.introduced_by_item))
14561458
.collect(),
14571459
main_def,
1460+
trait_impls: self.trait_impls,
14581461
}
14591462
}
14601463

@@ -1474,6 +1477,7 @@ impl<'a> Resolver<'a> {
14741477
.map(|(ident, entry)| (ident.name, entry.introduced_by_item))
14751478
.collect(),
14761479
main_def: self.main_def.clone(),
1480+
trait_impls: self.trait_impls.clone(),
14771481
}
14781482
}
14791483

compiler/rustc_typeck/src/coherence/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
195195
}
196196

197197
pub fn check_coherence(tcx: TyCtxt<'_>) {
198-
for &trait_def_id in tcx.hir().krate().trait_impls.keys() {
198+
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
199199
tcx.ensure().coherent_trait(trait_def_id);
200200
}
201201

0 commit comments

Comments
 (0)