Skip to content

Commit

Permalink
Move weak_lang_items checking to librustc_passes.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Feb 11, 2020
1 parent 98b46f7 commit b6f875d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 33 deletions.
32 changes: 32 additions & 0 deletions 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
}
3 changes: 2 additions & 1 deletion src/librustc_passes/lang_items.rs
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/librustc_passes/lib.rs
Expand Up @@ -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);
Expand Down
37 changes: 5 additions & 32 deletions 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>,
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}
}

0 comments on commit b6f875d

Please sign in to comment.