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-08-18 11:47:27 +01:00
|
|
|
use rustc_hir::lang_items::{self, LangItem};
|
2022-10-26 16:18:59 -05:00
|
|
|
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
|
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;
|
2014-05-19 09:30:09 -07:00
|
|
|
|
2022-10-14 02:24:58 +01:00
|
|
|
use crate::errors::{MissingLangItem, MissingPanicHandler, UnknownExternLangItem};
|
2022-08-22 10:05:13 +02:00
|
|
|
|
2014-05-19 09:30:09 -07:00
|
|
|
/// 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.
|
2022-12-20 22:10:40 +01:00
|
|
|
pub fn check_crate(tcx: TyCtxt<'_>, 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
|
|
|
}
|
2022-06-18 01:01:19 +03:00
|
|
|
if tcx.sess.target.os == "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
|
|
|
|
2022-04-07 18:21:37 -04:00
|
|
|
let crate_items = tcx.hir_crate_items(());
|
|
|
|
for id in crate_items.foreign_items() {
|
|
|
|
let attrs = tcx.hir().attrs(id.hir_id());
|
|
|
|
if let Some((lang_item, _)) = lang_items::extract(attrs) {
|
2022-10-26 16:18:59 -05:00
|
|
|
if let Some(item) = LangItem::from_name(lang_item)
|
|
|
|
&& item.is_weak()
|
|
|
|
{
|
|
|
|
if items.get(item).is_none() {
|
2022-04-07 18:21:37 -04:00
|
|
|
items.missing.push(item);
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-27 14:02:18 +11:00
|
|
|
let span = tcx.def_span(id.owner_id);
|
2022-09-21 22:49:30 -06:00
|
|
|
tcx.sess.emit_err(UnknownExternLangItem { span, lang_item });
|
2022-04-07 18:21:37 -04:00
|
|
|
}
|
|
|
|
}
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
2022-04-07 18:21:37 -04:00
|
|
|
|
2017-08-31 08:57:41 -07:00
|
|
|
verify(tcx, items);
|
2014-05-19 09:30:09 -07:00
|
|
|
}
|
|
|
|
|
2022-12-20 22:10:40 +01:00
|
|
|
fn verify(tcx: TyCtxt<'_>, 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.
|
2023-08-08 18:28:20 +08:00
|
|
|
let needs_check = tcx.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
|
|
|
|
2022-10-26 16:18:59 -05:00
|
|
|
for &item in WEAK_LANG_ITEMS.iter() {
|
|
|
|
if missing.contains(&item) && required(tcx, item) && items.get(item).is_none() {
|
2020-08-18 11:47:27 +01:00
|
|
|
if item == LangItem::PanicImpl {
|
2022-08-22 10:05:13 +02:00
|
|
|
tcx.sess.emit_err(MissingPanicHandler);
|
2018-06-29 16:00:34 -05:00
|
|
|
} else {
|
2022-10-26 16:18:59 -05:00
|
|
|
tcx.sess.emit_err(MissingLangItem { name: item.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
|
|
|
}
|