Skip to content

Commit be49206

Browse files
committed
Auto merge of #113166 - moulins:ref-niches-initial, r=oli-obk
Prototype: Add unstable `-Z reference-niches` option MCP: rust-lang/compiler-team#641 Relevant RFC: rust-lang/rfcs#3204 This prototype adds a new `-Z reference-niches` option, controlling the range of valid bit-patterns for reference types (`&T` and `&mut T`), thereby enabling new enum niching opportunities. Like `-Z randomize-layout`, this setting is crate-local; as such, references to built-in types (primitives, tuples, ...) are not affected. The possible settings are (here, `MAX` denotes the all-1 bit-pattern): | `-Z reference-niches=` | Valid range | |:---:|:---:| | `null` (the default) | `1..=MAX` | | `size` | `1..=(MAX- size)` | | `align` | `align..=MAX.align_down_to(align)` | | `size,align` | `align..=(MAX-size).align_down_to(align)` | ------ This is very WIP, and I'm not sure the approach I've taken here is the best one, but stage 1 tests pass locally; I believe this is in a good enough state to unleash this upon unsuspecting 3rd-party code, and see what breaks.
2 parents 4478090 + 9e0697f commit be49206

File tree

5 files changed

+12
-9
lines changed

5 files changed

+12
-9
lines changed

src/intptrcast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl<'mir, 'tcx> GlobalStateInner {
207207
.checked_add(max(size.bytes(), 1))
208208
.ok_or_else(|| err_exhaust!(AddressSpaceFull))?;
209209
// Even if `Size` didn't overflow, we might still have filled up the address space.
210-
if global_state.next_base_addr > ecx.target_usize_max() {
210+
if global_state.next_base_addr > ecx.data_layout().target_usize_max() {
211211
throw_exhaust!(AddressSpaceFull);
212212
}
213213
// Given that `next_base_addr` increases in each allocation, pushing the

src/shims/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use log::trace;
2121

2222
use rustc_middle::{mir, ty};
2323
use rustc_target::spec::abi::Abi;
24+
use rustc_target::abi::HasDataLayout as _;
2425

2526
use crate::*;
2627
use helpers::check_arg_count;
@@ -108,7 +109,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
108109
}
109110

110111
// Return error result (usize::MAX), and jump to caller.
111-
this.write_scalar(Scalar::from_target_usize(this.target_usize_max(), this), dest)?;
112+
let usize_max = this.data_layout().target_usize_max();
113+
this.write_scalar(Scalar::from_target_usize(usize_max, this), dest)?;
112114
this.go_to_block(ret);
113115
Ok(true)
114116
}

src/shims/unix/fs.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use log::trace;
1212

1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_middle::ty::TyCtxt;
15-
use rustc_target::abi::{Align, Size};
15+
use rustc_target::abi::{Align, Size, HasDataLayout as _};
1616

1717
use crate::shims::os_str::bytes_to_os_str;
1818
use crate::*;
@@ -753,7 +753,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
753753
// We cap the number of read bytes to the largest value that we are able to fit in both the
754754
// host's and target's `isize`. This saves us from having to handle overflows later.
755755
let count = count
756-
.min(u64::try_from(this.target_isize_max()).unwrap())
756+
.min(u64::try_from(this.data_layout().target_isize_max()).unwrap())
757757
.min(u64::try_from(isize::MAX).unwrap());
758758
let communicate = this.machine.communicate();
759759

@@ -807,7 +807,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
807807
// We cap the number of written bytes to the largest value that we are able to fit in both the
808808
// host's and target's `isize`. This saves us from having to handle overflows later.
809809
let count = count
810-
.min(u64::try_from(this.target_isize_max()).unwrap())
810+
.min(u64::try_from(this.data_layout().target_isize_max()).unwrap())
811811
.min(u64::try_from(isize::MAX).unwrap());
812812
let communicate = this.machine.communicate();
813813

tests/fail/layout_cycle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//@error-in-other-file: a cycle occurred during layout computation
2-
//~^ ERROR: cycle detected when computing layout of
2+
//~^ ERROR: cycle detected when computing layout (naive) of
33

44
use std::mem;
55

tests/fail/layout_cycle.stderr

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
error[E0391]: cycle detected when computing layout of `S<S<()>>`
1+
error[E0391]: cycle detected when computing layout (naive) of `S<S<()>>`
22
|
3-
= note: ...which requires computing layout of `<S<()> as Tr>::I`...
4-
= note: ...which again requires computing layout of `S<S<()>>`, completing the cycle
3+
= note: ...which requires computing layout (naive) of `<S<()> as Tr>::I`...
4+
= note: ...which again requires computing layout (naive) of `S<S<()>>`, completing the cycle
5+
= note: cycle used when computing layout of `S<S<()>>`
56
= note: see https://rustc-dev-guide.rust-lang.org/overview.html#queries and https://rustc-dev-guide.rust-lang.org/query.html for more information
67

78
error: post-monomorphization error: a cycle occurred during layout computation

0 commit comments

Comments
 (0)