Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Encode LangItem directly
  • Loading branch information
camsteffen committed Oct 29, 2022
commit ebfa1f018567932cc460eb1c293ceaac104f8451
7 changes: 1 addition & 6 deletions compiler/rustc_hir/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ macro_rules! expand_group {
pub struct LanguageItems {
/// Mappings from lang items to their possibly found [`DefId`]s.
/// 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.
pub missing: Vec<LangItem>,
/// Mapping from [`LangItemGroup`] discriminants to all
Expand Down Expand Up @@ -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.
pub fn group(&self, group: LangItemGroup) -> &[DefId] {
self.groups[group as usize].as_ref()
Expand Down
5 changes: 2 additions & 3 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::definitions::{DefKey, DefPath, DefPathData, DefPathHash};
use rustc_hir::diagnostic_items::DiagnosticItems;
use rustc_hir::lang_items;
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
Expand Down Expand Up @@ -967,7 +966,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}

/// 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(
self.root
.lang_items
Expand Down Expand Up @@ -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))
}

Expand Down
19 changes: 6 additions & 13 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rustc_hir::def_id::{
};
use rustc_hir::definitions::DefPathData;
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::middle::dependency_format::Linkage;
use rustc_middle::middle::exported_symbols::{
Expand Down Expand Up @@ -1905,22 +1905,15 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
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);
let tcx = self.tcx;
let lang_items = tcx.lang_items();
let lang_items = lang_items.items().iter();
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
let lang_items = self.tcx.lang_items().iter();
self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| {
def_id.as_local().map(|id| (id.local_def_index, lang_item))
}))
}

fn encode_lang_items_missing(&mut self) -> LazyArray<lang_items::LangItem> {
fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> {
empty_proc_macro!(self);
let tcx = self.tcx;
self.lazy_array(&tcx.lang_items().missing)
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use rustc_hir as hir;
use rustc_hir::def::{CtorKind, DefKind};
use rustc_hir::def_id::{CrateNum, DefId, DefIndex, DefPathHash, StableCrateId};
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::vec::IndexVec;
use rustc_middle::metadata::ModChild;
Expand Down Expand Up @@ -230,8 +230,8 @@ pub(crate) struct CrateRoot {
dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
lib_features: LazyArray<(Symbol, Option<Symbol>)>,
stability_implications: LazyArray<(Symbol, Symbol)>,
lang_items: LazyArray<(DefIndex, usize)>,
lang_items_missing: LazyArray<lang_items::LangItem>,
lang_items: LazyArray<(DefIndex, LangItem)>,
lang_items_missing: LazyArray<LangItem>,
diagnostic_items: LazyArray<(Symbol, DefIndex)>,
native_libraries: LazyArray<NativeLib>,
foreign_modules: LazyArray<ForeignModule>,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1705,7 +1705,7 @@ rustc_queries! {
}

/// 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" }
separate_provide_extern
}
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_passes/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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 {
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 mut dependency_of = Empty;
let is_local = item_def_id.is_local();
Expand Down Expand Up @@ -139,8 +139,8 @@ impl<'tcx> LanguageItemCollector<'tcx> {
}

// Matched.
self.items.items[item_index] = Some(item_def_id);
if let Some(group) = LangItem::from_u32(item_index as u32).unwrap().group() {
self.items.set(lang_item, item_def_id);
if let Some(group) = lang_item.group() {
self.items.groups[group as usize].push(item_def_id);
}
}
Expand Down Expand Up @@ -197,7 +197,7 @@ impl<'tcx> LanguageItemCollector<'tcx> {
}
}

self.collect_item(item_index, item_def_id);
self.collect_item(lang_item, item_def_id);
}
}

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

// Collect lang items in other crates.
for &cnum in tcx.crates(()).iter() {
for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() {
collector.collect_item(item_index, def_id);
for &(def_id, lang_item) in tcx.defined_lang_items(cnum).iter() {
collector.collect_item(lang_item, def_id);
}
}

Expand Down