Skip to content

Commit 4158e58

Browse files
committed
Enhance some comments
1 parent 354e510 commit 4158e58

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

compiler/rustc_mir/src/transform/check_consts/validation.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,8 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> {
584584
if borrowed_place_has_mut_interior {
585585
// Locals without StorageDead follow the "enclosing scope" rule, meaning
586586
// they are essentially anonymous static items themselves.
587+
// Note: This is only sound if every local that has a `StorageDead` has a
588+
// `StorageDead` in every control flow path leading to a `return` terminator.
587589
if self.local_has_storage_dead(place.local) {
588590
self.check_op(ops::CellBorrowBehindRef);
589591
} else {

src/test/ui/consts/std/cell.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
use std::cell::*;
44

5-
// not ok, because this would create a silent constant with interior mutability.
6-
// the rules could be relaxed in the future
5+
// not ok, because this creates a dangling pointer, just like `let x = Cell::new(42).as_ptr()` would
76
static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
87
//~^ ERROR encountered dangling pointer
98
const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
109
//~^ ERROR encountered dangling pointer
1110

11+
// Ok, these are just base values and it is the `Wrap` author's job to uphold `Send` and `Sync`
12+
// invariants, since they used `unsafe impl`.
1213
static FOO3: Wrap<Cell<u32>> = Wrap(Cell::new(42));
1314
const FOO3_CONST: Wrap<Cell<u32>> = Wrap(Cell::new(42));
1415

15-
// ok
16+
// ok, we are referring to the memory of another static item.
1617
static FOO4: Wrap<*mut u32> = Wrap(FOO3.0.as_ptr());
18+
19+
// not ok, the use of a constant here is equivalent to an inline declaration of the value, so
20+
// its memory will get freed before the constant is finished evaluating, thus creating a dangling
21+
// pointer. This would happen exactly the same at runtime.
1722
const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr());
1823
//~^ ERROR encountered dangling pointer
1924

20-
// not ok, because the `as_ptr` call takes a reference to a type with interior mutability
21-
// which is not allowed in constants
25+
// not ok, because the `as_ptr` call takes a reference to a temporary that will get freed
26+
// before the constant is finished evaluating.
2227
const FOO2: *mut u32 = Cell::new(42).as_ptr();
2328
//~^ ERROR encountered dangling pointer
2429

src/test/ui/consts/std/cell.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
error: encountered dangling pointer in final constant
2-
--> $DIR/cell.rs:7:1
2+
--> $DIR/cell.rs:6:1
33
|
44
LL | static FOO: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

77
error: encountered dangling pointer in final constant
8-
--> $DIR/cell.rs:9:1
8+
--> $DIR/cell.rs:8:1
99
|
1010
LL | const FOO_CONST: Wrap<*mut u32> = Wrap(Cell::new(42).as_ptr());
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: encountered dangling pointer in final constant
14-
--> $DIR/cell.rs:17:1
14+
--> $DIR/cell.rs:22:1
1515
|
1616
LL | const FOO4_CONST: Wrap<*mut u32> = Wrap(FOO3_CONST.0.as_ptr());
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

1919
error: encountered dangling pointer in final constant
20-
--> $DIR/cell.rs:22:1
20+
--> $DIR/cell.rs:27:1
2121
|
2222
LL | const FOO2: *mut u32 = Cell::new(42).as_ptr();
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)