Skip to content

Commit 610ea80

Browse files
authored
Rollup merge of rust-lang#68219 - oli-obk:fix_miri, r=RalfJung,wesleywiser
Untangle ZST validation from integer validation and generalize it to all zsts cc @RalfJung r? @wesleywiser
2 parents 20c49fc + 0e14b9f commit 610ea80

File tree

4 files changed

+37
-15
lines changed

4 files changed

+37
-15
lines changed

src/librustc_mir/interpret/validity.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -587,12 +587,6 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
587587
// padding.
588588
match tys.kind {
589589
ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
590-
ty::Tuple(tys) if tys.len() == 0 => true,
591-
ty::Adt(adt_def, _)
592-
if adt_def.is_struct() && adt_def.all_fields().next().is_none() =>
593-
{
594-
true
595-
}
596590
_ => false,
597591
}
598592
} =>
@@ -609,11 +603,6 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
609603
}
610604
// This is the element type size.
611605
let layout = self.ecx.layout_of(tys)?;
612-
// Empty tuples and fieldless structs (the only ZSTs that allow reaching this code)
613-
// have no data to be checked.
614-
if layout.is_zst() {
615-
return Ok(());
616-
}
617606
// This is the size in bytes of the whole array.
618607
let size = layout.size * len;
619608
// Size is not 0, get a pointer.
@@ -656,6 +645,13 @@ impl<'rt, 'mir, 'tcx, M: Machine<'mir, 'tcx>> ValueVisitor<'mir, 'tcx, M>
656645
}
657646
}
658647
}
648+
// Fast path for arrays and slices of ZSTs. We only need to check a single ZST element
649+
// of an array and not all of them, because there's only a single value of a specific
650+
// ZST type, so either validation fails for all elements or none.
651+
ty::Array(tys, ..) | ty::Slice(tys) if self.ecx.layout_of(tys)?.is_zst() => {
652+
// Validate just the first element
653+
self.walk_aggregate(op, fields.take(1))?
654+
}
659655
_ => {
660656
self.walk_aggregate(op, fields)? // default handler
661657
}

src/test/ui/consts/huge-values.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
// build-pass
22
// ignore-32bit
33

4+
// This test is a canary test that will essentially not compile in a reasonable time frame
5+
// (so it'll take hours) if any of the optimizations regress. With the optimizations, these compile
6+
// in milliseconds just as if the length were set to `1`.
7+
48
#[derive(Clone, Copy)]
59
struct Foo;
610

711
fn main() {
812
let _ = [(); 4_000_000_000];
913
let _ = [0u8; 4_000_000_000];
1014
let _ = [Foo; 4_000_000_000];
15+
let _ = [(Foo, (), Foo, ((), Foo, [0; 0])); 4_000_000_000];
16+
let _ = [[0; 0]; 4_000_000_000];
1117
}
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![feature(const_raw_ptr_deref, never_type)]
22

3-
const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
3+
const _: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
4+
const _: &[!; 0] = unsafe { &*(1_usize as *const [!; 0]) }; // ok
5+
const _: &[!] = unsafe { &*(1_usize as *const [!; 0]) }; // ok
6+
const _: &[!] = unsafe { &*(1_usize as *const [!; 1]) }; //~ ERROR undefined behavior
7+
const _: &[!] = unsafe { &*(1_usize as *const [!; 42]) }; //~ ERROR undefined behavior
48

59
fn main() {}
Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,27 @@
11
error[E0080]: it is undefined behavior to use this value
22
--> $DIR/validate_never_arrays.rs:3:1
33
|
4-
LL | const FOO: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
4+
LL | const _: &[!; 1] = unsafe { &*(1_usize as *const [!; 1]) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
66
|
77
= 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.
88

9-
error: aborting due to previous error
9+
error[E0080]: it is undefined behavior to use this value
10+
--> $DIR/validate_never_arrays.rs:6:1
11+
|
12+
LL | const _: &[!] = unsafe { &*(1_usize as *const [!; 1]) };
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>[0]
14+
|
15+
= 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.
16+
17+
error[E0080]: it is undefined behavior to use this value
18+
--> $DIR/validate_never_arrays.rs:7:1
19+
|
20+
LL | const _: &[!] = unsafe { &*(1_usize as *const [!; 42]) };
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>[0]
22+
|
23+
= 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.
24+
25+
error: aborting due to 3 previous errors
1026

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

0 commit comments

Comments
 (0)