Skip to content

Commit ecfe370

Browse files
committed
Auto merge of #51733 - varkor:ice-match-slice, r=oli-obk
Fix an ICE when matching over const slices Fixes #51655. I'm not super familiar with this code, so tell me if this is the wrong approach 😅 r? @oli-obk
2 parents 446aef6 + e14e48b commit ecfe370

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/librustc_mir/hair/pattern/_match.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use rustc::hir::RangeEnd;
2525
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
2626

2727
use rustc::mir::Field;
28+
use rustc::mir::interpret::ConstValue;
2829
use rustc::util::common::ErrorReported;
2930

3031
use syntax_pos::{Span, DUMMY_SP};
@@ -932,16 +933,20 @@ fn slice_pat_covered_by_constructor<'tcx>(
932933
suffix: &[Pattern<'tcx>]
933934
) -> Result<bool, ErrorReported> {
934935
let data: &[u8] = match *ctor {
935-
ConstantValue(const_val @ &ty::Const { val: ConstVal::Value(..), .. }) => {
936-
if let Some(ptr) = const_val.to_ptr() {
937-
let is_array_ptr = const_val.ty
936+
ConstantValue(&ty::Const { val: ConstVal::Value(const_val), ty }) => {
937+
let val = match const_val {
938+
ConstValue::ByRef(..) => bug!("unexpected ConstValue::ByRef"),
939+
ConstValue::Scalar(val) | ConstValue::ScalarPair(val, _) => val,
940+
};
941+
if let Ok(ptr) = val.to_ptr() {
942+
let is_array_ptr = ty
938943
.builtin_deref(true)
939944
.and_then(|t| t.ty.builtin_index())
940945
.map_or(false, |t| t == tcx.types.u8);
941946
assert!(is_array_ptr);
942947
tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id).bytes.as_ref()
943948
} else {
944-
bug!()
949+
bug!("unexpected non-ptr ConstantValue")
945950
}
946951
}
947952
_ => bug!()

src/test/run-pass/issue-51655.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(dead_code)]
12+
13+
const PATH_DOT: &[u8] = &[b'.'];
14+
15+
fn match_slice(element: &[u8]) {
16+
match element {
17+
&[] => {}
18+
PATH_DOT => {}
19+
_ => {}
20+
}
21+
}
22+
23+
fn main() {}

0 commit comments

Comments
 (0)