Skip to content

Commit ebfa1f0

Browse files
committed
Encode LangItem directly
1 parent 99de57a commit ebfa1f0

File tree

6 files changed

+21
-34
lines changed

6 files changed

+21
-34
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! expand_group {
4242
pub struct LanguageItems {
4343
/// Mappings from lang items to their possibly found [`DefId`]s.
4444
/// The index corresponds to the order in [`LangItem`].
45-
pub items: Vec<Option<DefId>>,
45+
items: Vec<Option<DefId>>,
4646
/// Lang items that were not found during collection.
4747
pub missing: Vec<LangItem>,
4848
/// Mapping from [`LangItemGroup`] discriminants to all
@@ -133,11 +133,6 @@ macro_rules! language_item_table {
133133
}
134134
}
135135

136-
/// Returns the mappings to the possibly found `DefId`s for each lang item.
137-
pub fn items(&self) -> &[Option<DefId>] {
138-
&*self.items
139-
}
140-
141136
/// Returns the [`DefId`]s of all lang items in a group.
142137
pub fn group(&self, group: LangItemGroup) -> &[DefId] {
143138
self.groups[group as usize].as_ref()

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1515
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, CRATE_DEF_INDEX, LOCAL_CRATE};
1616
use rustc_hir::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
1717
use rustc_hir::diagnostic_items::DiagnosticItems;
18-
use rustc_hir::lang_items;
1918
use rustc_index::vec::{Idx, IndexVec};
2019
use rustc_middle::metadata::ModChild;
2120
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
@@ -967,7 +966,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
967966
}
968967

969968
/// Iterates over the language items in the given crate.
970-
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] {
969+
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] {
971970
tcx.arena.alloc_from_iter(
972971
self.root
973972
.lang_items
@@ -1319,7 +1318,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13191318
)
13201319
}
13211320

1322-
fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] {
1321+
fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [LangItem] {
13231322
tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
13241323
}
13251324

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_hir::def_id::{
1717
};
1818
use rustc_hir::definitions::DefPathData;
1919
use rustc_hir::intravisit::{self, Visitor};
20-
use rustc_hir::lang_items;
20+
use rustc_hir::lang_items::LangItem;
2121
use rustc_middle::hir::nested_filter;
2222
use rustc_middle::middle::dependency_format::Linkage;
2323
use rustc_middle::middle::exported_symbols::{
@@ -1905,22 +1905,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
19051905
self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
19061906
}
19071907

1908-
fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, usize)> {
1908+
fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
19091909
empty_proc_macro!(self);
1910-
let tcx = self.tcx;
1911-
let lang_items = tcx.lang_items();
1912-
let lang_items = lang_items.items().iter();
1913-
self.lazy_array(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
1914-
if let Some(def_id) = opt_def_id {
1915-
if def_id.is_local() {
1916-
return Some((def_id.index, i));
1917-
}
1918-
}
1919-
None
1910+
let lang_items = self.tcx.lang_items().iter();
1911+
self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
1912+
def_id.as_local().map(|id| (id.local_def_index, lang_item))
19201913
}))
19211914
}
19221915

1923-
fn encode_lang_items_missing(&mut self) -> LazyArray<lang_items::LangItem> {
1916+
fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
19241917
empty_proc_macro!(self);
19251918
let tcx = self.tcx;
19261919
self.lazy_array(&tcx.lang_items().missing)

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_hir as hir;
1212
use rustc_hir::def::{CtorKind, DefKind};
1313
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId};
1414
use rustc_hir::definitions::DefKey;
15-
use rustc_hir::lang_items;
15+
use rustc_hir::lang_items::LangItem;
1616
use rustc_index::bit_set::{BitSet, FiniteBitSet};
1717
use rustc_index::vec::IndexVec;
1818
use rustc_middle::metadata::ModChild;
@@ -230,8 +230,8 @@ pub(crate) struct CrateRoot {
230230
dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
231231
lib_features: LazyArray<(Symbol, Option<Symbol>)>,
232232
stability_implications: LazyArray<(Symbol, Symbol)>,
233-
lang_items: LazyArray<(DefIndex, usize)>,
234-
lang_items_missing: LazyArray<lang_items::LangItem>,
233+
lang_items: LazyArray<(DefIndex, LangItem)>,
234+
lang_items_missing: LazyArray<LangItem>,
235235
diagnostic_items: LazyArray<(Symbol, DefIndex)>,
236236
native_libraries: LazyArray<NativeLib>,
237237
foreign_modules: LazyArray<ForeignModule>,

compiler/rustc_middle/src/query/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1705,7 +1705,7 @@ rustc_queries! {
17051705
}
17061706

17071707
/// Returns the lang items defined in another crate by loading it from metadata.
1708-
query defined_lang_items(_: CrateNum) -> &'tcx [(DefId, usize)] {
1708+
query defined_lang_items(_: CrateNum) -> &'tcx [(DefId, LangItem)] {
17091709
desc { "calculating the lang items defined in a crate" }
17101710
separate_provide_extern
17111711
}

compiler/rustc_passes/src/lang_items.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ impl<'tcx> LanguageItemCollector<'tcx> {
6565
}
6666
}
6767

68-
fn collect_item(&mut self, item_index: usize, item_def_id: DefId) {
68+
fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId) {
6969
// Check for duplicates.
70-
if let Some(original_def_id) = self.items.items[item_index] {
70+
if let Some(original_def_id) = self.items.get(lang_item) {
7171
if original_def_id != item_def_id {
7272
let local_span = self.tcx.hir().span_if_local(item_def_id);
73-
let lang_item_name = LangItem::from_u32(item_index as u32).unwrap().name();
73+
let lang_item_name = lang_item.name();
7474
let crate_name = self.tcx.crate_name(item_def_id.krate);
7575
let mut dependency_of = Empty;
7676
let is_local = item_def_id.is_local();
@@ -139,8 +139,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
139139
}
140140

141141
// Matched.
142-
self.items.items[item_index] = Some(item_def_id);
143-
if let Some(group) = LangItem::from_u32(item_index as u32).unwrap().group() {
142+
self.items.set(lang_item, item_def_id);
143+
if let Some(group) = lang_item.group() {
144144
self.items.groups[group as usize].push(item_def_id);
145145
}
146146
}
@@ -197,7 +197,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
197197
}
198198
}
199199

200-
self.collect_item(item_index, item_def_id);
200+
self.collect_item(lang_item, item_def_id);
201201
}
202202
}
203203

@@ -208,8 +208,8 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
208208

209209
// Collect lang items in other crates.
210210
for &cnum in tcx.crates(()).iter() {
211-
for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
212-
collector.collect_item(item_index, def_id);
211+
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
212+
collector.collect_item(lang_item, def_id);
213213
}
214214
}
215215

0 commit comments

Comments
 (0)