Skip to content

Commit 6250dfd

Browse files
authored
Unrolled build for rust-lang#130394
Rollup merge of rust-lang#130394 - RalfJung:mut-ref-to-immut, r=saethlin const: don't ICE when encountering a mutable ref to immutable memory Turns out that this can actually happen -- thanks to `@matthiaskrgr` for producing a testcase. :) Fixes rust-lang#130392
2 parents 04a3187 + 7dfffe7 commit 6250dfd

File tree

6 files changed

+37
-37
lines changed

6 files changed

+37
-37
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ use hir::def::DefKind;
1414
use rustc_ast::Mutability;
1515
use rustc_data_structures::fx::FxHashSet;
1616
use rustc_hir as hir;
17+
use rustc_middle::bug;
1718
use rustc_middle::mir::interpret::ValidationErrorKind::{self, *};
1819
use rustc_middle::mir::interpret::{
1920
alloc_range, ExpectedKind, InterpError, InvalidMetaKind, Misalignment, PointerKind, Provenance,
2021
UnsupportedOpInfo, ValidationErrorInfo,
2122
};
2223
use rustc_middle::ty::layout::{LayoutCx, LayoutOf, TyAndLayout};
2324
use rustc_middle::ty::{self, Ty, TyCtxt};
24-
use rustc_middle::{bug, span_bug};
2525
use rustc_span::symbol::{sym, Symbol};
2626
use rustc_target::abi::{
2727
Abi, FieldIdx, FieldsShape, Scalar as ScalarAbi, Size, VariantIdx, Variants, WrappingRange,
@@ -617,13 +617,7 @@ impl<'rt, 'tcx, M: Machine<'tcx>> ValidityVisitor<'rt, 'tcx, M> {
617617
if ptr_expected_mutbl == Mutability::Mut
618618
&& alloc_actual_mutbl == Mutability::Not
619619
{
620-
if !self.ecx.tcx.sess.opts.unstable_opts.unleash_the_miri_inside_of_you
621-
{
622-
span_bug!(
623-
self.ecx.tcx.span,
624-
"the static const safety checks accepted mutable references they should not have accepted"
625-
);
626-
}
620+
// This can actually occur with transmutes.
627621
throw_validation_failure!(self.path, MutableRefToImmutable);
628622
}
629623
// In a const, everything must be completely immutable.

tests/ui/consts/const-eval/transmute-const.64bit.stderr

-14
This file was deleted.

tests/ui/consts/const-eval/transmute-const.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@ stderr-per-bitwidth
21
use std::mem;
32

43
static FOO: bool = unsafe { mem::transmute(3u8) };

tests/ui/consts/const-eval/transmute-const.32bit.stderr tests/ui/consts/const-eval/transmute-const.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0080]: it is undefined behavior to use this value
2-
--> $DIR/transmute-const.rs:4:1
2+
--> $DIR/transmute-const.rs:3:1
33
|
44
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
55
| ^^^^^^^^^^^^^^^^ constructing invalid value: encountered 0x03, but expected a boolean

tests/ui/consts/const-mut-refs/mut_ref_in_final.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
//@ normalize-stderr-test: "(the raw bytes of the constant) \(size: [0-9]*, align: [0-9]*\)" -> "$1 (size: $$SIZE, align: $$ALIGN)"
2+
//@ normalize-stderr-test: "( 0x[0-9a-f][0-9a-f] │)? ([0-9a-f][0-9a-f] |__ |╾─*ALLOC[0-9]+(\+[a-z0-9]+)?(<imm>)?─*╼ )+ *│.*" -> " HEX_DUMP"
3+
//@ normalize-stderr-test: "HEX_DUMP\s*\n\s*HEX_DUMP" -> "HEX_DUMP"
4+
15
use std::cell::UnsafeCell;
6+
use std::mem;
27

38
const NULL: *mut i32 = std::ptr::null_mut();
49
const A: *const i32 = &4;
@@ -17,6 +22,11 @@ const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR temporary value dropped wh
1722
const fn helper(x: &mut i32) -> Option<&mut i32> { Some(x) }
1823
const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped while borrowed
1924

25+
// Not ok, since it points to read-only memory.
26+
const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
27+
//~^ ERROR undefined behavior to use this value
28+
//~| pointing to read-only memory
29+
2030
// Ok, because no references to mutable data exist here, since the `{}` moves
2131
// its value and then takes a reference to that.
2232
const C: *const i32 = &{
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0764]: mutable references are not allowed in the final value of constants
2-
--> $DIR/mut_ref_in_final.rs:9:21
2+
--> $DIR/mut_ref_in_final.rs:14:21
33
|
44
LL | const B: *mut i32 = &mut 4;
55
| ^^^^^^
66

77
error[E0716]: temporary value dropped while borrowed
8-
--> $DIR/mut_ref_in_final.rs:15:40
8+
--> $DIR/mut_ref_in_final.rs:20:40
99
|
1010
LL | const B3: Option<&mut i32> = Some(&mut 42);
1111
| ----------^^-
@@ -15,7 +15,7 @@ LL | const B3: Option<&mut i32> = Some(&mut 42);
1515
| using this value as a constant requires that borrow lasts for `'static`
1616

1717
error[E0716]: temporary value dropped while borrowed
18-
--> $DIR/mut_ref_in_final.rs:18:42
18+
--> $DIR/mut_ref_in_final.rs:23:42
1919
|
2020
LL | const B4: Option<&mut i32> = helper(&mut 42);
2121
| ------------^^-
@@ -24,8 +24,19 @@ LL | const B4: Option<&mut i32> = helper(&mut 42);
2424
| | creates a temporary value which is freed while still in use
2525
| using this value as a constant requires that borrow lasts for `'static`
2626

27+
error[E0080]: it is undefined behavior to use this value
28+
--> $DIR/mut_ref_in_final.rs:26:1
29+
|
30+
LL | const IMMUT_MUT_REF: &mut u16 = unsafe { mem::transmute(&13) };
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value: encountered mutable reference or box pointing to read-only memory
32+
|
33+
= 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.
34+
= note: the raw bytes of the constant (size: $SIZE, align: $ALIGN) {
35+
HEX_DUMP
36+
}
37+
2738
error[E0716]: temporary value dropped while borrowed
28-
--> $DIR/mut_ref_in_final.rs:40:65
39+
--> $DIR/mut_ref_in_final.rs:50:65
2940
|
3041
LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
3142
| -------------------------------^^--
@@ -35,7 +46,7 @@ LL | const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
3546
| using this value as a constant requires that borrow lasts for `'static`
3647

3748
error[E0716]: temporary value dropped while borrowed
38-
--> $DIR/mut_ref_in_final.rs:43:67
49+
--> $DIR/mut_ref_in_final.rs:53:67
3950
|
4051
LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4152
| -------------------------------^^--
@@ -45,7 +56,7 @@ LL | static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
4556
| using this value as a static requires that borrow lasts for `'static`
4657

4758
error[E0716]: temporary value dropped while borrowed
48-
--> $DIR/mut_ref_in_final.rs:46:71
59+
--> $DIR/mut_ref_in_final.rs:56:71
4960
|
5061
LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
5162
| -------------------------------^^--
@@ -55,30 +66,30 @@ LL | static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42));
5566
| using this value as a static requires that borrow lasts for `'static`
5667

5768
error[E0764]: mutable references are not allowed in the final value of statics
58-
--> $DIR/mut_ref_in_final.rs:59:53
69+
--> $DIR/mut_ref_in_final.rs:69:53
5970
|
6071
LL | static RAW_MUT_CAST_S: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
6172
| ^^^^^^^
6273

6374
error[E0764]: mutable references are not allowed in the final value of statics
64-
--> $DIR/mut_ref_in_final.rs:61:54
75+
--> $DIR/mut_ref_in_final.rs:71:54
6576
|
6677
LL | static RAW_MUT_COERCE_S: SyncPtr<i32> = SyncPtr { x: &mut 0 };
6778
| ^^^^^^
6879

6980
error[E0764]: mutable references are not allowed in the final value of constants
70-
--> $DIR/mut_ref_in_final.rs:63:52
81+
--> $DIR/mut_ref_in_final.rs:73:52
7182
|
7283
LL | const RAW_MUT_CAST_C: SyncPtr<i32> = SyncPtr { x : &mut 42 as *mut _ as *const _ };
7384
| ^^^^^^^
7485

7586
error[E0764]: mutable references are not allowed in the final value of constants
76-
--> $DIR/mut_ref_in_final.rs:65:53
87+
--> $DIR/mut_ref_in_final.rs:75:53
7788
|
7889
LL | const RAW_MUT_COERCE_C: SyncPtr<i32> = SyncPtr { x: &mut 0 };
7990
| ^^^^^^
8091

81-
error: aborting due to 10 previous errors
92+
error: aborting due to 11 previous errors
8293

83-
Some errors have detailed explanations: E0716, E0764.
84-
For more information about an error, try `rustc --explain E0716`.
94+
Some errors have detailed explanations: E0080, E0716, E0764.
95+
For more information about an error, try `rustc --explain E0080`.

0 commit comments

Comments
 (0)