From 60aaf90834de9ee10f92a49c0e7123f65f8a0cd8 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 30 Jan 2020 08:19:59 +0100 Subject: [PATCH 01/11] Move macro enum_from_u32 to rustc_data_structures. --- src/librustc/macros.rs | 37 ------------------------- src/librustc_data_structures/macros.rs | 38 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/librustc/macros.rs b/src/librustc/macros.rs index 2bda0c0bef02d..88ddd96eec8f5 100644 --- a/src/librustc/macros.rs +++ b/src/librustc/macros.rs @@ -1,40 +1,3 @@ -macro_rules! enum_from_u32 { - ($(#[$attr:meta])* pub enum $name:ident { - $($variant:ident = $e:expr,)* - }) => { - $(#[$attr])* - pub enum $name { - $($variant = $e),* - } - - impl $name { - pub fn from_u32(u: u32) -> Option<$name> { - $(if u == $name::$variant as u32 { - return Some($name::$variant) - })* - None - } - } - }; - ($(#[$attr:meta])* pub enum $name:ident { - $($variant:ident,)* - }) => { - $(#[$attr])* - pub enum $name { - $($variant,)* - } - - impl $name { - pub fn from_u32(u: u32) -> Option<$name> { - $(if u == $name::$variant as u32 { - return Some($name::$variant) - })* - None - } - } - } -} - #[macro_export] macro_rules! bug { () => ( bug!("impossible case reached") ); diff --git a/src/librustc_data_structures/macros.rs b/src/librustc_data_structures/macros.rs index 83e6dbedee226..67fbe3058cdb9 100644 --- a/src/librustc_data_structures/macros.rs +++ b/src/librustc_data_structures/macros.rs @@ -17,3 +17,41 @@ macro_rules! static_assert_size { const _: [(); $size] = [(); ::std::mem::size_of::<$ty>()]; }; } + +#[macro_export] +macro_rules! enum_from_u32 { + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident = $e:expr,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant = $e),* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + }; + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant,)* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + } +} From a056817aaed18914b43980bc0f5645202a0b7bad Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 26 Jan 2020 12:07:22 +0100 Subject: [PATCH 02/11] Move hir::check_attr::Target to librustc_lang_items. --- Cargo.lock | 15 +++++++++++++++ src/librustc/Cargo.toml | 1 + src/librustc/hir/mod.rs | 1 - src/librustc/middle/lang_items.rs | 2 +- src/librustc_lang_items/Cargo.toml | 18 ++++++++++++++++++ src/librustc_lang_items/lib.rs | 3 +++ .../target.rs} | 0 src/librustc_passes/Cargo.toml | 1 + src/librustc_passes/check_attr.rs | 2 +- 9 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/librustc_lang_items/Cargo.toml create mode 100644 src/librustc_lang_items/lib.rs rename src/{librustc/hir/check_attr.rs => librustc_lang_items/target.rs} (100%) diff --git a/Cargo.lock b/Cargo.lock index bbfda0fa2c846..ca47a11035354 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3100,6 +3100,7 @@ dependencies = [ "rustc_feature", "rustc_hir", "rustc_index", + "rustc_lang_items", "rustc_macros", "rustc_session", "rustc_span", @@ -3698,6 +3699,19 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "rustc_lang_items" +version = "0.0.0" +dependencies = [ + "lazy_static 1.4.0", + "rustc_data_structures", + "rustc_hir", + "rustc_macros", + "rustc_span", + "serialize", + "syntax", +] + [[package]] name = "rustc_lexer" version = "0.1.0" @@ -3851,6 +3865,7 @@ dependencies = [ "rustc_feature", "rustc_hir", "rustc_index", + "rustc_lang_items", "rustc_session", "rustc_span", "rustc_target", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index 782c6879ac58f..6cd979b0babf4 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -24,6 +24,7 @@ rustc_apfloat = { path = "../librustc_apfloat" } rustc_attr = { path = "../librustc_attr" } rustc_feature = { path = "../librustc_feature" } rustc_hir = { path = "../librustc_hir" } +rustc_lang_items = { path = "../librustc_lang_items" } rustc_target = { path = "../librustc_target" } rustc_macros = { path = "../librustc_macros" } rustc_data_structures = { path = "../librustc_data_structures" } diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 2e7e8fdd72491..7d48280661a64 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -2,7 +2,6 @@ //! //! [rustc guide]: https://rust-lang.github.io/rustc-guide/hir.html -pub mod check_attr; pub mod exports; pub mod map; diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 9e33ee8da2152..b697ef0155225 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -9,7 +9,6 @@ pub use self::LangItem::*; -use crate::hir::check_attr::Target; use crate::middle::cstore::ExternCrate; use crate::middle::weak_lang_items; use crate::ty::{self, TyCtxt}; @@ -19,6 +18,7 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::itemlikevisit::ItemLikeVisitor; +use rustc_lang_items::Target; use rustc_macros::HashStable; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; diff --git a/src/librustc_lang_items/Cargo.toml b/src/librustc_lang_items/Cargo.toml new file mode 100644 index 0000000000000..eddd4fb81dcc2 --- /dev/null +++ b/src/librustc_lang_items/Cargo.toml @@ -0,0 +1,18 @@ +[package] +authors = ["The Rust Project Developers"] +name = "rustc_lang_items" +version = "0.0.0" +edition = "2018" + +[lib] +name = "rustc_lang_items" +path = "lib.rs" + +[dependencies] +lazy_static = "1" +rustc_data_structures = { path = "../librustc_data_structures" } +rustc_hir = { path = "../librustc_hir" } +rustc_macros = { path = "../librustc_macros" } +rustc_serialize = { path = "../libserialize", package = "serialize" } +rustc_span = { path = "../librustc_span" } +syntax = { path = "../libsyntax" } diff --git a/src/librustc_lang_items/lib.rs b/src/librustc_lang_items/lib.rs new file mode 100644 index 0000000000000..1d2cbefa64a35 --- /dev/null +++ b/src/librustc_lang_items/lib.rs @@ -0,0 +1,3 @@ +mod target; + +pub use target::{MethodKind, Target}; diff --git a/src/librustc/hir/check_attr.rs b/src/librustc_lang_items/target.rs similarity index 100% rename from src/librustc/hir/check_attr.rs rename to src/librustc_lang_items/target.rs diff --git a/src/librustc_passes/Cargo.toml b/src/librustc_passes/Cargo.toml index 981ef7f8796d3..5ed192c593280 100644 --- a/src/librustc_passes/Cargo.toml +++ b/src/librustc_passes/Cargo.toml @@ -17,6 +17,7 @@ rustc_errors = { path = "../librustc_errors" } rustc_feature = { path = "../librustc_feature" } rustc_hir = { path = "../librustc_hir" } rustc_index = { path = "../librustc_index" } +rustc_lang_items = { path = "../librustc_lang_items" } rustc_session = { path = "../librustc_session" } rustc_target = { path = "../librustc_target" } syntax = { path = "../libsyntax" } diff --git a/src/librustc_passes/check_attr.rs b/src/librustc_passes/check_attr.rs index ecffd615e71c0..57a21bf716ddb 100644 --- a/src/librustc_passes/check_attr.rs +++ b/src/librustc_passes/check_attr.rs @@ -4,7 +4,6 @@ //! conflicts between multiple such attributes attached to the same //! item. -use rustc::hir::check_attr::{MethodKind, Target}; use rustc::hir::map::Map; use rustc::ty::query::Providers; use rustc::ty::TyCtxt; @@ -15,6 +14,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::DUMMY_HIR_ID; use rustc_hir::{self, HirId, Item, ItemKind, TraitItem}; +use rustc_lang_items::{MethodKind, Target}; use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES}; use rustc_span::symbol::sym; use rustc_span::Span; From ff369236a3895e144ef2ba1d8a24727e27e61f1c Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 26 Jan 2020 12:49:18 +0100 Subject: [PATCH 03/11] Move lang_items definitions to librustc_lang_items. --- src/librustc/ich/impls_hir.rs | 8 +- src/librustc/middle/lang_items.rs | 324 +++++-------------------- src/librustc/middle/weak_lang_items.rs | 2 +- src/librustc/traits/select.rs | 7 +- src/librustc/ty/instance.rs | 2 +- src/librustc/ty/print/pretty.rs | 2 +- src/librustc_lang_items/lang_items.rs | 261 ++++++++++++++++++++ src/librustc_lang_items/lib.rs | 41 ++++ src/librustc_mir/shim.rs | 2 +- src/librustc_typeck/check/closure.rs | 9 +- src/librustdoc/clean/utils.rs | 2 +- 11 files changed, 372 insertions(+), 288 deletions(-) create mode 100644 src/librustc_lang_items/lang_items.rs diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index 625d8a4670f22..fa50632e9db82 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -107,6 +107,8 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { } } +impl<'ctx> rustc_lang_items::HashStableContext for StableHashingContext<'ctx> {} + impl<'a> ToStableHashKey> for DefId { type KeyType = DefPathHash; @@ -251,12 +253,6 @@ impl<'a> ToStableHashKey> for hir::def_id::DefIndex { } } -impl<'a> HashStable> for crate::middle::lang_items::LangItem { - fn hash_stable(&self, _: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { - ::std::hash::Hash::hash(self, hasher); - } -} - impl<'a> HashStable> for hir::TraitCandidate { fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) { hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| { diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index b697ef0155225..aa235f545b537 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -13,139 +13,64 @@ use crate::middle::cstore::ExternCrate; use crate::middle::weak_lang_items; use crate::ty::{self, TyCtxt}; -use rustc_data_structures::fx::FxHashMap; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::itemlikevisit::ItemLikeVisitor; +use rustc_lang_items::lang_items::ITEM_REFS; use rustc_lang_items::Target; -use rustc_macros::HashStable; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; use syntax::ast; -// The actual lang items defined come at the end of this file in one handy table. -// So you probably just want to nip down to the end. -macro_rules! language_item_table { - ( - $( $variant:ident, $name:expr, $method:ident, $target:path; )* - ) => { - -enum_from_u32! { - /// A representation of all the valid language items in Rust. - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] - pub enum LangItem { - $($variant,)* - } -} - -impl LangItem { - /// Returns the `name` in `#[lang = "$name"]`. - /// For example, `LangItem::EqTraitLangItem`, - /// that is `#[lang = "eq"]` would result in `"eq"`. - fn name(self) -> &'static str { - match self { - $( $variant => $name, )* - } - } -} - -#[derive(HashStable)] -pub struct LanguageItems { - /// Mappings from lang items to their possibly found `DefId`s. - /// The index corresponds to the order in `LangItem`. - pub items: Vec>, - /// Lang items that were not found during collection. - pub missing: Vec, -} - -impl LanguageItems { - /// Construct an empty collection of lang items and no missing ones. - pub fn new() -> Self { - fn init_none(_: LangItem) -> Option { None } - - Self { - items: vec![$(init_none($variant)),*], - missing: Vec::new(), - } - } - - /// Returns the mappings to the possibly found `DefId`s for each lang item. - pub fn items(&self) -> &[Option] { - &*self.items - } - - /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. - /// If it wasn't bound, e.g. due to a missing `#[lang = ""]`, - /// returns an error message as a string. - pub fn require(&self, it: LangItem) -> Result { - self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) - } - - /// Returns the kind of closure that `id`, which is one of the `Fn*` traits, corresponds to. - /// If `id` is not one of the `Fn*` traits, `None` is returned. - 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), - _ => None - } - } - - $( - /// Returns the corresponding `DefId` for the lang item - #[doc = $name] - /// if it exists. - #[allow(dead_code)] - pub fn $method(&self) -> Option { - self.items[$variant as usize] - } - )* -} +pub use rustc_lang_items::{LangItem, LanguageItems}; struct LanguageItemCollector<'tcx> { items: LanguageItems, tcx: TyCtxt<'tcx>, - /// A mapping from the name of the lang item to its order and the form it must be of. - item_refs: FxHashMap<&'static str, (usize, Target)>, } impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { fn visit_item(&mut self, item: &hir::Item<'_>) { if let Some((value, span)) = extract(&item.attrs) { let actual_target = Target::from_item(item); - match self.item_refs.get(&*value.as_str()).cloned() { + match ITEM_REFS.get(&*value.as_str()).cloned() { // Known lang item with attribute on correct target. Some((item_index, expected_target)) if actual_target == expected_target => { let def_id = self.tcx.hir().local_def_id(item.hir_id); self.collect_item(item_index, def_id); - }, + } // Known lang item with attribute on incorrect target. Some((_, expected_target)) => { struct_span_err!( - self.tcx.sess, span, E0718, + self.tcx.sess, + span, + E0718, "`{}` language item must be applied to a {}", - value, expected_target, - ).span_label( + value, + expected_target, + ) + .span_label( span, format!( "attribute should be applied to a {}, not a {}", expected_target, actual_target, ), - ).emit(); - }, + ) + .emit(); + } // Unknown lang item. _ => { struct_span_err!( - self.tcx.sess, span, E0522, + self.tcx.sess, + span, + E0522, "definition of an unknown language item: `{}`", value - ).span_label( - span, - format!("definition of unknown language item `{}`", value) - ).emit(); - }, + ) + .span_label(span, format!("definition of unknown language item `{}`", value)) + .emit(); + } } } } @@ -161,15 +86,7 @@ impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { impl LanguageItemCollector<'tcx> { fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> { - let mut item_refs = FxHashMap::default(); - - $( item_refs.insert($name, ($variant as usize, $target)); )* - - LanguageItemCollector { - tcx, - items: LanguageItems::new(), - item_refs, - } + LanguageItemCollector { tcx, items: LanguageItems::new() } } fn collect_item(&mut self, item_index: usize, item_def_id: DefId) { @@ -185,37 +102,37 @@ impl LanguageItemCollector<'tcx> { "found duplicate lang item `{}`", name ), - None => { - match self.tcx.extern_crate(item_def_id) { - Some(ExternCrate {dependency_of, ..}) => { - self.tcx.sess.struct_err(&format!( + None => match self.tcx.extern_crate(item_def_id) { + Some(ExternCrate { dependency_of, .. }) => { + self.tcx.sess.struct_err(&format!( "duplicate lang item in crate `{}` (which `{}` depends on): `{}`.", self.tcx.crate_name(item_def_id.krate), self.tcx.crate_name(*dependency_of), - name)) - }, - _ => { - self.tcx.sess.struct_err(&format!( - "duplicate lang item in crate `{}`: `{}`.", - self.tcx.crate_name(item_def_id.krate), - name)) - } + name + )) } + _ => self.tcx.sess.struct_err(&format!( + "duplicate lang item in crate `{}`: `{}`.", + self.tcx.crate_name(item_def_id.krate), + name + )), }, }; if let Some(span) = self.tcx.hir().span_if_local(original_def_id) { err.span_note(span, "the lang item is first defined here"); } else { match self.tcx.extern_crate(original_def_id) { - Some(ExternCrate {dependency_of, ..}) => { + Some(ExternCrate { dependency_of, .. }) => { err.note(&format!( "the lang item is first defined in crate `{}` (which `{}` depends on)", self.tcx.crate_name(original_def_id.krate), self.tcx.crate_name(*dependency_of))); - }, + } _ => { - err.note(&format!("the lang item is first defined in crate `{}`.", - self.tcx.crate_name(original_def_id.krate))); + err.note(&format!( + "the lang item is first defined in crate `{}`.", + self.tcx.crate_name(original_def_id.krate) + )); } } } @@ -232,12 +149,14 @@ impl LanguageItemCollector<'tcx> { /// The attributes `#[panic_handler]` and `#[alloc_error_handler]` /// are also extracted out when found. pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { - attrs.iter().find_map(|attr| Some(match attr { - _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), - _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), - _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), - _ => return None, - })) + attrs.iter().find_map(|attr| { + Some(match attr { + _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), + _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), + _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), + _ => return None, + }) + }) } /// Traverses and collects all the lang items in all crates. @@ -264,147 +183,6 @@ pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems { items } -// End of the macro - } -} - -language_item_table! { -// Variant name, Name, Method name, Target; - BoolImplItem, "bool", bool_impl, Target::Impl; - CharImplItem, "char", char_impl, Target::Impl; - StrImplItem, "str", str_impl, Target::Impl; - SliceImplItem, "slice", slice_impl, Target::Impl; - SliceU8ImplItem, "slice_u8", slice_u8_impl, Target::Impl; - StrAllocImplItem, "str_alloc", str_alloc_impl, Target::Impl; - SliceAllocImplItem, "slice_alloc", slice_alloc_impl, Target::Impl; - SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl; - ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl; - MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl; - I8ImplItem, "i8", i8_impl, Target::Impl; - I16ImplItem, "i16", i16_impl, Target::Impl; - I32ImplItem, "i32", i32_impl, Target::Impl; - I64ImplItem, "i64", i64_impl, Target::Impl; - I128ImplItem, "i128", i128_impl, Target::Impl; - IsizeImplItem, "isize", isize_impl, Target::Impl; - U8ImplItem, "u8", u8_impl, Target::Impl; - U16ImplItem, "u16", u16_impl, Target::Impl; - U32ImplItem, "u32", u32_impl, Target::Impl; - U64ImplItem, "u64", u64_impl, Target::Impl; - U128ImplItem, "u128", u128_impl, Target::Impl; - UsizeImplItem, "usize", usize_impl, Target::Impl; - F32ImplItem, "f32", f32_impl, Target::Impl; - F64ImplItem, "f64", f64_impl, Target::Impl; - F32RuntimeImplItem, "f32_runtime", f32_runtime_impl, Target::Impl; - F64RuntimeImplItem, "f64_runtime", f64_runtime_impl, Target::Impl; - - SizedTraitLangItem, "sized", sized_trait, Target::Trait; - UnsizeTraitLangItem, "unsize", unsize_trait, Target::Trait; - // trait injected by #[derive(PartialEq)], (i.e. "Partial EQ"). - StructuralPeqTraitLangItem, "structural_peq", structural_peq_trait, Target::Trait; - // trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize). - StructuralTeqTraitLangItem, "structural_teq", structural_teq_trait, Target::Trait; - CopyTraitLangItem, "copy", copy_trait, Target::Trait; - CloneTraitLangItem, "clone", clone_trait, Target::Trait; - SyncTraitLangItem, "sync", sync_trait, Target::Trait; - FreezeTraitLangItem, "freeze", freeze_trait, Target::Trait; - - DropTraitLangItem, "drop", drop_trait, Target::Trait; - - CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait; - DispatchFromDynTraitLangItem,"dispatch_from_dyn", dispatch_from_dyn_trait, Target::Trait; - - AddTraitLangItem, "add", add_trait, Target::Trait; - SubTraitLangItem, "sub", sub_trait, Target::Trait; - MulTraitLangItem, "mul", mul_trait, Target::Trait; - DivTraitLangItem, "div", div_trait, Target::Trait; - RemTraitLangItem, "rem", rem_trait, Target::Trait; - NegTraitLangItem, "neg", neg_trait, Target::Trait; - NotTraitLangItem, "not", not_trait, Target::Trait; - BitXorTraitLangItem, "bitxor", bitxor_trait, Target::Trait; - BitAndTraitLangItem, "bitand", bitand_trait, Target::Trait; - BitOrTraitLangItem, "bitor", bitor_trait, Target::Trait; - ShlTraitLangItem, "shl", shl_trait, Target::Trait; - ShrTraitLangItem, "shr", shr_trait, Target::Trait; - AddAssignTraitLangItem, "add_assign", add_assign_trait, Target::Trait; - SubAssignTraitLangItem, "sub_assign", sub_assign_trait, Target::Trait; - MulAssignTraitLangItem, "mul_assign", mul_assign_trait, Target::Trait; - DivAssignTraitLangItem, "div_assign", div_assign_trait, Target::Trait; - RemAssignTraitLangItem, "rem_assign", rem_assign_trait, Target::Trait; - BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait, Target::Trait; - BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait, Target::Trait; - BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait, Target::Trait; - ShlAssignTraitLangItem, "shl_assign", shl_assign_trait, Target::Trait; - ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait; - IndexTraitLangItem, "index", index_trait, Target::Trait; - IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait; - - UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct; - VaListTypeLangItem, "va_list", va_list, Target::Struct; - - DerefTraitLangItem, "deref", deref_trait, Target::Trait; - DerefMutTraitLangItem, "deref_mut", deref_mut_trait, Target::Trait; - ReceiverTraitLangItem, "receiver", receiver_trait, Target::Trait; - - FnTraitLangItem, "fn", fn_trait, Target::Trait; - FnMutTraitLangItem, "fn_mut", fn_mut_trait, Target::Trait; - FnOnceTraitLangItem, "fn_once", fn_once_trait, Target::Trait; - - FutureTraitLangItem, "future_trait", future_trait, Target::Trait; - GeneratorStateLangItem, "generator_state", gen_state, Target::Enum; - GeneratorTraitLangItem, "generator", gen_trait, Target::Trait; - UnpinTraitLangItem, "unpin", unpin_trait, Target::Trait; - PinTypeLangItem, "pin", pin_type, Target::Struct; - - // Don't be fooled by the naming here: this lang item denotes `PartialEq`, not `Eq`. - EqTraitLangItem, "eq", eq_trait, Target::Trait; - PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait; - - // A number of panic-related lang items. The `panic` item corresponds to - // divide-by-zero and various panic cases with `match`. The - // `panic_bounds_check` item is for indexing arrays. - // - // The `begin_unwind` lang item has a predefined symbol name and is sort of - // a "weak lang item" in the sense that a crate is not required to have it - // defined to use it, but a final product is required to define it - // somewhere. Additionally, there are restrictions on crates that use a weak - // lang item, but do not have it defined. - PanicFnLangItem, "panic", panic_fn, Target::Fn; - PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn; - PanicInfoLangItem, "panic_info", panic_info, Target::Struct; - PanicLocationLangItem, "panic_location", panic_location, Target::Struct; - PanicImplLangItem, "panic_impl", panic_impl, Target::Fn; - // Libstd panic entry point. Necessary for const eval to be able to catch it - BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn; - - ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn, Target::Fn; - BoxFreeFnLangItem, "box_free", box_free_fn, Target::Fn; - DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn, Target::Fn; - OomLangItem, "oom", oom, Target::Fn; - AllocLayoutLangItem, "alloc_layout", alloc_layout, Target::Struct; - - StartFnLangItem, "start", start_fn, Target::Fn; - - EhPersonalityLangItem, "eh_personality", eh_personality, Target::Fn; - EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume, Target::Fn; - EhCatchTypeinfoLangItem, "eh_catch_typeinfo", eh_catch_typeinfo, Target::Static; - - OwnedBoxLangItem, "owned_box", owned_box, Target::Struct; - - PhantomDataItem, "phantom_data", phantom_data, Target::Struct; - - ManuallyDropItem, "manually_drop", manually_drop, Target::Struct; - - MaybeUninitLangItem, "maybe_uninit", maybe_uninit, Target::Union; - - // Align offset for stride != 1; must not panic. - AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn; - - TerminationTraitLangItem, "termination", termination, Target::Trait; - - Arc, "arc", arc, Target::Struct; - Rc, "rc", rc, Target::Struct; -} - impl<'tcx> TyCtxt<'tcx> { /// Returns the `DefId` for a given `LangItem`. /// If not found, fatally aborts compilation. @@ -417,4 +195,14 @@ impl<'tcx> TyCtxt<'tcx> { } }) } + + pub fn fn_trait_lang_item(&self, id: DefId) -> Option { + let items = self.lang_items(); + match Some(id) { + x if x == items.fn_trait() => Some(ty::ClosureKind::Fn), + x if x == items.fn_mut_trait() => Some(ty::ClosureKind::FnMut), + x if x == items.fn_once_trait() => Some(ty::ClosureKind::FnOnce), + _ => None, + } + } } diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs index 5571f8f2313d5..82bf0d200d75f 100644 --- a/src/librustc/middle/weak_lang_items.rs +++ b/src/librustc/middle/weak_lang_items.rs @@ -46,7 +46,7 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, } pub fn link_name(attrs: &[ast::Attribute]) -> Option { - lang_items::extract(attrs).and_then(|(name, _)| { + rustc_lang_items::lang_items::extract(attrs).and_then(|(name, _)| { $(if name == sym::$name { Some(sym::$sym) } else)* { diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index 17337ff0c92a6..4c4947d134fcc 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -1634,7 +1634,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { - let kind = match self.tcx().lang_items().fn_trait_kind(obligation.predicate.def_id()) { + let kind = match self.tcx().fn_trait_lang_item(obligation.predicate.def_id()) { Some(k) => k, None => { return Ok(()); @@ -1677,7 +1677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { // We provide impl of all fn traits for fn pointers. - if self.tcx().lang_items().fn_trait_kind(obligation.predicate.def_id()).is_none() { + if self.tcx().fn_trait_lang_item(obligation.predicate.def_id()).is_none() { return Ok(()); } @@ -2889,8 +2889,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let kind = self .tcx() - .lang_items() - .fn_trait_kind(obligation.predicate.def_id()) + .fn_trait_lang_item(obligation.predicate.def_id()) .unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation)); // Okay to skip binder because the substs on closure types never diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index c7f19513f666e..b175de6e0e88c 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -450,7 +450,7 @@ fn resolve_associated_item<'tcx>( substs: generator_data.substs, }), traits::VtableClosure(closure_data) => { - let trait_closure_kind = tcx.lang_items().fn_trait_kind(trait_id).unwrap(); + let trait_closure_kind = tcx.fn_trait_lang_item(trait_id).unwrap(); Some(Instance::resolve_closure( tcx, closure_data.closure_def_id, diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 0da680d1f915e..2a43d0486d06b 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -724,7 +724,7 @@ pub trait PrettyPrinter<'tcx>: let mut resugared = false; // Special-case `Fn(...) -> ...` and resugar it. - let fn_trait_kind = self.tcx().lang_items().fn_trait_kind(principal.def_id); + let fn_trait_kind = self.tcx().fn_trait_lang_item(principal.def_id); if !self.tcx().sess.verbose() && fn_trait_kind.is_some() { if let ty::Tuple(ref args) = principal.substs.type_at(0).kind { let mut projections = predicates.projection_bounds(); diff --git a/src/librustc_lang_items/lang_items.rs b/src/librustc_lang_items/lang_items.rs new file mode 100644 index 0000000000000..6cccad73fcbad --- /dev/null +++ b/src/librustc_lang_items/lang_items.rs @@ -0,0 +1,261 @@ +//! Detecting language items. +//! +//! Language items are items that represent concepts intrinsic to the language +//! itself. Examples are: +//! +//! * Traits that specify "kinds"; e.g., `Sync`, `Send`. +//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`. +//! * Functions called by the compiler itself. + +pub use self::LangItem::*; + +use crate::Target; + +use rustc_data_structures::fx::FxHashMap; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; +use rustc_hir::def_id::DefId; +use rustc_macros::HashStable_Generic; +use rustc_span::symbol::{sym, Symbol}; +use rustc_span::Span; +use syntax::ast; + +use lazy_static::lazy_static; + +// The actual lang items defined come at the end of this file in one handy table. +// So you probably just want to nip down to the end. +macro_rules! language_item_table { + ( + $( $variant:ident, $name:expr, $method:ident, $target:path; )* + ) => { + +enum_from_u32! { + /// A representation of all the valid language items in Rust. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] + pub enum LangItem { + $($variant,)* + } +} + +impl LangItem { + /// Returns the `name` in `#[lang = "$name"]`. + /// For example, `LangItem::EqTraitLangItem`, + /// that is `#[lang = "eq"]` would result in `"eq"`. + pub fn name(self) -> &'static str { + match self { + $( $variant => $name, )* + } + } +} + +#[derive(HashStable_Generic)] +pub struct LanguageItems { + /// Mappings from lang items to their possibly found `DefId`s. + /// The index corresponds to the order in `LangItem`. + pub items: Vec>, + /// Lang items that were not found during collection. + pub missing: Vec, +} + +impl LanguageItems { + /// Construct an empty collection of lang items and no missing ones. + pub fn new() -> Self { + fn init_none(_: LangItem) -> Option { None } + + Self { + items: vec![$(init_none($variant)),*], + missing: Vec::new(), + } + } + + /// Returns the mappings to the possibly found `DefId`s for each lang item. + pub fn items(&self) -> &[Option] { + &*self.items + } + + /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. + /// If it wasn't bound, e.g. due to a missing `#[lang = ""]`, + /// returns an error message as a string. + pub fn require(&self, it: LangItem) -> Result { + self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) + } + + $( + /// Returns the corresponding `DefId` for the lang item + #[doc = $name] + /// if it exists. + #[allow(dead_code)] + pub fn $method(&self) -> Option { + self.items[$variant as usize] + } + )* +} + +lazy_static! { + /// A mapping from the name of the lang item to its order and the form it must be of. + pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = { + let mut item_refs = FxHashMap::default(); + $( item_refs.insert($name, ($variant as usize, $target)); )* + item_refs + }; +} + +// End of the macro + } +} + +impl HashStable for LangItem { + fn hash_stable(&self, _: &mut CTX, hasher: &mut StableHasher) { + ::std::hash::Hash::hash(self, hasher); + } +} + +/// Extracts the first `lang = "$name"` out of a list of attributes. +/// The attributes `#[panic_handler]` and `#[alloc_error_handler]` +/// are also extracted out when found. +pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { + attrs.iter().find_map(|attr| { + Some(match attr { + _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), + _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), + _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), + _ => return None, + }) + }) +} + +language_item_table! { +// Variant name, Name, Method name, Target; + BoolImplItem, "bool", bool_impl, Target::Impl; + CharImplItem, "char", char_impl, Target::Impl; + StrImplItem, "str", str_impl, Target::Impl; + SliceImplItem, "slice", slice_impl, Target::Impl; + SliceU8ImplItem, "slice_u8", slice_u8_impl, Target::Impl; + StrAllocImplItem, "str_alloc", str_alloc_impl, Target::Impl; + SliceAllocImplItem, "slice_alloc", slice_alloc_impl, Target::Impl; + SliceU8AllocImplItem, "slice_u8_alloc", slice_u8_alloc_impl, Target::Impl; + ConstPtrImplItem, "const_ptr", const_ptr_impl, Target::Impl; + MutPtrImplItem, "mut_ptr", mut_ptr_impl, Target::Impl; + I8ImplItem, "i8", i8_impl, Target::Impl; + I16ImplItem, "i16", i16_impl, Target::Impl; + I32ImplItem, "i32", i32_impl, Target::Impl; + I64ImplItem, "i64", i64_impl, Target::Impl; + I128ImplItem, "i128", i128_impl, Target::Impl; + IsizeImplItem, "isize", isize_impl, Target::Impl; + U8ImplItem, "u8", u8_impl, Target::Impl; + U16ImplItem, "u16", u16_impl, Target::Impl; + U32ImplItem, "u32", u32_impl, Target::Impl; + U64ImplItem, "u64", u64_impl, Target::Impl; + U128ImplItem, "u128", u128_impl, Target::Impl; + UsizeImplItem, "usize", usize_impl, Target::Impl; + F32ImplItem, "f32", f32_impl, Target::Impl; + F64ImplItem, "f64", f64_impl, Target::Impl; + F32RuntimeImplItem, "f32_runtime", f32_runtime_impl, Target::Impl; + F64RuntimeImplItem, "f64_runtime", f64_runtime_impl, Target::Impl; + + SizedTraitLangItem, "sized", sized_trait, Target::Trait; + UnsizeTraitLangItem, "unsize", unsize_trait, Target::Trait; + // trait injected by #[derive(PartialEq)], (i.e. "Partial EQ"). + StructuralPeqTraitLangItem, "structural_peq", structural_peq_trait, Target::Trait; + // trait injected by #[derive(Eq)], (i.e. "Total EQ"; no, I will not apologize). + StructuralTeqTraitLangItem, "structural_teq", structural_teq_trait, Target::Trait; + CopyTraitLangItem, "copy", copy_trait, Target::Trait; + CloneTraitLangItem, "clone", clone_trait, Target::Trait; + SyncTraitLangItem, "sync", sync_trait, Target::Trait; + FreezeTraitLangItem, "freeze", freeze_trait, Target::Trait; + + DropTraitLangItem, "drop", drop_trait, Target::Trait; + + CoerceUnsizedTraitLangItem, "coerce_unsized", coerce_unsized_trait, Target::Trait; + DispatchFromDynTraitLangItem,"dispatch_from_dyn", dispatch_from_dyn_trait, Target::Trait; + + AddTraitLangItem, "add", add_trait, Target::Trait; + SubTraitLangItem, "sub", sub_trait, Target::Trait; + MulTraitLangItem, "mul", mul_trait, Target::Trait; + DivTraitLangItem, "div", div_trait, Target::Trait; + RemTraitLangItem, "rem", rem_trait, Target::Trait; + NegTraitLangItem, "neg", neg_trait, Target::Trait; + NotTraitLangItem, "not", not_trait, Target::Trait; + BitXorTraitLangItem, "bitxor", bitxor_trait, Target::Trait; + BitAndTraitLangItem, "bitand", bitand_trait, Target::Trait; + BitOrTraitLangItem, "bitor", bitor_trait, Target::Trait; + ShlTraitLangItem, "shl", shl_trait, Target::Trait; + ShrTraitLangItem, "shr", shr_trait, Target::Trait; + AddAssignTraitLangItem, "add_assign", add_assign_trait, Target::Trait; + SubAssignTraitLangItem, "sub_assign", sub_assign_trait, Target::Trait; + MulAssignTraitLangItem, "mul_assign", mul_assign_trait, Target::Trait; + DivAssignTraitLangItem, "div_assign", div_assign_trait, Target::Trait; + RemAssignTraitLangItem, "rem_assign", rem_assign_trait, Target::Trait; + BitXorAssignTraitLangItem, "bitxor_assign", bitxor_assign_trait, Target::Trait; + BitAndAssignTraitLangItem, "bitand_assign", bitand_assign_trait, Target::Trait; + BitOrAssignTraitLangItem, "bitor_assign", bitor_assign_trait, Target::Trait; + ShlAssignTraitLangItem, "shl_assign", shl_assign_trait, Target::Trait; + ShrAssignTraitLangItem, "shr_assign", shr_assign_trait, Target::Trait; + IndexTraitLangItem, "index", index_trait, Target::Trait; + IndexMutTraitLangItem, "index_mut", index_mut_trait, Target::Trait; + + UnsafeCellTypeLangItem, "unsafe_cell", unsafe_cell_type, Target::Struct; + VaListTypeLangItem, "va_list", va_list, Target::Struct; + + DerefTraitLangItem, "deref", deref_trait, Target::Trait; + DerefMutTraitLangItem, "deref_mut", deref_mut_trait, Target::Trait; + ReceiverTraitLangItem, "receiver", receiver_trait, Target::Trait; + + FnTraitLangItem, "fn", fn_trait, Target::Trait; + FnMutTraitLangItem, "fn_mut", fn_mut_trait, Target::Trait; + FnOnceTraitLangItem, "fn_once", fn_once_trait, Target::Trait; + + FutureTraitLangItem, "future_trait", future_trait, Target::Trait; + GeneratorStateLangItem, "generator_state", gen_state, Target::Enum; + GeneratorTraitLangItem, "generator", gen_trait, Target::Trait; + UnpinTraitLangItem, "unpin", unpin_trait, Target::Trait; + PinTypeLangItem, "pin", pin_type, Target::Struct; + + // Don't be fooled by the naming here: this lang item denotes `PartialEq`, not `Eq`. + EqTraitLangItem, "eq", eq_trait, Target::Trait; + PartialOrdTraitLangItem, "partial_ord", partial_ord_trait, Target::Trait; + + // A number of panic-related lang items. The `panic` item corresponds to + // divide-by-zero and various panic cases with `match`. The + // `panic_bounds_check` item is for indexing arrays. + // + // The `begin_unwind` lang item has a predefined symbol name and is sort of + // a "weak lang item" in the sense that a crate is not required to have it + // defined to use it, but a final product is required to define it + // somewhere. Additionally, there are restrictions on crates that use a weak + // lang item, but do not have it defined. + PanicFnLangItem, "panic", panic_fn, Target::Fn; + PanicBoundsCheckFnLangItem, "panic_bounds_check", panic_bounds_check_fn, Target::Fn; + PanicInfoLangItem, "panic_info", panic_info, Target::Struct; + PanicLocationLangItem, "panic_location", panic_location, Target::Struct; + PanicImplLangItem, "panic_impl", panic_impl, Target::Fn; + // Libstd panic entry point. Necessary for const eval to be able to catch it + BeginPanicFnLangItem, "begin_panic", begin_panic_fn, Target::Fn; + + ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn, Target::Fn; + BoxFreeFnLangItem, "box_free", box_free_fn, Target::Fn; + DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn, Target::Fn; + OomLangItem, "oom", oom, Target::Fn; + AllocLayoutLangItem, "alloc_layout", alloc_layout, Target::Struct; + + StartFnLangItem, "start", start_fn, Target::Fn; + + EhPersonalityLangItem, "eh_personality", eh_personality, Target::Fn; + EhUnwindResumeLangItem, "eh_unwind_resume", eh_unwind_resume, Target::Fn; + EhCatchTypeinfoLangItem, "eh_catch_typeinfo", eh_catch_typeinfo, Target::Static; + + OwnedBoxLangItem, "owned_box", owned_box, Target::Struct; + + PhantomDataItem, "phantom_data", phantom_data, Target::Struct; + + ManuallyDropItem, "manually_drop", manually_drop, Target::Struct; + + MaybeUninitLangItem, "maybe_uninit", maybe_uninit, Target::Union; + + // Align offset for stride != 1; must not panic. + AlignOffsetLangItem, "align_offset", align_offset_fn, Target::Fn; + + TerminationTraitLangItem, "termination", termination, Target::Trait; + + Arc, "arc", arc, Target::Struct; + Rc, "rc", rc, Target::Struct; +} diff --git a/src/librustc_lang_items/lib.rs b/src/librustc_lang_items/lib.rs index 1d2cbefa64a35..90d090e6f61a1 100644 --- a/src/librustc_lang_items/lib.rs +++ b/src/librustc_lang_items/lib.rs @@ -1,3 +1,44 @@ +macro_rules! enum_from_u32 { + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident = $e:expr,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant = $e),* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + }; + ($(#[$attr:meta])* pub enum $name:ident { + $($variant:ident,)* + }) => { + $(#[$attr])* + pub enum $name { + $($variant,)* + } + + impl $name { + pub fn from_u32(u: u32) -> Option<$name> { + $(if u == $name::$variant as u32 { + return Some($name::$variant) + })* + None + } + } + } +} + +pub mod lang_items; mod target; +pub use lang_items::{LangItem, LanguageItems}; pub use target::{MethodKind, Target}; + +pub trait HashStableContext: rustc_hir::HashStableContext {} diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index a8c66be359c4a..cc5e142351d4f 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -40,7 +40,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx ), ty::InstanceDef::FnPtrShim(def_id, ty) => { let trait_ = tcx.trait_of_item(def_id).unwrap(); - let adjustment = match tcx.lang_items().fn_trait_kind(trait_) { + let adjustment = match tcx.fn_trait_lang_item(trait_) { Some(ty::ClosureKind::FnOnce) => Adjustment::Identity, Some(ty::ClosureKind::FnMut) | Some(ty::ClosureKind::Fn) => Adjustment::Deref, None => bug!("fn pointer {:?} is not an fn", ty), diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index 707125b3fd522..f601a0e8253c1 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -174,9 +174,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.deduce_sig_from_projection(None, &pb) }) .next(); - let kind = object_type - .principal_def_id() - .and_then(|did| self.tcx.lang_items().fn_trait_kind(did)); + let kind = + object_type.principal_def_id().and_then(|did| self.tcx.fn_trait_lang_item(did)); (sig, kind) } ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid), @@ -214,7 +213,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // many viable options, so pick the most restrictive. let expected_kind = self .obligations_for_self_ty(expected_vid) - .filter_map(|(tr, _)| self.tcx.lang_items().fn_trait_kind(tr.def_id())) + .filter_map(|(tr, _)| self.tcx.fn_trait_lang_item(tr.def_id())) .fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur)))); (expected_sig, expected_kind) @@ -237,7 +236,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_ref = projection.to_poly_trait_ref(tcx); - let is_fn = tcx.lang_items().fn_trait_kind(trait_ref.def_id()).is_some(); + let is_fn = tcx.fn_trait_lang_item(trait_ref.def_id()).is_some(); let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span); let is_gen = gen_trait == trait_ref.def_id(); if !is_fn && !is_gen { diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index 356660763a755..c179674432332 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -138,7 +138,7 @@ pub fn external_generic_args( match trait_did { // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C - Some(did) if cx.tcx.lang_items().fn_trait_kind(did).is_some() => { + Some(did) if cx.tcx.fn_trait_lang_item(did).is_some() => { assert!(ty_kind.is_some()); let inputs = match ty_kind { Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(), From c04195da9f47e8090bc49bad605765e9a2a721b4 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 31 Jan 2020 23:06:06 +0100 Subject: [PATCH 04/11] Move get_lang_items query in librustc_passes. --- src/librustc/middle/lang_items.rs | 167 ------------------------ src/librustc/middle/weak_lang_items.rs | 2 +- src/librustc/ty/context.rs | 4 - src/librustc_passes/lang_items.rs | 173 +++++++++++++++++++++++++ src/librustc_passes/lib.rs | 2 + 5 files changed, 176 insertions(+), 172 deletions(-) create mode 100644 src/librustc_passes/lang_items.rs diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index aa235f545b537..5f4c5e0b0e301 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -9,180 +9,13 @@ pub use self::LangItem::*; -use crate::middle::cstore::ExternCrate; -use crate::middle::weak_lang_items; use crate::ty::{self, TyCtxt}; -use rustc_errors::struct_span_err; -use rustc_hir as hir; use rustc_hir::def_id::DefId; -use rustc_hir::itemlikevisit::ItemLikeVisitor; -use rustc_lang_items::lang_items::ITEM_REFS; -use rustc_lang_items::Target; -use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; -use syntax::ast; pub use rustc_lang_items::{LangItem, LanguageItems}; -struct LanguageItemCollector<'tcx> { - items: LanguageItems, - tcx: TyCtxt<'tcx>, -} - -impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { - fn visit_item(&mut self, item: &hir::Item<'_>) { - if let Some((value, span)) = extract(&item.attrs) { - let actual_target = Target::from_item(item); - match ITEM_REFS.get(&*value.as_str()).cloned() { - // Known lang item with attribute on correct target. - Some((item_index, expected_target)) if actual_target == expected_target => { - let def_id = self.tcx.hir().local_def_id(item.hir_id); - self.collect_item(item_index, def_id); - } - // Known lang item with attribute on incorrect target. - Some((_, expected_target)) => { - struct_span_err!( - self.tcx.sess, - span, - E0718, - "`{}` language item must be applied to a {}", - value, - expected_target, - ) - .span_label( - span, - format!( - "attribute should be applied to a {}, not a {}", - expected_target, actual_target, - ), - ) - .emit(); - } - // Unknown lang item. - _ => { - struct_span_err!( - self.tcx.sess, - span, - E0522, - "definition of an unknown language item: `{}`", - value - ) - .span_label(span, format!("definition of unknown language item `{}`", value)) - .emit(); - } - } - } - } - - fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) { - // At present, lang items are always items, not trait items. - } - - fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) { - // At present, lang items are always items, not impl items. - } -} - -impl LanguageItemCollector<'tcx> { - fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> { - LanguageItemCollector { tcx, items: LanguageItems::new() } - } - - fn collect_item(&mut self, item_index: usize, item_def_id: DefId) { - // Check for duplicates. - if let Some(original_def_id) = self.items.items[item_index] { - if original_def_id != item_def_id { - 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, - span, - E0152, - "found duplicate lang item `{}`", - name - ), - None => match self.tcx.extern_crate(item_def_id) { - Some(ExternCrate { dependency_of, .. }) => { - self.tcx.sess.struct_err(&format!( - "duplicate lang item in crate `{}` (which `{}` depends on): `{}`.", - self.tcx.crate_name(item_def_id.krate), - self.tcx.crate_name(*dependency_of), - name - )) - } - _ => self.tcx.sess.struct_err(&format!( - "duplicate lang item in crate `{}`: `{}`.", - self.tcx.crate_name(item_def_id.krate), - name - )), - }, - }; - if let Some(span) = self.tcx.hir().span_if_local(original_def_id) { - err.span_note(span, "the lang item is first defined here"); - } else { - match self.tcx.extern_crate(original_def_id) { - Some(ExternCrate { dependency_of, .. }) => { - err.note(&format!( - "the lang item is first defined in crate `{}` (which `{}` depends on)", - self.tcx.crate_name(original_def_id.krate), - self.tcx.crate_name(*dependency_of))); - } - _ => { - err.note(&format!( - "the lang item is first defined in crate `{}`.", - self.tcx.crate_name(original_def_id.krate) - )); - } - } - } - err.emit(); - } - } - - // Matched. - self.items.items[item_index] = Some(item_def_id); - } -} - -/// Extracts the first `lang = "$name"` out of a list of attributes. -/// The attributes `#[panic_handler]` and `#[alloc_error_handler]` -/// are also extracted out when found. -pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> { - attrs.iter().find_map(|attr| { - Some(match attr { - _ if attr.check_name(sym::lang) => (attr.value_str()?, attr.span), - _ if attr.check_name(sym::panic_handler) => (sym::panic_impl, attr.span), - _ if attr.check_name(sym::alloc_error_handler) => (sym::oom, attr.span), - _ => return None, - }) - }) -} - -/// Traverses and collects all the lang items in all crates. -pub fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems { - // Initialize the collector. - let mut collector = LanguageItemCollector::new(tcx); - - // Collect lang items in other crates. - for &cnum in tcx.crates().iter() { - for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() { - collector.collect_item(item_index, def_id); - } - } - - // Collect lang items in this crate. - tcx.hir().krate().visit_all_item_likes(&mut collector); - - // Extract out the found lang items. - let LanguageItemCollector { mut items, .. } = collector; - - // Find all required but not-yet-defined lang items. - weak_lang_items::check_crate(tcx, &mut items); - - items -} - impl<'tcx> TyCtxt<'tcx> { /// Returns the `DefId` for a given `LangItem`. /// If not found, fatally aborts compilation. diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs index 82bf0d200d75f..5649a2b9132d8 100644 --- a/src/librustc/middle/weak_lang_items.rs +++ b/src/librustc/middle/weak_lang_items.rs @@ -141,7 +141,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { } fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) { - if let Some((lang_item, _)) = lang_items::extract(&i.attrs) { + if let Some((lang_item, _)) = rustc_lang_items::lang_items::extract(&i.attrs) { self.register(lang_item, i.span); } intravisit::walk_foreign_item(self, i) diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index f2ad01b3d5961..2adf708641798 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -2716,10 +2716,6 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) { assert_eq!(id, LOCAL_CRATE); tcx.crate_name }; - providers.get_lang_items = |tcx, id| { - assert_eq!(id, LOCAL_CRATE); - tcx.arena.alloc(middle::lang_items::collect(tcx)) - }; providers.maybe_unused_trait_import = |tcx, id| tcx.maybe_unused_trait_imports.contains(&id); providers.maybe_unused_extern_crates = |tcx, cnum| { assert_eq!(cnum, LOCAL_CRATE); diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs new file mode 100644 index 0000000000000..d964777bd7cb0 --- /dev/null +++ b/src/librustc_passes/lang_items.rs @@ -0,0 +1,173 @@ +//! Detecting language items. +//! +//! Language items are items that represent concepts intrinsic to the language +//! itself. Examples are: +//! +//! * Traits that specify "kinds"; e.g., `Sync`, `Send`. +//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`. +//! * Functions called by the compiler itself. + +use rustc::middle::cstore::ExternCrate; +use rustc::middle::weak_lang_items; +use rustc::ty::TyCtxt; + +use rustc_errors::struct_span_err; +use rustc_hir as hir; +use rustc_hir::def_id::{DefId, LOCAL_CRATE}; +use rustc_hir::itemlikevisit::ItemLikeVisitor; +use rustc_lang_items::lang_items::{extract, ITEM_REFS}; +use rustc_lang_items::{LangItem, LanguageItems, Target}; + +use rustc::ty::query::Providers; + +struct LanguageItemCollector<'tcx> { + items: LanguageItems, + tcx: TyCtxt<'tcx>, +} + +impl ItemLikeVisitor<'v> for LanguageItemCollector<'tcx> { + fn visit_item(&mut self, item: &hir::Item<'_>) { + if let Some((value, span)) = extract(&item.attrs) { + let actual_target = Target::from_item(item); + match ITEM_REFS.get(&*value.as_str()).cloned() { + // Known lang item with attribute on correct target. + Some((item_index, expected_target)) if actual_target == expected_target => { + let def_id = self.tcx.hir().local_def_id(item.hir_id); + self.collect_item(item_index, def_id); + } + // Known lang item with attribute on incorrect target. + Some((_, expected_target)) => { + struct_span_err!( + self.tcx.sess, + span, + E0718, + "`{}` language item must be applied to a {}", + value, + expected_target, + ) + .span_label( + span, + format!( + "attribute should be applied to a {}, not a {}", + expected_target, actual_target, + ), + ) + .emit(); + } + // Unknown lang item. + _ => { + struct_span_err!( + self.tcx.sess, + span, + E0522, + "definition of an unknown language item: `{}`", + value + ) + .span_label(span, format!("definition of unknown language item `{}`", value)) + .emit(); + } + } + } + } + + fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) { + // At present, lang items are always items, not trait items. + } + + fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) { + // At present, lang items are always items, not impl items. + } +} + +impl LanguageItemCollector<'tcx> { + fn new(tcx: TyCtxt<'tcx>) -> LanguageItemCollector<'tcx> { + LanguageItemCollector { tcx, items: LanguageItems::new() } + } + + fn collect_item(&mut self, item_index: usize, item_def_id: DefId) { + // Check for duplicates. + if let Some(original_def_id) = self.items.items[item_index] { + if original_def_id != item_def_id { + 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, + span, + E0152, + "found duplicate lang item `{}`", + name + ), + None => match self.tcx.extern_crate(item_def_id) { + Some(ExternCrate { dependency_of, .. }) => { + self.tcx.sess.struct_err(&format!( + "duplicate lang item in crate `{}` (which `{}` depends on): `{}`.", + self.tcx.crate_name(item_def_id.krate), + self.tcx.crate_name(*dependency_of), + name + )) + } + _ => self.tcx.sess.struct_err(&format!( + "duplicate lang item in crate `{}`: `{}`.", + self.tcx.crate_name(item_def_id.krate), + name + )), + }, + }; + if let Some(span) = self.tcx.hir().span_if_local(original_def_id) { + err.span_note(span, "the lang item is first defined here"); + } else { + match self.tcx.extern_crate(original_def_id) { + Some(ExternCrate { dependency_of, .. }) => { + err.note(&format!( + "the lang item is first defined in crate `{}` (which `{}` depends on)", + self.tcx.crate_name(original_def_id.krate), + self.tcx.crate_name(*dependency_of) + )); + } + _ => { + err.note(&format!( + "the lang item is first defined in crate `{}`.", + self.tcx.crate_name(original_def_id.krate) + )); + } + } + } + err.emit(); + } + } + + // Matched. + self.items.items[item_index] = Some(item_def_id); + } +} + +/// Traverses and collects all the lang items in all crates. +fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> LanguageItems { + // Initialize the collector. + let mut collector = LanguageItemCollector::new(tcx); + + // Collect lang items in other crates. + for &cnum in tcx.crates().iter() { + for &(def_id, item_index) in tcx.defined_lang_items(cnum).iter() { + collector.collect_item(item_index, def_id); + } + } + + // Collect lang items in this crate. + tcx.hir().krate().visit_all_item_likes(&mut collector); + + // Extract out the found lang items. + let LanguageItemCollector { mut items, .. } = collector; + + // Find all required but not-yet-defined lang items. + weak_lang_items::check_crate(tcx, &mut items); + + items +} + +pub fn provide(providers: &mut Providers<'_>) { + providers.get_lang_items = |tcx, id| { + assert_eq!(id, LOCAL_CRATE); + tcx.arena.alloc(collect(tcx)) + }; +} diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index c5e5bc969158f..d0b1c70be65be 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -23,6 +23,7 @@ mod diagnostic_items; pub mod entry; pub mod hir_stats; mod intrinsicck; +mod lang_items; pub mod layout_test; mod lib_features; mod liveness; @@ -37,6 +38,7 @@ pub fn provide(providers: &mut Providers<'_>) { check_const::provide(providers); diagnostic_items::provide(providers); entry::provide(providers); + lang_items::provide(providers); lib_features::provide(providers); loops::provide(providers); liveness::provide(providers); From 4ecba94dcb415b0642ebd8695a08adc4ec07441e Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 26 Jan 2020 13:16:02 +0100 Subject: [PATCH 05/11] Move weak lang items to librustc_lang_items. --- src/librustc/middle/weak_lang_items.rs | 99 +++++++--------------- src/librustc_lang_items/lib.rs | 1 + src/librustc_lang_items/weak_lang_items.rs | 48 +++++++++++ 3 files changed, 80 insertions(+), 68 deletions(-) create mode 100644 src/librustc_lang_items/weak_lang_items.rs diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs index 5649a2b9132d8..ab19483a62d6e 100644 --- a/src/librustc/middle/weak_lang_items.rs +++ b/src/librustc/middle/weak_lang_items.rs @@ -10,13 +10,12 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; -use rustc_span::symbol::{sym, Symbol}; +use rustc_lang_items::weak_lang_items::WEAK_ITEMS_REFS; +use rustc_span::symbol::Symbol; use rustc_span::Span; use rustc_target::spec::PanicStrategy; -use syntax::ast; -macro_rules! weak_lang_items { - ($($name:ident, $item:ident, $sym:ident;)*) => ( +pub use rustc_lang_items::weak_lang_items::link_name; struct Context<'a, 'tcx> { tcx: TyCtxt<'tcx>, @@ -25,16 +24,14 @@ struct Context<'a, 'tcx> { /// Checks the crate for usage of weak lang items, returning a vector of all the /// language items required by this crate, but not defined yet. -pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, - items: &mut lang_items::LanguageItems) { +pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) { // These are never called by user code, they're generated by the compiler. // They will never implicitly be added to the `missing` array unless we do // so here. if items.eh_personality().is_none() { items.missing.push(lang_items::EhPersonalityLangItem); } - if tcx.sess.target.target.options.custom_unwind_resume & - items.eh_unwind_resume().is_none() { + if tcx.sess.target.target.options.custom_unwind_resume & items.eh_unwind_resume().is_none() { items.missing.push(lang_items::EhUnwindResumeLangItem); } @@ -45,16 +42,6 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, verify(tcx, items); } -pub fn link_name(attrs: &[ast::Attribute]) -> Option { - rustc_lang_items::lang_items::extract(attrs).and_then(|(name, _)| { - $(if name == sym::$name { - Some(sym::$sym) - } else)* { - None - } - }) -} - /// Returns `true` if the specified `lang_item` doesn't actually need to be /// present for this compilation. /// @@ -66,29 +53,26 @@ pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: lang_items::LangItem) -> bool { // symbols. Other panic runtimes ensure that the relevant symbols are // available to link things together, but they're never exercised. if tcx.sess.panic_strategy() != PanicStrategy::Unwind { - return lang_item == lang_items::EhPersonalityLangItem || - lang_item == lang_items::EhUnwindResumeLangItem + return lang_item == lang_items::EhPersonalityLangItem + || lang_item == lang_items::EhUnwindResumeLangItem; } false } -fn verify<'tcx>(tcx: TyCtxt<'tcx>, - items: &lang_items::LanguageItems) { +fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { // We only need to check for the presence of weak lang items if we're // emitting something that's not an rlib. - let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| { - match *kind { - config::CrateType::Dylib | - config::CrateType::ProcMacro | - config::CrateType::Cdylib | - config::CrateType::Executable | - config::CrateType::Staticlib => true, - config::CrateType::Rlib => false, - } + let needs_check = tcx.sess.crate_types.borrow().iter().any(|kind| match *kind { + config::CrateType::Dylib + | config::CrateType::ProcMacro + | config::CrateType::Cdylib + | config::CrateType::Executable + | config::CrateType::Staticlib => true, + config::CrateType::Rlib => false, }); if !needs_check { - return + return; } let mut missing = FxHashSet::default(); @@ -98,37 +82,28 @@ fn verify<'tcx>(tcx: TyCtxt<'tcx>, } } - $( - if missing.contains(&lang_items::$item) && - !whitelisted(tcx, lang_items::$item) && - items.$name().is_none() { - if lang_items::$item == lang_items::PanicImplLangItem { - tcx.sess.err(&format!("`#[panic_handler]` function required, \ - but not found")); - } else if lang_items::$item == lang_items::OomLangItem { - tcx.sess.err(&format!("`#[alloc_error_handler]` function required, \ - but not found")); + for (name, &item) in WEAK_ITEMS_REFS.iter() { + if missing.contains(&item) && !whitelisted(tcx, item) && items.require(item).is_err() { + if item == lang_items::PanicImplLangItem { + tcx.sess.err(&format!("`#[panic_handler]` function required, but not found")); + } else if item == lang_items::OomLangItem { + tcx.sess.err(&format!("`#[alloc_error_handler]` function required, but not found")); } else { - tcx.sess.err(&format!("language item required, but not found: `{}`", - stringify!($name))); + tcx.sess.err(&format!("language item required, but not found: `{}`", name)); } } - )* + } } impl<'a, 'tcx> Context<'a, 'tcx> { fn register(&mut self, name: Symbol, span: Span) { - $(if name == sym::$name { - if self.items.$name().is_none() { - self.items.missing.push(lang_items::$item); + if let Some(&item) = WEAK_ITEMS_REFS.get(&name) { + if self.items.require(item).is_err() { + self.items.missing.push(item); } - } else)* { - struct_span_err!( - self.tcx.sess, span, E0264, - "unknown external lang item: `{}`", - name - ) - .emit(); + } else { + struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name) + .emit(); } } } @@ -150,18 +125,6 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { impl<'tcx> TyCtxt<'tcx> { pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { - let lang_items = self.lang_items(); - let did = Some(item_def_id); - - $(lang_items.$name() == did)||* + self.lang_items().is_weak_lang_item(item_def_id) } } - -) } - -weak_lang_items! { - panic_impl, PanicImplLangItem, rust_begin_unwind; - eh_personality, EhPersonalityLangItem, rust_eh_personality; - eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume; - oom, OomLangItem, rust_oom; -} diff --git a/src/librustc_lang_items/lib.rs b/src/librustc_lang_items/lib.rs index 90d090e6f61a1..9bcd54b6cbfa1 100644 --- a/src/librustc_lang_items/lib.rs +++ b/src/librustc_lang_items/lib.rs @@ -37,6 +37,7 @@ macro_rules! enum_from_u32 { pub mod lang_items; mod target; +pub mod weak_lang_items; pub use lang_items::{LangItem, LanguageItems}; pub use target::{MethodKind, Target}; diff --git a/src/librustc_lang_items/weak_lang_items.rs b/src/librustc_lang_items/weak_lang_items.rs new file mode 100644 index 0000000000000..ab2955fb3fcf0 --- /dev/null +++ b/src/librustc_lang_items/weak_lang_items.rs @@ -0,0 +1,48 @@ +//! Validity checking for weak lang items + +use crate::{lang_items, LangItem, LanguageItems}; + +use rustc_data_structures::fx::FxHashMap; +use rustc_hir::def_id::DefId; +use rustc_span::symbol::{sym, Symbol}; +use syntax::ast; + +use lazy_static::lazy_static; + +macro_rules! weak_lang_items { + ($($name:ident, $item:ident, $sym:ident;)*) => ( + +lazy_static! { + pub static ref WEAK_ITEMS_REFS: FxHashMap = { + let mut map = FxHashMap::default(); + $(map.insert(sym::$name, lang_items::$item);)* + map + }; +} + +pub fn link_name(attrs: &[ast::Attribute]) -> Option { + lang_items::extract(attrs).and_then(|(name, _)| { + $(if name == sym::$name { + Some(sym::$sym) + } else)* { + None + } + }) +} + +impl LanguageItems { + pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { + let did = Some(item_def_id); + + $(self.$name() == did)||* + } +} + +) } + +weak_lang_items! { + panic_impl, PanicImplLangItem, rust_begin_unwind; + eh_personality, EhPersonalityLangItem, rust_eh_personality; + eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume; + oom, OomLangItem, rust_oom; +} From 98b46f77969316e0f80f60c29c4121db8b3735c4 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Wed, 1 Jan 2020 17:09:29 +0100 Subject: [PATCH 06/11] Move weak_lang_items.rs to librustc_passes. --- src/{librustc/middle => librustc_passes}/weak_lang_items.rs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{librustc/middle => librustc_passes}/weak_lang_items.rs (100%) diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs similarity index 100% rename from src/librustc/middle/weak_lang_items.rs rename to src/librustc_passes/weak_lang_items.rs From b6f875d678de6c3e19bdb87d99f9cff189a96e54 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 28 Dec 2019 18:31:37 +0100 Subject: [PATCH 07/11] Move weak_lang_items checking to librustc_passes. --- src/librustc/middle/weak_lang_items.rs | 32 ++++++++++++++++++++++ src/librustc_passes/lang_items.rs | 3 ++- src/librustc_passes/lib.rs | 1 + src/librustc_passes/weak_lang_items.rs | 37 ++++---------------------- 4 files changed, 40 insertions(+), 33 deletions(-) create mode 100644 src/librustc/middle/weak_lang_items.rs diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs new file mode 100644 index 0000000000000..c59c1953111d4 --- /dev/null +++ b/src/librustc/middle/weak_lang_items.rs @@ -0,0 +1,32 @@ +//! Validity checking for weak lang items + +use crate::ty::TyCtxt; +use rustc_hir::def_id::DefId; +use rustc_lang_items::{lang_items, LangItem}; +use rustc_target::spec::PanicStrategy; + +pub use rustc_lang_items::weak_lang_items::link_name; + +impl<'tcx> TyCtxt<'tcx> { + pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { + self.lang_items().is_weak_lang_item(item_def_id) + } +} + +/// Returns `true` if the specified `lang_item` doesn't actually need to be +/// present for this compilation. +/// +/// Not all lang items are always required for each compilation, particularly in +/// the case of panic=abort. In these situations some lang items are injected by +/// crates and don't actually need to be defined in libstd. +pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: LangItem) -> bool { + // If we're not compiling with unwinding, we won't actually need these + // symbols. Other panic runtimes ensure that the relevant symbols are + // available to link things together, but they're never exercised. + if tcx.sess.panic_strategy() != PanicStrategy::Unwind { + return lang_item == lang_items::EhPersonalityLangItem + || lang_item == lang_items::EhUnwindResumeLangItem; + } + + false +} diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index d964777bd7cb0..ea5ff000512ed 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -7,8 +7,9 @@ //! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`. //! * Functions called by the compiler itself. +use crate::weak_lang_items; + use rustc::middle::cstore::ExternCrate; -use rustc::middle::weak_lang_items; use rustc::ty::TyCtxt; use rustc_errors::struct_span_err; diff --git a/src/librustc_passes/lib.rs b/src/librustc_passes/lib.rs index d0b1c70be65be..afafbacb8fa88 100644 --- a/src/librustc_passes/lib.rs +++ b/src/librustc_passes/lib.rs @@ -32,6 +32,7 @@ mod reachable; mod region; pub mod stability; mod upvars; +mod weak_lang_items; pub fn provide(providers: &mut Providers<'_>) { check_attr::provide(providers); diff --git a/src/librustc_passes/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs index ab19483a62d6e..a579b9354d7db 100644 --- a/src/librustc_passes/weak_lang_items.rs +++ b/src/librustc_passes/weak_lang_items.rs @@ -1,21 +1,18 @@ //! Validity checking for weak lang items -use crate::middle::lang_items; -use crate::session::config; +use rustc::middle::lang_items; +use rustc::middle::weak_lang_items::whitelisted; +use rustc::session::config; -use crate::hir::map::Map; -use crate::ty::TyCtxt; +use rustc::hir::map::Map; +use rustc::ty::TyCtxt; use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; -use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_lang_items::weak_lang_items::WEAK_ITEMS_REFS; use rustc_span::symbol::Symbol; use rustc_span::Span; -use rustc_target::spec::PanicStrategy; - -pub use rustc_lang_items::weak_lang_items::link_name; struct Context<'a, 'tcx> { tcx: TyCtxt<'tcx>, @@ -42,24 +39,6 @@ pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItem verify(tcx, items); } -/// Returns `true` if the specified `lang_item` doesn't actually need to be -/// present for this compilation. -/// -/// Not all lang items are always required for each compilation, particularly in -/// the case of panic=abort. In these situations some lang items are injected by -/// crates and don't actually need to be defined in libstd. -pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: lang_items::LangItem) -> bool { - // If we're not compiling with unwinding, we won't actually need these - // symbols. Other panic runtimes ensure that the relevant symbols are - // available to link things together, but they're never exercised. - if tcx.sess.panic_strategy() != PanicStrategy::Unwind { - return lang_item == lang_items::EhPersonalityLangItem - || lang_item == lang_items::EhUnwindResumeLangItem; - } - - false -} - fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) { // We only need to check for the presence of weak lang items if we're // emitting something that's not an rlib. @@ -122,9 +101,3 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { intravisit::walk_foreign_item(self, i) } } - -impl<'tcx> TyCtxt<'tcx> { - pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { - self.lang_items().is_weak_lang_item(item_def_id) - } -} From 443a42a8d3398a4238f5de3e2257476dd6f9bdf5 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sun, 26 Jan 2020 22:43:35 +0100 Subject: [PATCH 08/11] Nits. --- src/librustc_lang_items/lang_items.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_lang_items/lang_items.rs b/src/librustc_lang_items/lang_items.rs index 6cccad73fcbad..c98aa70106fa5 100644 --- a/src/librustc_lang_items/lang_items.rs +++ b/src/librustc_lang_items/lang_items.rs @@ -1,4 +1,4 @@ -//! Detecting language items. +//! Defines language items. //! //! Language items are items that represent concepts intrinsic to the language //! itself. Examples are: From d3b2385d40bfed00533b016e4685974b739c2da1 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 30 Jan 2020 01:24:51 +0100 Subject: [PATCH 09/11] Move it all into rustc_hir. --- Cargo.lock | 16 +------ src/librustc/Cargo.toml | 1 - src/librustc/ich/impls_hir.rs | 2 - src/librustc/middle/lang_items.rs | 2 +- src/librustc/middle/weak_lang_items.rs | 4 +- src/librustc_hir/Cargo.toml | 1 + .../lang_items.rs | 2 +- src/librustc_hir/lib.rs | 6 +++ .../target.rs | 4 +- .../weak_lang_items.rs | 2 +- src/librustc_lang_items/Cargo.toml | 18 -------- src/librustc_lang_items/lib.rs | 45 ------------------- src/librustc_passes/Cargo.toml | 1 - src/librustc_passes/check_attr.rs | 2 +- src/librustc_passes/lang_items.rs | 4 +- src/librustc_passes/weak_lang_items.rs | 4 +- 16 files changed, 20 insertions(+), 94 deletions(-) rename src/{librustc_lang_items => librustc_hir}/lang_items.rs (99%) rename src/{librustc_lang_items => librustc_hir}/target.rs (98%) rename src/{librustc_lang_items => librustc_hir}/weak_lang_items.rs (97%) delete mode 100644 src/librustc_lang_items/Cargo.toml delete mode 100644 src/librustc_lang_items/lib.rs diff --git a/Cargo.lock b/Cargo.lock index ca47a11035354..ea2dc7f180307 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3100,7 +3100,6 @@ dependencies = [ "rustc_feature", "rustc_hir", "rustc_index", - "rustc_lang_items", "rustc_macros", "rustc_session", "rustc_span", @@ -3620,6 +3619,7 @@ version = "0.0.0" name = "rustc_hir" version = "0.0.0" dependencies = [ + "lazy_static 1.4.0", "rustc_ast_pretty", "rustc_data_structures", "rustc_errors", @@ -3699,19 +3699,6 @@ dependencies = [ "winapi 0.3.8", ] -[[package]] -name = "rustc_lang_items" -version = "0.0.0" -dependencies = [ - "lazy_static 1.4.0", - "rustc_data_structures", - "rustc_hir", - "rustc_macros", - "rustc_span", - "serialize", - "syntax", -] - [[package]] name = "rustc_lexer" version = "0.1.0" @@ -3865,7 +3852,6 @@ dependencies = [ "rustc_feature", "rustc_hir", "rustc_index", - "rustc_lang_items", "rustc_session", "rustc_span", "rustc_target", diff --git a/src/librustc/Cargo.toml b/src/librustc/Cargo.toml index 6cd979b0babf4..782c6879ac58f 100644 --- a/src/librustc/Cargo.toml +++ b/src/librustc/Cargo.toml @@ -24,7 +24,6 @@ rustc_apfloat = { path = "../librustc_apfloat" } rustc_attr = { path = "../librustc_attr" } rustc_feature = { path = "../librustc_feature" } rustc_hir = { path = "../librustc_hir" } -rustc_lang_items = { path = "../librustc_lang_items" } rustc_target = { path = "../librustc_target" } rustc_macros = { path = "../librustc_macros" } rustc_data_structures = { path = "../librustc_data_structures" } diff --git a/src/librustc/ich/impls_hir.rs b/src/librustc/ich/impls_hir.rs index fa50632e9db82..1a763e43d557d 100644 --- a/src/librustc/ich/impls_hir.rs +++ b/src/librustc/ich/impls_hir.rs @@ -107,8 +107,6 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> { } } -impl<'ctx> rustc_lang_items::HashStableContext for StableHashingContext<'ctx> {} - impl<'a> ToStableHashKey> for DefId { type KeyType = DefPathHash; diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 5f4c5e0b0e301..d9387767655a1 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -14,7 +14,7 @@ use crate::ty::{self, TyCtxt}; use rustc_hir::def_id::DefId; use rustc_span::Span; -pub use rustc_lang_items::{LangItem, LanguageItems}; +pub use rustc_hir::{LangItem, LanguageItems}; impl<'tcx> TyCtxt<'tcx> { /// Returns the `DefId` for a given `LangItem`. diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs index c59c1953111d4..c04a6f32215c4 100644 --- a/src/librustc/middle/weak_lang_items.rs +++ b/src/librustc/middle/weak_lang_items.rs @@ -2,10 +2,10 @@ use crate::ty::TyCtxt; use rustc_hir::def_id::DefId; -use rustc_lang_items::{lang_items, LangItem}; +use rustc_hir::{lang_items, LangItem}; use rustc_target::spec::PanicStrategy; -pub use rustc_lang_items::weak_lang_items::link_name; +pub use rustc_hir::weak_lang_items::link_name; impl<'tcx> TyCtxt<'tcx> { pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { diff --git a/src/librustc_hir/Cargo.toml b/src/librustc_hir/Cargo.toml index 3ae943a4ce08b..cff6413253202 100644 --- a/src/librustc_hir/Cargo.toml +++ b/src/librustc_hir/Cargo.toml @@ -19,4 +19,5 @@ rustc_span = { path = "../librustc_span" } rustc_errors = { path = "../librustc_errors" } rustc_serialize = { path = "../libserialize", package = "serialize" } syntax = { path = "../libsyntax" } +lazy_static = "1" smallvec = { version = "1.0", features = ["union", "may_dangle"] } diff --git a/src/librustc_lang_items/lang_items.rs b/src/librustc_hir/lang_items.rs similarity index 99% rename from src/librustc_lang_items/lang_items.rs rename to src/librustc_hir/lang_items.rs index c98aa70106fa5..c5bad979f5416 100644 --- a/src/librustc_lang_items/lang_items.rs +++ b/src/librustc_hir/lang_items.rs @@ -9,11 +9,11 @@ pub use self::LangItem::*; +use crate::def_id::DefId; use crate::Target; use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; -use rustc_hir::def_id::DefId; use rustc_macros::HashStable_Generic; use rustc_span::symbol::{sym, Symbol}; use rustc_span::Span; diff --git a/src/librustc_hir/lib.rs b/src/librustc_hir/lib.rs index e4edd34bd6e23..d958dfc681b00 100644 --- a/src/librustc_hir/lib.rs +++ b/src/librustc_hir/lib.rs @@ -17,9 +17,15 @@ mod hir; pub mod hir_id; pub mod intravisit; pub mod itemlikevisit; +pub mod lang_items; pub mod pat_util; pub mod print; mod stable_hash_impls; +mod target; +pub mod weak_lang_items; + pub use hir::*; pub use hir_id::*; +pub use lang_items::{LangItem, LanguageItems}; pub use stable_hash_impls::HashStableContext; +pub use target::{MethodKind, Target}; diff --git a/src/librustc_lang_items/target.rs b/src/librustc_hir/target.rs similarity index 98% rename from src/librustc_lang_items/target.rs rename to src/librustc_hir/target.rs index db5e31981c08f..501976fc3cb39 100644 --- a/src/librustc_lang_items/target.rs +++ b/src/librustc_hir/target.rs @@ -4,8 +4,8 @@ //! conflicts between multiple such attributes attached to the same //! item. -use rustc_hir as hir; -use rustc_hir::{Item, ItemKind, TraitItem, TraitItemKind}; +use crate::hir; +use crate::{Item, ItemKind, TraitItem, TraitItemKind}; use std::fmt::{self, Display}; diff --git a/src/librustc_lang_items/weak_lang_items.rs b/src/librustc_hir/weak_lang_items.rs similarity index 97% rename from src/librustc_lang_items/weak_lang_items.rs rename to src/librustc_hir/weak_lang_items.rs index ab2955fb3fcf0..79182caae8c80 100644 --- a/src/librustc_lang_items/weak_lang_items.rs +++ b/src/librustc_hir/weak_lang_items.rs @@ -1,9 +1,9 @@ //! Validity checking for weak lang items +use crate::def_id::DefId; use crate::{lang_items, LangItem, LanguageItems}; use rustc_data_structures::fx::FxHashMap; -use rustc_hir::def_id::DefId; use rustc_span::symbol::{sym, Symbol}; use syntax::ast; diff --git a/src/librustc_lang_items/Cargo.toml b/src/librustc_lang_items/Cargo.toml deleted file mode 100644 index eddd4fb81dcc2..0000000000000 --- a/src/librustc_lang_items/Cargo.toml +++ /dev/null @@ -1,18 +0,0 @@ -[package] -authors = ["The Rust Project Developers"] -name = "rustc_lang_items" -version = "0.0.0" -edition = "2018" - -[lib] -name = "rustc_lang_items" -path = "lib.rs" - -[dependencies] -lazy_static = "1" -rustc_data_structures = { path = "../librustc_data_structures" } -rustc_hir = { path = "../librustc_hir" } -rustc_macros = { path = "../librustc_macros" } -rustc_serialize = { path = "../libserialize", package = "serialize" } -rustc_span = { path = "../librustc_span" } -syntax = { path = "../libsyntax" } diff --git a/src/librustc_lang_items/lib.rs b/src/librustc_lang_items/lib.rs deleted file mode 100644 index 9bcd54b6cbfa1..0000000000000 --- a/src/librustc_lang_items/lib.rs +++ /dev/null @@ -1,45 +0,0 @@ -macro_rules! enum_from_u32 { - ($(#[$attr:meta])* pub enum $name:ident { - $($variant:ident = $e:expr,)* - }) => { - $(#[$attr])* - pub enum $name { - $($variant = $e),* - } - - impl $name { - pub fn from_u32(u: u32) -> Option<$name> { - $(if u == $name::$variant as u32 { - return Some($name::$variant) - })* - None - } - } - }; - ($(#[$attr:meta])* pub enum $name:ident { - $($variant:ident,)* - }) => { - $(#[$attr])* - pub enum $name { - $($variant,)* - } - - impl $name { - pub fn from_u32(u: u32) -> Option<$name> { - $(if u == $name::$variant as u32 { - return Some($name::$variant) - })* - None - } - } - } -} - -pub mod lang_items; -mod target; -pub mod weak_lang_items; - -pub use lang_items::{LangItem, LanguageItems}; -pub use target::{MethodKind, Target}; - -pub trait HashStableContext: rustc_hir::HashStableContext {} diff --git a/src/librustc_passes/Cargo.toml b/src/librustc_passes/Cargo.toml index 5ed192c593280..981ef7f8796d3 100644 --- a/src/librustc_passes/Cargo.toml +++ b/src/librustc_passes/Cargo.toml @@ -17,7 +17,6 @@ rustc_errors = { path = "../librustc_errors" } rustc_feature = { path = "../librustc_feature" } rustc_hir = { path = "../librustc_hir" } rustc_index = { path = "../librustc_index" } -rustc_lang_items = { path = "../librustc_lang_items" } rustc_session = { path = "../librustc_session" } rustc_target = { path = "../librustc_target" } syntax = { path = "../libsyntax" } diff --git a/src/librustc_passes/check_attr.rs b/src/librustc_passes/check_attr.rs index 57a21bf716ddb..d5b0971811ef1 100644 --- a/src/librustc_passes/check_attr.rs +++ b/src/librustc_passes/check_attr.rs @@ -14,7 +14,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; use rustc_hir::DUMMY_HIR_ID; use rustc_hir::{self, HirId, Item, ItemKind, TraitItem}; -use rustc_lang_items::{MethodKind, Target}; +use rustc_hir::{MethodKind, Target}; use rustc_session::lint::builtin::{CONFLICTING_REPR_HINTS, UNUSED_ATTRIBUTES}; use rustc_span::symbol::sym; use rustc_span::Span; diff --git a/src/librustc_passes/lang_items.rs b/src/librustc_passes/lang_items.rs index ea5ff000512ed..5f5acc7fdf815 100644 --- a/src/librustc_passes/lang_items.rs +++ b/src/librustc_passes/lang_items.rs @@ -16,8 +16,8 @@ use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::def_id::{DefId, LOCAL_CRATE}; use rustc_hir::itemlikevisit::ItemLikeVisitor; -use rustc_lang_items::lang_items::{extract, ITEM_REFS}; -use rustc_lang_items::{LangItem, LanguageItems, Target}; +use rustc_hir::lang_items::{extract, ITEM_REFS}; +use rustc_hir::{LangItem, LanguageItems, Target}; use rustc::ty::query::Providers; diff --git a/src/librustc_passes/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs index a579b9354d7db..55af437377853 100644 --- a/src/librustc_passes/weak_lang_items.rs +++ b/src/librustc_passes/weak_lang_items.rs @@ -10,7 +10,7 @@ use rustc_data_structures::fx::FxHashSet; use rustc_errors::struct_span_err; use rustc_hir as hir; use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor}; -use rustc_lang_items::weak_lang_items::WEAK_ITEMS_REFS; +use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS; use rustc_span::symbol::Symbol; use rustc_span::Span; @@ -95,7 +95,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { } fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) { - if let Some((lang_item, _)) = rustc_lang_items::lang_items::extract(&i.attrs) { + if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) { self.register(lang_item, i.span); } intravisit::walk_foreign_item(self, i) From 513eb744c0c2ead6b10a3a3e5f0267e4136a9b4f Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 30 Jan 2020 08:53:34 +0100 Subject: [PATCH 10/11] Merge rustc::middle::*lang_items. --- src/librustc/middle/lang_items.rs | 24 +++++++++++++++++++ src/librustc/middle/mod.rs | 1 - src/librustc/middle/weak_lang_items.rs | 32 -------------------------- src/librustc_codegen_ssa/base.rs | 9 +++----- src/librustc_passes/weak_lang_items.rs | 2 +- src/librustc_typeck/collect.rs | 4 ++-- 6 files changed, 30 insertions(+), 42 deletions(-) delete mode 100644 src/librustc/middle/weak_lang_items.rs diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index d9387767655a1..9fbb151ccdf5e 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -13,7 +13,9 @@ use crate::ty::{self, TyCtxt}; use rustc_hir::def_id::DefId; use rustc_span::Span; +use rustc_target::spec::PanicStrategy; +pub use rustc_hir::weak_lang_items::link_name; pub use rustc_hir::{LangItem, LanguageItems}; impl<'tcx> TyCtxt<'tcx> { @@ -38,4 +40,26 @@ impl<'tcx> TyCtxt<'tcx> { _ => None, } } + + pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { + self.lang_items().is_weak_lang_item(item_def_id) + } +} + +/// Returns `true` if the specified `lang_item` doesn't actually need to be +/// present for this compilation. +/// +/// Not all lang items are always required for each compilation, particularly in +/// the case of panic=abort. In these situations some lang items are injected by +/// crates and don't actually need to be defined in libstd. +pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: LangItem) -> bool { + // If we're not compiling with unwinding, we won't actually need these + // symbols. Other panic runtimes ensure that the relevant symbols are + // available to link things together, but they're never exercised. + if tcx.sess.panic_strategy() != PanicStrategy::Unwind { + return lang_item == LangItem::EhPersonalityLangItem + || lang_item == LangItem::EhUnwindResumeLangItem; + } + + false } diff --git a/src/librustc/middle/mod.rs b/src/librustc/middle/mod.rs index c2959766c570a..b20f2cf3a85c1 100644 --- a/src/librustc/middle/mod.rs +++ b/src/librustc/middle/mod.rs @@ -33,4 +33,3 @@ pub mod recursion_limit; pub mod region; pub mod resolve_lifetime; pub mod stability; -pub mod weak_lang_items; diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs deleted file mode 100644 index c04a6f32215c4..0000000000000 --- a/src/librustc/middle/weak_lang_items.rs +++ /dev/null @@ -1,32 +0,0 @@ -//! Validity checking for weak lang items - -use crate::ty::TyCtxt; -use rustc_hir::def_id::DefId; -use rustc_hir::{lang_items, LangItem}; -use rustc_target::spec::PanicStrategy; - -pub use rustc_hir::weak_lang_items::link_name; - -impl<'tcx> TyCtxt<'tcx> { - pub fn is_weak_lang_item(&self, item_def_id: DefId) -> bool { - self.lang_items().is_weak_lang_item(item_def_id) - } -} - -/// Returns `true` if the specified `lang_item` doesn't actually need to be -/// present for this compilation. -/// -/// Not all lang items are always required for each compilation, particularly in -/// the case of panic=abort. In these situations some lang items are injected by -/// crates and don't actually need to be defined in libstd. -pub fn whitelisted(tcx: TyCtxt<'_>, lang_item: LangItem) -> bool { - // If we're not compiling with unwinding, we won't actually need these - // symbols. Other panic runtimes ensure that the relevant symbols are - // available to link things together, but they're never exercised. - if tcx.sess.panic_strategy() != PanicStrategy::Unwind { - return lang_item == lang_items::EhPersonalityLangItem - || lang_item == lang_items::EhUnwindResumeLangItem; - } - - false -} diff --git a/src/librustc_codegen_ssa/base.rs b/src/librustc_codegen_ssa/base.rs index 900150913842c..e9431d94863ef 100644 --- a/src/librustc_codegen_ssa/base.rs +++ b/src/librustc_codegen_ssa/base.rs @@ -28,8 +28,8 @@ use crate::{CachedModuleCodegen, CrateInfo, MemFlags, ModuleCodegen, ModuleKind} use rustc::middle::codegen_fn_attrs::CodegenFnAttrs; use rustc::middle::cstore::EncodedMetadata; use rustc::middle::cstore::{self, LinkagePreference}; +use rustc::middle::lang_items; use rustc::middle::lang_items::StartFnLangItem; -use rustc::middle::weak_lang_items; use rustc::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem}; use rustc::session::config::{self, EntryFnType, Lto}; use rustc::session::Session; @@ -847,11 +847,8 @@ impl CrateInfo { // No need to look for lang items that are whitelisted and don't // actually need to exist. - let missing = missing - .iter() - .cloned() - .filter(|&l| !weak_lang_items::whitelisted(tcx, l)) - .collect(); + let missing = + missing.iter().cloned().filter(|&l| !lang_items::whitelisted(tcx, l)).collect(); info.missing_lang_items.insert(cnum, missing); } diff --git a/src/librustc_passes/weak_lang_items.rs b/src/librustc_passes/weak_lang_items.rs index 55af437377853..010712c28ba75 100644 --- a/src/librustc_passes/weak_lang_items.rs +++ b/src/librustc_passes/weak_lang_items.rs @@ -1,7 +1,7 @@ //! Validity checking for weak lang items use rustc::middle::lang_items; -use rustc::middle::weak_lang_items::whitelisted; +use rustc::middle::lang_items::whitelisted; use rustc::session::config; use rustc::hir::map::Map; diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index b661006d1ddd0..ea0e4719a24fd 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -20,8 +20,8 @@ use crate::astconv::{AstConv, Bounds, SizedByDefault}; use crate::check::intrinsic::intrinsic_operation_unsafety; use crate::constrained_generic_params as cgp; use crate::lint; +use crate::middle::lang_items; use crate::middle::resolve_lifetime as rl; -use crate::middle::weak_lang_items; use rustc::hir::map::blocks::FnLikeNode; use rustc::hir::map::Map; use rustc::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; @@ -2977,7 +2977,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, id: DefId) -> CodegenFnAttrs { if tcx.is_weak_lang_item(id) { codegen_fn_attrs.flags |= CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL; } - if let Some(name) = weak_lang_items::link_name(&attrs) { + if let Some(name) = lang_items::link_name(&attrs) { codegen_fn_attrs.export_name = Some(name); codegen_fn_attrs.link_name = Some(name); } From fc73e196d9cf045c722aff27b9a2e595357384db Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 11 Feb 2020 23:21:21 +0100 Subject: [PATCH 11/11] Review comments. --- src/librustc/middle/lang_items.rs | 2 +- src/librustc/traits/select.rs | 6 +- src/librustc/ty/instance.rs | 2 +- src/librustc/ty/print/pretty.rs | 2 +- src/librustc_hir/lang_items.rs | 126 +++++++++++++-------------- src/librustc_mir/shim.rs | 2 +- src/librustc_typeck/check/closure.rs | 9 +- src/librustdoc/clean/utils.rs | 2 +- 8 files changed, 76 insertions(+), 75 deletions(-) diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 9fbb151ccdf5e..c8e284be6fc09 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -31,7 +31,7 @@ impl<'tcx> TyCtxt<'tcx> { }) } - pub fn fn_trait_lang_item(&self, id: DefId) -> Option { + pub fn fn_trait_kind_from_lang_item(&self, id: DefId) -> Option { let items = self.lang_items(); match Some(id) { x if x == items.fn_trait() => Some(ty::ClosureKind::Fn), diff --git a/src/librustc/traits/select.rs b/src/librustc/traits/select.rs index 4c4947d134fcc..bf82d743c2b04 100644 --- a/src/librustc/traits/select.rs +++ b/src/librustc/traits/select.rs @@ -1634,7 +1634,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { obligation: &TraitObligation<'tcx>, candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { - let kind = match self.tcx().fn_trait_lang_item(obligation.predicate.def_id()) { + let kind = match self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()) { Some(k) => k, None => { return Ok(()); @@ -1677,7 +1677,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { candidates: &mut SelectionCandidateSet<'tcx>, ) -> Result<(), SelectionError<'tcx>> { // We provide impl of all fn traits for fn pointers. - if self.tcx().fn_trait_lang_item(obligation.predicate.def_id()).is_none() { + if self.tcx().fn_trait_kind_from_lang_item(obligation.predicate.def_id()).is_none() { return Ok(()); } @@ -2889,7 +2889,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> { let kind = self .tcx() - .fn_trait_lang_item(obligation.predicate.def_id()) + .fn_trait_kind_from_lang_item(obligation.predicate.def_id()) .unwrap_or_else(|| bug!("closure candidate for non-fn trait {:?}", obligation)); // Okay to skip binder because the substs on closure types never diff --git a/src/librustc/ty/instance.rs b/src/librustc/ty/instance.rs index b175de6e0e88c..b5e17661c5de1 100644 --- a/src/librustc/ty/instance.rs +++ b/src/librustc/ty/instance.rs @@ -450,7 +450,7 @@ fn resolve_associated_item<'tcx>( substs: generator_data.substs, }), traits::VtableClosure(closure_data) => { - let trait_closure_kind = tcx.fn_trait_lang_item(trait_id).unwrap(); + let trait_closure_kind = tcx.fn_trait_kind_from_lang_item(trait_id).unwrap(); Some(Instance::resolve_closure( tcx, closure_data.closure_def_id, diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 2a43d0486d06b..274482cba64cc 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -724,7 +724,7 @@ pub trait PrettyPrinter<'tcx>: let mut resugared = false; // Special-case `Fn(...) -> ...` and resugar it. - let fn_trait_kind = self.tcx().fn_trait_lang_item(principal.def_id); + let fn_trait_kind = self.tcx().fn_trait_kind_from_lang_item(principal.def_id); if !self.tcx().sess.verbose() && fn_trait_kind.is_some() { if let ty::Tuple(ref args) = principal.substs.type_at(0).kind { let mut projections = predicates.projection_bounds(); diff --git a/src/librustc_hir/lang_items.rs b/src/librustc_hir/lang_items.rs index c5bad979f5416..cb5ebba463394 100644 --- a/src/librustc_hir/lang_items.rs +++ b/src/librustc_hir/lang_items.rs @@ -28,76 +28,76 @@ macro_rules! language_item_table { $( $variant:ident, $name:expr, $method:ident, $target:path; )* ) => { -enum_from_u32! { - /// A representation of all the valid language items in Rust. - #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] - pub enum LangItem { - $($variant,)* - } -} - -impl LangItem { - /// Returns the `name` in `#[lang = "$name"]`. - /// For example, `LangItem::EqTraitLangItem`, - /// that is `#[lang = "eq"]` would result in `"eq"`. - pub fn name(self) -> &'static str { - match self { - $( $variant => $name, )* + enum_from_u32! { + /// A representation of all the valid language items in Rust. + #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)] + pub enum LangItem { + $($variant,)* + } } - } -} - -#[derive(HashStable_Generic)] -pub struct LanguageItems { - /// Mappings from lang items to their possibly found `DefId`s. - /// The index corresponds to the order in `LangItem`. - pub items: Vec>, - /// Lang items that were not found during collection. - pub missing: Vec, -} - -impl LanguageItems { - /// Construct an empty collection of lang items and no missing ones. - pub fn new() -> Self { - fn init_none(_: LangItem) -> Option { None } - Self { - items: vec![$(init_none($variant)),*], - missing: Vec::new(), + impl LangItem { + /// Returns the `name` in `#[lang = "$name"]`. + /// For example, `LangItem::EqTraitLangItem`, + /// that is `#[lang = "eq"]` would result in `"eq"`. + pub fn name(self) -> &'static str { + match self { + $( $variant => $name, )* + } + } } - } - - /// Returns the mappings to the possibly found `DefId`s for each lang item. - pub fn items(&self) -> &[Option] { - &*self.items - } - /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. - /// If it wasn't bound, e.g. due to a missing `#[lang = ""]`, - /// returns an error message as a string. - pub fn require(&self, it: LangItem) -> Result { - self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) - } + #[derive(HashStable_Generic)] + pub struct LanguageItems { + /// Mappings from lang items to their possibly found `DefId`s. + /// The index corresponds to the order in `LangItem`. + pub items: Vec>, + /// Lang items that were not found during collection. + pub missing: Vec, + } - $( - /// Returns the corresponding `DefId` for the lang item - #[doc = $name] - /// if it exists. - #[allow(dead_code)] - pub fn $method(&self) -> Option { - self.items[$variant as usize] + impl LanguageItems { + /// Construct an empty collection of lang items and no missing ones. + pub fn new() -> Self { + fn init_none(_: LangItem) -> Option { None } + + Self { + items: vec![$(init_none($variant)),*], + missing: Vec::new(), + } + } + + /// Returns the mappings to the possibly found `DefId`s for each lang item. + pub fn items(&self) -> &[Option] { + &*self.items + } + + /// Requires that a given `LangItem` was bound and returns the corresponding `DefId`. + /// If it wasn't bound, e.g. due to a missing `#[lang = ""]`, + /// returns an error message as a string. + pub fn require(&self, it: LangItem) -> Result { + self.items[it as usize].ok_or_else(|| format!("requires `{}` lang_item", it.name())) + } + + $( + /// Returns the corresponding `DefId` for the lang item + #[doc = $name] + /// if it exists. + #[allow(dead_code)] + pub fn $method(&self) -> Option { + self.items[$variant as usize] + } + )* } - )* -} -lazy_static! { - /// A mapping from the name of the lang item to its order and the form it must be of. - pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = { - let mut item_refs = FxHashMap::default(); - $( item_refs.insert($name, ($variant as usize, $target)); )* - item_refs - }; -} + lazy_static! { + /// A mapping from the name of the lang item to its order and the form it must be of. + pub static ref ITEM_REFS: FxHashMap<&'static str, (usize, Target)> = { + let mut item_refs = FxHashMap::default(); + $( item_refs.insert($name, ($variant as usize, $target)); )* + item_refs + }; + } // End of the macro } diff --git a/src/librustc_mir/shim.rs b/src/librustc_mir/shim.rs index cc5e142351d4f..bfd30ff5da553 100644 --- a/src/librustc_mir/shim.rs +++ b/src/librustc_mir/shim.rs @@ -40,7 +40,7 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> &'tcx ), ty::InstanceDef::FnPtrShim(def_id, ty) => { let trait_ = tcx.trait_of_item(def_id).unwrap(); - let adjustment = match tcx.fn_trait_lang_item(trait_) { + let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) { Some(ty::ClosureKind::FnOnce) => Adjustment::Identity, Some(ty::ClosureKind::FnMut) | Some(ty::ClosureKind::Fn) => Adjustment::Deref, None => bug!("fn pointer {:?} is not an fn", ty), diff --git a/src/librustc_typeck/check/closure.rs b/src/librustc_typeck/check/closure.rs index f601a0e8253c1..396534b3caeb7 100644 --- a/src/librustc_typeck/check/closure.rs +++ b/src/librustc_typeck/check/closure.rs @@ -174,8 +174,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { self.deduce_sig_from_projection(None, &pb) }) .next(); - let kind = - object_type.principal_def_id().and_then(|did| self.tcx.fn_trait_lang_item(did)); + let kind = object_type + .principal_def_id() + .and_then(|did| self.tcx.fn_trait_kind_from_lang_item(did)); (sig, kind) } ty::Infer(ty::TyVar(vid)) => self.deduce_expectations_from_obligations(vid), @@ -213,7 +214,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { // many viable options, so pick the most restrictive. let expected_kind = self .obligations_for_self_ty(expected_vid) - .filter_map(|(tr, _)| self.tcx.fn_trait_lang_item(tr.def_id())) + .filter_map(|(tr, _)| self.tcx.fn_trait_kind_from_lang_item(tr.def_id())) .fold(None, |best, cur| Some(best.map_or(cur, |best| cmp::min(best, cur)))); (expected_sig, expected_kind) @@ -236,7 +237,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { let trait_ref = projection.to_poly_trait_ref(tcx); - let is_fn = tcx.fn_trait_lang_item(trait_ref.def_id()).is_some(); + let is_fn = tcx.fn_trait_kind_from_lang_item(trait_ref.def_id()).is_some(); let gen_trait = tcx.require_lang_item(lang_items::GeneratorTraitLangItem, cause_span); let is_gen = gen_trait == trait_ref.def_id(); if !is_fn && !is_gen { diff --git a/src/librustdoc/clean/utils.rs b/src/librustdoc/clean/utils.rs index c179674432332..ef35705650452 100644 --- a/src/librustdoc/clean/utils.rs +++ b/src/librustdoc/clean/utils.rs @@ -138,7 +138,7 @@ pub fn external_generic_args( match trait_did { // Attempt to sugar an external path like Fn<(A, B,), C> to Fn(A, B) -> C - Some(did) if cx.tcx.fn_trait_lang_item(did).is_some() => { + Some(did) if cx.tcx.fn_trait_kind_from_lang_item(did).is_some() => { assert!(ty_kind.is_some()); let inputs = match ty_kind { Some(ty::Tuple(ref tys)) => tys.iter().map(|t| t.expect_ty().clean(cx)).collect(),