Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Make TraitCandidate generic.
  • Loading branch information
cjgillot committed Feb 13, 2020
commit 2a899e2a2f3eb442af7637f3b9fce529f8441978
5 changes: 4 additions & 1 deletion src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,10 @@ impl<'tcx> TyCtxt<'tcx> {
for (k, v) in resolutions.trait_map {
let hir_id = hir.node_to_hir_id(k);
let map = trait_map.entry(hir_id.owner).or_default();
let v = v.into_iter().map(|tc| tc.node_to_hir_id(&hir.definitions())).collect();
let v = v
.into_iter()
.map(|tc| tc.map_import_ids(|id| hir.definitions().node_to_hir_id(id)))
.collect();
map.insert(hir_id.local_id, StableVec::new(v));
}

Expand Down
22 changes: 2 additions & 20 deletions src/librustc/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use rustc_data_structures::sync::{self, par_iter, Lrc, ParallelIterator};
use rustc_hir as hir;
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::{Constness, GlobMap, Node};
use rustc_hir::{Constness, GlobMap, Node, TraitMap};
use rustc_index::vec::{Idx, IndexVec};
use rustc_macros::HashStable;
use rustc_serialize::{self, Encodable, Encoder};
Expand All @@ -46,7 +46,6 @@ use rustc_target::abi::Align;
use syntax::ast::{self, Ident, Name};
use syntax::node_id::{NodeId, NodeMap, NodeSet};

use smallvec::SmallVec;
use std::cell::RefCell;
use std::cmp::{self, Ordering};
use std::fmt;
Expand Down Expand Up @@ -123,28 +122,11 @@ mod sty;

// Data types

#[derive(Clone, Debug)]
pub struct TraitCandidate {
pub def_id: DefId,
pub import_ids: SmallVec<[NodeId; 1]>,
}

impl TraitCandidate {
fn node_to_hir_id(self, definitions: &hir_map::Definitions) -> hir::TraitCandidate {
let TraitCandidate { def_id, import_ids } = self;
let import_ids =
import_ids.into_iter().map(|node_id| definitions.node_to_hir_id(node_id)).collect();
hir::TraitCandidate { def_id, import_ids }
}
}

pub type TraitMap = NodeMap<Vec<TraitCandidate>>;

pub struct ResolverOutputs {
pub definitions: hir_map::Definitions,
pub cstore: Box<CrateStoreDyn>,
pub extern_crate_map: NodeMap<CrateNum>,
pub trait_map: TraitMap,
pub trait_map: TraitMap<NodeId>,
pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
pub export_map: ExportMap<NodeId>,
Expand Down
17 changes: 14 additions & 3 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2608,13 +2608,24 @@ pub type CaptureModeMap = NodeMap<CaptureBy>;
// has length > 0 if the trait is found through an chain of imports, starting with the
// import/use statement in the scope where the trait is used.
#[derive(Clone, Debug)]
pub struct TraitCandidate {
pub struct TraitCandidate<ID = HirId> {
pub def_id: DefId,
pub import_ids: SmallVec<[HirId; 1]>,
pub import_ids: SmallVec<[ID; 1]>,
}

impl<ID> TraitCandidate<ID> {
pub fn map_import_ids<F, T>(self, f: F) -> TraitCandidate<T>
where
F: Fn(ID) -> T,
{
let TraitCandidate { def_id, import_ids } = self;
let import_ids = import_ids.into_iter().map(f).collect();
TraitCandidate { def_id, import_ids }
}
}

// Trait method resolution
pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
pub type TraitMap<ID = HirId> = NodeMap<Vec<TraitCandidate<ID>>>;

// Map from the NodeId of a glob import to a list of items which are actually
// imported.
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use crate::{path_names_to_string, BindingError, CrateLint, LexicalScopeBinding};
use crate::{Module, ModuleOrUniformRoot, NameBindingKind, ParentScope, PathResult};
use crate::{ResolutionError, Resolver, Segment, UseError};

use rustc::ty::TraitCandidate;
use rustc::{bug, lint, span_bug};
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::DiagnosticId;
use rustc_hir::def::Namespace::{self, *};
use rustc_hir::def::{self, CtorKind, DefKind, PartialRes, PerNS};
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc_hir::TraitCandidate;
use rustc_span::symbol::{kw, sym};
use rustc_span::Span;
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -2078,7 +2078,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
&mut self,
mut ident: Ident,
ns: Namespace,
) -> Vec<TraitCandidate> {
) -> Vec<TraitCandidate<NodeId>> {
debug!("(getting traits containing item) looking for '{}'", ident.name);

let mut found_traits = Vec::new();
Expand Down Expand Up @@ -2123,7 +2123,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
ident: Ident,
ns: Namespace,
module: Module<'a>,
found_traits: &mut Vec<TraitCandidate>,
found_traits: &mut Vec<TraitCandidate<NodeId>>,
) {
assert!(ns == TypeNS || ns == ValueNS);
let mut traits = module.traits.borrow_mut();
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc::lint;
use rustc::middle::cstore::{CrateStore, MetadataLoaderDyn};
use rustc::span_bug;
use rustc::ty::query::Providers;
use rustc::ty::{self, DefIdTree, ResolverOutputs, TraitMap};
use rustc::ty::{self, DefIdTree, ResolverOutputs};
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap};
use rustc_data_structures::ptr_key::PtrKey;
Expand All @@ -32,8 +32,8 @@ use rustc_expand::base::SyntaxExtension;
use rustc_hir::def::Namespace::*;
use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::GlobMap;
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
use rustc_hir::{GlobMap, TraitMap};
use rustc_metadata::creader::{CStore, CrateLoader};
use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
use rustc_session::Session;
Expand Down Expand Up @@ -865,7 +865,7 @@ pub struct Resolver<'a> {
/// `CrateNum` resolutions of `extern crate` items.
extern_crate_map: NodeMap<CrateNum>,
export_map: ExportMap<NodeId>,
trait_map: TraitMap,
trait_map: TraitMap<NodeId>,

/// A map from nodes to anonymous modules.
/// Anonymous modules are pseudo-modules that are implicitly created around items
Expand Down