Skip to content

Commit 11ef652

Browse files
authored
Merge pull request #1637 from RalfJung/immutable
Clarify definition of "immutable bytes"
2 parents 6394eb9 + cd966df commit 11ef652

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/behavior-considered-undefined.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ undefined behavior, it is *unsound*.
4040
All this also applies when values of these
4141
types are passed in a (nested) field of a compound type, but not behind
4242
pointer indirections.
43-
* Mutating immutable bytes. All bytes inside a [`const`] item or within an implicitly [const-promoted] expression are immutable.
43+
* Mutating immutable bytes.
44+
All bytes reachable through a [const-promoted] expression are immutable, as well as bytes reachable through borrows in `static` and `const` initializers that have been [lifetime-extended] to `'static`.
4445
The bytes owned by an immutable binding or immutable `static` are immutable, unless those bytes are part of an [`UnsafeCell<U>`].
4546

4647
Moreover, the bytes [pointed to] by a shared reference, including transitively through other references (both shared and mutable) and `Box`es, are immutable; transitivity includes those references stored in fields of compound types.
@@ -179,3 +180,4 @@ reading uninitialized memory is permitted are inside `union`s and in "padding"
179180
[project-tuple]: expressions/tuple-expr.md#tuple-indexing-expressions
180181
[project-slice]: expressions/array-expr.md#array-and-slice-indexing-expressions
181182
[const-promoted]: destructors.md#constant-promotion
183+
[lifetime-extended]: destructors.md#temporary-lifetime-extension

src/destructors.md

+12
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,18 @@ let x = &mut 0;
325325
println!("{}", x);
326326
```
327327

328+
r[destructors.scope.lifetime-extension.static]
329+
Lifetime extension also applies to `static` and `const` items, where it
330+
makes temporaries live until the end of the program. For example:
331+
332+
```rust
333+
const C: &Vec<i32> = &Vec::new();
334+
// Usually this would be a dangling reference as the `Vec` would only
335+
// exist inside the initializer expression of `C`, but instead the
336+
// borrow gets lifetime-extended so it effectively has `'static` lifetime.
337+
println!("{:?}", C);
338+
```
339+
328340
r[destructors.scope.lifetime-extension.sub-expressions]
329341
If a [borrow][borrow expression], [dereference][dereference expression],
330342
[field][field expression], or [tuple indexing expression] has an extended

0 commit comments

Comments
 (0)