2014-05-19 09:30:09 -07:00
|
|
|
//! Validity checking for weak lang items
|
|
|
|
|
2021-07-29 12:00:41 -05:00
|
|
|
use rustc_ast::Attribute;
|
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-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::{self, LangItem};
|
2020-01-30 01:24:51 +01:00
|
|
|
use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
|
2020-07-07 11:12:44 -04:00
|
|
|
use rustc_middle::middle::lang_items::required;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-05-02 01:30:23 +03:00
|
|
|
use rustc_session::config::CrateType;
|
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() {
|
2020-08-18 11:47:27 +01:00
|
|
|
items.missing.push(LangItem::EhPersonality);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2020-11-08 14:27:51 +03:00
|
|
|
if tcx.sess.target.is_like_emscripten && items.eh_catch_typeinfo().is_none() {
|
2020-03-21 07:50:38 +00:00
|
|
|
items.missing.push(LangItem::EhCatchTypeinfo);
|
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
|
|
|
|
{
|
2017-08-31 08:57:41 -07:00
|
|
|
let mut cx = Context { tcx, items };
|
2021-09-12 01:11:22 +02:00
|
|
|
tcx.hir().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();
|
2021-06-07 11:03:17 +02: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
|
|
|
|
2021-01-25 14:37:27 -05:00
|
|
|
for (name, item) in WEAK_ITEMS_REFS.clone().into_sorted_vector().into_iter() {
|
2020-07-07 11:12:44 -04:00
|
|
|
if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
|
2020-08-18 11:47:27 +01:00
|
|
|
if item == LangItem::PanicImpl {
|
2020-02-27 13:34:08 +01:00
|
|
|
tcx.sess.err("`#[panic_handler]` function required, but not found");
|
2020-08-18 11:47:27 +01:00
|
|
|
} else if item == LangItem::Oom {
|
2020-09-07 10:45:20 +02:00
|
|
|
if !tcx.features().default_alloc_error_handler {
|
2021-10-03 15:53:02 +09:00
|
|
|
tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
|
|
|
|
tcx.sess.note_without_error("Use `#![feature(default_alloc_error_handler)]` for a default error handler");
|
2020-09-07 10:45:20 +02:00
|
|
|
}
|
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-08-15 04:42:13 -07:00
|
|
|
fn register(&mut self, name: Symbol, span: Span) {
|
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-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<'_>) {
|
2021-07-29 12:00:41 -05:00
|
|
|
let check_name = |attr: &Attribute, sym| attr.has_name(sym);
|
2020-11-27 00:35:22 +01:00
|
|
|
let attrs = self.tcx.hir().attrs(i.hir_id());
|
|
|
|
if let Some((lang_item, _)) = lang_items::extract(check_name, attrs) {
|
2020-08-15 04:42:13 -07:00
|
|
|
self.register(lang_item, i.span);
|
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
|
|
|
}
|
|
|
|
}
|