Skip to content

Commit 472b416

Browse files
committed
Querify all_traits
1 parent 49a2b80 commit 472b416

File tree

8 files changed

+40
-51
lines changed

8 files changed

+40
-51
lines changed

src/librustc/dep_graph/dep_node.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,7 @@ define_dep_nodes!( <'tcx>
632632
[input] MaybeUnusedTraitImport(DefId),
633633
[input] MaybeUnusedExternCrates,
634634
[eval_always] StabilityIndex,
635+
[eval_always] AllTraits,
635636
[input] AllCrateNums,
636637
[] ExportedSymbols(CrateNum),
637638
[eval_always] CollectAndPartitionTranslationItems,

src/librustc/ty/context.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -898,12 +898,6 @@ pub struct GlobalCtxt<'tcx> {
898898

899899
layout_interner: Lock<FxHashSet<&'tcx LayoutDetails>>,
900900

901-
/// A vector of every trait accessible in the whole crate
902-
/// (i.e. including those from subcrates). This is used only for
903-
/// error reporting, and so is lazily initialized and generally
904-
/// shouldn't taint the common path (hence the RefCell).
905-
pub all_traits: RefCell<Option<Vec<DefId>>>,
906-
907901
/// A general purpose channel to throw data out the back towards LLVM worker
908902
/// threads.
909903
///
@@ -1283,7 +1277,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12831277
derive_macros: RefCell::new(NodeMap()),
12841278
stability_interner: Lock::new(FxHashSet()),
12851279
interpret_interner: Default::default(),
1286-
all_traits: RefCell::new(None),
12871280
tx_to_llvm_workers: Lock::new(tx),
12881281
output_filenames: Arc::new(output_filenames.clone()),
12891282
};

src/librustc/ty/maps/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::stability_index<'tcx> {
586586
}
587587
}
588588

