Skip to content

Commit b51632e

Browse files
committed
Auto merge of #56070 - oli-obk:const_let, r=eddyb
Allow assignments in const contexts fixes #54098 fixes #51251 fixes #52613
2 parents 423291f + 6db8c6c commit b51632e

35 files changed

+382
-43
lines changed

src/librustc/ich/impls_ty.rs

+1
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,7 @@ impl_stable_hash_for!(
429429
CalledClosureAsFunction,
430430
VtableForArgumentlessMethod,
431431
ModifiedConstantMemory,
432+
ModifiedStatic,
432433
AssumptionNotHeld,
433434
InlineAsm,
434435
ReallocateNonBasePtr,

src/librustc/mir/interpret/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ pub enum EvalErrorKind<'tcx, O> {
308308
CalledClosureAsFunction,
309309
VtableForArgumentlessMethod,
310310
ModifiedConstantMemory,
311+
ModifiedStatic,
311312
AssumptionNotHeld,
312313
InlineAsm,
313314
TypeNotPrimitive(Ty<'tcx>),
@@ -412,6 +413,8 @@ impl<'tcx, O> EvalErrorKind<'tcx, O> {
412413
"tried to call a vtable function without arguments",
413414
ModifiedConstantMemory =>
414415
"tried to modify constant memory",
416+
ModifiedStatic =>
417+
"tried to modify a static's initial value from another static's initializer",
415418
AssumptionNotHeld =>
416419
"`assume` argument was false",
417420
InlineAsm =>

src/librustc_mir/interpret/machine.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
8989
Default +
9090
Clone;
9191

92-
/// The memory kind to use for copied statics -- or None if those are not supported.
92+
/// The memory kind to use for copied statics -- or None if statics should not be mutated
93+
/// and thus any such attempt will cause a `ModifiedStatic` error to be raised.
9394
/// Statics are copied under two circumstances: When they are mutated, and when
9495
/// `static_with_default_tag` or `find_foreign_static` (see below) returns an owned allocation
9596
/// that is added to the memory so that the work is not done twice.

src/librustc_mir/interpret/memory.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,10 @@ impl<'a, 'mir, 'tcx, M: Machine<'a, 'mir, 'tcx>> Memory<'a, 'mir, 'tcx, M> {
397397
if alloc.mutability == Mutability::Immutable {
398398
return err!(ModifiedConstantMemory);
399399
}
400-
let kind = M::STATIC_KIND.expect(
401-
"An allocation is being mutated but the machine does not expect that to happen"
402-
);
403-
Ok((MemoryKind::Machine(kind), alloc.into_owned()))
400+
match M::STATIC_KIND {
401+
Some(kind) => Ok((MemoryKind::Machine(kind), alloc.into_owned())),
402+
None => err!(ModifiedStatic),
403+
}
404404
});
405405
// Unpack the error type manually because type inference doesn't
406406
// work otherwise (and we cannot help it because `impl Trait`)

src/librustc_mir/transform/const_prop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ impl<'a, 'mir, 'tcx> ConstPropagator<'a, 'mir, 'tcx> {
199199
| CalledClosureAsFunction
200200
| VtableForArgumentlessMethod
201201
| ModifiedConstantMemory
202+
| ModifiedStatic
202203
| AssumptionNotHeld
203204
// FIXME: should probably be removed and turned into a bug! call
204205
| TypeNotPrimitive(_)

src/librustc_mir/transform/qualify_consts.rs

+55-6
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,52 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
243243
return;
244244
}
245245

246-
match *dest {
247-
Place::Local(index) if (self.mir.local_kind(index) == LocalKind::Var ||
248-
self.mir.local_kind(index) == LocalKind::Arg) &&
249-
self.tcx.sess.features_untracked().const_let => {
250-
debug!("store to var {:?}", index);
251-
self.local_qualif[index] = Some(self.qualif);
246+
if self.tcx.features().const_let {
247+
let mut dest = dest;
248+
let index = loop {
249+
match dest {
250+
// with `const_let` active, we treat all locals equal
251+
Place::Local(index) => break *index,
252+
// projections are transparent for assignments
253+
// we qualify the entire destination at once, even if just a field would have
254+
// stricter qualification
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+
},
265+
Place::Promoted(..) => bug!("promoteds don't exist yet during promotion"),
266+
Place::Static(..) => {
267+
// Catch more errors in the destination. `visit_place` also checks that we
268+
// do not try to access statics from constants or try to mutate statics
269+
self.visit_place(
270+
dest,
271+
PlaceContext::MutatingUse(MutatingUseContext::Store),
272+
location
273+
);
274+
return;
275+
}
276+
}
277+
};
278+
debug!("store to var {:?}", index);
279+
match &mut self.local_qualif[index] {
280+
// this is overly restrictive, because even full assignments do not clear the qualif
281+
// While we could special case full assignments, this would be inconsistent with
282+
// aggregates where we overwrite all fields via assignments, which would not get
283+
// that feature.
284+
Some(ref mut qualif) => *qualif = *qualif | self.qualif,
285+
// insert new qualification
286+
qualif @ None => *qualif = Some(self.qualif),
252287
}
288+
return;
289+
}
290+
291+
match *dest {
253292
Place::Local(index) if self.mir.local_kind(index) == LocalKind::Temp ||
254293
self.mir.local_kind(index) == LocalKind::ReturnPointer => {
255294
debug!("store to {:?} (temp or return pointer)", index);
@@ -478,6 +517,16 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
478517

479518
// Only allow statics (not consts) to refer to other statics.
480519
if self.mode == Mode::Static || self.mode == Mode::StaticMut {
520+
if context.is_mutating_use() {
521+
// this is not strictly necessary as miri will also bail out
522+
// For interior mutability we can't really catch this statically as that
523+
// goes through raw pointers and intermediate temporaries, so miri has
524+
// to catch this anyway
525+
self.tcx.sess.span_err(
526+
self.span,
527+
"cannot mutate statics in the initializer of another static",
528+
);
529+
}
481530
return;
482531
}
483532
self.add(Qualif::NOT_CONST);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// New test for #53818: modifying static memory at compile-time is not allowed.
12+
// The test should never compile successfully
13+
14+
#![feature(const_raw_ptr_deref)]
15+
#![feature(const_let)]
16+
17+
use std::cell::UnsafeCell;
18+
19+
struct Foo(UnsafeCell<u32>);
20+
21+
unsafe impl Send for Foo {}
22+
unsafe impl Sync for Foo {}
23+
24+
static FOO: Foo = Foo(UnsafeCell::new(42));
25+
26+
static BAR: () = unsafe {
27+
*FOO.0.get() = 5; //~ ERROR could not evaluate static initializer
28+
};
29+
30+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0080]: could not evaluate static initializer
2+
--> $DIR/assign-to-static-within-other-static-2.rs:27:5
3+
|
4+
LL | *FOO.0.get() = 5; //~ ERROR could not evaluate static initializer
5+
| ^^^^^^^^^^^^^^^^ tried to modify a static's initial value from another static's initializer
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0080`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// New test for #53818: modifying static memory at compile-time is not allowed.
12+
// The test should never compile successfully
13+
14+
#![feature(const_raw_ptr_deref)]
15+
#![feature(const_let)]
16+
17+
use std::cell::UnsafeCell;
18+
19+
static mut FOO: u32 = 42;
20+
static BOO: () = unsafe {
21+
FOO = 5; //~ ERROR cannot mutate statics in the initializer of another static
22+
};
23+
24+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: cannot mutate statics in the initializer of another static
2+
--> $DIR/assign-to-static-within-other-static.rs:21:5
3+
|
4+
LL | FOO = 5; //~ ERROR cannot mutate statics in the initializer of another static
5+
| ^^^^^^^
6+
7+
error: aborting due to previous error
8+

src/test/ui/consts/const-eval/mod-static-with-const-fn.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
// New test for #53818: modifying static memory at compile-time is not allowed.
12-
// The test should never succeed.
12+
// The test should never compile successfully
1313

1414
#![feature(const_raw_ptr_deref)]
1515
#![feature(const_let)]
@@ -27,9 +27,9 @@ fn foo() {}
2727

2828
static BAR: () = unsafe {
2929
*FOO.0.get() = 5;
30-
//~^ ERROR statements in statics are unstable (see issue #48821)
31-
// This error is caused by a separate bug that the feature gate error is reported
32-
// even though the feature gate "const_let" is active.
30+
// we do not error on the above access, because that is not detectable statically. Instead,
31+
// const evaluation will error when trying to evaluate it. Due to the error below, we never even
32+
// attempt to const evaluate `BAR`, so we don't see the error
3333

3434
foo();
3535
//~^ ERROR calls in statics are limited to constant functions, tuple structs and tuple variants
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
error[E0658]: statements in statics are unstable (see issue #48821)
2-
--> $DIR/mod-static-with-const-fn.rs:29:5
3-
|
4-
LL | *FOO.0.get() = 5;
5-
| ^^^^^^^^^^^^^^^^
6-
|
7-
= help: add #![feature(const_let)] to the crate attributes to enable
8-
91
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
102
--> $DIR/mod-static-with-const-fn.rs:34:5
113
|
124
LL | foo();
135
| ^^^^^
146

15-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
168

17-
Some errors occurred: E0015, E0658.
18-
For more information about an error, try `rustc --explain E0015`.
9+
For more information about this error, try `rustc --explain E0015`.
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// compile-pass
2+
3+
#![feature(const_let)]
4+
5+
struct S(i32);
6+
7+
const A: () = {
8+
let mut s = S(0);
9+
s.0 = 1;
10+
};
11+
12+
fn main() {}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// compile-pass
2+
3+
#![feature(const_let)]
4+
#![feature(const_fn)]
5+
6+
pub struct AA {
7+
pub data: [u8; 10],
8+
}
9+
10+
impl AA {
11+
pub const fn new() -> Self {
12+
let mut res: AA = AA { data: [0; 10] };
13+
res.data[0] = 5;
14+
res
15+
}
16+
}
17+
18+
static mut BB: AA = AA::new();
19+
20+
fn main() {
21+
let ptr = unsafe { &mut BB };
22+
for a in ptr.data.iter() {
23+
println!("{}", a);
24+
}
25+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(const_let)]
2+
#![feature(const_fn)]
3+
4+
struct S {
5+
state: u32,
6+
}
7+
8+
impl S {
9+
const fn foo(&mut self, x: u32) {
10+
self.state = x;
11+
}
12+
}
13+
14+
const FOO: S = {
15+
let mut s = S { state: 42 };
16+
s.foo(3); //~ ERROR references in constants may only refer to immutable values
17+
s
18+
};
19+
20+
fn main() {
21+
assert_eq!(FOO.state, 3);
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0017]: references in constants may only refer to immutable values
2+
--> $DIR/const_let_assign3.rs:16:5
3+
|
4+
LL | s.foo(3); //~ ERROR references in constants may only refer to immutable values
5+
| ^ constants require immutable values
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0017`.

src/test/ui/consts/partial_qualif.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#![feature(const_let)]
2+
3+
use std::cell::Cell;
4+
5+
const FOO: &(Cell<usize>, bool) = {
6+
let mut a = (Cell::new(0), false);
7+
a.1 = true; // sets `qualif(a)` to `qualif(a) | qualif(true)`
8+
&{a} //~ ERROR cannot borrow a constant which may contain interior mutability
9+
};
10+
11+
fn main() {}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead
2+
--> $DIR/partial_qualif.rs:8:5
3+
|
4+
LL | &{a} //~ ERROR cannot borrow a constant which may contain interior mutability
5+
| ^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0492`.
+14
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() {}
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`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0597]: `y` does not live long enough
2+
--> $DIR/promote_const_let.rs:6:9
3+
|
4+
LL | let x: &'static u32 = {
5+
| ------------ type annotation requires that `y` is borrowed for `'static`
6+
LL | let y = 42;
7+
LL | &y //~ ERROR does not live long enough
8+
| ^^ borrowed value does not live long enough
9+
LL | };
10+
| - `y` dropped here while still borrowed
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0597`.

0 commit comments

Comments
 (0)