/compiler/rustc_passes/src/weak_lang_items.rs

https://gitlab.com/rust-lang/rust · Rust · 91 lines · 76 code · 7 blank · 8 comment · 23 complexity · 828b02c8d76a2d968998dec5ca03f568 MD5 · raw file

  1. //! Validity checking for weak lang items
  2. use rustc_data_structures::fx::FxHashSet;
  3. use rustc_errors::struct_span_err;
  4. use rustc_hir::lang_items::{self, LangItem};
  5. use rustc_hir::weak_lang_items::WEAK_ITEMS_REFS;
  6. use rustc_middle::middle::lang_items::required;
  7. use rustc_middle::ty::TyCtxt;
  8. use rustc_session::config::CrateType;
  9. /// Checks the crate for usage of weak lang items, returning a vector of all the
  10. /// language items required by this crate, but not defined yet.
  11. pub fn check_crate<'tcx>(tcx: TyCtxt<'tcx>, items: &mut lang_items::LanguageItems) {
  12. // These are never called by user code, they're generated by the compiler.
  13. // They will never implicitly be added to the `missing` array unless we do
  14. // so here.
  15. if items.eh_personality().is_none() {
  16. items.missing.push(LangItem::EhPersonality);
  17. }
  18. if tcx.sess.target.is_like_emscripten && items.eh_catch_typeinfo().is_none() {
  19. items.missing.push(LangItem::EhCatchTypeinfo);
  20. }
  21. let crate_items = tcx.hir_crate_items(());
  22. for id in crate_items.foreign_items() {
  23. let attrs = tcx.hir().attrs(id.hir_id());
  24. if let Some((lang_item, _)) = lang_items::extract(attrs) {
  25. if let Some(&item) = WEAK_ITEMS_REFS.get(&lang_item) {
  26. if items.require(item).is_err() {
  27. items.missing.push(item);
  28. }
  29. } else {
  30. let span = tcx.def_span(id.def_id);
  31. struct_span_err!(
  32. tcx.sess,
  33. span,
  34. E0264,
  35. "unknown external lang item: `{}`",
  36. lang_item
  37. )
  38. .emit();
  39. }
  40. }
  41. }
  42. verify(tcx, items);
  43. }
  44. fn verify<'tcx>(tcx: TyCtxt<'tcx>, items: &lang_items::LanguageItems) {
  45. // We only need to check for the presence of weak lang items if we're
  46. // emitting something that's not an rlib.
  47. let needs_check = tcx.sess.crate_types().iter().any(|kind| match *kind {
  48. CrateType::Dylib
  49. | CrateType::ProcMacro
  50. | CrateType::Cdylib
  51. | CrateType::Executable
  52. | CrateType::Staticlib => true,
  53. CrateType::Rlib => false,
  54. });
  55. if !needs_check {
  56. return;
  57. }
  58. let mut missing = FxHashSet::default();
  59. for &cnum in tcx.crates(()).iter() {
  60. for &item in tcx.missing_lang_items(cnum).iter() {
  61. missing.insert(item);
  62. }
  63. }
  64. for (name, item) in WEAK_ITEMS_REFS.clone().into_sorted_vector().into_iter() {
  65. if missing.contains(&item) && required(tcx, item) && items.require(item).is_err() {
  66. if item == LangItem::PanicImpl {
  67. tcx.sess.err("`#[panic_handler]` function required, but not found");
  68. } else if item == LangItem::Oom {
  69. if !tcx.features().default_alloc_error_handler {
  70. tcx.sess.err("`#[alloc_error_handler]` function required, but not found");
  71. tcx.sess.note_without_error("use `#![feature(default_alloc_error_handler)]` for a default error handler");
  72. }
  73. } else {
  74. tcx
  75. .sess
  76. .diagnostic()
  77. .struct_err(&format!("language item required, but not found: `{}`", name))
  78. .note(&format!("this can occur when a binary crate with `#![no_std]` is compiled for a target where `{}` is defined in the standard library", name))
  79. .help(&format!("you may be able to compile for a target that doesn't need `{}`, specify a target with `--target` or in `.cargo/config`", name))
  80. .emit();
  81. }
  82. }
  83. }
  84. }