Introduce get_diagnostic_name
This commit is contained in:
parent
d7539a6af0
commit
33b9b95305
9 changed files with 59 additions and 34 deletions
17
compiler/rustc_hir/src/diagnostic_items.rs
Normal file
17
compiler/rustc_hir/src/diagnostic_items.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use crate::def_id::DefId;
|
||||||
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
||||||
|
use rustc_span::Symbol;
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct DiagnosticItems {
|
||||||
|
pub id_to_name: FxHashMap<DefId, Symbol>,
|
||||||
|
pub name_to_id: FxHashMap<Symbol, DefId>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<CTX: crate::HashStableContext> HashStable<CTX> for DiagnosticItems {
|
||||||
|
#[inline]
|
||||||
|
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
|
||||||
|
self.name_to_id.hash_stable(ctx, hasher);
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,6 +18,7 @@ mod arena;
|
||||||
pub mod def;
|
pub mod def;
|
||||||
pub mod def_path_hash_map;
|
pub mod def_path_hash_map;
|
||||||
pub mod definitions;
|
pub mod definitions;
|
||||||
|
pub mod diagnostic_items;
|
||||||
pub use rustc_span::def_id;
|
pub use rustc_span::def_id;
|
||||||
mod hir;
|
mod hir;
|
||||||
pub mod hir_id;
|
pub mod hir_id;
|
||||||
|
|
|
@ -18,6 +18,7 @@ use rustc_hir as hir;
|
||||||
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
|
||||||
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
|
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
|
||||||
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
|
||||||
|
use rustc_hir::diagnostic_items::DiagnosticItems;
|
||||||
use rustc_hir::lang_items;
|
use rustc_hir::lang_items;
|
||||||
use rustc_index::vec::{Idx, IndexVec};
|
use rustc_index::vec::{Idx, IndexVec};
|
||||||
use rustc_middle::hir::exports::Export;
|
use rustc_middle::hir::exports::Export;
|
||||||
|
@ -1052,16 +1053,23 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Iterates over the diagnostic items in the given crate.
|
/// Iterates over the diagnostic items in the given crate.
|
||||||
fn get_diagnostic_items(&self) -> FxHashMap<Symbol, DefId> {
|
fn get_diagnostic_items(&self) -> DiagnosticItems {
|
||||||
if self.root.is_proc_macro_crate() {
|
if self.root.is_proc_macro_crate() {
|
||||||
// Proc macro crates do not export any diagnostic-items to the target.
|
// Proc macro crates do not export any diagnostic-items to the target.
|
||||||
Default::default()
|
Default::default()
|
||||||
} else {
|
} else {
|
||||||
self.root
|
let mut id_to_name = FxHashMap::default();
|
||||||
|
let name_to_id = self
|
||||||
|
.root
|
||||||
.diagnostic_items
|
.diagnostic_items
|
||||||
.decode(self)
|
.decode(self)
|
||||||
.map(|(name, def_index)| (name, self.local_def_id(def_index)))
|
.map(|(name, def_index)| {
|
||||||
.collect()
|
let id = self.local_def_id(def_index);
|
||||||
|
id_to_name.insert(id, name);
|
||||||
|
(name, id)
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
DiagnosticItems { id_to_name, name_to_id }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1750,7 +1750,7 @@ impl EncodeContext<'a, 'tcx> {
|
||||||
fn encode_diagnostic_items(&mut self) -> Lazy<[(Symbol, DefIndex)]> {
|
fn encode_diagnostic_items(&mut self) -> Lazy<[(Symbol, DefIndex)]> {
|
||||||
empty_proc_macro!(self);
|
empty_proc_macro!(self);
|
||||||
let tcx = self.tcx;
|
let tcx = self.tcx;
|
||||||
let diagnostic_items = tcx.diagnostic_items(LOCAL_CRATE);
|
let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id;
|
||||||
self.lazy(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
|
self.lazy(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1442,7 +1442,7 @@ rustc_queries! {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns all diagnostic items defined in all crates.
|
/// Returns all diagnostic items defined in all crates.
|
||||||
query all_diagnostic_items(_: ()) -> FxHashMap<Symbol, DefId> {
|
query all_diagnostic_items(_: ()) -> rustc_hir::diagnostic_items::DiagnosticItems {
|
||||||
storage(ArenaCacheSelector<'tcx>)
|
storage(ArenaCacheSelector<'tcx>)
|
||||||
eval_always
|
eval_always
|
||||||
desc { "calculating the diagnostic items map" }
|
desc { "calculating the diagnostic items map" }
|
||||||
|
@ -1454,7 +1454,7 @@ rustc_queries! {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the diagnostic items defined in a crate.
|
/// Returns the diagnostic items defined in a crate.
|
||||||
query diagnostic_items(_: CrateNum) -> FxHashMap<Symbol, DefId> {
|
query diagnostic_items(_: CrateNum) -> rustc_hir::diagnostic_items::DiagnosticItems {
|
||||||
storage(ArenaCacheSelector<'tcx>)
|
storage(ArenaCacheSelector<'tcx>)
|
||||||
desc { "calculating the diagnostic items map in a crate" }
|
desc { "calculating the diagnostic items map in a crate" }
|
||||||
}
|
}
|
||||||
|
|
|
@ -1232,12 +1232,17 @@ impl<'tcx> TyCtxt<'tcx> {
|
||||||
/// Obtain the given diagnostic item's `DefId`. Use `is_diagnostic_item` if you just want to
|
/// Obtain the given diagnostic item's `DefId`. Use `is_diagnostic_item` if you just want to
|
||||||
/// compare against another `DefId`, since `is_diagnostic_item` is cheaper.
|
/// compare against another `DefId`, since `is_diagnostic_item` is cheaper.
|
||||||
pub fn get_diagnostic_item(self, name: Symbol) -> Option<DefId> {
|
pub fn get_diagnostic_item(self, name: Symbol) -> Option<DefId> {
|
||||||
self.all_diagnostic_items(()).get(&name).copied()
|
self.all_diagnostic_items(()).name_to_id.get(&name).copied()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Obtain the diagnostic item's name
|
||||||
|
pub fn get_diagnostic_name(self, id: DefId) -> Option<Symbol> {
|
||||||
|
self.diagnostic_items(id.krate).id_to_name.get(&id).copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Check whether the diagnostic item with the given `name` has the given `DefId`.
|
/// Check whether the diagnostic item with the given `name` has the given `DefId`.
|
||||||
pub fn is_diagnostic_item(self, name: Symbol, did: DefId) -> bool {
|
pub fn is_diagnostic_item(self, name: Symbol, did: DefId) -> bool {
|
||||||
self.diagnostic_items(did.krate).get(&name) == Some(&did)
|
self.diagnostic_items(did.krate).name_to_id.get(&name) == Some(&did)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn stability(self) -> &'tcx stability::Index<'tcx> {
|
pub fn stability(self) -> &'tcx stability::Index<'tcx> {
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
//! * Compiler internal types like `Ty` and `TyCtxt`
|
//! * Compiler internal types like `Ty` and `TyCtxt`
|
||||||
|
|
||||||
use rustc_ast as ast;
|
use rustc_ast as ast;
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
|
use rustc_hir::diagnostic_items::DiagnosticItems;
|
||||||
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::TyCtxt;
|
use rustc_middle::ty::TyCtxt;
|
||||||
|
@ -19,9 +19,8 @@ use rustc_span::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
|
||||||
use rustc_span::symbol::{sym, Symbol};
|
use rustc_span::symbol::{sym, Symbol};
|
||||||
|
|
||||||
struct DiagnosticItemCollector<'tcx> {
|
struct DiagnosticItemCollector<'tcx> {
|
||||||
// items from this crate
|
|
||||||
items: FxHashMap<Symbol, DefId>,
|
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
|
diagnostic_items: DiagnosticItems,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
|
impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
|
||||||
|
@ -44,7 +43,7 @@ impl<'v, 'tcx> ItemLikeVisitor<'v> for DiagnosticItemCollector<'tcx> {
|
||||||
|
|
||||||
impl<'tcx> DiagnosticItemCollector<'tcx> {
|
impl<'tcx> DiagnosticItemCollector<'tcx> {
|
||||||
fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
|
fn new(tcx: TyCtxt<'tcx>) -> DiagnosticItemCollector<'tcx> {
|
||||||
DiagnosticItemCollector { tcx, items: Default::default() }
|
DiagnosticItemCollector { tcx, diagnostic_items: DiagnosticItems::default() }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn observe_item(&mut self, def_id: LocalDefId) {
|
fn observe_item(&mut self, def_id: LocalDefId) {
|
||||||
|
@ -52,19 +51,14 @@ impl<'tcx> DiagnosticItemCollector<'tcx> {
|
||||||
let attrs = self.tcx.hir().attrs(hir_id);
|
let attrs = self.tcx.hir().attrs(hir_id);
|
||||||
if let Some(name) = extract(attrs) {
|
if let Some(name) = extract(attrs) {
|
||||||
// insert into our table
|
// insert into our table
|
||||||
collect_item(self.tcx, &mut self.items, name, def_id.to_def_id());
|
collect_item(self.tcx, &mut self.diagnostic_items, name, def_id.to_def_id());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn collect_item(
|
fn collect_item(tcx: TyCtxt<'_>, items: &mut DiagnosticItems, name: Symbol, item_def_id: DefId) {
|
||||||
tcx: TyCtxt<'_>,
|
items.id_to_name.insert(item_def_id, name);
|
||||||
items: &mut FxHashMap<Symbol, DefId>,
|
if let Some(original_def_id) = items.name_to_id.insert(name, item_def_id) {
|
||||||
name: Symbol,
|
|
||||||
item_def_id: DefId,
|
|
||||||
) {
|
|
||||||
// Check for duplicates.
|
|
||||||
if let Some(original_def_id) = items.insert(name, item_def_id) {
|
|
||||||
if original_def_id != item_def_id {
|
if original_def_id != item_def_id {
|
||||||
let mut err = match tcx.hir().span_if_local(item_def_id) {
|
let mut err = match tcx.hir().span_if_local(item_def_id) {
|
||||||
Some(span) => tcx.sess.struct_span_err(
|
Some(span) => tcx.sess.struct_span_err(
|
||||||
|
@ -98,7 +92,7 @@ fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Traverse and collect the diagnostic items in the current
|
/// Traverse and collect the diagnostic items in the current
|
||||||
fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol, DefId> {
|
fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> DiagnosticItems {
|
||||||
assert_eq!(cnum, LOCAL_CRATE);
|
assert_eq!(cnum, LOCAL_CRATE);
|
||||||
|
|
||||||
// Initialize the collector.
|
// Initialize the collector.
|
||||||
|
@ -107,22 +101,22 @@ fn diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> FxHashMap<Symbol
|
||||||
// Collect diagnostic items in this crate.
|
// Collect diagnostic items in this crate.
|
||||||
tcx.hir().visit_all_item_likes(&mut collector);
|
tcx.hir().visit_all_item_likes(&mut collector);
|
||||||
|
|
||||||
collector.items
|
collector.diagnostic_items
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Traverse and collect all the diagnostic items in all crates.
|
/// Traverse and collect all the diagnostic items in all crates.
|
||||||
fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashMap<Symbol, DefId> {
|
fn all_diagnostic_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> DiagnosticItems {
|
||||||
// Initialize the collector.
|
// Initialize the collector.
|
||||||
let mut collector = FxHashMap::default();
|
let mut items = DiagnosticItems::default();
|
||||||
|
|
||||||
// Collect diagnostic items in other crates.
|
// Collect diagnostic items in other crates.
|
||||||
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
|
for &cnum in tcx.crates(()).iter().chain(std::iter::once(&LOCAL_CRATE)) {
|
||||||
for (&name, &def_id) in tcx.diagnostic_items(cnum).iter() {
|
for (&name, &def_id) in &tcx.diagnostic_items(cnum).name_to_id {
|
||||||
collect_item(tcx, &mut collector, name, def_id);
|
collect_item(tcx, &mut items, name, def_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
collector
|
items
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn provide(providers: &mut Providers) {
|
pub fn provide(providers: &mut Providers) {
|
||||||
|
|
|
@ -1636,12 +1636,12 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
|
||||||
|
|
||||||
// Special case the primary error message when send or sync is the trait that was
|
// Special case the primary error message when send or sync is the trait that was
|
||||||
// not implemented.
|
// not implemented.
|
||||||
let is_send = self.tcx.is_diagnostic_item(sym::Send, trait_ref.def_id);
|
|
||||||
let is_sync = self.tcx.is_diagnostic_item(sym::Sync, trait_ref.def_id);
|
|
||||||
let hir = self.tcx.hir();
|
let hir = self.tcx.hir();
|
||||||
let trait_explanation = if is_send || is_sync {
|
let trait_explanation = if let Some(name @ (sym::Send | sym::Sync)) =
|
||||||
|
self.tcx.get_diagnostic_name(trait_ref.def_id)
|
||||||
|
{
|
||||||
let (trait_name, trait_verb) =
|
let (trait_name, trait_verb) =
|
||||||
if is_send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
|
if name == sym::Send { ("`Send`", "sent") } else { ("`Sync`", "shared") };
|
||||||
|
|
||||||
err.clear_code();
|
err.clear_code();
|
||||||
err.set_primary_message(format!(
|
err.set_primary_message(format!(
|
||||||
|
|
|
@ -1076,7 +1076,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
if adt_def.did.is_local() {
|
if adt_def.did.is_local() {
|
||||||
let diagnostic_items = self.tcx.diagnostic_items(trait_ref.def_id.krate);
|
let diagnostic_items = self.tcx.diagnostic_items(trait_ref.def_id.krate);
|
||||||
return derivables.iter().find_map(|trait_derivable| {
|
return derivables.iter().find_map(|trait_derivable| {
|
||||||
let item_def_id = diagnostic_items.get(trait_derivable)?;
|
let item_def_id = diagnostic_items.name_to_id.get(trait_derivable)?;
|
||||||
if item_def_id == &trait_pred.trait_ref.def_id
|
if item_def_id == &trait_pred.trait_ref.def_id
|
||||||
&& !(adt_def.is_enum() && *trait_derivable == sym::Default)
|
&& !(adt_def.is_enum() && *trait_derivable == sym::Default)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue