Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion compiler/rustc_const_eval/src/check_consts/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,10 @@ impl<C> DebugWithContext<C> for State {

impl JoinSemiLattice for State {
fn join(&mut self, other: &Self) -> bool {
self.qualif.join(&other.qualif) || self.borrow.join(&other.borrow)
// `join` has a side-effect on `self` so make sure we always call it on both fields.
let a = self.qualif.join(&other.qualif);
let b = self.borrow.join(&other.borrow);
a | b
}
}

Expand Down
1 change: 1 addition & 0 deletions library/alloctests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#![feature(const_btree_len)]
#![feature(const_cmp)]
#![feature(const_heap)]
#![feature(const_precise_live_drops)]
#![feature(const_trait_impl)]
#![feature(core_intrinsics)]
#![feature(core_io_borrowed_buf)]
Expand Down
1 change: 1 addition & 0 deletions library/core/src/mem/maybe_dangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ impl<P: ?Sized> MaybeDangling<P> {
/// Extracts the value from the `MaybeDangling` container.
///
/// Note that this is UB if the inner value is currently dangling.
#[rustc_allow_const_fn_unstable(const_precise_live_drops)]
pub const fn into_inner(self) -> P
where
P: Sized,
Expand Down
1 change: 1 addition & 0 deletions library/coretests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#![feature(const_iter)]
#![feature(const_ops)]
#![feature(const_option_ops)]
#![feature(const_precise_live_drops)]
#![feature(const_ref_cell)]
#![feature(const_result_trait_fn)]
#![feature(const_select_unpredictable)]
Expand Down
22 changes: 22 additions & 0 deletions tests/ui/consts/qualif-indirect-mutation-fail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,25 @@ pub const fn address_of_const() {
let y: Option<NotConstDestruct> = None; //~ ERROR destructor of
std::ptr::addr_of!(y);
}

// Regression test for an incorrect implementation of state join.
pub const fn regression_test_for_incorrect_join() {
let mut a = Some(NotConstDestruct); //~ ERROR destructor of
let b = NotConstDestruct;
let p = &raw mut a;
let x = unsafe { std::ptr::read(p) };
std::mem::forget(a);
std::mem::forget(x);
a = None;
unsafe { std::ptr::write(p, Some(b)) };
}

// Use after move cannot sneak past checks.
// NB: Semantics of MIR move elimination might make it possible to accept this.
pub const fn use_after_move() {
let mut a = None; //~ ERROR destructor of
let p = &raw mut a;
std::mem::forget(a);
a = None;
unsafe { std::ptr::write(p, Some(NotConstDestruct)) };
}
Comment thread
tmiasko marked this conversation as resolved.
20 changes: 19 additions & 1 deletion tests/ui/consts/qualif-indirect-mutation-fail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ LL | let x: Option<NotConstDestruct> = None;
LL | }
| - value is dropped here

error: aborting due to 9 previous errors
error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:75:9
|
LL | let mut a = Some(NotConstDestruct);
| ^^^^^ the destructor for this type cannot be evaluated in constant functions
...
LL | }
| - value is dropped here

error[E0493]: destructor of `Option<NotConstDestruct>` cannot be evaluated at compile-time
--> $DIR/qualif-indirect-mutation-fail.rs:88:9
|
LL | let mut a = None;
| ^^^^^ the destructor for this type cannot be evaluated in constant functions
...
LL | }
| - value is dropped here

error: aborting due to 11 previous errors

For more information about this error, try `rustc --explain E0493`.