]> git.lizzy.rs Git - rust.git/blob - src/doc/unstable-book/src/language-features/lang-items.md
Rollup merge of #70038 - DutchGhost:const-forget-tests, r=RalfJung
[rust.git] / src / doc / unstable-book / src / language-features / lang-items.md
1 # `lang_items`
2
3 The tracking issue for this feature is: None.
4
5 ------------------------
6
7 The `rustc` compiler has certain pluggable operations, that is,
8 functionality that isn't hard-coded into the language, but is
9 implemented in libraries, with a special marker to tell the compiler
10 it exists. The marker is the attribute `#[lang = "..."]` and there are
11 various different values of `...`, i.e. various different 'lang
12 items'.
13
14 For example, `Box` pointers require two lang items, one for allocation
15 and one for deallocation. A freestanding program that uses the `Box`
16 sugar for dynamic allocations via `malloc` and `free`:
17
18 ```rust,ignore
19 #![feature(lang_items, box_syntax, start, libc, core_intrinsics)]
20 #![no_std]
21 use core::intrinsics;
22 use core::panic::PanicInfo;
23
24 extern crate libc;
25
26 #[lang = "owned_box"]
27 pub struct Box<T>(*mut T);
28
29 #[lang = "exchange_malloc"]
30 unsafe fn allocate(size: usize, _align: usize) -> *mut u8 {
31     let p = libc::malloc(size as libc::size_t) as *mut u8;
32
33     // Check if `malloc` failed:
34     if p as usize == 0 {
35         intrinsics::abort();
36     }
37
38     p
39 }
40
41 #[lang = "box_free"]
42 unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
43     libc::free(ptr as *mut libc::c_void)
44 }
45
46 #[start]
47 fn main(_argc: isize, _argv: *const *const u8) -> isize {
48     let _x = box 1;
49
50     0
51 }
52
53 #[lang = "eh_personality"] extern fn rust_eh_personality() {}
54 #[lang = "panic_impl"] extern fn rust_begin_panic(info: &PanicInfo) -> ! { unsafe { intrinsics::abort() } }
55 #[no_mangle] pub extern fn rust_eh_register_frames () {}
56 #[no_mangle] pub extern fn rust_eh_unregister_frames () {}
57 ```
58
59 Note the use of `abort`: the `exchange_malloc` lang item is assumed to
60 return a valid pointer, and so needs to do the check internally.
61
62 Other 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`,
69   `panic` and `panic_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
76 Lang 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`
78 and `box_free`. `rustc` will emit an error when an item is needed
79 but not found in the current crate or any that it depends on.
80
81 Most lang items are defined by `libcore`, but if you're trying to build
82 an executable without the standard library, you'll run into the need
83 for lang items. The rest of this page focuses on this use-case, even though
84 lang items are a bit broader than that.
85
86 ### Using libc
87
88 In order to build a `#[no_std]` executable we will need libc as a dependency.
89 We can specify this using our `Cargo.toml` file:
90
91 ```toml
92 [dependencies]
93 libc = { version = "0.2.14", default-features = false }
94 ```
95
96 Note that the default features have been disabled. This is a critical step -
97 **the default features of libc include the standard library and so must be
98 disabled.**
99
100 ### Writing an executable without stdlib
101
102 Controlling the entry point is possible in two ways: the `#[start]` attribute,
103 or overriding the default shim for the C `main` function with your own.
104
105 The function marked `#[start]` is passed the command line parameters
106 in the same format as C:
107
108 ```rust,ignore
109 #![feature(lang_items, core_intrinsics)]
110 #![feature(start)]
111 #![no_std]
112 use core::intrinsics;
113 use core::panic::PanicInfo;
114
115 // Pull in the system libc library for what crt0.o likely requires.
116 extern crate libc;
117
118 // Entry point for this program.
119 #[start]
120 fn start(_argc: isize, _argv: *const *const u8) -> isize {
121     0
122 }
123
124 // These functions are used by the compiler, but not
125 // for a bare-bones hello world. These are normally
126 // provided by libstd.
127 #[lang = "eh_personality"]
128 #[no_mangle]
129 pub extern fn rust_eh_personality() {
130 }
131
132 #[lang = "panic_impl"]
133 #[no_mangle]
134 pub extern fn rust_begin_panic(info: &PanicInfo) -> ! {
135     unsafe { intrinsics::abort() }
136 }
137 ```
138
139 To override the compiler-inserted `main` shim, one has to disable it
140 with `#![no_main]` and then create the appropriate symbol with the
141 correct ABI and the correct name, which requires overriding the
142 compiler's name mangling too:
143
144 ```rust,ignore
145 #![feature(lang_items, core_intrinsics)]
146 #![feature(start)]
147 #![no_std]
148 #![no_main]
149 use core::intrinsics;
150 use core::panic::PanicInfo;
151
152 // Pull in the system libc library for what crt0.o likely requires.
153 extern crate libc;
154
155 // Entry point for this program.
156 #[no_mangle] // ensure that this symbol is called `main` in the output
157 pub extern fn main(_argc: i32, _argv: *const *const u8) -> i32 {
158     0
159 }
160
161 // These functions are used by the compiler, but not
162 // for a bare-bones hello world. These are normally
163 // provided by libstd.
164 #[lang = "eh_personality"]
165 #[no_mangle]
166 pub extern fn rust_eh_personality() {
167 }
168
169 #[lang = "panic_impl"]
170 #[no_mangle]
171 pub extern fn rust_begin_panic(info: &PanicInfo) -> ! {
172     unsafe { intrinsics::abort() }
173 }
174 ```
175
176 In many cases, you may need to manually link to the `compiler_builtins` crate
177 when building a `no_std` binary. You may observe this via linker error messages
178 such as "```undefined reference to `__rust_probestack'```".
179
180 ## More about the language items
181
182 The compiler currently makes a few assumptions about symbols which are
183 available in the executable to call. Normally these functions are provided by
184 the standard library, but without it you must define your own. These symbols
185 are called "language items", and they each have an internal name, and then a
186 signature that an implementation must conform to.
187
188 The first of these functions, `rust_eh_personality`, is used by the failure
189 mechanisms of the compiler. This is often mapped to GCC's personality function
190 (see the [libstd implementation][unwind] for more information), but crates
191 which do not trigger a panic can be assured that this function is never
192 called. The language item's name is `eh_personality`.
193
194 [unwind]: https://github.com/rust-lang/rust/blob/master/src/libpanic_unwind/gcc.rs
195
196 The second function, `rust_begin_panic`, is also used by the failure mechanisms of the
197 compiler. When a panic happens, this controls the message that's displayed on
198 the screen. While the language item's name is `panic_impl`, the symbol name is
199 `rust_begin_panic`.
200
201 Finally, a `eh_catch_typeinfo` static is needed for certain targets which
202 implement Rust panics on top of C++ exceptions.
203
204 ## List of all language items
205
206 This is a list of all language items in Rust along with where they are located in
207 the source code.
208
209 - Primitives
210   - `i8`: `libcore/num/mod.rs`
211   - `i16`: `libcore/num/mod.rs`
212   - `i32`: `libcore/num/mod.rs`
213   - `i64`: `libcore/num/mod.rs`
214   - `i128`: `libcore/num/mod.rs`
215   - `isize`: `libcore/num/mod.rs`
216   - `u8`: `libcore/num/mod.rs`
217   - `u16`: `libcore/num/mod.rs`
218   - `u32`: `libcore/num/mod.rs`
219   - `u64`: `libcore/num/mod.rs`
220   - `u128`: `libcore/num/mod.rs`
221   - `usize`: `libcore/num/mod.rs`
222   - `f32`: `libstd/f32.rs`
223   - `f64`: `libstd/f64.rs`
224   - `char`: `libcore/char.rs`
225   - `slice`: `liballoc/slice.rs`
226   - `str`: `liballoc/str.rs`
227   - `const_ptr`: `libcore/ptr.rs`
228   - `mut_ptr`: `libcore/ptr.rs`
229   - `unsafe_cell`: `libcore/cell.rs`
230 - Runtime
231   - `start`: `libstd/rt.rs`
232   - `eh_personality`: `libpanic_unwind/emcc.rs` (EMCC)
233   - `eh_personality`: `libpanic_unwind/gcc.rs` (GNU)
234   - `eh_personality`: `libpanic_unwind/seh.rs` (SEH)
235   - `eh_catch_typeinfo`: `libpanic_unwind/emcc.rs` (EMCC)
236   - `panic`: `libcore/panicking.rs`
237   - `panic_bounds_check`: `libcore/panicking.rs`
238   - `panic_impl`: `libcore/panicking.rs`
239   - `panic_impl`: `libstd/panicking.rs`
240 - Allocations
241   - `owned_box`: `liballoc/boxed.rs`
242   - `exchange_malloc`: `liballoc/heap.rs`
243   - `box_free`: `liballoc/heap.rs`
244 - Operands
245   - `not`: `libcore/ops/bit.rs`
246   - `bitand`: `libcore/ops/bit.rs`
247   - `bitor`: `libcore/ops/bit.rs`
248   - `bitxor`: `libcore/ops/bit.rs`
249   - `shl`: `libcore/ops/bit.rs`
250   - `shr`: `libcore/ops/bit.rs`
251   - `bitand_assign`: `libcore/ops/bit.rs`
252   - `bitor_assign`: `libcore/ops/bit.rs`
253   - `bitxor_assign`: `libcore/ops/bit.rs`
254   - `shl_assign`: `libcore/ops/bit.rs`
255   - `shr_assign`: `libcore/ops/bit.rs`
256   - `deref`: `libcore/ops/deref.rs`
257   - `deref_mut`: `libcore/ops/deref.rs`
258   - `index`: `libcore/ops/index.rs`
259   - `index_mut`: `libcore/ops/index.rs`
260   - `add`: `libcore/ops/arith.rs`
261   - `sub`: `libcore/ops/arith.rs`
262   - `mul`: `libcore/ops/arith.rs`
263   - `div`: `libcore/ops/arith.rs`
264   - `rem`: `libcore/ops/arith.rs`
265   - `neg`: `libcore/ops/arith.rs`
266   - `add_assign`: `libcore/ops/arith.rs`
267   - `sub_assign`: `libcore/ops/arith.rs`
268   - `mul_assign`: `libcore/ops/arith.rs`
269   - `div_assign`: `libcore/ops/arith.rs`
270   - `rem_assign`: `libcore/ops/arith.rs`
271   - `eq`: `libcore/cmp.rs`
272   - `ord`: `libcore/cmp.rs`
273 - Functions
274   - `fn`: `libcore/ops/function.rs`
275   - `fn_mut`: `libcore/ops/function.rs`
276   - `fn_once`: `libcore/ops/function.rs`
277   - `generator_state`: `libcore/ops/generator.rs`
278   - `generator`: `libcore/ops/generator.rs`
279 - Other
280   - `coerce_unsized`: `libcore/ops/unsize.rs`
281   - `drop`: `libcore/ops/drop.rs`
282   - `drop_in_place`: `libcore/ptr.rs`
283   - `clone`: `libcore/clone.rs`
284   - `copy`: `libcore/marker.rs`
285   - `send`: `libcore/marker.rs`
286   - `sized`: `libcore/marker.rs`
287   - `unsize`: `libcore/marker.rs`
288   - `sync`: `libcore/marker.rs`
289   - `phantom_data`: `libcore/marker.rs`
290   - `freeze`: `libcore/marker.rs`
291   - `debug_trait`: `libcore/fmt/mod.rs`
292   - `non_zero`: `libcore/nonzero.rs`
293   - `arc`: `liballoc/sync.rs`
294   - `rc`: `liballoc/rc.rs`