Skip to content

Commit 1acbf4b

Browse files
committed
Early abort instead of building up zero sized values
1 parent 9520551 commit 1acbf4b

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,6 @@ impl<Tag> Allocation<Tag> {
127127
extra: (),
128128
}
129129
}
130-
131-
pub fn zst(align: Align) -> Self {
132-
Self::undef(Size::ZERO, align)
133-
}
134130
}
135131

136132
impl Allocation<(), ()> {

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,11 @@ use super::{FieldPat, Pat, PatKind, PatRange};
237237

238238
use rustc::hir::def_id::DefId;
239239
use rustc::hir::{HirId, RangeEnd};
240-
use rustc::ty::layout::{Align, Integer, IntegerExt, Size, VariantIdx};
240+
use rustc::ty::layout::{Integer, IntegerExt, Size, VariantIdx};
241241
use rustc::ty::{self, Const, Ty, TyCtxt, TypeFoldable, VariantDef};
242242

243243
use rustc::lint;
244-
use rustc::mir::interpret::{truncate, AllocId, Allocation, ConstValue, Pointer, Scalar};
244+
use rustc::mir::interpret::{truncate, AllocId, ConstValue, Pointer, Scalar};
245245
use rustc::mir::Field;
246246
use rustc::util::captures::Captures;
247247
use rustc::util::common::ErrorReported;
@@ -2366,17 +2366,19 @@ fn specialize_one_pattern<'p, 'tcx>(
23662366
let (alloc, offset, n, ty) = match value.ty.kind {
23672367
ty::Array(t, n) => {
23682368
let n = n.eval_usize(cx.tcx, cx.param_env);
2369+
// Shortcut for `n == 0` where no matter what `alloc` and `offset` we produce,
2370+
// the result would be exactly what we early return here.
2371+
if n == 0 {
2372+
if ctor_wild_subpatterns.len() as u64 == 0 {
2373+
return Some(PatStack::from_slice(&[]));
2374+
} else {
2375+
return None;
2376+
}
2377+
}
23692378
match value.val {
23702379
ty::ConstKind::Value(ConstValue::ByRef { offset, alloc, .. }) => {
23712380
(Cow::Borrowed(alloc), offset, n, t)
23722381
}
2373-
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. }))
2374-
if n == 0 =>
2375-
{
2376-
let align = Align::from_bytes(data as u64).unwrap();
2377-
// empty array
2378-
(Cow::Owned(Allocation::zst(align)), Size::ZERO, 0, t)
2379-
}
23802382
_ => span_bug!(pat.span, "array pattern is {:?}", value,),
23812383
}
23822384
}

0 commit comments

Comments
 (0)