Skip to content

Commit 1c7294e

Browse files
committed
rustc: Move implementations_of_trait to a query
While we're at it, make it two separate queries so one's for rustdoc and one's for the compiler, hopefully being a bit more targeted.
1 parent 4dec2a8 commit 1c7294e

File tree

6 files changed

+71
-35
lines changed

6 files changed

+71
-35
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,9 @@ define_dep_nodes!( <'tcx>
542542
[] CrateDisambiguator(CrateNum),
543543
[] CrateHash(CrateNum),
544544
[] OriginalCrateName(CrateNum),
545+
546+
[] ImplementationsOfTrait { krate: CrateNum, trait_id: DefId },
547+
[] AllTraitImplementations(CrateNum),
545548
);
546549

547550
trait DepNodeParams<'a, 'gcx: 'tcx + 'a, 'tcx: 'a> : fmt::Debug {

src/librustc/middle/cstore.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,9 +233,6 @@ pub trait CrateStore {
233233
fn visible_parent_map<'a>(&'a self, sess: &Session) -> ::std::cell::Ref<'a, DefIdMap<DefId>>;
234234
fn item_generics_cloned(&self, def: DefId) -> ty::Generics;
235235

236-
// trait info
237-
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>;
238-
239236
// trait/impl-item info
240237
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem;
241238

@@ -328,9 +325,6 @@ impl CrateStore for DummyCrateStore {
328325
fn item_generics_cloned(&self, def: DefId) -> ty::Generics
329326
{ bug!("item_generics_cloned") }
330327

331-
// trait info
332-
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId> { vec![] }
333-
334328
// trait/impl-item info
335329
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
336330
{ bug!("associated_item_cloned") }

src/librustc/ty/maps.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,18 @@ impl<'tcx> QueryDescription for queries::original_crate_name<'tcx> {
630630
}
631631
}
632632

633+
impl<'tcx> QueryDescription for queries::implementations_of_trait<'tcx> {
634+
fn describe(_tcx: TyCtxt, _: (CrateNum, DefId)) -> String {
635+
format!("looking up implementations of a trait in a crate")
636+
}
637+
}
638+
639+
impl<'tcx> QueryDescription for queries::all_trait_implementations<'tcx> {
640+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
641+
format!("looking up all (?) trait implementations")
642+
}
643+
}
644+
633645
// If enabled, send a message to the profile-queries thread
634646
macro_rules! profq_msg {
635647
($tcx:expr, $msg:expr) => {
@@ -1213,6 +1225,11 @@ define_maps! { <'tcx>
12131225
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> Symbol,
12141226
[] fn crate_hash: CrateHash(CrateNum) -> Svh,
12151227
[] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol,
1228+
1229+
[] fn implementations_of_trait: implementations_of_trait_node((CrateNum, DefId))
1230+
-> Rc<Vec<DefId>>,
1231+
[] fn all_trait_implementations: AllTraitImplementations(CrateNum)
1232+
-> Rc<Vec<DefId>>,
12161233
}
12171234

12181235
fn type_param_predicates<'tcx>((item_id, param_id): (DefId, DefId)) -> DepConstructor<'tcx> {
@@ -1292,3 +1309,9 @@ fn lint_levels_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
12921309
fn specializes_node<'tcx>((a, b): (DefId, DefId)) -> DepConstructor<'tcx> {
12931310
DepConstructor::Specializes { impl1: a, impl2: b }
12941311
}
1312+
1313+
fn implementations_of_trait_node<'tcx>((krate, trait_id): (CrateNum, DefId))
1314+
-> DepConstructor<'tcx>
1315+
{
1316+
DepConstructor::ImplementationsOfTrait { krate, trait_id }
1317+
}

src/librustc/ty/trait_def.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,16 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
141141
pub(super) fn trait_impls_of_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
142142
trait_id: DefId)
143143
-> Rc<TraitImpls> {
144-
let remote_impls = if trait_id.is_local() {
145-
// Traits defined in the current crate can't have impls in upstream
146-
// crates, so we don't bother querying the cstore.
147-
Vec::new()
148-
} else {
149-
tcx.sess.cstore.implementations_of_trait(Some(trait_id))
150-
};
144+
let mut remote_impls = Vec::new();
145+
146+
// Traits defined in the current crate can't have impls in upstream
147+
// crates, so we don't bother querying the cstore.
148+
if !trait_id.is_local() {
149+
for cnum in tcx.sess.cstore.crates() {
150+
let impls = tcx.implementations_of_trait((cnum, trait_id));
151+
remote_impls.extend(impls.iter().cloned());
152+
}
153+
}
151154

152155
let mut blanket_impls = Vec::new();
153156
let mut non_blanket_impls = FxHashMap();

src/librustc_metadata/cstore_impl.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ use rustc::hir::svh::Svh;
4242
use rustc::hir;
4343

4444
macro_rules! provide {
45-
(<$lt:tt> $tcx:ident, $def_id:ident, $cdata:ident, $($name:ident => $compute:block)*) => {
45+
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
46+
$($name:ident => $compute:block)*) => {
4647
pub fn provide<$lt>(providers: &mut Providers<$lt>) {
4748
$(fn $name<'a, $lt:$lt, T>($tcx: TyCtxt<'a, $lt, $lt>, def_id_arg: T)
4849
-> <ty::queries::$name<$lt> as
4950
QueryConfig>::Value
50-
where T: IntoDefId,
51+
where T: IntoArgs,
5152
{
52-
let $def_id = def_id_arg.into_def_id();
53+
#[allow(unused_variables)]
54+
let ($def_id, $other) = def_id_arg.into_args();
5355
assert!(!$def_id.is_local());
5456

5557
let def_path_hash = $tcx.def_path_hash($def_id);
@@ -71,19 +73,25 @@ macro_rules! provide {
7173
}
7274
}
7375

74-
trait IntoDefId {
75-
fn into_def_id(self) -> DefId;
76+
// small trait to work around different signature queries all being defined via
77+
// the macro above.
78+
trait IntoArgs {
79+
fn into_args(self) -> (DefId, DefId);
7680
}
7781

78-
impl IntoDefId for DefId {
79-
fn into_def_id(self) -> DefId { self }
82+
impl IntoArgs for DefId {
83+
fn into_args(self) -> (DefId, DefId) { (self, self) }
8084
}
8185

82-
impl IntoDefId for CrateNum {
83-
fn into_def_id(self) -> DefId { self.as_def_id() }
86+
impl IntoArgs for CrateNum {
87+
fn into_args(self) -> (DefId, DefId) { (self.as_def_id(), self.as_def_id()) }
8488
}
8589

86-
provide! { <'tcx> tcx, def_id, cdata,
90+
impl IntoArgs for (CrateNum, DefId) {
91+
fn into_args(self) -> (DefId, DefId) { (self.0.as_def_id(), self.1) }
92+
}
93+
94+
provide! { <'tcx> tcx, def_id, other, cdata,
8795
type_of => { cdata.get_type(def_id.index, tcx) }
8896
generics_of => { tcx.alloc_generics(cdata.get_generics(def_id.index)) }
8997
predicates_of => { cdata.get_predicates(def_id.index, tcx) }
@@ -178,6 +186,19 @@ provide! { <'tcx> tcx, def_id, cdata,
178186
crate_disambiguator => { cdata.disambiguator() }
179187
crate_hash => { cdata.hash() }
180188
original_crate_name => { cdata.name() }
189+
190+
implementations_of_trait => {
191+
let mut result = vec![];
192+
let filter = Some(other);
193+
cdata.get_implementations_for_trait(filter, &tcx.dep_graph, &mut result);
194+
Rc::new(result)
195+
}
196+
197+
all_trait_implementations => {
198+
let mut result = vec![];
199+
cdata.get_implementations_for_trait(None, &tcx.dep_graph, &mut result);
200+
Rc::new(result)
201+
}
181202
}
182203

183204
pub fn provide_local<'tcx>(providers: &mut Providers<'tcx>) {
@@ -217,16 +238,6 @@ impl CrateStore for cstore::CStore {
217238
self.get_crate_data(def.krate).get_generics(def.index)
218239
}
219240

220-
fn implementations_of_trait(&self, filter: Option<DefId>) -> Vec<DefId>
221-
{
222-
let mut result = vec![];
223-
224-
self.iter_crate_data(|_, cdata| {
225-
cdata.get_implementations_for_trait(filter, &self.dep_graph, &mut result)
226-
});
227-
result
228-
}
229-
230241
fn associated_item_cloned(&self, def: DefId) -> ty::AssociatedItem
231242
{
232243
self.read_dep_node(def);

src/librustdoc/clean/inline.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,10 @@ pub fn build_impls(cx: &DocContext, did: DefId) -> Vec<clean::Item> {
236236

237237
cx.populated_all_crate_impls.set(true);
238238

239-
for did in tcx.sess.cstore.implementations_of_trait(None) {
240-
build_impl(cx, did, &mut impls);
239+
for cnum in tcx.sess.cstore.crates() {
240+
for did in tcx.all_trait_implementations(cnum).iter() {
241+
build_impl(cx, *did, &mut impls);
242+
}
241243
}
242244

243245
// Also try to inline primitive impls from other crates.

0 commit comments

Comments
 (0)