1
Fork 0

Encode LangItem directly

This commit is contained in:
Cameron Steffen 2022-10-26 16:18:29 -05:00
parent 99de57ae13
commit ebfa1f0185
6 changed files with 21 additions and 34 deletions

View file

@ -42,7 +42,7 @@ macro_rules! expand_group {
pub struct LanguageItems { pub struct LanguageItems {
/// Mappings from lang items to their possibly found [`DefId`]s. /// Mappings from lang items to their possibly found [`DefId`]s.
/// The index corresponds to the order in [`LangItem`]. /// The index corresponds to the order in [`LangItem`].
pub items: Vec<Option<DefId>>, items: Vec<Option<DefId>>,
/// Lang items that were not found during collection. /// Lang items that were not found during collection.
pub missing: Vec<LangItem>, pub missing: Vec<LangItem>,
/// Mapping from [`LangItemGroup`] discriminants to all /// Mapping from [`LangItemGroup`] discriminants to all
@ -133,11 +133,6 @@ macro_rules! language_item_table {
} }
} }
/// Returns the mappings to the possibly found `DefId`s for each lang item.
pub fn items(&self) -> &[Option<DefId>] {
&*self.items
}
/// Returns the [`DefId`]s of all lang items in a group. /// Returns the [`DefId`]s of all lang items in a group.
pub fn group(&self, group: LangItemGroup) -> &[DefId] { pub fn group(&self, group: LangItemGroup) -> &[DefId] {
self.groups[group as usize].as_ref() self.groups[group as usize].as_ref()

View file

@ -15,7 +15,6 @@ 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::diagnostic_items::DiagnosticItems;
use rustc_hir::lang_items;
use rustc_index::vec::{Idx, IndexVec}; use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::metadata::ModChild; use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo}; use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
@ -967,7 +966,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
} }
/// Iterates over the language items in the given crate. /// Iterates over the language items in the given crate.
fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, usize)] { fn get_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [(DefId, LangItem)] {
tcx.arena.alloc_from_iter( tcx.arena.alloc_from_iter(
self.root self.root
.lang_items .lang_items
@ -1319,7 +1318,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
) )
} }
fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [lang_items::LangItem] { fn get_missing_lang_items(self, tcx: TyCtxt<'tcx>) -> &'tcx [LangItem] {
tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self)) tcx.arena.alloc_from_iter(self.root.lang_items_missing.decode(self))
} }

View file

@ -17,7 +17,7 @@ use rustc_hir::def_id::{
}; };
use rustc_hir::definitions::DefPathData; use rustc_hir::definitions::DefPathData;
use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::lang_items; use rustc_hir::lang_items::LangItem;
use rustc_middle::hir::nested_filter; use rustc_middle::hir::nested_filter;
use rustc_middle::middle::dependency_format::Linkage; use rustc_middle::middle::dependency_format::Linkage;
use rustc_middle::middle::exported_symbols::{ use rustc_middle::middle::exported_symbols::{
@ -1905,22 +1905,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index))) self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
} }
fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, usize)> { fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> {
empty_proc_macro!(self); empty_proc_macro!(self);
let tcx = self.tcx; let lang_items = self.tcx.lang_items().iter();
let lang_items = tcx.lang_items(); self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
let lang_items = lang_items.items().iter(); def_id.as_local().map(|id| (id.local_def_index, lang_item))
self.lazy_array(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
if let Some(def_id) = opt_def_id {
if def_id.is_local() {
return Some((def_id.index, i));
}
}
None
})) }))
} }
fn encode_lang_items_missing(&mut self) -> LazyArray<lang_items::LangItem> { fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
empty_proc_macro!(self); empty_proc_macro!(self);
let tcx = self.tcx; let tcx = self.tcx;
self.lazy_array(&tcx.lang_items().missing) self.lazy_array(&tcx.lang_items().missing)

View file

@ -12,7 +12,7 @@ use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind}; use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId}; use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId};
use rustc_hir::definitions::DefKey; use rustc_hir::definitions::DefKey;
use rustc_hir::lang_items; use rustc_hir::lang_items::LangItem;
use rustc_index::bit_set::{BitSet, FiniteBitSet}; use rustc_index::bit_set::{BitSet, FiniteBitSet};
use rustc_index::vec::IndexVec; use rustc_index::vec::IndexVec;
use rustc_middle::metadata::ModChild; use rustc_middle::metadata::ModChild;
@ -230,8 +230,8 @@ pub(crate) struct CrateRoot {
dylib_dependency_formats: LazyArray<Option<LinkagePreference>>, dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
lib_features: LazyArray<(Symbol, Option<Symbol>)>, lib_features: LazyArray<(Symbol, Option<Symbol>)>,
stability_implications: LazyArray<(Symbol, Symbol)>, stability_implications: LazyArray<(Symbol, Symbol)>,
lang_items: LazyArray<(DefIndex, usize)>, lang_items: LazyArray<(DefIndex, LangItem)>,
lang_items_missing: LazyArray<lang_items::LangItem>, lang_items_missing: LazyArray<LangItem>,
diagnostic_items: LazyArray<(Symbol, DefIndex)>, diagnostic_items: LazyArray<(Symbol, DefIndex)>,
native_libraries: LazyArray<NativeLib>, native_libraries: LazyArray<NativeLib>,
foreign_modules: LazyArray<ForeignModule>, foreign_modules: LazyArray<ForeignModule>,

View file

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

View file

@ -65,12 +65,12 @@ impl<'tcx> LanguageItemCollector<'tcx> {
} }
} }
fn collect_item(&mut self, item_index: usize, item_def_id: DefId) { fn collect_item(&mut self, lang_item: LangItem, item_def_id: DefId) {
// Check for duplicates. // Check for duplicates.
if let Some(original_def_id) = self.items.items[item_index] { if let Some(original_def_id) = self.items.get(lang_item) {
if original_def_id != item_def_id { if original_def_id != item_def_id {
let local_span = self.tcx.hir().span_if_local(item_def_id); let local_span = self.tcx.hir().span_if_local(item_def_id);
let lang_item_name = LangItem::from_u32(item_index as u32).unwrap().name(); let lang_item_name = lang_item.name();
let crate_name = self.tcx.crate_name(item_def_id.krate); let crate_name = self.tcx.crate_name(item_def_id.krate);
let mut dependency_of = Empty; let mut dependency_of = Empty;
let is_local = item_def_id.is_local(); let is_local = item_def_id.is_local();
@ -139,8 +139,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
} }
// Matched. // Matched.
self.items.items[item_index] = Some(item_def_id); self.items.set(lang_item, item_def_id);
if let Some(group) = LangItem::from_u32(item_index as u32).unwrap().group() { if let Some(group) = lang_item.group() {
self.items.groups[group as usize].push(item_def_id); self.items.groups[group as usize].push(item_def_id);
} }
} }
@ -197,7 +197,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
} }
} }
self.collect_item(item_index, item_def_id); self.collect_item(lang_item, item_def_id);
} }
} }
@ -208,8 +208,8 @@ fn get_lang_items(tcx: TyCtxt<'_>, (): ()) -> LanguageItems {
// Collect lang items in other crates. // Collect lang items in other crates.
for &cnum in tcx.crates(()).iter() { for &cnum in tcx.crates(()).iter() {
for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() { for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
collector.collect_item(item_index, def_id); collector.collect_item(lang_item, def_id);
} }
} }