Skip to content

Commit ab30b6a

Browse files
committed
Refactor how to get a lang item's name
Small refactorings
1 parent 74107f9 commit ab30b6a

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

src/librustc/middle/lang_items.rs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ enum_from_u32! {
4646
}
4747
}
4848

49+
impl LangItem {
50+
fn name(self) -> &'static str {
51+
match self {
52+
$( $variant => $name, )*
53+
}
54+
}
55+
}
56+
4957
pub struct LanguageItems {
5058
pub items: Vec<Option<DefId>>,
5159
pub missing: Vec<LangItem>,
@@ -65,22 +73,8 @@ impl LanguageItems {
6573
&*self.items
6674
}
6775

68-
pub fn item_name(index: usize) -> &'static str {
69-
let item: Option<LangItem> = LangItem::from_u32(index as u32);
70-
match item {
71-
$( Some($variant) => $name, )*
72-
None => "???"
73-
}
74-
}
75-
7676
pub fn require(&self, it: LangItem) -> Result<DefId, String> {
77-
match self.items[it as usize] {
78-
Some(id) => Ok(id),
79-
None => {
80-
Err(format!("requires `{}` lang_item",
81-
LanguageItems::item_name(it as usize)))
82-
}
83-
}
77+
self.items[it as usize].ok_or(format!("requires `{}` lang_item", it.name()))
8478
}
8579

8680
pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
@@ -158,7 +152,7 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
158152
// Check for duplicates.
159153
match self.items.items[item_index] {
160154
Some(original_def_id) if original_def_id != item_def_id => {
161-
let name = LanguageItems::item_name(item_index);
155+
let name = LangItem::from_u32(item_index as u32).unwrap().name();
162156
let mut err = match self.tcx.hir.span_if_local(item_def_id) {
163157
Some(span) => struct_span_err!(
164158
self.tcx.sess,

0 commit comments

Comments
 (0)