# What does \`unsafe\` mean?

**URL:** <https://internals.rust-lang.org/t/what-does-unsafe-mean/6696>\
**Category:** Unsafe Code Guidelines\
**Created:** [February 2, 2018, 9:26pm UTC](https://internals.rust-lang.org/t/what-does-unsafe-mean/6696 "2018-02-02T21:26:20Z")\
**Posts on this page:** 1\
**Showing post:** 37

<div class="post-metadata">

**Author:** ![RalfJung](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ralfjung/32/2415_2.png) [@RalfJung](https://internals.rust-lang.org/u/RalfJung)\
**Post date:** [July 17, 2018, 7:17pm UTC](https://internals.rust-lang.org/t/what-does-unsafe-mean/6696/37 "2018-07-17T19:17:57Z")

</div>

Thanks @alercah for these very interesting thoughts! Let me just comment on one thing. 🙂

> [@alercah](#):
>
> If this is the case, though, why is UB special? I couldn’t really think of a fundamental technical reason to draw a special distinction around it, given the above. So it needs to be philosophical instead.

I would say there is a technical point, and you have hinted at it later: _UB is the part of the API contract that the compiler knows about_. The compiler has been taught to assume that its input program is UB-free, and that is exploited all over the place for the purpose of optimizations. You are admitting this later in your post, but I would say that this is a technical difference between UB and other kinds of API contraints.

From a formal methods perspective, "UB is what the compiler knows about" translates into "UB is baked into the very definition of the language semantics itself". That also makes it different than other API constraints which only locally affect that particular API -- UB literally affects _all code_. If your language is defined operationally and comes e.g. with a reference interpreter for a "specification machine"/"VM" that is used to define program behavior, then UB must be defined that way as well. It is for this reason that I cannot just propose to use the RustBelt model of borrowing and lifetimes as the definition for aliasing-related UB in Rust -- that's not an operational model. It will (likely) never be immediate UB to use raw pointer to have two aliasing mutable references to a ZST, but we will still consider that illegal code.

Of course, "UB is what the compiler knows about" doesn't tell us (during language design) which API violations we should make UB and which "just" funny logic errors like a misbehaving `BTreeMap`, and maybe that's what you are referring to. The delineation between UB and other constraints is ultimately arbitrary, but once the choice is made, there is a qualitative, technical difference.

---

_[View the full topic](https://internals.rust-lang.org/t/what-does-unsafe-mean/6696)._
