From 4738231cc78f25fe628a7a01269802466b346737 Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Tue, 12 Sep 2017 17:04:26 -0300 Subject: [PATCH 1/5] Remove deprecated lang items They have been deprecated for years and there is no trace left of them in the compiler. --- src/librustc/middle/lang_items.rs | 8 -------- src/librustc_typeck/variance/terms.rs | 9 --------- 2 files changed, 17 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index cb59d9870faac..ad243a1061d5f 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -327,14 +327,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; diff --git a/src/librustc_typeck/variance/terms.rs b/src/librustc_typeck/variance/terms.rs index 6062ac96ada78..ac3d575b64882 100644 --- a/src/librustc_typeck/variance/terms.rs +++ b/src/librustc_typeck/variance/terms.rs @@ -98,15 +98,6 @@ fn lang_items(tcx: TyCtxt) -> Vec<(ast::NodeId, Vec)> { 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, Variance) From 74107f987b35e401969ca3b596cddb1897646d06 Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Wed, 13 Sep 2017 11:16:45 -0300 Subject: [PATCH 2/5] Remove require_owned_box It's unused and unnecessary --- src/librustc/middle/lang_items.rs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index ad243a1061d5f..645da2475c627 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -83,10 +83,6 @@ impl LanguageItems { } } - pub fn require_owned_box(&self) -> Result { - self.require(OwnedBoxLangItem) - } - pub fn fn_trait_kind(&self, id: DefId) -> Option { let def_id_kinds = [ (self.fn_trait(), ty::ClosureKind::Fn), From ab30b6a6edac27b618240f05c0bdbcb39a999f8a Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Wed, 13 Sep 2017 12:00:40 -0300 Subject: [PATCH 3/5] Refactor how to get a lang item's name Small refactorings --- src/librustc/middle/lang_items.rs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 645da2475c627..9a70d7b9e3f89 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -46,6 +46,14 @@ enum_from_u32! { } } +impl LangItem { + fn name(self) -> &'static str { + match self { + $( $variant => $name, )* + } + } +} + pub struct LanguageItems { pub items: Vec>, pub missing: Vec, @@ -65,22 +73,8 @@ impl LanguageItems { &*self.items } - pub fn item_name(index: usize) -> &'static str { - let item: Option = LangItem::from_u32(index as u32); - match item { - $( Some($variant) => $name, )* - None => "???" - } - } - pub fn require(&self, it: LangItem) -> Result { - match self.items[it as usize] { - Some(id) => Ok(id), - None => { - Err(format!("requires `{}` lang_item", - LanguageItems::item_name(it as usize))) - } - } + self.items[it as usize].ok_or(format!("requires `{}` lang_item", it.name())) } pub fn fn_trait_kind(&self, id: DefId) -> Option { @@ -158,7 +152,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, From c2fb70be6b73b74df2b7c82288e6f11b0f35b1bc Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Wed, 13 Sep 2017 12:19:37 -0300 Subject: [PATCH 4/5] Refactor fn_trait_kind Short and sweet --- src/librustc/middle/lang_items.rs | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 9a70d7b9e3f89..086b598497d57 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -78,19 +78,12 @@ impl LanguageItems { } pub fn fn_trait_kind(&self, id: DefId) -> Option { - 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), + _ => None } - - None } $( From 9218e444342d13088092edec90a4f889f98ecff2 Mon Sep 17 00:00:00 2001 From: "leonardo.yvens" Date: Wed, 13 Sep 2017 14:21:16 -0300 Subject: [PATCH 5/5] Fix initial review --- src/librustc/middle/lang_items.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 086b598497d57..da9f9128cf4ce 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -74,14 +74,14 @@ impl LanguageItems { } pub fn require(&self, it: LangItem) -> Result { - self.items[it as usize].ok_or(format!("requires `{}` lang_item", it.name())) + self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) } pub fn fn_trait_kind(&self, id: DefId) -> Option { 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), + x if x == self.fn_once_trait() => Some(ty::ClosureKind::FnOnce), _ => None } }