Skip to content

Commit a6180ed

Browse files
committed
Simplify lang item groups
1 parent 6621279 commit a6180ed

File tree

3 files changed

+73
-82
lines changed

3 files changed

+73
-82
lines changed

compiler/rustc_const_eval/src/util/call_kind.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! context.
44
55
use rustc_hir::def_id::DefId;
6-
use rustc_hir::lang_items::LangItemGroup;
6+
use rustc_hir::lang_items;
77
use rustc_middle::ty::subst::SubstsRef;
88
use rustc_middle::ty::{self, AssocItemContainer, DefIdTree, Instance, ParamEnv, Ty, TyCtxt};
99
use rustc_span::symbol::Ident;
@@ -74,22 +74,24 @@ pub fn call_kind<'tcx>(
7474
}
7575
});
7676

77-
let fn_call = parent
78-
.and_then(|p| tcx.lang_items().group(LangItemGroup::Fn).iter().find(|did| **did == p));
77+
let fn_call = parent.and_then(|p| {
78+
lang_items::FN_TRAITS.iter().filter_map(|&l| tcx.lang_items().get(l)).find(|&id| id == p)
79+
});
7980

80-
let operator = (!from_hir_call)
81-
.then(|| parent)
82-
.flatten()
83-
.and_then(|p| tcx.lang_items().group(LangItemGroup::Op).iter().find(|did| **did == p));
81+
let operator = if !from_hir_call && let Some(p) = parent {
82+
lang_items::OPERATORS.iter().filter_map(|&l| tcx.lang_items().get(l)).find(|&id| id == p)
83+
} else {
84+
None
85+
};
8486

8587
let is_deref = !from_hir_call && tcx.is_diagnostic_item(sym::deref_method, method_did);
8688

