Skip to content

Commit

Permalink
Rollup merge of #67546 - oli-obk:slice_pattern_ice, r=varkor
Browse files Browse the repository at this point in the history
Fix ICE in mir interpretation

Indices from the end start at 1 so you can immediately subtract them from the length to get the index instead of having to do an additional `-1`. Kinda documented in https://doc.rust-lang.org/nightly/nightly-rustc/rustc/mir/enum.ProjectionElem.html#variant.ConstantIndex
  • Loading branch information
Centril authored Dec 23, 2019
2 parents 286cb14 + 5b8df34 commit 4a551ff
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,12 @@ where
// This can only be reached in ConstProp and non-rustc-MIR.
throw_ub!(BoundsCheckFailed { len: min_length as u64, index: n as u64 });
}
assert!(offset < min_length);

let index = if from_end {
assert!(0 < offset && offset - 1 < min_length);
n - u64::from(offset)
} else {
assert!(offset < min_length);
u64::from(offset)
};

Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/consts/const_prop_slice_pat_ice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// check-pass
#![feature(slice_patterns)]

fn main() {
match &[0, 1] as &[i32] {
[a @ .., x] => {}
&[] => {}
}
}

0 comments on commit 4a551ff

Please sign in to comment.