Skip to content

Commit

Permalink
Fix doctest
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 2, 2024
1 parent e0d98ae commit 5d445d1
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/doc/unstable-book/src/language-features/intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,35 @@ some features that only exist on some backends. Backends can simply not implemen
intrinsics without causing any code miscompilations or failures to compile.

```rust
#![feature(core_intrinsics)]
#![feature(rustc_attrs, effects)]
#![allow(internal_features)]

#[rustc_intrinsic]
fn some_intrinsic_name() -> u32 {
42
const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}
```

Since these are just regular functions, it is perfectly ok to create the intrinsic twice:

```rust
#![feature(rustc_attrs, effects)]
#![allow(internal_features)]

#[rustc_intrinsic]
const unsafe fn const_deallocate(_ptr: *mut u8, _size: usize, _align: usize) {}

mod foo {
#[rustc_intrinsic]
const unsafe fn const_deallocate(ptr: *mut u8, size: usize, align: usize) {
eprintln!("noisy const dealloc: {ptr:p}, {size}, {align}")
}
}

```

The behaviour on backends that override the intrinsic is exactly the same. On other
backends, the intrinsic behaviour depends on which implementation is called, just like
with any regular function.

## Intrinsics lowered to MIR instructions

Various intrinsics have native MIR operations that they correspond to. Instead of requiring
Expand Down Expand Up @@ -53,4 +74,5 @@ extern "rust-intrinsic" {
}
```

As with any other FFI functions, these are always `unsafe` to call.
As with any other FFI functions, these are by default always `unsafe` to call.
You can add `#[rustc_safe_intrinsic]` to the intrinsic to make it safe to call.

0 comments on commit 5d445d1

Please sign in to comment.