2014-05-19 09:30:09 -07:00
|
|
|
//! Validity checking for weak lang items
|
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::struct_span_err;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-03-31 21:38:14 +02:00
|
|
|
use rustc_hir::lang_items;
|
2020-06-05 09:14:45 -07:00
|
|
|
use rustc_hir::lang_items::ITEM_REFS;
|
2020-01-30 01:24:51 +01:00
|
|
|
use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::lang_items::whitelisted;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-05-02 01:30:23 +03:00
|
|
|
use rustc_session::config::CrateType;
|
2020-06-05 09:14:45 -07:00
|
|
|
use rustc_span::symbol::sym;
|
2020-01-26 13:16:02 +01:00
|
|
|
use rustc_span::symbol::Symbol;
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct Context<'a, 'tcx> {
|
2019-06-14 00:48:52 +03:00
|
|
|
tcx: TyCtxt<'tcx>,
|
2014-05-19 09:30:09 -07:00
|
|
|
items: &'a mut lang_items::LanguageItems,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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.
|
2020-01-26 13:16:02 +01:00
|
|
|
pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
|
2014-05-19 09:30:09 -07:00
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-08-31 08:57:41 -07:00
|
|
|
let mut cx = Context { tcx, items };
|
2018-12-04 13:45:36 +01:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut cx.as_deep_visitor());
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2017-08-31 08:57:41 -07:00
|
|
|
verify(tcx, items);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
|
|
|
|
2020-01-26 13:16:02 +01:00
|
|
|
fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
|
2014-05-19 09:30:09 -07:00
|
|
|
// We only need to check for the presence of weak lang items if we're
|
|
|
|
// emitting something that's not an rlib.
|
2020-05-15 21:44:28 -07:00
|
|
|
let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
|
2020-05-02 01:30:23 +03:00
|
|
|
CrateType::Dylib
|
|
|
|
| CrateType::ProcMacro
|
|
|
|
| CrateType::Cdylib
|
|
|
|
| CrateType::Executable
|
|
|
|
| CrateType::Staticlib => true,
|
|
|
|
CrateType::Rlib => false,
|
2014-05-19 09:30:09 -07:00
|
|
|
});
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
if !needs_check {
|
2020-01-26 13:16:02 +01:00
|
|
|
return;
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2018-08-18 13:55:43 +03:00
|
|
|
let mut missing = FxHashSet::default();
|
2017-09-07 08:13:41 -07:00
|
|
|
for &cnum in tcx.crates().iter() {
|
2017-08-31 09:19:33 -07:00
|
|
|
for &item in tcx.missing_lang_items(cnum).iter() {
|
2015-11-20 14:51:18 +02:00
|
|
|
missing.insert(item);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2015-11-21 21:39:05 +02:00
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2020-01-26 13:16:02 +01:00
|
|
|
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 {
|
2020-02-27 13:34:08 +01:00
|
|
|
tcx.sess.err("`#[panic_handler]` function required, but not found");
|
2020-01-26 13:16:02 +01:00
|
|
|
} else if item == lang_items::OomLangItem {
|
2020-02-27 13:34:08 +01:00
|
|
|
tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
|
2018-06-29 16:00:34 -05:00
|
|
|
} else {
|
2020-01-26 13:16:02 +01:00
|
|
|
tcx.sess.err(&format!("language item required, but not found: `{}`", name));
|
2018-06-29 16:00:34 -05:00
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2020-01-26 13:16:02 +01:00
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
|
|
|
|
2017-08-31 08:57:41 -07:00
|
|
|
impl<'a, 'tcx> Context<'a, 'tcx> {
|
2020-06-05 09:14:45 -07:00
|
|
|
fn register(&mut self, name: Symbol, span: Span, hir_id: hir::HirId) {
|
2020-01-26 13:16:02 +01:00
|
|
|
if let Some(&item) = WEAK_ITEMS_REFS.get(&name) {
|
|
|
|
if self.items.require(item).is_err() {
|
|
|
|
self.items.missing.push(item);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2020-06-05 09:14:45 -07:00
|
|
|
} else if name == sym::count_code_region {
|
|
|
|
// `core::intrinsics::code_count_region()` is (currently) the only `extern` lang item
|
|
|
|
// that is never actually linked. It is not a `weak_lang_item` that can be registered
|
|
|
|
// when used, and should be registered here instead.
|
|
|
|
if let Some((item_index, _)) = ITEM_REFS.get(&*name.as_str()).cloned() {
|
|
|
|
if self.items.items[item_index].is_none() {
|
|
|
|
let item_def_id = self.tcx.hir().local_def_id(hir_id).to_def_id();
|
|
|
|
self.items.items[item_index] = Some(item_def_id);
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 13:16:02 +01:00
|
|
|
} else {
|
|
|
|
struct_span_err!(self.tcx.sess, span, E0264, "unknown external lang item: `{}`", name)
|
|
|
|
.emit();
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 08:57:41 -07:00
|
|
|
impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> {
|
2020-03-11 12:05:32 +01:00
|
|
|
type Map = intravisit::ErasedMap<'v>;
|
2020-01-07 17:25:33 +01:00
|
|
|
|
2020-03-11 12:05:32 +01:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2016-11-28 14:00:26 -05:00
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
2016-11-24 20:15:11 +01:00
|
|
|
|
2019-11-28 20:18:29 +01:00
|
|
|
fn visit_foreign_item(&mut self, i: &hir::ForeignItem<'_>) {
|
2020-01-30 01:24:51 +01:00
|
|
|
if let Some((lang_item, _)) = hir::lang_items::extract(&i.attrs) {
|
2020-06-05 09:14:45 -07:00
|
|
|
self.register(lang_item, i.span, i.hir_id);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2015-11-17 17:51:44 -05:00
|
|
|
intravisit::walk_foreign_item(self, i)
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
|
|
|
}
|