Skip to content

Commit

Permalink
Rollup merge of rust-lang#73357 - petrochenkov:tmap, r=davidtwco
Browse files Browse the repository at this point in the history
Use `LocalDefId` for import IDs in trait map

cc rust-lang#73291 (comment)
  • Loading branch information
Manishearth authored Jun 19, 2020
2 parents 2ec6bf0 + fc13fd0 commit 3e4140d
Show file tree
Hide file tree
Showing 13 changed files with 38 additions and 73 deletions.
19 changes: 3 additions & 16 deletions src/librustc_hir/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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>),
Expand Down
1 change: 1 addition & 0 deletions src/librustc_middle/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ macro_rules! arena_types {

// This is used to decode the &'tcx [Span] for InlineAsm's line_spans.
[decode] span: rustc_span::Span, rustc_span::Span;
[decode] used_trait_imports: rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>, rustc_data_structures::fx::FxHashSet<rustc_hir::def_id::LocalDefId>;
], $tcx);
)
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_middle/ich/impls_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ rustc_queries! {
}

Other {
query used_trait_imports(key: LocalDefId) -> &'tcx DefIdSet {
query used_trait_imports(key: LocalDefId) -> &'tcx FxHashSet<LocalDefId> {
desc { |tcx| "used_trait_imports `{}`", tcx.def_path_str(key.to_def_id()) }
cache_on_disk_if { true }
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use rustc_data_structures::sync::{self, Lock, Lrc, WorkerLocal};
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
use rustc_hir::definitions::{DefPathHash, Definitions};
use rustc_hir::lang_items::{self, PanicLocationLangItem};
use rustc_hir::{HirId, ItemKind, ItemLocalId, ItemLocalMap, ItemLocalSet, Node, TraitCandidate};
Expand Down Expand Up @@ -408,7 +408,7 @@ pub struct TypeckTables<'tcx> {
/// This is used for warning unused imports. During type
/// checking, this `Lrc` should not be cloned: it must have a ref-count
/// of 1 so that we can insert things into the set mutably.
pub used_trait_imports: Lrc<DefIdSet>,
pub used_trait_imports: Lrc<FxHashSet<LocalDefId>>,

/// If any errors occurred while type-checking this body,
/// this field will be set to `Some(ErrorReported)`.
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down
9 changes: 5 additions & 4 deletions src/librustc_resolve/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
25 changes: 4 additions & 21 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -881,7 +881,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
Expand Down Expand Up @@ -1287,14 +1287,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;
Expand Down Expand Up @@ -1325,17 +1318,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(),
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_typeck/check/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

self.tcx.check_stability(pick.item.def_id, Some(call_expr.hir_id), span);
Expand Down Expand Up @@ -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);
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/librustc_typeck/check/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -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
//
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ use rustc_errors::ErrorReported;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticId};
use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Res};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::lang_items::{
Expand Down Expand Up @@ -840,7 +840,7 @@ fn has_typeck_tables(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
}
}

fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &DefIdSet {
fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &FxHashSet<LocalDefId> {
&*tcx.typeck_tables_of(def_id).used_trait_imports
}

Expand Down
7 changes: 1 addition & 6 deletions src/librustc_typeck/check/writeback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@

use crate::check::FnCtxt;

use rustc_data_structures::sync::Lrc;
use rustc_errors::ErrorReported;
use rustc_hir as hir;
use rustc_hir::def_id::DefIdSet;
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
use rustc_infer::infer::error_reporting::TypeAnnotationNeeded::E0282;
use rustc_infer::infer::InferCtxt;
Expand Down Expand Up @@ -67,10 +65,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
wbcx.visit_user_provided_sigs();
wbcx.visit_generator_interior_types();

let used_trait_imports = mem::replace(
&mut self.tables.borrow_mut().used_trait_imports,
Lrc::new(DefIdSet::default()),
);
let used_trait_imports = mem::take(&mut self.tables.borrow_mut().used_trait_imports);
debug!("used_trait_imports({:?}) = {:?}", item_def_id, used_trait_imports);
wbcx.tables.used_trait_imports = used_trait_imports;

Expand Down
10 changes: 5 additions & 5 deletions src/librustc_typeck/check_unused.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_hir::def_id::{DefId, DefIdSet, LocalDefId, LOCAL_CRATE};
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_middle::ty::TyCtxt;
use rustc_session::lint;
use rustc_span::{Span, Symbol};

pub fn check_crate(tcx: TyCtxt<'_>) {
let mut used_trait_imports = DefIdSet::default();
let mut used_trait_imports = FxHashSet::default();
for &body_id in tcx.hir().krate().bodies.keys() {
let item_def_id = tcx.hir().body_owner_def_id(body_id);
let imports = tcx.used_trait_imports(item_def_id);
Expand Down Expand Up @@ -39,7 +39,7 @@ impl ItemLikeVisitor<'v> for CheckVisitor<'tcx> {

struct CheckVisitor<'tcx> {
tcx: TyCtxt<'tcx>,
used_trait_imports: DefIdSet,
used_trait_imports: FxHashSet<LocalDefId>,
}

impl CheckVisitor<'tcx> {
Expand All @@ -49,7 +49,7 @@ impl CheckVisitor<'tcx> {
return;
}

if self.used_trait_imports.contains(&def_id.to_def_id()) {
if self.used_trait_imports.contains(&def_id) {
return;
}

Expand Down

0 comments on commit 3e4140d

Please sign in to comment.