rustc_middle/middle/
lang_items.rs

1//! Detecting lang items.
2//!
3//! Language items are items that represent concepts intrinsic to the language
4//! itself. Examples are:
5//!
6//! * Traits that specify "kinds"; e.g., `Sync`, `Send`.
7//! * Traits that represent operators; e.g., `Add`, `Sub`, `Index`.
8//! * Functions called by the compiler itself.
9
10use rustc_hir::LangItem;
11use rustc_hir::def_id::DefId;
12use rustc_span::Span;
13use rustc_target::spec::PanicStrategy;
14
15use crate::ty::{self, TyCtxt};
16
17impl<'tcx> TyCtxt<'tcx> {
18    /// Returns the `DefId` for a given `LangItem`.
19    /// If not found, fatally aborts compilation.
20    pub fn require_lang_item(self, lang_item: LangItem, span: Option<Span>) -> DefId {
21        self.lang_items().get(lang_item).unwrap_or_else(|| {
22            self.dcx().emit_fatal(crate::error::RequiresLangItem { span, name: lang_item.name() });
23        })
24    }
25
26    pub fn is_lang_item(self, def_id: DefId, lang_item: LangItem) -> bool {
27        self.lang_items().get(lang_item) == Some(def_id)
28    }
29
30    pub fn as_lang_item(self, def_id: DefId) -> Option<LangItem> {
31        self.lang_items().from_def_id(def_id)
32    }
33
34    /// Given a [`DefId`] of one of the [`Fn`], [`FnMut`] or [`FnOnce`] traits,
35    /// returns a corresponding [`ty::ClosureKind`].
36    /// For any other [`DefId`] return `None`.
37    pub fn fn_trait_kind_from_def_id(self, id: DefId) -> Option<ty::ClosureKind> {
38        match self.as_lang_item(id)? {
39            LangItem::Fn => Some(ty::ClosureKind::Fn),
40            LangItem::FnMut => Some(ty::ClosureKind::FnMut),
41            LangItem::FnOnce => Some(ty::ClosureKind::FnOnce),
42            _ => None,
43        }
44    }
45
46    /// Given a [`DefId`] of one of the `AsyncFn`, `AsyncFnMut` or `AsyncFnOnce` traits,
47    /// returns a corresponding [`ty::ClosureKind`].
48    /// For any other [`DefId`] return `None`.
49    pub fn async_fn_trait_kind_from_def_id(self, id: DefId) -> Option<ty::ClosureKind> {
50        match self.as_lang_item(id)? {
51            LangItem::AsyncFn => Some(ty::ClosureKind::Fn),
52            LangItem::AsyncFnMut => Some(ty::ClosureKind::FnMut),
53            LangItem::AsyncFnOnce => Some(ty::ClosureKind::FnOnce),
54            _ => None,
55        }
56    }
57
58    /// Given a [`ty::ClosureKind`], get the [`DefId`] of its corresponding `Fn`-family
59    /// trait, if it is defined.
60    pub fn fn_trait_kind_to_def_id(self, kind: ty::ClosureKind) -> Option<DefId> {
61        let items = self.lang_items();
62        match kind {
63            ty::ClosureKind::Fn => items.fn_trait(),
64            ty::ClosureKind::FnMut => items.fn_mut_trait(),
65            ty::ClosureKind::FnOnce => items.fn_once_trait(),
66        }
67    }
68
69    /// Given a [`ty::ClosureKind`], get the [`DefId`] of its corresponding `Fn`-family
70    /// trait, if it is defined.
71    pub fn async_fn_trait_kind_to_def_id(self, kind: ty::ClosureKind) -> Option<DefId> {
72        let items = self.lang_items();
73        match kind {
74            ty::ClosureKind::Fn => items.async_fn_trait(),
75            ty::ClosureKind::FnMut => items.async_fn_mut_trait(),
76            ty::ClosureKind::FnOnce => items.async_fn_once_trait(),
77        }
78    }
79
80    /// Returns `true` if `id` is a `DefId` of [`Fn`], [`FnMut`] or [`FnOnce`] traits.
81    pub fn is_fn_trait(self, id: DefId) -> bool {
82        self.fn_trait_kind_from_def_id(id).is_some()
83    }
84}
85
86/// Returns `true` if the specified `lang_item` must be present for this
87/// compilation.
88///
89/// Not all lang items are always required for each compilation, particularly in
90/// the case of panic=abort. In these situations some lang items are injected by
91/// crates and don't actually need to be defined in libstd.
92pub fn required(tcx: TyCtxt<'_>, lang_item: LangItem) -> bool {
93    // If we're not compiling with unwinding, we won't actually need these
94    // symbols. Other panic runtimes ensure that the relevant symbols are
95    // available to link things together, but they're never exercised.
96    match tcx.sess.panic_strategy() {
97        PanicStrategy::Abort => {
98            lang_item != LangItem::EhPersonality && lang_item != LangItem::EhCatchTypeinfo
99        }
100        PanicStrategy::Unwind => true,
101    }
102}