5 ms·
Is this optimization required by the language semantics or some standard (vs. being only a description of mainline rustc's implemented optimizations)?
by blt 7y ago
Is this optimization required by the language semantics or some standard (vs. being only a description of mainline rustc's implemented optimizations)?
- steveklabnik 7y agoIt’s a defined part of the language semantics.
- eridius 7y agoIs there actually a formal language semantics definition anywhere? I thought all we had was stuff like https://doc.rust-lang.org/reference/index.html https://doc.rust-lang.org/reference/index.html which is explicitly listed as "not a formal spec".
- steveklabnik 7y agoThings have different degrees of guarantees. This is something we’ve explicitly said is guaranteed.
- kurtisc 7y agoWhere are things like this said/defined/collated?
- steveklabnik 7y agoWe are generally at a point where if it's documented, it's guaranteed. This feature is really easy to describe, and so we have a high degree of confidence that when we say "this behavior is defined", we won't run into an edge case where we'd have to break it. So, in this case, it's a documented part of Option. I realize this answer isn't fantastic, but it's where we're at now. It's why we're working on the reference this year!
- kurtisc 7y ago>we're working on the reference this year! I didn't know this, it's good to hear. Thanks!
- eridius 7y agoAFAIK there is no prescriptive standard for Rust, there's only descriptions of how the rustc compiler works. But this was a very intentional decision quite some time ago, and is also the reason why the `std::ptr::NonNull` type exists (the documentation explicitly states this allows enums to use the forbidden value as a discriminant). FWIW the module documentation of `std::option` does describe this optimization for Option<Box<T>>. > This usage of Option to create safe nullable pointers is so common that Rust does special optimizations to make the representation of Option<Box<T>> a single pointer. Optional pointers in Rust are stored as efficiently as any other pointer type.