about summary refs log tree commit diff
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2020-01-26 13:16:02 +0100
committerCamille GILLOT <gillot.camille@gmail.com>2020-02-11 23:11:29 +0100
commit4ecba94dcb415b0642ebd8695a08adc4ec07441e (patch)
tree52bfcae745e45e4c9fd4ff9b1fb7e464bb543ff1
parentc04195da9f47e8090bc49bad605765e9a2a721b4 (diff)
downloadrust-4ecba94dcb415b0642ebd8695a08adc4ec07441e.tar.gz
rust-4ecba94dcb415b0642ebd8695a08adc4ec07441e.zip
Move weak lang items to librustc_lang_items.
-rw-r--r--src/librustc/middle/weak_lang_items.rs99
-rw-r--r--src/librustc_lang_items/lib.rs1
-rw-r--r--src/librustc_lang_items/weak_lang_items.rs48
3 files changed, 80 insertions, 68 deletions
diff --git a/src/librustc/middle/weak_lang_items.rs b/src/librustc/middle/weak_lang_items.rs
index 5649a2b9132..ab19483a62d 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<Symbol> {
-    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 90d090e6f61..9bcd54b6cbf 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 00000000000..ab2955fb3fc
--- /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<Symbol, LangItem> = {
+        let mut map = FxHashMap::default();
+        $(map.insert(sym::$name, lang_items::$item);)*
+        map
+    };
+}
+
+pub fn link_name(attrs: &[ast::Attribute]) -> Option<Symbol> {
+    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;
+}