Skip to content

Commit 99de57a

Browse files
committed
Improve LanguageItems api
1 parent 5e97720 commit 99de57a

File tree

3 files changed

+43
-28
lines changed

3 files changed

+43
-28
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,44 @@ macro_rules! expand_group {
3636
};
3737
}
3838

39+
/// All of the language items, defined or not.
40+
/// Defined lang items can come from the current crate or its dependencies.
41+
#[derive(HashStable_Generic, Debug)]
42+
pub struct LanguageItems {
43+
/// Mappings from lang items to their possibly found [`DefId`]s.
44+
/// The index corresponds to the order in [`LangItem`].
45+
pub items: Vec<Option<DefId>>,
46+
/// Lang items that were not found during collection.
47+
pub missing: Vec<LangItem>,
48+
/// Mapping from [`LangItemGroup`] discriminants to all
49+
/// [`DefId`]s of lang items in that group.
50+
pub groups: [Vec<DefId>; NUM_GROUPS],
51+
}
52+
53+
impl LanguageItems {
54+
pub fn get(&self, item: LangItem) -> Option<DefId> {
55+
self.items[item as usize]
56+
}
57+
58+
pub fn set(&mut self, item: LangItem, def_id: DefId) {
59+
self.items[item as usize] = Some(def_id);
60+
}
61+
62+
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
63+
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
64+
/// returns an error encapsulating the `LangItem`.
65+
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
66+
self.get(it).ok_or_else(|| LangItemError(it))
67+
}
68+
69+
pub fn iter<'a>(&'a self) -> impl Iterator<Item = (LangItem, DefId)> + 'a {
70+
self.items
71+
.iter()
72+
.enumerate()
73+
.filter_map(|(i, id)| id.map(|id| (LangItem::from_u32(i as u32).unwrap(), id)))
74+
}
75+
}
76+
3977
// The actual lang items defined come at the end of this file in one handy table.
4078
// So you probably just want to nip down to the end.
4179
macro_rules! language_item_table {
@@ -82,20 +120,6 @@ macro_rules! language_item_table {
82120
}
83121
}
84122

85-
/// All of the language items, defined or not.
86-
/// Defined lang items can come from the current crate or its dependencies.
87-
#[derive(HashStable_Generic, Debug)]
88-
pub struct LanguageItems {
89-
/// Mappings from lang items to their possibly found [`DefId`]s.
90-
/// The index corresponds to the order in [`LangItem`].
91-
pub items: Vec<Option<DefId>>,
92-
/// Lang items that were not found during collection.
93-
pub missing: Vec<LangItem>,
94-
/// Mapping from [`LangItemGroup`] discriminants to all
95-
/// [`DefId`]s of lang items in that group.
96-
pub groups: [Vec<DefId>; NUM_GROUPS],
97-
}
98-
99123
impl LanguageItems {
100124
/// Construct an empty collection of lang items and no missing ones.
101125
pub fn new() -> Self {
@@ -114,13 +138,6 @@ macro_rules! language_item_table {
114138
&*self.items
115139
}
116140

117-
/// Requires that a given `LangItem` was bound and returns the corresponding `DefId`.
118-
/// If it wasn't bound, e.g. due to a missing `#[lang = "<it.name()>"]`,
119-
/// returns an error encapsulating the `LangItem`.
120-
pub fn require(&self, it: LangItem) -> Result<DefId, LangItemError> {
121-
self.items[it as usize].ok_or_else(|| LangItemError(it))
122-
}
123-
124141
/// Returns the [`DefId`]s of all lang items in a group.
125142
pub fn group(&self, group: LangItemGroup) -> &[DefId] {
126143
self.groups[group as usize].as_ref()

compiler/rustc_passes/src/reachable.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,11 +380,9 @@ fn reachable_set<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> FxHashSet<LocalDefId> {
380380
})
381381
.collect::<Vec<_>>();
382382

383-
for item in tcx.lang_items().items().iter() {
384-
if let Some(def_id) = *item {
385-
if let Some(def_id) = def_id.as_local() {
386-
reachable_context.worklist.push(def_id);
387-
}
383+
for (_, def_id) in tcx.lang_items().iter() {
384+
if let Some(def_id) = def_id.as_local() {
385+
reachable_context.worklist.push(def_id);
388386
}
389387
}
390388
{

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
974974
// useful for less general traits.
975975
if peeled
976976
&& !self.tcx.trait_is_auto(def_id)
977-
&& !self.tcx.lang_items().items().contains(&Some(def_id))
977+
&& !self.tcx.lang_items().iter().any(|(_, id)| id == def_id)
978978
{
979979
let trait_ref = trait_pred.to_poly_trait_ref();
980980
let impl_candidates =
@@ -1898,7 +1898,7 @@ impl<'tcx> InferCtxtPrivExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
18981898
let def_id = trait_ref.def_id();
18991899
if impl_candidates.is_empty() {
19001900
if self.tcx.trait_is_auto(def_id)
1901-
|| self.tcx.lang_items().items().contains(&Some(def_id))
1901+
|| self.tcx.lang_items().iter().any(|(_, id)| id == def_id)
19021902
|| self.tcx.get_diagnostic_name(def_id).is_some()
19031903
{
19041904
// Mentioning implementers of `Copy`, `Debug` and friends is not useful.

0 commit comments

Comments
 (0)