Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
55 changes: 15 additions & 40 deletions src/librustc/middle/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ enum_from_u32! {
}
}

impl LangItem {
fn name(self) -> &'static str {
match self {
$( $variant => $name, )*
}
}
}

pub struct LanguageItems {
pub items: Vec<Option<DefId>>,
pub missing: Vec<LangItem>,
Expand All @@ -65,42 +73,17 @@ impl LanguageItems {
&*self.items
}

pub fn item_name(index: usize) -> &'static str {
let item: Option<LangItem> = LangItem::from_u32(index as u32);
match item {
$( Some($variant) => $name, )*
None => "???"
}
}

pub fn require(&self, it: LangItem) -> Result<DefId, String> {
match self.items[it as usize] {
Some(id) => Ok(id),
None => {
Err(format!("requires `{}` lang_item",
LanguageItems::item_name(it as usize)))
}
}
}

pub fn require_owned_box(&self) -> Result<DefId, String> {
self.require(OwnedBoxLangItem)
self.items[it as usize].ok_or(format!("requires `{}` lang_item", it.name()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make this an ok_or_else since otherwise we allocate a String each name, and also go through a lookup table to get the name of the lang item, both of which are somewhat expensive operations to do for lang item lookup.

}

pub fn fn_trait_kind(&self, id: DefId) -> Option<ty::ClosureKind> {
let def_id_kinds = [
(self.fn_trait(), ty::ClosureKind::Fn),
(self.fn_mut_trait(), ty::ClosureKind::FnMut),
(self.fn_once_trait(), ty::ClosureKind::FnOnce),
];

for &(opt_def_id, kind) in &def_id_kinds {
if Some(id) == opt_def_id {
return Some(kind);
}
match Some(id) {
x if x == self.fn_trait() => Some(ty::ClosureKind::Fn),
x if x == self.fn_mut_trait() => Some(ty::ClosureKind::FnMut),
x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Extra space after the =>.

_ => None
}

None
}

$(
Expand Down Expand Up @@ -162,7 +145,7 @@ impl<'a, 'tcx> LanguageItemCollector<'a, 'tcx> {
// Check for duplicates.
match self.items.items[item_index] {
Some(original_def_id) if original_def_id != item_def_id => {
let name = LanguageItems::item_name(item_index);
let name = LangItem::from_u32(item_index as u32).unwrap().name();
let mut err = match self.tcx.hir.span_if_local(item_def_id) {
Some(span) => struct_span_err!(
self.tcx.sess,
Expand Down Expand Up @@ -327,14 +310,6 @@ language_item_table! {

PhantomDataItem, "phantom_data", phantom_data;

// Deprecated:
CovariantTypeItem, "covariant_type", covariant_type;
ContravariantTypeItem, "contravariant_type", contravariant_type;
InvariantTypeItem, "invariant_type", invariant_type;
CovariantLifetimeItem, "covariant_lifetime", covariant_lifetime;
ContravariantLifetimeItem, "contravariant_lifetime", contravariant_lifetime;
InvariantLifetimeItem, "invariant_lifetime", invariant_lifetime;

NonZeroItem, "non_zero", non_zero;

DebugTraitLangItem, "debug_trait", debug_trait;
Expand Down
9 changes: 0 additions & 9 deletions src/librustc_typeck/variance/terms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,6 @@ fn lang_items(tcx: TyCtxt) -> Vec<(ast::NodeId, Vec<ty::Variance>)> {
let all = vec![
(lang_items.phantom_data(), vec![ty::Covariant]),
(lang_items.unsafe_cell_type(), vec![ty::Invariant]),

// Deprecated:
(lang_items.covariant_type(), vec![ty::Covariant]),
(lang_items.contravariant_type(), vec![ty::Contravariant]),
(lang_items.invariant_type(), vec![ty::Invariant]),
(lang_items.covariant_lifetime(), vec![ty::Covariant]),
(lang_items.contravariant_lifetime(), vec![ty::Contravariant]),
(lang_items.invariant_lifetime(), vec![ty::Invariant]),

];

all.into_iter() // iterating over (Option<DefId>, Variance)
Expand Down