Skip to content

Fix an ICE when matching over const slices #51733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/librustc_mir/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use rustc::hir::RangeEnd;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};

use rustc::mir::Field;
use rustc::mir::interpret::ConstValue;
use rustc::util::common::ErrorReported;

use syntax_pos::{Span, DUMMY_SP};
Expand Down Expand Up @@ -932,16 +933,20 @@ fn slice_pat_covered_by_constructor<'tcx>(
suffix: &[Pattern<'tcx>]
) -> Result<bool, ErrorReported> {
let data: &[u8] = match *ctor {
ConstantValue(const_val @ &ty::Const { val: ConstVal::Value(..), .. }) => {
if let Some(ptr) = const_val.to_ptr() {
let is_array_ptr = const_val.ty
ConstantValue(&ty::Const { val: ConstVal::Value(const_val), ty }) => {
let val = match const_val {
ConstValue::ByRef(..) => bug!("unexpected ConstValue::ByRef"),
ConstValue::Scalar(val) | ConstValue::ScalarPair(val, _) => val,
};
if let Ok(ptr) = val.to_ptr() {
let is_array_ptr = ty
.builtin_deref(true)
.and_then(|t| t.ty.builtin_index())
.map_or(false, |t| t == tcx.types.u8);
assert!(is_array_ptr);
tcx.alloc_map.lock().unwrap_memory(ptr.alloc_id).bytes.as_ref()
} else {
bug!()
bug!("unexpected non-ptr ConstantValue")
}
}
_ => bug!()
Expand Down
23 changes: 23 additions & 0 deletions src/test/run-pass/issue-51655.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#![allow(dead_code)]

const PATH_DOT: &[u8] = &[b'.'];

fn match_slice(element: &[u8]) {
match element {
&[] => {}
PATH_DOT => {}
_ => {}
}
}

fn main() {}