Skip to content

Commit

Permalink
Removing lang item-based matching for KnownNames
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsemakula committed Jan 1, 2025
1 parent a049e35 commit 46944ed
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 32 deletions.
3 changes: 1 addition & 2 deletions checker/src/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,15 +151,14 @@ impl MiraiCallbacks {
self.file_name, summary_store_path
);
let call_graph_config = self.options.call_graph_config.to_owned();
let lang_items = tcx.lang_items();
let mut crate_visitor = CrateVisitor {
buffered_diagnostics: Vec::new(),
constant_time_tag_cache: None,
constant_time_tag_not_found: false,
constant_value_cache: ConstantValueCache::default(),
diagnostics_for: HashMap::new(),
file_name: self.file_name.as_str(),
known_names_cache: KnownNamesCache::create_cache_from_language_items(lang_items),
known_names_cache: KnownNamesCache::create_cache(),
options: &std::mem::take(&mut self.options),
session: &compiler.sess,
generic_args_cache: HashMap::new(),
Expand Down
2 changes: 1 addition & 1 deletion checker/src/crate_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub struct CrateVisitor<'compilation, 'tcx> {
pub diagnostics_for: HashMap<DefId, Vec<Diag<'compilation, ()>>>,
pub file_name: &'compilation str,
pub generic_args_cache: HashMap<DefId, GenericArgsRef<'tcx>>,
pub known_names_cache: KnownNamesCache<'tcx>,
pub known_names_cache: KnownNamesCache,
pub options: &'compilation Options,
pub session: &'compilation Session,
pub summary_cache: SummaryCache<'tcx>,
Expand Down
33 changes: 4 additions & 29 deletions checker/src/known_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

use rustc_hir::def_id::DefId;
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
use rustc_hir::LanguageItems;
use rustc_middle::ty::TyCtxt;

use std::collections::HashMap;
Expand Down Expand Up @@ -170,22 +169,16 @@ pub enum KnownNames {
}

/// An analysis lifetime cache that contains a map from def ids to known names.
pub struct KnownNamesCache<'tcx> {
pub struct KnownNamesCache {
name_cache: HashMap<DefId, KnownNames>,
lang_items: &'tcx LanguageItems,
}

impl<'tcx> KnownNamesCache<'tcx> {
impl KnownNamesCache {
/// Create an empty known names cache.
/// This cache is re-used by every successive MIR visitor instance.
pub fn create_cache_from_language_items(
lang_items: &'tcx LanguageItems,
) -> KnownNamesCache<'tcx> {
pub fn create_cache() -> KnownNamesCache {
let name_cache = HashMap::new();
KnownNamesCache {
name_cache,
lang_items,
}
KnownNamesCache { name_cache }
}

/// Get the well known name for the given def id and cache the association.
Expand All @@ -206,12 +199,6 @@ impl<'tcx> KnownNamesCache<'tcx> {
/// Uses information obtained from tcx to figure out which well known name (if any)
/// this def id corresponds to.
pub fn get_known_name_for(&self, tcx: TyCtxt<'_>, def_id: DefId) -> KnownNames {
// Use `rustc` lang items (if available) for the most precise `DefId` matching.
if let Some(known_name) = known_name_for_lang_item(def_id, self.lang_items) {
return known_name;
}

// Fallback to def path comparisons for other items.
// Ref: <https://doc.rust-lang.org/reference/names/namespaces.html>
// Ref: <https://doc.rust-lang.org/beta/nightly-rustc/rustc_hir/definitions/enum.DefPathData.html>
let def_path = &tcx.def_path(def_id);
Expand All @@ -238,18 +225,6 @@ macro_rules! match_names {
};
}

fn known_name_for_lang_item(def_id: DefId, lang_items: &LanguageItems) -> Option<KnownNames> {
let lang_item = lang_items.from_def_id(def_id)?;
match_names!(
lang_item,
rustc_hir::LangItem::CloneFn => KnownNames::StdCloneClone,
rustc_hir::LangItem::PhantomData => KnownNames::StdMarkerPhantomData,
// TODO: (@davidsemakula) Can we safely add more panic lang items?
rustc_hir::LangItem::BeginPanic | rustc_hir::LangItem::Panic => KnownNames::StdPanickingBeginPanic,
rustc_hir::LangItem::ConstPanicFmt | rustc_hir::LangItem::PanicFmt => KnownNames::StdPanickingBeginPanicFmt,
)
}

fn known_name_for_mirai_annotations(
path_segments: &[DisambiguatedDefPathData],
) -> Option<KnownNames> {
Expand Down

0 comments on commit 46944ed

Please sign in to comment.