]> git.proxmox.com Git - rustc.git/blame - src/doc/trpl/lang-items.md
Imported Upstream version 1.2.0+dfsg1
[rustc.git] / src / doc / trpl / lang-items.md
CommitLineData
c34b1796
AL
1% Lang items
2
3> **Note**: lang items are often provided by crates in the Rust distribution,
4> and lang items themselves have an unstable interface. It is recommended to use
5> officially distributed crates instead of defining your own lang items.
6
7The `rustc` compiler has certain pluggable operations, that is,
8functionality that isn't hard-coded into the language, but is
9implemented in libraries, with a special marker to tell the compiler
bd371182 10it exists. The marker is the attribute `#[lang = "..."]` and there are
c34b1796
AL
11various different values of `...`, i.e. various different 'lang
12items'.
13
14For example, `Box` pointers require two lang items, one for allocation
15and one for deallocation. A freestanding program that uses the `Box`
16sugar for dynamic allocations via `malloc` and `free`:
17
62682a34 18```rust
c34b1796
AL
19#![feature(lang_items, box_syntax, start, no_std, libc)]
20#![no_std]
21
22extern crate libc;
23
24extern {
25 fn abort() -> !;
26}
27
28#[lang = "owned_box"]
29pub struct Box<T>(*mut T);
30
bd371182 31#[lang = "exchange_malloc"]
c34b1796
AL
32unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
33 let p = libc::malloc(size as libc::size_t) as *mut u8;
34
35 // malloc failed
36 if p as usize == 0 {
37 abort();
38 }
39
40 p
41}
bd371182 42#[lang = "exchange_free"]
c34b1796
AL
43unsafe fn deallocate(ptr: *mut u8, _size: usize, _align: usize) {
44 libc::free(ptr as *mut libc::c_void)
45}
46
47#[start]
48fn main(argc: isize, argv: *const *const u8) -> isize {
49 let x = box 1;
50
51 0
52}
53
54#[lang = "stack_exhausted"] extern fn stack_exhausted() {}
55#[lang = "eh_personality"] extern fn eh_personality() {}
56#[lang = "panic_fmt"] fn panic_fmt() -> ! { loop {} }
57```
58
59Note the use of `abort`: the `exchange_malloc` lang item is assumed to
60return a valid pointer, and so needs to do the check internally.
61
62Other features provided by lang items include:
63
64- overloadable operators via traits: the traits corresponding to the
65 `==`, `<`, dereferencing (`*`) and `+` (etc.) operators are all
66 marked with lang items; those specific four are `eq`, `ord`,
67 `deref`, and `add` respectively.
68- stack unwinding and general failure; the `eh_personality`, `fail`
69 and `fail_bounds_checks` lang items.
70- the traits in `std::marker` used to indicate types of
71 various kinds; lang items `send`, `sync` and `copy`.
72- the marker types and variance indicators found in
73 `std::marker`; lang items `covariant_type`,
74 `contravariant_lifetime`, etc.
75
76Lang items are loaded lazily by the compiler; e.g. if one never uses
77`Box` then there is no need to define functions for `exchange_malloc`
78and `exchange_free`. `rustc` will emit an error when an item is needed
79but not found in the current crate or any that it depends on.