mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-26 16:54:01 +00:00
Rollup merge of #69108 - cjgillot:trait_candidate, r=Zoxc
Use HirId in TraitCandidate. I had to duplicate the `TraitMap` type to hold `NodeId`s until AST->HIR lowering is done. r? @Zoxc
This commit is contained in:
commit
b5ee867b50
@ -270,7 +270,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::TraitCandidate {
|
||||
|
||||
let import_keys = import_ids
|
||||
.iter()
|
||||
.map(|node_id| hcx.node_to_hir_id(*node_id))
|
||||
.map(|hir_id| (hcx.local_def_path_hash(hir_id.owner), hir_id.local_id))
|
||||
.collect();
|
||||
(hcx.def_path_hash(*def_id), import_keys)
|
||||
|
@ -1161,6 +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.map_import_ids(|id| hir.definitions().node_to_hir_id(id)))
|
||||
.collect();
|
||||
map.insert(hir_id.local_id, StableVec::new(v));
|
||||
}
|
||||
|
||||
|
@ -126,7 +126,7 @@ 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>,
|
||||
|
@ -16,7 +16,7 @@ use rustc_span::source_map::{SourceMap, Spanned};
|
||||
use rustc_span::symbol::{kw, sym, Symbol};
|
||||
use rustc_span::{MultiSpan, Span, DUMMY_SP};
|
||||
use rustc_target::spec::abi::Abi;
|
||||
use syntax::ast::{self, AsmDialect, CrateSugar, Ident, Name, NodeId};
|
||||
use syntax::ast::{self, AsmDialect, CrateSugar, Ident, Name};
|
||||
use syntax::ast::{AttrVec, Attribute, FloatTy, IntTy, Label, LitKind, StrStyle, UintTy};
|
||||
pub use syntax::ast::{BorrowKind, ImplPolarity, IsAuto};
|
||||
pub use syntax::ast::{CaptureBy, Movability, Mutability};
|
||||
@ -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<[NodeId; 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.
|
||||
|
@ -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();
|
||||
@ -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();
|
||||
|
@ -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
|
||||
|
@ -902,13 +902,10 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||
for trait_candidate in applicable_traits.iter() {
|
||||
let trait_did = trait_candidate.def_id;
|
||||
if duplicates.insert(trait_did) {
|
||||
let import_ids = trait_candidate
|
||||
.import_ids
|
||||
.iter()
|
||||
.map(|node_id| self.fcx.tcx.hir().node_to_hir_id(*node_id))
|
||||
.collect();
|
||||
let result =
|
||||
self.assemble_extension_candidates_for_trait(import_ids, trait_did);
|
||||
let result = self.assemble_extension_candidates_for_trait(
|
||||
&trait_candidate.import_ids,
|
||||
trait_did,
|
||||
);
|
||||
result?;
|
||||
}
|
||||
}
|
||||
@ -920,7 +917,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||
let mut duplicates = FxHashSet::default();
|
||||
for trait_info in suggest::all_traits(self.tcx) {
|
||||
if duplicates.insert(trait_info.def_id) {
|
||||
self.assemble_extension_candidates_for_trait(smallvec![], trait_info.def_id)?;
|
||||
self.assemble_extension_candidates_for_trait(&smallvec![], trait_info.def_id)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@ -959,7 +956,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||
|
||||
fn assemble_extension_candidates_for_trait(
|
||||
&mut self,
|
||||
import_ids: SmallVec<[hir::HirId; 1]>,
|
||||
import_ids: &SmallVec<[hir::HirId; 1]>,
|
||||
trait_def_id: DefId,
|
||||
) -> Result<(), MethodError<'tcx>> {
|
||||
debug!("assemble_extension_candidates_for_trait(trait_def_id={:?})", trait_def_id);
|
||||
|
Loading…
Reference in New Issue
Block a user