]> git.lizzy.rs Git - rust.git/commitdiff
Compute all_traits_impls during resolution.
authorCamille GILLOT <gillot.camille@gmail.com>
Fri, 16 Jul 2021 19:55:10 +0000 (21:55 +0200)
committerCamille GILLOT <gillot.camille@gmail.com>
Wed, 1 Sep 2021 18:13:16 +0000 (20:13 +0200)
compiler/rustc_ast_lowering/src/item.rs
compiler/rustc_ast_lowering/src/lib.rs
compiler/rustc_hir/src/hir.rs
compiler/rustc_middle/src/hir/mod.rs
compiler/rustc_middle/src/ty/mod.rs
compiler/rustc_resolve/src/late.rs
compiler/rustc_resolve/src/lib.rs
compiler/rustc_typeck/src/coherence/mod.rs

index 8daeef0cbd95fbaa0e8c5f869fcf9eaf9079d84e..8e74a20f60aae6969b3e1b45e496aad7dbe94693 100644 (file)
@@ -383,15 +383,6 @@ fn lower_item_kind(
                             this.lower_trait_ref(trait_ref, ImplTraitContext::disallowed())
                         });
 
-                        if let Some(ref trait_ref) = trait_ref {
-                            if let Res::Def(DefKind::Trait, def_id) = trait_ref.path.res {
-                                this.trait_impls
-                                    .entry(def_id)
-                                    .or_default()
-                                    .push(lowered_trait_def_id);
-                            }
-                        }
-
                         let lowered_ty = this.lower_ty(ty, ImplTraitContext::disallowed());
 
                         (trait_ref, lowered_ty)
index 5d3a5fc20e04278e35d158df1d6de178f96fd090..7a26aa5c3d6c8f816eb06aa57d9a7942c16d2786 100644 (file)
@@ -104,8 +104,6 @@ struct LoweringContext<'a, 'hir: 'a> {
     owners: IndexVec<LocalDefId, Option<hir::OwnerNode<'hir>>>,
     bodies: BTreeMap<hir::BodyId, hir::Body<'hir>>,
 
-    trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
-
     modules: BTreeMap<LocalDefId, hir::ModuleItems>,
 
     generator_kind: Option<hir::GeneratorKind>,
@@ -324,7 +322,6 @@ pub fn lower_crate<'a, 'hir>(
         arena,
         owners: IndexVec::default(),
         bodies: BTreeMap::new(),
-        trait_impls: BTreeMap::new(),
         modules: BTreeMap::new(),
         attrs: BTreeMap::default(),
         catch_scopes: Vec::new(),
@@ -512,7 +509,6 @@ fn visit_foreign_item(&mut self, item: &'tcx ForeignItem) {
         let krate = hir::Crate {
             owners: self.owners,
             bodies: self.bodies,
-            trait_impls: self.trait_impls,
             modules: self.modules,
             proc_macros,
             trait_map,
index d629cb602145631f25acc8197bc2543ef6513aaa..489b2848b0985833f03c75f87ea1e96894cac1bc 100644 (file)
@@ -672,7 +672,6 @@ pub struct ModuleItems {
 pub struct Crate<'hir> {
     pub owners: IndexVec<LocalDefId, Option<OwnerNode<'hir>>>,
     pub bodies: BTreeMap<BodyId, Body<'hir>>,
-    pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
 
     /// A list of modules written out in the order in which they
     /// appear in the crate. This includes the main crate module.
index 158499bc0aef787661ac8e3164344293597f62c4..34aee4f1b3b3fc24703738a8be4c59927c4532c0 100644 (file)
@@ -170,7 +170,7 @@ pub fn provide(providers: &mut Providers) {
         }
     };
     providers.opt_def_kind = |tcx, def_id| tcx.hir().opt_def_kind(def_id.expect_local());
-    providers.all_local_trait_impls = |tcx, ()| &tcx.hir_crate(()).trait_impls;
+    providers.all_local_trait_impls = |tcx, ()| &tcx.resolutions(()).trait_impls;
     providers.expn_that_defined = |tcx, id| {
         let id = id.expect_local();
         tcx.resolutions(()).definitions.expansion_that_defined(id)
index cfb49b7d9b8c57681850e9ff83708d18abd9f3bc..6ff1215b149bbb9f061eddfa142ab74258ffe873 100644 (file)
@@ -44,6 +44,7 @@
 use rustc_target::abi::Align;
 
 use std::cmp::Ordering;
+use std::collections::BTreeMap;
 use std::hash::{Hash, Hasher};
 use std::ops::ControlFlow;
 use std::{fmt, ptr, str};
@@ -132,6 +133,7 @@ pub struct ResolverOutputs {
     /// via `extern crate` item and not `--extern` option or compiler built-in.
     pub extern_prelude: FxHashMap<Symbol, bool>,
     pub main_def: Option<MainDefinition>,
+    pub trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
 }
 
 #[derive(Clone, Copy, Debug)]
index 4a0287d83ffd912a7e05716343e2b4e4a33ad067..7b65ab2acf6c454cf10a174a085dfa9930b4cb58 100644 (file)
@@ -1281,7 +1281,14 @@ fn resolve_implementation(
             this.with_self_rib(Res::SelfTy(None, None), |this| {
                 // Resolve the trait reference, if necessary.
                 this.with_optional_trait_ref(opt_trait_reference.as_ref(), |this, trait_id| {
-                    let item_def_id = this.r.local_def_id(item_id).to_def_id();
+                    let item_def_id = this.r.local_def_id(item_id);
+
+                    // Register the trait definitions from here.
+                    if let Some(trait_id) = trait_id {
+                        this.r.trait_impls.entry(trait_id).or_default().push(item_def_id);
+                    }
+
+                    let item_def_id = item_def_id.to_def_id();
                     this.with_self_rib(Res::SelfTy(trait_id, Some((item_def_id, false))), |this| {
                         if let Some(trait_ref) = opt_trait_reference.as_ref() {
                             // Resolve type arguments in the trait path.
index 2101381553be127836a18d5b34115bba0844664f..51e0ee0a57fe2002f18dc3ec2ef723e4bfb54725 100644 (file)
@@ -60,7 +60,7 @@
 
 use smallvec::{smallvec, SmallVec};
 use std::cell::{Cell, RefCell};
-use std::collections::BTreeSet;
+use std::collections::{BTreeMap, BTreeSet};
 use std::ops::ControlFlow;
 use std::{cmp, fmt, iter, ptr};
 use tracing::debug;
@@ -1034,6 +1034,7 @@ pub struct Resolver<'a> {
     item_generics_num_lifetimes: FxHashMap<LocalDefId, usize>,
 
     main_def: Option<MainDefinition>,
+    trait_impls: BTreeMap<DefId, Vec<LocalDefId>>,
 }
 
 /// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1398,6 +1399,7 @@ pub fn new(
             legacy_const_generic_args: Default::default(),
             item_generics_num_lifetimes: Default::default(),
             main_def: Default::default(),
+            trait_impls: Default::default(),
         };
 
         let root_parent_scope = ParentScope::module(graph_root, &resolver);
@@ -1455,6 +1457,7 @@ pub fn into_outputs(self) -> ResolverOutputs {
                 .map(|(ident, entry)| (ident.name, entry.introduced_by_item))
                 .collect(),
             main_def,
+            trait_impls: self.trait_impls,
         }
     }
 
@@ -1474,6 +1477,7 @@ pub fn clone_outputs(&self) -> ResolverOutputs {
                 .map(|(ident, entry)| (ident.name, entry.introduced_by_item))
                 .collect(),
             main_def: self.main_def.clone(),
+            trait_impls: self.trait_impls.clone(),
         }
     }
 
index 03a9fe01795f5c70a76735257e4993d106b6effc..7ac26a31872df4792b80aeb08af9b3658cae035f 100644 (file)
@@ -195,7 +195,7 @@ fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
 }
 
 pub fn check_coherence(tcx: TyCtxt<'_>) {
-    for &trait_def_id in tcx.hir().krate().trait_impls.keys() {
+    for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
         tcx.ensure().coherent_trait(trait_def_id);
     }