|
| 1 | +# Lang items |
| 2 | + |
| 3 | +The compiler has certain pluggable operations; that is, functionality that isn't hard-coded into |
| 4 | +the language, but is implemented in libraries, with a special marker to tell the compiler it |
| 5 | +exists. The marker is the attribute `#[lang = "..."]`, and there are various different values of |
| 6 | +`...`, i.e. various different 'lang items'. |
| 7 | + |
| 8 | +Many such lang items can be implemented only in one sensible way, such as `add` (`trait |
| 9 | +core::ops::Add`) or `future_trait` (`trait core::future::Future`). Others can be overriden to |
| 10 | +achieve some specific goals; for example, you can control your binary's entrypoint. |
| 11 | + |
| 12 | +Features provided by lang items include: |
| 13 | + |
| 14 | +- overloadable operators via traits: the traits corresponding to the |
| 15 | + `==`, `<`, dereference (`*`), `+`, etc. operators are all |
| 16 | + marked with lang items; those specific four are `eq`, `ord`, |
| 17 | + `deref`, and `add` respectively. |
| 18 | +- panicking and stack unwinding; the `eh_personality`, `panic` and |
| 19 | + `panic_bounds_checks` lang items. |
| 20 | +- the traits in `std::marker` used to indicate properties of types used by the compiler; |
| 21 | + lang items `send`, `sync` and `copy`. |
| 22 | +- the special marker types used for variance indicators found in |
| 23 | + `core::marker`; lang item `phantom_data`. |
| 24 | + |
| 25 | +Lang items are loaded lazily by the compiler; e.g. if one never uses `Box` |
| 26 | +then there is no need to define functions for `exchange_malloc` and |
| 27 | +`box_free`. `rustc` will emit an error when an item is needed but not found |
| 28 | +in the current crate or any that it depends on. |
| 29 | + |
| 30 | +Most lang items are defined by the `core` library, but if you're trying to build an |
| 31 | +executable with `#![no_std]`, you'll still need to define a few lang items that are |
| 32 | +usually provided by `std`. |
| 33 | + |
| 34 | +## Retrieving a language item |
| 35 | + |
| 36 | +You can retrieve lang items by calling [`tcx.lang_items()`]. |
| 37 | + |
| 38 | +Here's a small example of retrieving the `trait Sized {}` language item: |
| 39 | + |
| 40 | +```rust |
| 41 | +// Note that in case of `#![no_core]`, the trait is not available. |
| 42 | +if let Some(sized_trait_def_id) = tcx.lang_items().sized_trait() { |
| 43 | + // do something with `sized_trait_def_id` |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +Note that `sized_trait()` returns an `Option`, not the `DefId` itself. |
| 48 | +That's because language items are defined in the standard libray, so if someone compiles with |
| 49 | +`#![no_core]` (or for some lang items, `#![no_std]`), the lang item may not be present. |
| 50 | +You can either: |
| 51 | + |
| 52 | +- Give a hard error if the lang item is necessary to continue (don't panic, since this can happen in |
| 53 | + user code). |
| 54 | +- Proceed with limited functionality, by just omitting whatever you were going to do with the |
| 55 | + `DefId`. |
| 56 | + |
| 57 | +[`tcx.lang_items()`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.lang_items |
| 58 | + |
| 59 | +## List of all language items |
| 60 | + |
| 61 | +You can find language items in the following places: |
| 62 | +- An exhaustive reference in the compiler documentation: [`rustc_hir::LangItem`] |
| 63 | +- An auto-generated list with source locations by using ripgrep: `rg '#\[.*lang =' library/` |
| 64 | + |
| 65 | +Note that language items are explicitly unstable and may change in any new release. |
| 66 | + |
| 67 | +[`rustc_hir::LangItem`]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/lang_items/enum.LangItem.html |
0 commit comments