Skip to content

Commit 2a08220

Browse files
committed
Fix PlaceElem::Subslice length computation
1 parent 0080f55 commit 2a08220

File tree

3 files changed

+104
-1
lines changed

3 files changed

+104
-1
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Based on https://github.com/rust-lang/rust/blob/c5840f9d252c2f5cc16698dbf385a29c5de3ca07/src/test/ui/array-slice-vec/subslice-patterns-const-eval-match.rs
2+
3+
// Test that array subslice patterns are correctly handled in const evaluation.
4+
5+
// run-pass
6+
7+
#![feature(slice_patterns)]
8+
9+
#[derive(PartialEq, Debug, Clone)]
10+
struct N(u8);
11+
12+
#[derive(PartialEq, Debug, Clone)]
13+
struct Z;
14+
15+
macro_rules! n {
16+
($($e:expr),* $(,)?) => {
17+
[$(N($e)),*]
18+
}
19+
}
20+
21+
// This macro has an unused variable so that it can be repeated base on the
22+
// number of times a repeated variable (`$e` in `z`) occurs.
23+
macro_rules! zed {
24+
($e:expr) => { Z }
25+
}
26+
27+
macro_rules! z {
28+
($($e:expr),* $(,)?) => {
29+
[$(zed!($e)),*]
30+
}
31+
}
32+
33+
// Compare constant evaluation and runtime evaluation of a given expression.
34+
macro_rules! compare_evaluation {
35+
($e:expr, $t:ty $(,)?) => {{
36+
const CONST_EVAL: $t = $e;
37+
const fn const_eval() -> $t { $e }
38+
static CONST_EVAL2: $t = const_eval();
39+
let runtime_eval = $e;
40+
assert_eq!(CONST_EVAL, runtime_eval);
41+
assert_eq!(CONST_EVAL2, runtime_eval);
42+
}}
43+
}
44+
45+
// Repeat `$test`, substituting the given macro variables with the given
46+
// identifiers.
47+
//
48+
// For example:
49+
//
50+
// repeat! {
51+
// ($name); X; Y:
52+
// struct $name;
53+
// }
54+
//
55+
// Expands to:
56+
//
57+
// struct X; struct Y;
58+
//
59+
// This is used to repeat the tests using both the `N` and `Z`
60+
// types.
61+
macro_rules! repeat {
62+
(($($dollar:tt $placeholder:ident)*); $($($values:ident),+);*: $($test:tt)*) => {
63+
macro_rules! single {
64+
($($dollar $placeholder:ident),*) => { $($test)* }
65+
}
66+
$(single!($($values),+);)*
67+
}
68+
}
69+
70+
fn main() {
71+
repeat! {
72+
($arr $Ty); n, N; z, Z:
73+
compare_evaluation!({ let [_, x @ .., _] = $arr!(1, 2, 3, 4); x }, [$Ty; 2]);
74+
compare_evaluation!({ let [_, ref x @ .., _] = $arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
75+
compare_evaluation!({ let [_, x @ .., _] = &$arr!(1, 2, 3, 4); x }, &'static [$Ty; 2]);
76+
77+
compare_evaluation!({ let [_, _, x @ .., _, _] = $arr!(1, 2, 3, 4); x }, [$Ty; 0]);
78+
compare_evaluation!(
79+
{ let [_, _, ref x @ .., _, _] = $arr!(1, 2, 3, 4); x },
80+
&'static [$Ty; 0],
81+
);
82+
compare_evaluation!(
83+
{ let [_, _, x @ .., _, _] = &$arr!(1, 2, 3, 4); x },
84+
&'static [$Ty; 0],
85+
);
86+
87+
compare_evaluation!({ let [_, .., x] = $arr!(1, 2, 3, 4); x }, $Ty);
88+
compare_evaluation!({ let [_, .., ref x] = $arr!(1, 2, 3, 4); x }, &'static $Ty);
89+
compare_evaluation!({ let [_, _y @ .., x] = &$arr!(1, 2, 3, 4); x }, &'static $Ty);
90+
}
91+
92+
compare_evaluation!({ let [_, .., N(x)] = n!(1, 2, 3, 4); x }, u8);
93+
compare_evaluation!({ let [_, .., N(ref x)] = n!(1, 2, 3, 4); x }, &'static u8);
94+
compare_evaluation!({ let [_, .., N(x)] = &n!(1, 2, 3, 4); x }, &'static u8);
95+
96+
compare_evaluation!({ let [N(x), .., _] = n!(1, 2, 3, 4); x }, u8);
97+
compare_evaluation!({ let [N(ref x), .., _] = n!(1, 2, 3, 4); x }, &'static u8);
98+
compare_evaluation!({ let [N(x), .., _] = &n!(1, 2, 3, 4); x }, &'static u8);
99+
}

src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -688,7 +688,7 @@ pub fn trans_place<'tcx>(
688688
.eval_usize(fx.tcx, ParamEnv::reveal_all());
689689
cplace = CPlace::for_ptr(
690690
ptr.offset_i64(fx, elem_layout.size.bytes() as i64 * from as i64),
691-
fx.layout_of(fx.tcx.mk_array(elem_ty, len - from as u64 - to as u64)),
691+
fx.layout_of(fx.tcx.mk_array(elem_ty, to as u64 - from as u64)),
692692
);
693693
}
694694
ty::Slice(elem_ty) => {

test.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ echo "[AOT] std_example"
6060
$RUSTC example/std_example.rs --crate-type bin
6161
./target/out/std_example
6262

63+
echo "[AOT] subslice-patterns-const-eval"
64+
$RUSTC example/subslice-patterns-const-eval.rs --crate-type bin -Cpanic=abort
65+
./target/out/subslice-patterns-const-eval
66+
6367
echo "[BUILD] mod_bench"
6468
$RUSTC example/mod_bench.rs --crate-type bin
6569

0 commit comments

Comments
 (0)