Factor out ITEM_REFS

This commit is contained in:
Cameron Steffen 2022-10-26 16:18:39 -05:00
parent ebfa1f0185
commit f808430497
2 changed files with 21 additions and 20 deletions

View File

@ -12,14 +12,11 @@ use crate::errors::LangItemError;
use crate::{MethodKind, Target}; use crate::{MethodKind, Target};
use rustc_ast as ast; use rustc_ast as ast;
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_macros::HashStable_Generic; use rustc_macros::HashStable_Generic;
use rustc_span::symbol::{kw, sym, Symbol}; use rustc_span::symbol::{kw, sym, Symbol};
use rustc_span::Span; use rustc_span::Span;
use std::sync::LazyLock;
pub enum LangItemGroup { pub enum LangItemGroup {
Op, Op,
Fn, Fn,
@ -104,6 +101,14 @@ macro_rules! language_item_table {
} }
} }
/// Opposite of [`LangItem::name`]
pub fn from_name(name: Symbol) -> Option<Self> {
match name {
$( $module::$name => Some(LangItem::$variant), )*
_ => None,
}
}
/// The [group](LangItemGroup) that this lang item belongs to, /// The [group](LangItemGroup) that this lang item belongs to,
/// or `None` if it doesn't belong to a group. /// or `None` if it doesn't belong to a group.
pub fn group(self) -> Option<LangItemGroup> { pub fn group(self) -> Option<LangItemGroup> {
@ -113,6 +118,12 @@ macro_rules! language_item_table {
} }
} }
pub fn target(self) -> Target {
match self {
$( LangItem::$variant => $target, )*
}
}
pub fn required_generics(&self) -> GenericRequirement { pub fn required_generics(&self) -> GenericRequirement {
match self { match self {
$( LangItem::$variant => $generics, )* $( LangItem::$variant => $generics, )*
@ -145,15 +156,6 @@ macro_rules! language_item_table {
} }
)* )*
} }
/// A mapping from the name of the lang item to its order and the form it must be of.
pub static ITEM_REFS: LazyLock<FxIndexMap<Symbol, (usize, Target)>> = LazyLock::new(|| {
let mut item_refs = FxIndexMap::default();
$( item_refs.insert($module::$name, (LangItem::$variant as usize, $target)); )*
item_refs
});
// End of the macro
} }
} }

View File

@ -16,7 +16,7 @@ use crate::weak_lang_items;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_hir::def::DefKind; use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId; use rustc_hir::def_id::DefId;
use rustc_hir::lang_items::{extract, GenericRequirement, ITEM_REFS}; use rustc_hir::lang_items::{extract, GenericRequirement};
use rustc_hir::{HirId, LangItem, LanguageItems, Target}; use rustc_hir::{HirId, LangItem, LanguageItems, Target};
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::cstore::ExternCrate; use rustc_session::cstore::ExternCrate;
@ -43,17 +43,17 @@ impl<'tcx> LanguageItemCollector<'tcx> {
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) { fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) {
let attrs = self.tcx.hir().attrs(hir_id); let attrs = self.tcx.hir().attrs(hir_id);
if let Some((name, span)) = extract(&attrs) { if let Some((name, span)) = extract(&attrs) {
match ITEM_REFS.get(&name).cloned() { match LangItem::from_name(name) {
// Known lang item with attribute on correct target. // Known lang item with attribute on correct target.
Some((item_index, expected_target)) if actual_target == expected_target => { Some(lang_item) if actual_target == lang_item.target() => {
self.collect_item_extended(item_index, hir_id, span); self.collect_item_extended(lang_item, hir_id, span);
} }
// Known lang item with attribute on incorrect target. // Known lang item with attribute on incorrect target.
Some((_, expected_target)) => { Some(lang_item) => {
self.tcx.sess.emit_err(LangItemOnIncorrectTarget { self.tcx.sess.emit_err(LangItemOnIncorrectTarget {
span, span,
name, name,
expected_target, expected_target: lang_item.target(),
actual_target, actual_target,
}); });
} }
@ -147,9 +147,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
// Like collect_item() above, but also checks whether the lang item is declared // Like collect_item() above, but also checks whether the lang item is declared
// with the right number of generic arguments. // with the right number of generic arguments.
fn collect_item_extended(&mut self, item_index: usize, hir_id: HirId, span: Span) { fn collect_item_extended(&mut self, lang_item: LangItem, hir_id: HirId, span: Span) {
let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id(); let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
let lang_item = LangItem::from_u32(item_index as u32).unwrap();
let name = lang_item.name(); let name = lang_item.name();
// Now check whether the lang_item has the expected number of generic // Now check whether the lang_item has the expected number of generic