Skip to content

Commit dc4e026

Browse files
committed
Document soundness of Integer -> Pointer -> Integer conversions in const contexts.
see this [zulip thread](https://rust-lang.zulipchat.com/#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Soundness.20of.20Integer.20-.3E.20Pointer.20-.3E.20Integer.20conversions)
1 parent 4896daa commit dc4e026

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

library/core/src/intrinsics.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,10 @@ extern "rust-intrinsic" {
11301130
/// may lead to unexpected and unstable compilation results. This makes `transmute` **incredibly
11311131
/// unsafe**. `transmute` should be the absolute last resort.
11321132
///
1133-
/// Transmuting pointers to integers in a `const` context is [undefined behavior][ub].
1133+
/// Transmuting pointers *to* integers in a `const` context is [undefined behavior][ub],
1134+
/// unless the pointer was originally created *from* an integer.
1135+
/// (That includes this function specifically, integer-to-pointer casts, and helpers like [`invalid`][crate::ptr::invalid],
1136+
/// but also semantically-equivalent conversions such as punning through `repr(C)` union fields.)
11341137
/// Any attempt to use the resulting value for integer operations will abort const-evaluation.
11351138
/// (And even outside `const`, such transmutation is touching on many unspecified aspects of the
11361139
/// Rust memory model and should be avoided. See below for alternatives.)

0 commit comments

Comments
 (0)