Skip to content

Commit 341170d

Browse files
committed
move &mut-in-const check from interning to validation
1 parent 849c929 commit 341170d

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

compiler/rustc_mir/src/interpret/intern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,13 +263,13 @@ impl<'rt, 'mir, 'tcx: 'mir, M: CompileTimeMachine<'mir, 'tcx>> ValueVisitor<'mir
263263
// This helps to prevent users from accidentally exploiting UB that they
264264
// caused (by somehow getting a mutable reference in a `const`).
265265
if ref_mutability == Mutability::Mut {
266-
match referenced_ty.kind() {
266+
/*match referenced_ty.kind() {
267267
ty::Array(_, n) if n.eval_usize(*tcx, self.ecx.param_env) == 0 => {}
268268
ty::Slice(_)
269269
if mplace.meta.unwrap_meta().to_machine_usize(self.ecx)?
270270
== 0 => {}
271271
_ => mutable_memory_in_const(tcx, "`&mut`"),
272-
}
272+
}*/
273273
} else {
274274
// A shared reference. We cannot check `freeze` here due to references
275275
// like `&dyn Trait` that are actually immutable. We do check for

compiler/rustc_mir/src/interpret/validity.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,15 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
540540
}
541541
Ok(true)
542542
}
543-
ty::Ref(..) => {
543+
ty::Ref(_, ty, mutbl) => {
544+
if matches!(self.ctfe_mode, Some(CtfeValidationMode::Const { .. })) && *mutbl == hir::Mutability::Mut {
545+
// A mutable reference inside a const? That does not seem right (except of it is
546+
// a ZST).
547+
let layout = self.ecx.layout_of(ty)?;
548+
if !layout.is_zst() {
549+
throw_validation_failure!(self.path, { "mutable reference in a `const`" });
550+
}
551+
}
544552
self.check_safe_pointer(value, "reference")?;
545553
Ok(true)
546554
}

src/test/ui/consts/miri_unleashed/mutable_references_err.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
2828

2929
// Make sure we also catch mutable references.
3030
const BLUNT: &mut i32 = &mut 42;
31-
//~^ ERROR: mutable memory (`&mut`) is not allowed in constant
31+
//~^ ERROR: it is undefined behavior to use this value
3232

3333
fn main() {
3434
unsafe {

src/test/ui/consts/miri_unleashed/mutable_references_err.stderr

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ LL | const SNEAKY: &dyn Sync = &Synced { x: UnsafeCell::new(42) };
1616
|
1717
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
1818

19-
error: mutable memory (`&mut`) is not allowed in constant
19+
error[E0080]: it is undefined behavior to use this value
2020
--> $DIR/mutable_references_err.rs:30:1
2121
|
2222
LL | const BLUNT: &mut i32 = &mut 42;
23-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered mutable reference in a `const`
24+
|
25+
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
2426

2527
warning: skipping const checks
2628
|

0 commit comments

Comments
 (0)