Skip to content

Commit 7db6ebb

Browse files
committed
add fn ptrs to reference
1 parent c27e319 commit 7db6ebb

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

reference/src/SUMMARY.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
- [Packed SIMD vectors](./layout/packed-simd-vectors.md)
1414
- [Validity](./validity.md)
1515
- [Unions](./validity/unions.md)
16+
- [Function Pointers](./validity/function-pointers.md)
1617
- [Optimizations](./optimizations.md)
1718
- [Return value optimization](./optimizations/return_value_optimization.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Validity of function pointers
2+
3+
**Disclaimer**: This chapter is a work-in-progress. What's contained here
4+
represents the consensus from issue [#72]. The statements in here are not (yet)
5+
"guaranteed" not to change until an RFC ratifies them.
6+
7+
A function pointer is "valid" (in the sense that it can be produced without causing immediate UB) if and only if it is non-null.
8+
9+
That makes this code UB:
10+
11+
```rust
12+
fn bad() {
13+
let x: fn() = unsafe { mem::transmute(0usize) } // This is UB!
14+
}
15+
```
16+
17+
However, any integer value other than NULL is allowed for function pointers:
18+
19+
```rust
20+
fn good() {
21+
let x: fn() = unsafe { mem::transmute(1usize) } // This is not UB.
22+
}
23+
```
24+
25+
[#72]: https://github.com/rust-lang/unsafe-code-guidelines/issues/72

0 commit comments

Comments
 (0)