589+
impl<'tcx> QueryDescription<'tcx> for queries::all_traits<'tcx> {
590+
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
591+
format!("fetching all foreign and local traits")
592+
}
593+
}
594+
589595
impl<'tcx> QueryDescription<'tcx> for queries::all_crate_nums<'tcx> {
590596
fn describe(_tcx: TyCtxt, _: CrateNum) -> String {
591597
format!("fetching all foreign CrateNum instances")

src/librustc/ty/maps/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ define_maps! { <'tcx>
386386
[] fn stability_index: stability_index_node(CrateNum) -> Lrc<stability::Index<'tcx>>,
387387
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<Vec<CrateNum>>,
388388

389+
/// A vector of every trait accessible in the whole crate
390+
/// (i.e. including those from subcrates). This is used only for
391+
/// error reporting.
392+
[] fn all_traits: all_traits_node(CrateNum) -> Lrc<Vec<DefId>>,
393+
389394
[] fn exported_symbols: ExportedSymbols(CrateNum)
390395
-> Arc<Vec<(ExportedSymbol<'tcx>, SymbolExportLevel)>>,
391396
[] fn collect_and_partition_translation_items:
@@ -575,6 +580,10 @@ fn all_crate_nums_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
575580
DepConstructor::AllCrateNums
576581
}
577582

583+
fn all_traits_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
584+
DepConstructor::AllTraits
585+
}
586+
578587
fn collect_and_partition_translation_items_node<'tcx>(_: CrateNum) -> DepConstructor<'tcx> {
579588
DepConstructor::CollectAndPartitionTranslationItems
580589
}

src/librustc/ty/maps/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
11151115
}
11161116
DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
11171117
DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
1118+
DepKind::AllTraits => { force!(all_traits, LOCAL_CRATE); }
11181119
DepKind::AllCrateNums => { force!(all_crate_nums, LOCAL_CRATE); }
11191120
DepKind::ExportedSymbols => { force!(exported_symbols, krate!()); }
11201121
DepKind::CollectAndPartitionTranslationItems => {

src/librustc_typeck/check/method/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,18 @@ use rustc_data_structures::sync::Lrc;
3131

3232
pub use self::MethodError::*;
3333
pub use self::CandidateSource::*;
34+
pub use self::suggest::TraitInfo;
3435

3536
mod confirm;
3637
pub mod probe;
3738
mod suggest;
3839

3940
use self::probe::{IsSuggestion, ProbeScope};
4041

42+
pub fn provide(providers: &mut ty::maps::Providers) {
43+
suggest::provide(providers);
44+
}
45+
4146
#[derive(Clone, Copy, Debug)]
4247
pub struct MethodCallee<'tcx> {
4348
/// Impl method ID, for inherent methods, or trait method ID, otherwise.

src/librustc_typeck/check/method/suggest.rs

Lines changed: 17 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
1414
use check::FnCtxt;
1515
use rustc::hir::map as hir_map;
16+
use rustc_data_structures::sync::Lrc;
1617
use rustc::ty::{self, Ty, TyCtxt, ToPolyTraitRef, ToPredicate, TypeFoldable};
1718
use hir::def::Def;
1819
use hir::def_id::{CRATE_DEF_INDEX, DefId};
@@ -26,12 +27,12 @@ use syntax::util::lev_distance::find_best_match_for_name;
2627
use errors::DiagnosticBuilder;
2728
use syntax_pos::Span;
2829

30+
use rustc::hir::def_id::LOCAL_CRATE;
2931
use rustc::hir;
3032
use rustc::hir::print;
3133
use rustc::infer::type_variable::TypeVariableOrigin;
3234
use rustc::ty::TyAdt;
3335

34-
use std::cell;
3536
use std::cmp::Ordering;
3637

3738
use super::{MethodError, NoMatchData, CandidateSource};
@@ -208,6 +209,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
208209
// be used exists at all, and the type is an ambiuous numeric type
209210
// ({integer}/{float}).
210211
let mut candidates = all_traits(self.tcx)
212+
.into_iter()
211213
.filter(|info| {
212214
self.associated_item(info.def_id, item_name, Namespace::Value).is_some()
213215
});
@@ -519,6 +521,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
519521
// implement, by finding ones that have the item name, and are
520522
// legal to implement.
521523
let mut candidates = all_traits(self.tcx)
524+
.into_iter()
522525
.filter(|info| {
523526
// we approximate the coherence rules to only suggest
524527
// traits that are legal to implement by requiring that
@@ -603,18 +606,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
603606
}
604607
}
605608

606-
pub type AllTraitsVec = Vec<DefId>;
607-
608609
#[derive(Copy, Clone)]
609610
pub struct TraitInfo {
610611
pub def_id: DefId,
611612
}
612613

613-
impl TraitInfo {
614-
fn new(def_id: DefId) -> TraitInfo {
615-
TraitInfo { def_id: def_id }
616-
}
617-
}
618614
impl PartialEq for TraitInfo {
619615
fn eq(&self, other: &TraitInfo) -> bool {
620616
self.cmp(other) == Ordering::Equal
@@ -638,8 +634,12 @@ impl Ord for TraitInfo {
638634
}
639635

640636
/// Retrieve all traits in this crate and any dependent crates.
641-
pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a> {
642-
if tcx.all_traits.borrow().is_none() {
637+
pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Vec<TraitInfo> {
638+
tcx.all_traits(LOCAL_CRATE).iter().map(|&def_id| TraitInfo { def_id }).collect()
639+
}
640+
641+
/// Compute all traits in this crate and any dependent crates.
642+
fn compute_all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Vec<DefId> {
643643
use rustc::hir::itemlikevisit;
644644

645645
let mut traits = vec![];
@@ -649,7 +649,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
649649
// meh.
650650
struct Visitor<'a, 'tcx: 'a> {
651651
map: &'a hir_map::Map<'tcx>,
652-
traits: &'a mut AllTraitsVec,
652+
traits: &'a mut Vec<DefId>,
653653
}
654654
impl<'v, 'a, 'tcx> itemlikevisit::ItemLikeVisitor<'v> for Visitor<'a, 'tcx> {
655655
fn visit_item(&mut self, i: &'v hir::Item) {
@@ -676,7 +676,7 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
676676
// Cross-crate:
677677
let mut external_mods = FxHashSet();
678678
fn handle_external_def(tcx: TyCtxt,
679-
traits: &mut AllTraitsVec,
679+
traits: &mut Vec<DefId>,
680680
external_mods: &mut FxHashSet<DefId>,
681681
def: Def) {
682682
let def_id = def.def_id();
@@ -703,43 +703,16 @@ pub fn all_traits<'a, 'gcx, 'tcx>(tcx: TyCtxt<'a, 'gcx, 'tcx>) -> AllTraits<'a>
703703
handle_external_def(tcx, &mut traits, &mut external_mods, Def::Mod(def_id));
704704
}
705705

706-
*tcx.all_traits.borrow_mut() = Some(traits);
707-
}
708-
709-
let borrow = tcx.all_traits.borrow();
710-
assert!(borrow.is_some());
711-
AllTraits {
712-
borrow,
713-
idx: 0,
714-
}
706+
traits
715707
}
716708

717-
pub struct AllTraits<'a> {
718-
borrow: cell::Ref<'a, Option<AllTraitsVec>>,
719-
idx: usize,
720-
}
721-
722-
impl<'a> Iterator for AllTraits<'a> {
723-
type Item = TraitInfo;
724-
725-
fn next(&mut self) -> Option<TraitInfo> {
726-
let AllTraits { ref borrow, ref mut idx } = *self;
727-
// ugh.
728-
borrow.as_ref().unwrap().get(*idx).map(|info| {
729-
*idx += 1;
730-
TraitInfo::new(*info)
731-
})
732-
}
733-
734-
fn size_hint(&self) -> (usize, Option<usize>) {
735-
let len = self.borrow.as_ref().unwrap().len() - self.idx;
736-
(len, Some(len))
709+
pub fn provide(providers: &mut ty::maps::Providers) {
710+
providers.all_traits = |tcx, cnum| {
711+
assert_eq!(cnum, LOCAL_CRATE);
712+
Lrc::new(compute_all_traits(tcx))
737713
}
738714
}
739715

740-
impl<'a> ExactSizeIterator for AllTraits<'a> {}
741-
742-
743716
struct UsePlacementFinder<'a, 'tcx: 'a, 'gcx: 'tcx> {
744717
target_module: ast::NodeId,
745718
span: Option<Span>,

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ fn check_impl_item_well_formed<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: De
730730
}
731731

732732
pub fn provide(providers: &mut Providers) {
733+
method::provide(providers);
733734
*providers = Providers {
734735
typeck_item_bodies,
735736
typeck_tables_of,

0 commit comments

Comments
 (0)