Skip to content

Commit 3c290a5

Browse files
committed
Ensure assignments don't allow skipping projection checks
1 parent 301ce8b commit 3c290a5

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,16 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
252252
// projections are transparent for assignments
253253
// we qualify the entire destination at once, even if just a field would have
254254
// stricter qualification
255-
Place::Projection(proj) => dest = &proj.base,
255+
Place::Projection(proj) => {
256+
// Catch more errors in the destination. `visit_place` also checks various
257+
// projection rules like union field access and raw pointer deref
258+
self.visit_place(
259+
dest,
260+
PlaceContext::MutatingUse(MutatingUseContext::Store),
261+
location
262+
);
263+
dest = &proj.base;
264+
},
256265
Place::Promoted(..) => bug!("promoteds don't exist yet during promotion"),
257266
Place::Static(..) => {
258267
// Catch more errors in the destination. `visit_place` also checks that we
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(const_let)]
2+
3+
use std::cell::Cell;
4+
5+
const FOO: &u32 = {
6+
let mut a = 42;
7+
{
8+
let b: *mut u32 = &mut a; //~ ERROR may only refer to immutable values
9+
unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
10+
}
11+
&{a}
12+
};
13+
14+
fn main() {}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0017]: references in constants may only refer to immutable values
2+
--> $DIR/projection_qualif.rs:8:27
3+
|
4+
LL | let b: *mut u32 = &mut a; //~ ERROR may only refer to immutable values
5+
| ^^^^^^ constants require immutable values
6+
7+
error[E0658]: dereferencing raw pointers in constants is unstable (see issue #51911)
8+
--> $DIR/projection_qualif.rs:9:18
9+
|
10+
LL | unsafe { *b = 5; } //~ ERROR dereferencing raw pointers in constants
11+
| ^^^^^^
12+
|
13+
= help: add #![feature(const_raw_ptr_deref)] to the crate attributes to enable
14+
15+
error: aborting due to 2 previous errors
16+
17+
Some errors occurred: E0017, E0658.
18+
For more information about an error, try `rustc --explain E0017`.

0 commit comments

Comments
 (0)