Skip to content

Commit f808430

Browse files
committed
Factor out ITEM_REFS
1 parent ebfa1f0 commit f808430

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,11 @@ use crate::errors::LangItemError;
1212
use crate::{MethodKind, Target};
1313

1414
use rustc_ast as ast;
15-
use rustc_data_structures::fx::FxIndexMap;
1615
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
1716
use rustc_macros::HashStable_Generic;
1817
use rustc_span::symbol::{kw, sym, Symbol};
1918
use rustc_span::Span;
2019

21-
use std::sync::LazyLock;
22-
2320
pub enum LangItemGroup {
2421
Op,
2522
Fn,
@@ -104,6 +101,14 @@ macro_rules! language_item_table {
104101
}
105102
}
106103

104+
/// Opposite of [`LangItem::name`]
105+
pub fn from_name(name: Symbol) -> Option<Self> {
106+
match name {
107+
$( $module::$name => Some(LangItem::$variant), )*
108+
_ => None,
109+
}
110+
}
111+
107112
/// The [group](LangItemGroup) that this lang item belongs to,
108113
/// or `None` if it doesn't belong to a group.
109114
pub fn group(self) -> Option<LangItemGroup> {
@@ -113,6 +118,12 @@ macro_rules! language_item_table {
113118
}
114119
}
115120

121+
pub fn target(self) -> Target {
122+
match self {
123+
$( LangItem::$variant => $target, )*
124+
}
125+
}
126+
116127
pub fn required_generics(&self) -> GenericRequirement {
117128
match self {
118129
$( LangItem::$variant => $generics, )*
@@ -145,15 +156,6 @@ macro_rules! language_item_table {
145156
}
146157
)*
147158
}
148-
149-
/// A mapping from the name of the lang item to its order and the form it must be of.
150-
pub static ITEM_REFS: LazyLock<FxIndexMap<Symbol, (usize, Target)>> = LazyLock::new(|| {
151-
let mut item_refs = FxIndexMap::default();
152-
$( item_refs.insert($module::$name, (LangItem::$variant as usize, $target)); )*
153-
item_refs
154-
});
155-
156-
// End of the macro
157159
}
158160
}
159161

compiler/rustc_passes/src/lang_items.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::weak_lang_items;
1616
use rustc_hir as hir;
1717
use rustc_hir::def::DefKind;
1818
use rustc_hir::def_id::DefId;
19-
use rustc_hir::lang_items::{extract, GenericRequirement, ITEM_REFS};
19+
use rustc_hir::lang_items::{extract, GenericRequirement};
2020
use rustc_hir::{HirId, LangItem, LanguageItems, Target};
2121
use rustc_middle::ty::TyCtxt;
2222
use rustc_session::cstore::ExternCrate;
@@ -43,17 +43,17 @@ impl<'tcx> LanguageItemCollector<'tcx> {
4343
fn check_for_lang(&mut self, actual_target: Target, hir_id: HirId) {
4444
let attrs = self.tcx.hir().attrs(hir_id);
4545
if let Some((name, span)) = extract(&attrs) {
46-
match ITEM_REFS.get(&name).cloned() {
46+
match LangItem::from_name(name) {
4747
// Known lang item with attribute on correct target.
48-
Some((item_index, expected_target)) if actual_target == expected_target => {
49-
self.collect_item_extended(item_index, hir_id, span);
48+
Some(lang_item) if actual_target == lang_item.target() => {
49+
self.collect_item_extended(lang_item, hir_id, span);
5050
}
5151
// Known lang item with attribute on incorrect target.
52-
Some((_, expected_target)) => {
52+
Some(lang_item) => {
5353
self.tcx.sess.emit_err(LangItemOnIncorrectTarget {
5454
span,
5555
name,
56-
expected_target,
56+
expected_target: lang_item.target(),
5757
actual_target,
5858
});
5959
}
@@ -147,9 +147,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
147147

148148
// Like collect_item() above, but also checks whether the lang item is declared
149149
// with the right number of generic arguments.
150-
fn collect_item_extended(&mut self, item_index: usize, hir_id: HirId, span: Span) {
150+
fn collect_item_extended(&mut self, lang_item: LangItem, hir_id: HirId, span: Span) {
151151
let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
152-
let lang_item = LangItem::from_u32(item_index as u32).unwrap();
153152
let name = lang_item.name();
154153

155154
// Now check whether the lang_item has the expected number of generic

0 commit comments

Comments
 (0)