Use LocalDefId for import IDs in trait map
This commit is contained in:
parent
ff4a2533a0
commit
a4337ccc10
7 changed files with 26 additions and 57 deletions
|
|
@ -12,6 +12,7 @@ use rustc_ast::node_id::NodeMap;
|
|||
use rustc_ast::util::parser::ExprPrecedence;
|
||||
use rustc_data_structures::sync::{par_for_each_in, Send, Sync};
|
||||
use rustc_macros::HashStable_Generic;
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::source_map::{SourceMap, Spanned};
|
||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||
use rustc_span::{MultiSpan, Span, DUMMY_SP};
|
||||
|
|
@ -2651,25 +2652,11 @@ 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<ID = HirId> {
|
||||
pub struct TraitCandidate {
|
||||
pub def_id: DefId,
|
||||
pub import_ids: SmallVec<[ID; 1]>,
|
||||
pub import_ids: SmallVec<[LocalDefId; 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<ID = HirId> = NodeMap<Vec<TraitCandidate<ID>>>;
|
||||
|
||||
#[derive(Copy, Clone, Debug, HashStable_Generic)]
|
||||
pub enum Node<'hir> {
|
||||
Param(&'hir Param<'hir>),
|
||||
|
|
|
|||
|
|
@ -210,16 +210,15 @@ impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitCandidate {
|
|||
}
|
||||
|
||||
impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
|
||||
type KeyType = (DefPathHash, SmallVec<[(DefPathHash, hir::ItemLocalId); 1]>);
|
||||
type KeyType = (DefPathHash, SmallVec<[DefPathHash; 1]>);
|
||||
|
||||
fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Self::KeyType {
|
||||
let hir::TraitCandidate { def_id, import_ids } = self;
|
||||
|
||||
let import_keys = import_ids
|
||||
.iter()
|
||||
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), hir_id.local_id))
|
||||
.collect();
|
||||
(hcx.def_path_hash(*def_id), import_keys)
|
||||
(
|
||||
hcx.def_path_hash(*def_id),
|
||||
import_ids.iter().map(|def_id| hcx.local_def_path_hash(*def_id)).collect(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -121,7 +121,7 @@ pub struct ResolverOutputs {
|
|||
pub definitions: rustc_hir::definitions::Definitions,
|
||||
pub cstore: Box<CrateStoreDyn>,
|
||||
pub extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
|
||||
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate<hir::HirId>>>,
|
||||
pub trait_map: FxHashMap<hir::HirId, Vec<hir::TraitCandidate>>,
|
||||
pub maybe_unused_trait_imports: FxHashSet<LocalDefId>,
|
||||
pub maybe_unused_extern_crates: Vec<(LocalDefId, Span)>,
|
||||
pub export_map: ExportMap<LocalDefId>,
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@ use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
|
|||
use rustc_hir::TraitCandidate;
|
||||
use rustc_middle::{bug, span_bug};
|
||||
use rustc_session::lint;
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::symbol::{kw, sym, Ident, Symbol};
|
||||
use rustc_span::Span;
|
||||
use smallvec::{smallvec, SmallVec};
|
||||
|
|
@ -2188,7 +2189,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
&mut self,
|
||||
mut ident: Ident,
|
||||
ns: Namespace,
|
||||
) -> Vec<TraitCandidate<NodeId>> {
|
||||
) -> Vec<TraitCandidate> {
|
||||
debug!("(getting traits containing item) looking for '{}'", ident.name);
|
||||
|
||||
let mut found_traits = Vec::new();
|
||||
|
|
@ -2233,7 +2234,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
ident: Ident,
|
||||
ns: Namespace,
|
||||
module: Module<'a>,
|
||||
found_traits: &mut Vec<TraitCandidate<NodeId>>,
|
||||
found_traits: &mut Vec<TraitCandidate>,
|
||||
) {
|
||||
assert!(ns == TypeNS || ns == ValueNS);
|
||||
let mut traits = module.traits.borrow_mut();
|
||||
|
|
@ -2292,13 +2293,13 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
|
|||
&mut self,
|
||||
mut kind: &NameBindingKind<'_>,
|
||||
trait_name: Ident,
|
||||
) -> SmallVec<[NodeId; 1]> {
|
||||
) -> SmallVec<[LocalDefId; 1]> {
|
||||
let mut import_ids = smallvec![];
|
||||
while let NameBindingKind::Import { import, binding, .. } = kind {
|
||||
let id = self.r.definitions.local_def_id(import.id);
|
||||
self.r.maybe_unused_trait_imports.insert(id);
|
||||
self.r.add_to_glob_map(&import, trait_name);
|
||||
import_ids.push(import.id);
|
||||
import_ids.push(id);
|
||||
kind = &binding.kind;
|
||||
}
|
||||
import_ids
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ use rustc_hir::def::{self, CtorOf, DefKind, NonMacroAttrKind, PartialRes};
|
|||
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, CRATE_DEF_INDEX};
|
||||
use rustc_hir::definitions::{DefKey, Definitions};
|
||||
use rustc_hir::PrimTy::{self, Bool, Char, Float, Int, Str, Uint};
|
||||
use rustc_hir::TraitMap;
|
||||
use rustc_hir::TraitCandidate;
|
||||
use rustc_metadata::creader::{CStore, CrateLoader};
|
||||
use rustc_middle::hir::exports::ExportMap;
|
||||
use rustc_middle::middle::cstore::{CrateStore, MetadataLoaderDyn};
|
||||
|
|
@ -879,7 +879,7 @@ pub struct Resolver<'a> {
|
|||
/// `CrateNum` resolutions of `extern crate` items.
|
||||
extern_crate_map: FxHashMap<LocalDefId, CrateNum>,
|
||||
export_map: ExportMap<LocalDefId>,
|
||||
trait_map: TraitMap<NodeId>,
|
||||
trait_map: NodeMap<Vec<TraitCandidate>>,
|
||||
|
||||
/// A map from nodes to anonymous modules.
|
||||
/// Anonymous modules are pseudo-modules that are implicitly created around items
|
||||
|
|
@ -1285,14 +1285,7 @@ impl<'a> Resolver<'a> {
|
|||
let trait_map = self
|
||||
.trait_map
|
||||
.into_iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
definitions.node_id_to_hir_id(k),
|
||||
v.into_iter()
|
||||
.map(|tc| tc.map_import_ids(|id| definitions.node_id_to_hir_id(id)))
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
.map(|(k, v)| (definitions.node_id_to_hir_id(k), v))
|
||||
.collect();
|
||||
let maybe_unused_trait_imports = self.maybe_unused_trait_imports;
|
||||
let maybe_unused_extern_crates = self.maybe_unused_extern_crates;
|
||||
|
|
@ -1323,17 +1316,7 @@ impl<'a> Resolver<'a> {
|
|||
trait_map: self
|
||||
.trait_map
|
||||
.iter()
|
||||
.map(|(&k, v)| {
|
||||
(
|
||||
self.definitions.node_id_to_hir_id(k),
|
||||
v.iter()
|
||||
.cloned()
|
||||
.map(|tc| {
|
||||
tc.map_import_ids(|id| self.definitions.node_id_to_hir_id(id))
|
||||
})
|
||||
.collect(),
|
||||
)
|
||||
})
|
||||
.map(|(&k, v)| (self.definitions.node_id_to_hir_id(k), v.clone()))
|
||||
.collect(),
|
||||
glob_map: self.glob_map.clone(),
|
||||
maybe_unused_trait_imports: self.maybe_unused_trait_imports.clone(),
|
||||
|
|
|
|||
|
|
@ -194,11 +194,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
self.lookup_probe(span, segment.ident, self_ty, call_expr, ProbeScope::TraitsInScope)?;
|
||||
|
||||
for import_id in &pick.import_ids {
|
||||
let import_def_id = self.tcx.hir().local_def_id(*import_id);
|
||||
debug!("used_trait_import: {:?}", import_def_id);
|
||||
debug!("used_trait_import: {:?}", import_id);
|
||||
Lrc::get_mut(&mut self.tables.borrow_mut().used_trait_imports)
|
||||
.unwrap()
|
||||
.insert(import_def_id.to_def_id());
|
||||
.insert(import_id.to_def_id());
|
||||
}
|
||||
|
||||
self.tcx.check_stability(pick.item.def_id, Some(call_expr.hir_id), span);
|
||||
|
|
@ -461,9 +460,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
|||
let mut tables = self.tables.borrow_mut();
|
||||
let used_trait_imports = Lrc::get_mut(&mut tables.used_trait_imports).unwrap();
|
||||
for import_id in pick.import_ids {
|
||||
let import_def_id = tcx.hir().local_def_id(import_id);
|
||||
debug!("resolve_ufcs: used_trait_import: {:?}", import_def_id);
|
||||
used_trait_imports.insert(import_def_id.to_def_id());
|
||||
debug!("resolve_ufcs: used_trait_import: {:?}", import_id);
|
||||
used_trait_imports.insert(import_id.to_def_id());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -28,6 +28,7 @@ use rustc_middle::ty::{
|
|||
};
|
||||
use rustc_session::config::nightly_options;
|
||||
use rustc_session::lint;
|
||||
use rustc_span::def_id::LocalDefId;
|
||||
use rustc_span::{symbol::Ident, Span, Symbol, DUMMY_SP};
|
||||
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
|
||||
use rustc_trait_selection::traits::query::method_autoderef::MethodAutoderefBadTy;
|
||||
|
|
@ -129,7 +130,7 @@ struct Candidate<'tcx> {
|
|||
xform_ret_ty: Option<Ty<'tcx>>,
|
||||
item: ty::AssocItem,
|
||||
kind: CandidateKind<'tcx>,
|
||||
import_ids: SmallVec<[hir::HirId; 1]>,
|
||||
import_ids: SmallVec<[LocalDefId; 1]>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -158,7 +159,7 @@ enum ProbeResult {
|
|||
pub struct Pick<'tcx> {
|
||||
pub item: ty::AssocItem,
|
||||
pub kind: PickKind<'tcx>,
|
||||
pub import_ids: SmallVec<[hir::HirId; 1]>,
|
||||
pub import_ids: SmallVec<[LocalDefId; 1]>,
|
||||
|
||||
// Indicates that the source expression should be autoderef'd N times
|
||||
//
|
||||
|
|
@ -930,7 +931,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
|||
|
||||
fn assemble_extension_candidates_for_trait(
|
||||
&mut self,
|
||||
import_ids: &SmallVec<[hir::HirId; 1]>,
|
||||
import_ids: &SmallVec<[LocalDefId; 1]>,
|
||||
trait_def_id: DefId,
|
||||
) -> Result<(), MethodError<'tcx>> {
|
||||
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue