Skip to content

Commit 21cb46a

Browse files
committed
Auto merge of #55922 - oli-obk:slice_pat_ice, r=zackmdavis
Fix ICE in `const` slice patterns fixes #55911 based on #55915 New commits start at eabc1551e0d0953f1951020afb5919ab4c129cf5
2 parents d8046ed + 8f9a093 commit 21cb46a

File tree

5 files changed

+132
-66
lines changed

5 files changed

+132
-66
lines changed

src/librustc_mir/hair/pattern/_match.rs

+58-64
Original file line numberDiff line numberDiff line change
@@ -309,13 +309,15 @@ pub struct MatchCheckCtxt<'a, 'tcx: 'a> {
309309
/// outside it's module and should not be matchable with an empty match
310310
/// statement.
311311
pub module: DefId,
312+
param_env: ty::ParamEnv<'tcx>,
312313
pub pattern_arena: &'a TypedArena<Pattern<'tcx>>,
313314
pub byte_array_map: FxHashMap<*const Pattern<'tcx>, Vec<&'a Pattern<'tcx>>>,
314315
}
315316

316317
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
317318
pub fn create_and_enter<F, R>(
318319
tcx: TyCtxt<'a, 'tcx, 'tcx>,
320+
param_env: ty::ParamEnv<'tcx>,
319321
module: DefId,
320322
f: F) -> R
321323
where F: for<'b> FnOnce(MatchCheckCtxt<'b, 'tcx>) -> R
@@ -324,53 +326,13 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
324326

325327
f(MatchCheckCtxt {
326328
tcx,
329+
param_env,
327330
module,
328331
pattern_arena: &pattern_arena,
329332
byte_array_map: FxHashMap::default(),
330333
})
331334
}
332335

333-
// convert a byte-string pattern to a list of u8 patterns.
334-
fn lower_byte_str_pattern<'p>(&mut self, pat: &'p Pattern<'tcx>) -> Vec<&'p Pattern<'tcx>>
335-
where 'a: 'p
336-
{
337-
let pattern_arena = &*self.pattern_arena;
338-
let tcx = self.tcx;
339-
self.byte_array_map.entry(pat).or_insert_with(|| {
340-
match pat.kind {
341-
box PatternKind::Constant {
342-
value: const_val
343-
} => {
344-
if let Some(ptr) = const_val.to_ptr() {
345-
let is_array_ptr = const_val.ty
346-
.builtin_deref(true)
347-
.and_then(|t| t.ty.builtin_index())
348-
.map_or(false, |t| t == tcx.types.u8);
349-
assert!(is_array_ptr);
350-
let alloc = tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
351-
assert_eq!(ptr.offset.bytes(), 0);
352-
// FIXME: check length
353-
alloc.bytes.iter().map(|b| {
354-
&*pattern_arena.alloc(Pattern {
355-
ty: tcx.types.u8,
356-
span: pat.span,
357-
kind: box PatternKind::Constant {
358-
value: ty::Const::from_bits(
359-
tcx,
360-
*b as u128,
361-
ty::ParamEnv::empty().and(tcx.types.u8))
362-
}
363-
})
364-
}).collect()
365-
} else {
366-
bug!("not a byte str: {:?}", const_val)
367-
}
368-
}
369-
_ => span_bug!(pat.span, "unexpected byte array pattern {:?}", pat)
370-
}
371-
}).clone()
372-
}
373-
374336
fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
375337
if self.tcx.features().exhaustive_patterns {
376338
self.tcx.is_ty_uninhabited_from(self.module, ty)
@@ -1393,11 +1355,6 @@ fn slice_pat_covered_by_constructor<'tcx>(
13931355
ConstValue::Scalar(val) | ConstValue::ScalarPair(val, _) => val,
13941356
};
13951357
if let Ok(ptr) = val.to_ptr() {
1396-
let is_array_ptr = const_val.ty
1397-
.builtin_deref(true)
1398-
.and_then(|t| t.ty.builtin_index())
1399-
.map_or(false, |t| t == tcx.types.u8);
1400-
assert!(is_array_ptr);
14011358
tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id).bytes.as_ref()
14021359
} else {
14031360
bug!("unexpected non-ptr ConstantValue")
@@ -1705,26 +1662,63 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
17051662
PatternKind::Constant { value } => {
17061663
match *constructor {
17071664
Slice(..) => {
1708-
if let Some(ptr) = value.to_ptr() {
1709-
let is_array_ptr = value.ty
1710-
.builtin_deref(true)
1711-
.and_then(|t| t.ty.builtin_index())
1712-
.map_or(false, |t| t == cx.tcx.types.u8);
1713-
assert!(is_array_ptr);
1714-
let data_len = cx.tcx
1715-
.alloc_map
1716-
.lock()
1717-
.unwrap_memory(ptr.alloc_id)
1718-
.bytes
1719-
.len();
1720-
if wild_patterns.len() == data_len {
1721-
Some(cx.lower_byte_str_pattern(pat))
1722-
} else {
1723-
None
1665+
// we extract an `Option` for the pointer because slices of zero elements don't
1666+
// necessarily point to memory, they are usually just integers. The only time
1667+
// they should be pointing to memory is when they are subslices of nonzero
1668+
// slices
1669+
let (opt_ptr, n, ty) = match value.ty.builtin_deref(false).unwrap().ty.sty {
1670+
ty::TyKind::Array(t, n) => (value.to_ptr(), n.unwrap_usize(cx.tcx), t),
1671+
ty::TyKind::Slice(t) => {
1672+
match value.val {
1673+
ConstValue::ScalarPair(ptr, n) => (
1674+
ptr.to_ptr().ok(),
1675+
n.to_bits(cx.tcx.data_layout.pointer_size).unwrap() as u64,
1676+
t,
1677+
),
1678+
_ => span_bug!(
1679+
pat.span,
1680+
"slice pattern constant must be scalar pair but is {:?}",
1681+
value,
1682+
),
1683+
}
1684+
},
1685+
_ => span_bug!(
1686+
pat.span,
1687+
"unexpected const-val {:?} with ctor {:?}",
1688+
value,
1689+
constructor,
1690+
),
1691+
};
1692+
if wild_patterns.len() as u64 == n {
1693+
// convert a constant slice/array pattern to a list of patterns.
1694+
match (n, opt_ptr) {
1695+
(0, _) => Some(Vec::new()),
1696+
(_, Some(ptr)) => {
1697+
let alloc = cx.tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id);
1698+
let layout = cx.tcx.layout_of(cx.param_env.and(ty)).ok()?;
1699+
(0..n).map(|i| {
1700+
let ptr = ptr.offset(layout.size * i, &cx.tcx).ok()?;
1701+
let scalar = alloc.read_scalar(
1702+
&cx.tcx, ptr, layout.size,
1703+
).ok()?;
1704+
let scalar = scalar.not_undef().ok()?;
1705+
let value = ty::Const::from_scalar(cx.tcx, scalar, ty);
1706+
let pattern = Pattern {
1707+
ty,
1708+
span: pat.span,
1709+
kind: box PatternKind::Constant { value },
1710+
};
1711+
Some(&*cx.pattern_arena.alloc(pattern))
1712+
}).collect()
1713+
},
1714+
(_, None) => span_bug!(
1715+
pat.span,
1716+
"non zero length slice with const-val {:?}",
1717+
value,
1718+
),
17241719
}
17251720
} else {
1726-
span_bug!(pat.span,
1727-
"unexpected const-val {:?} with ctor {:?}", value, constructor)
1721+
None
17281722
}
17291723
}
17301724
_ => {

src/librustc_mir/hair/pattern/check_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
193193
}
194194

195195
let module = self.tcx.hir.get_module_parent(scrut.id);
196-
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {
196+
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |ref mut cx| {
197197
let mut have_errors = false;
198198

199199
let inlined_arms : Vec<(Vec<_>, _)> = arms.iter().map(|arm| (
@@ -268,7 +268,7 @@ impl<'a, 'tcx> MatchVisitor<'a, 'tcx> {
268268

269269
fn check_irrefutable(&self, pat: &'tcx Pat, origin: &str) {
270270
let module = self.tcx.hir.get_module_parent(pat.id);
271-
MatchCheckCtxt::create_and_enter(self.tcx, module, |ref mut cx| {
271+
MatchCheckCtxt::create_and_enter(self.tcx, self.param_env, module, |ref mut cx| {
272272
let mut patcx = PatternContext::new(self.tcx,
273273
self.param_env.and(self.identity_substs),
274274
self.tables);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-pass
2+
3+
fn main() {
4+
let s = &[0x00; 4][..]; //Slice of any value
5+
const MAGIC_TEST: &[u32] = &[4, 5, 6, 7]; //Const slice to pattern match with
6+
match s {
7+
MAGIC_TEST => (),
8+
[0x00, 0x00, 0x00, 0x00] => (),
9+
[4, 5, 6, 7] => (), // this should warn
10+
_ => (),
11+
}
12+
match s {
13+
[0x00, 0x00, 0x00, 0x00] => (),
14+
MAGIC_TEST => (),
15+
[4, 5, 6, 7] => (), // this should warn
16+
_ => (),
17+
}
18+
match s {
19+
[0x00, 0x00, 0x00, 0x00] => (),
20+
[4, 5, 6, 7] => (),
21+
MAGIC_TEST => (), // this should warn
22+
_ => (),
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-pass
2+
3+
fn main() {
4+
let s = &["0x00"; 4][..]; //Slice of any value
5+
const MAGIC_TEST: &[&str] = &["4", "5", "6", "7"]; //Const slice to pattern match with
6+
match s {
7+
MAGIC_TEST => (),
8+
["0x00", "0x00", "0x00", "0x00"] => (),
9+
["4", "5", "6", "7"] => (), // this should warn
10+
_ => (),
11+
}
12+
match s {
13+
["0x00", "0x00", "0x00", "0x00"] => (),
14+
MAGIC_TEST => (),
15+
["4", "5", "6", "7"] => (), // this should warn
16+
_ => (),
17+
}
18+
match s {
19+
["0x00", "0x00", "0x00", "0x00"] => (),
20+
["4", "5", "6", "7"] => (),
21+
MAGIC_TEST => (), // this should warn
22+
_ => (),
23+
}
24+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//compile-pass
2+
3+
fn main() {
4+
let s = &[0x00; 4][..]; //Slice of any value
5+
const MAGIC_TEST: &[u8] = b"TEST"; //Const slice to pattern match with
6+
match s {
7+
MAGIC_TEST => (),
8+
[0x00, 0x00, 0x00, 0x00] => (),
9+
[84, 69, 83, 84] => (), // this should warn
10+
_ => (),
11+
}
12+
match s {
13+
[0x00, 0x00, 0x00, 0x00] => (),
14+
MAGIC_TEST => (),
15+
[84, 69, 83, 84] => (), // this should warn
16+
_ => (),
17+
}
18+
match s {
19+
[0x00, 0x00, 0x00, 0x00] => (),
20+
[84, 69, 83, 84] => (),
21+
MAGIC_TEST => (), // this should warn
22+
_ => (),
23+
}
24+
}

0 commit comments

Comments
 (0)