8789
// Check for a 'special' use of 'self' -
8890
// an FnOnce call, an operator (e.g. `<<`), or a
8991
// deref coercion.
90-
let kind = if let Some(&trait_id) = fn_call {
92+
let kind = if let Some(trait_id) = fn_call {
9193
Some(CallKind::FnCall { fn_trait_id: trait_id, self_ty: method_substs.type_at(0) })
92-
} else if let Some(&trait_id) = operator {
94+
} else if let Some(trait_id) = operator {
9395
Some(CallKind::Operator { self_arg, trait_id, self_ty: method_substs.type_at(0) })
9496
} else if is_deref {
9597
let deref_target = tcx.get_diagnostic_item(sym::deref_target).and_then(|deref_target| {

compiler/rustc_hir/src/lang_items.rs

+62-70
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,6 @@ use rustc_macros::HashStable_Generic;
1717
use rustc_span::symbol::{kw, sym, Symbol};
1818
use rustc_span::Span;
1919

20-
pub enum LangItemGroup {
21-
Op,
22-
Fn,
23-
}
24-
25-
const NUM_GROUPS: usize = 2;
26-
27-
macro_rules! expand_group {
28-
() => {
29-
None
30-
};
31-
($group:expr) => {
32-
Some($group)
33-
};
34-
}
35-
3620
/// All of the language items, defined or not.
3721
/// Defined lang items can come from the current crate or its dependencies.
3822
#[derive(HashStable_Generic, Debug)]
@@ -42,21 +26,12 @@ pub struct LanguageItems {
4226
items: [Option<DefId>; std::mem::variant_count::<LangItem>()],
4327
/// Lang items that were not found during collection.
4428
pub missing: Vec<LangItem>,
45-
/// Mapping from [`LangItemGroup`] discriminants to all
46-
/// [`DefId`]s of lang items in that group.
47-
pub groups: [Vec<DefId>; NUM_GROUPS],
4829
}
4930

5031
impl LanguageItems {
5132
/// Construct an empty collection of lang items and no missing ones.
5233
pub fn new() -> Self {
53-
const EMPTY: Vec<DefId> = Vec::new();
54-
55-
Self {
56-
items: [None; std::mem::variant_count::<LangItem>()],
57-
missing: Vec::new(),
58-
groups: [EMPTY; NUM_GROUPS],
59-
}
34+
Self { items: [None; std::mem::variant_count::<LangItem>()], missing: Vec::new() }
6035
}
6136

6237
pub fn get(&self, item: LangItem) -> Option<DefId> {
@@ -86,7 +61,7 @@ impl LanguageItems {
8661
// So you probably just want to nip down to the end.
8762
macro_rules! language_item_table {
8863
(
89-
$( $(#[$attr:meta])* $variant:ident $($group:expr)?, $module:ident :: $name:ident, $method:ident, $target:expr, $generics:expr; )*
64+
$( $(#[$attr:meta])* $variant:ident, $module:ident :: $name:ident, $method:ident, $target:expr, $generics:expr; )*
9065
) => {
9166

9267
enum_from_u32! {
@@ -120,15 +95,6 @@ macro_rules! language_item_table {
12095
}
12196
}
12297

123-
/// The [group](LangItemGroup) that this lang item belongs to,
124-
/// or `None` if it doesn't belong to a group.
125-
pub fn group(self) -> Option<LangItemGroup> {
126-
use LangItemGroup::*;
127-
match self {
128-
$( LangItem::$variant => expand_group!($($group)*), )*
129-
}
130-
}
131-
13298
pub fn target(self) -> Target {
13399
match self {
134100
$( LangItem::$variant => $target, )*
@@ -143,11 +109,6 @@ macro_rules! language_item_table {
143109
}
144110

145111
impl LanguageItems {
146-
/// Returns the [`DefId`]s of all lang items in a group.
147-
pub fn group(&self, group: LangItemGroup) -> &[DefId] {
148-
self.groups[group as usize].as_ref()
149-
}
150-
151112
$(
152113
#[doc = concat!("Returns the [`DefId`] of the `", stringify!($name), "` lang item if it is defined.")]
153114
pub fn $method(&self) -> Option<DefId> {
@@ -209,30 +170,30 @@ language_item_table! {
209170
TransmuteOpts, sym::transmute_opts, transmute_opts, Target::Struct, GenericRequirement::Exact(0);
210171
TransmuteTrait, sym::transmute_trait, transmute_trait, Target::Trait, GenericRequirement::Exact(3);
211172

212-
Add(Op), sym::add, add_trait, Target::Trait, GenericRequirement::Exact(1);
213-
Sub(Op), sym::sub, sub_trait, Target::Trait, GenericRequirement::Exact(1);
214-
Mul(Op), sym::mul, mul_trait, Target::Trait, GenericRequirement::Exact(1);
215-
Div(Op), sym::div, div_trait, Target::Trait, GenericRequirement::Exact(1);
216-
Rem(Op), sym::rem, rem_trait, Target::Trait, GenericRequirement::Exact(1);
217-
Neg(Op), sym::neg, neg_trait, Target::Trait, GenericRequirement::Exact(0);
218-
Not(Op), sym::not, not_trait, Target::Trait, GenericRequirement::Exact(0);
219-
BitXor(Op), sym::bitxor, bitxor_trait, Target::Trait, GenericRequirement::Exact(1);
220-
BitAnd(Op), sym::bitand, bitand_trait, Target::Trait, GenericRequirement::Exact(1);
221-
BitOr(Op), sym::bitor, bitor_trait, Target::Trait, GenericRequirement::Exact(1);
222-
Shl(Op), sym::shl, shl_trait, Target::Trait, GenericRequirement::Exact(1);
223-
Shr(Op), sym::shr, shr_trait, Target::Trait, GenericRequirement::Exact(1);
224-
AddAssign(Op), sym::add_assign, add_assign_trait, Target::Trait, GenericRequirement::Exact(1);
225-
SubAssign(Op), sym::sub_assign, sub_assign_trait, Target::Trait, GenericRequirement::Exact(1);
226-
MulAssign(Op), sym::mul_assign, mul_assign_trait, Target::Trait, GenericRequirement::Exact(1);
227-
DivAssign(Op), sym::div_assign, div_assign_trait, Target::Trait, GenericRequirement::Exact(1);
228-
RemAssign(Op), sym::rem_assign, rem_assign_trait, Target::Trait, GenericRequirement::Exact(1);
229-
BitXorAssign(Op), sym::bitxor_assign, bitxor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
230-
BitAndAssign(Op), sym::bitand_assign, bitand_assign_trait, Target::Trait, GenericRequirement::Exact(1);
231-
BitOrAssign(Op), sym::bitor_assign, bitor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
232-
ShlAssign(Op), sym::shl_assign, shl_assign_trait, Target::Trait, GenericRequirement::Exact(1);
233-
ShrAssign(Op), sym::shr_assign, shr_assign_trait, Target::Trait, GenericRequirement::Exact(1);
234-
Index(Op), sym::index, index_trait, Target::Trait, GenericRequirement::Exact(1);
235-
IndexMut(Op), sym::index_mut, index_mut_trait, Target::Trait, GenericRequirement::Exact(1);
173+
Add, sym::add, add_trait, Target::Trait, GenericRequirement::Exact(1);
174+
Sub, sym::sub, sub_trait, Target::Trait, GenericRequirement::Exact(1);
175+
Mul, sym::mul, mul_trait, Target::Trait, GenericRequirement::Exact(1);
176+
Div, sym::div, div_trait, Target::Trait, GenericRequirement::Exact(1);
177+
Rem, sym::rem, rem_trait, Target::Trait, GenericRequirement::Exact(1);
178+
Neg, sym::neg, neg_trait, Target::Trait, GenericRequirement::Exact(0);
179+
Not, sym::not, not_trait, Target::Trait, GenericRequirement::Exact(0);
180+
BitXor, sym::bitxor, bitxor_trait, Target::Trait, GenericRequirement::Exact(1);
181+
BitAnd, sym::bitand, bitand_trait, Target::Trait, GenericRequirement::Exact(1);
182+
BitOr, sym::bitor, bitor_trait, Target::Trait, GenericRequirement::Exact(1);
183+
Shl, sym::shl, shl_trait, Target::Trait, GenericRequirement::Exact(1);
184+
Shr, sym::shr, shr_trait, Target::Trait, GenericRequirement::Exact(1);
185+
AddAssign, sym::add_assign, add_assign_trait, Target::Trait, GenericRequirement::Exact(1);
186+
SubAssign, sym::sub_assign, sub_assign_trait, Target::Trait, GenericRequirement::Exact(1);
187+
MulAssign, sym::mul_assign, mul_assign_trait, Target::Trait, GenericRequirement::Exact(1);
188+
DivAssign, sym::div_assign, div_assign_trait, Target::Trait, GenericRequirement::Exact(1);
189+
RemAssign, sym::rem_assign, rem_assign_trait, Target::Trait, GenericRequirement::Exact(1);
190+
BitXorAssign, sym::bitxor_assign, bitxor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
191+
BitAndAssign, sym::bitand_assign, bitand_assign_trait, Target::Trait, GenericRequirement::Exact(1);
192+
BitOrAssign, sym::bitor_assign, bitor_assign_trait, Target::Trait, GenericRequirement::Exact(1);
193+
ShlAssign, sym::shl_assign, shl_assign_trait, Target::Trait, GenericRequirement::Exact(1);
194+
ShrAssign, sym::shr_assign, shr_assign_trait, Target::Trait, GenericRequirement::Exact(1);
195+
Index, sym::index, index_trait, Target::Trait, GenericRequirement::Exact(1);
196+
IndexMut, sym::index_mut, index_mut_trait, Target::Trait, GenericRequirement::Exact(1);
236197

237198
UnsafeCell, sym::unsafe_cell, unsafe_cell_type, Target::Struct, GenericRequirement::None;
238199
VaList, sym::va_list, va_list, Target::Struct, GenericRequirement::None;
@@ -242,9 +203,9 @@ language_item_table! {
242203
DerefTarget, sym::deref_target, deref_target, Target::AssocTy, GenericRequirement::None;
243204
Receiver, sym::receiver, receiver_trait, Target::Trait, GenericRequirement::None;
244205

245-
Fn(Fn), kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
246-
FnMut(Fn), sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
247-
FnOnce(Fn), sym::fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
206+
Fn, kw::Fn, fn_trait, Target::Trait, GenericRequirement::Exact(1);
207+
FnMut, sym::fn_mut, fn_mut_trait, Target::Trait, GenericRequirement::Exact(1);
208+
FnOnce, sym::fn_once, fn_once_trait, Target::Trait, GenericRequirement::Exact(1);
248209

249210
FnOnceOutput, sym::fn_once_output, fn_once_output, Target::AssocTy, GenericRequirement::None;
250211

@@ -254,8 +215,8 @@ language_item_table! {
254215
Unpin, sym::unpin, unpin_trait, Target::Trait, GenericRequirement::None;
255216
Pin, sym::pin, pin_type, Target::Struct, GenericRequirement::None;
256217

257-
PartialEq(Op), sym::eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
258-
PartialOrd(Op), sym::partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);
218+
PartialEq, sym::eq, eq_trait, Target::Trait, GenericRequirement::Exact(1);
219+
PartialOrd, sym::partial_ord, partial_ord_trait, Target::Trait, GenericRequirement::Exact(1);
259220

260221
// A number of panic-related lang items. The `panic` item corresponds to divide-by-zero and
261222
// various panic cases with `match`. The `panic_bounds_check` item is for indexing arrays.
@@ -351,3 +312,34 @@ pub enum GenericRequirement {
351312
Minimum(usize),
352313
Exact(usize),
353314
}
315+
316+
pub static FN_TRAITS: &'static [LangItem] = &[LangItem::Fn, LangItem::FnMut, LangItem::FnOnce];
317+
318+
pub static OPERATORS: &'static [LangItem] = &[
319+
LangItem::Add,
320+
LangItem::Sub,
321+
LangItem::Mul,
322+
LangItem::Div,
323+
LangItem::Rem,
324+
LangItem::Neg,
325+
LangItem::Not,
326+
LangItem::BitXor,
327+
LangItem::BitAnd,
328+
LangItem::BitOr,
329+
LangItem::Shl,
330+
LangItem::Shr,
331+
LangItem::AddAssign,
332+
LangItem::SubAssign,
333+
LangItem::MulAssign,
334+
LangItem::DivAssign,
335+
LangItem::RemAssign,
336+
LangItem::BitXorAssign,
337+
LangItem::BitAndAssign,
338+
LangItem::BitOrAssign,
339+
LangItem::ShlAssign,
340+
LangItem::ShrAssign,
341+
LangItem::Index,
342+
LangItem::IndexMut,
343+
LangItem::PartialEq,
344+
LangItem::PartialOrd,
345+
];

compiler/rustc_passes/src/lang_items.rs

-3
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,6 @@ impl<'tcx> LanguageItemCollector<'tcx> {
140140

141141
// Matched.
142142
self.items.set(lang_item, item_def_id);
143-
if let Some(group) = lang_item.group() {
144-
self.items.groups[group as usize].push(item_def_id);
145-
}
146143
}
147144

148145
// Like collect_item() above, but also checks whether the lang item is declared

0 commit comments

Comments
 (0)