From 1acbf4b8028ae4eb86128c07945ac0cdc8f7d811 Mon Sep 17 00:00:00 2001 From: Oliver Scherer Date: Sun, 22 Dec 2019 20:39:35 +0100 Subject: [PATCH] Early abort instead of building up zero sized values --- src/librustc/mir/interpret/allocation.rs | 4 ---- src/librustc_mir/hair/pattern/_match.rs | 20 +++++++++++--------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/librustc/mir/interpret/allocation.rs b/src/librustc/mir/interpret/allocation.rs index 957a028a59eb..67f1c8072d68 100644 --- a/src/librustc/mir/interpret/allocation.rs +++ b/src/librustc/mir/interpret/allocation.rs @@ -127,10 +127,6 @@ impl Allocation { extra: (), } } - - pub fn zst(align: Align) -> Self { - Self::undef(Size::ZERO, align) - } } impl Allocation<(), ()> { diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs index 9851f2468974..c6efbe883327 100644 --- a/src/librustc_mir/hair/pattern/_match.rs +++ b/src/librustc_mir/hair/pattern/_match.rs @@ -237,11 +237,11 @@ use super::{FieldPat, Pat, PatKind, PatRange}; use rustc::hir::def_id::DefId; use rustc::hir::{HirId, RangeEnd}; -use rustc::ty::layout::{Align, Integer, IntegerExt, Size, VariantIdx}; +use rustc::ty::layout::{Integer, IntegerExt, Size, VariantIdx}; use rustc::ty::{self, Const, Ty, TyCtxt, TypeFoldable, VariantDef}; use rustc::lint; -use rustc::mir::interpret::{truncate, AllocId, Allocation, ConstValue, Pointer, Scalar}; +use rustc::mir::interpret::{truncate, AllocId, ConstValue, Pointer, Scalar}; use rustc::mir::Field; use rustc::util::captures::Captures; use rustc::util::common::ErrorReported; @@ -2366,17 +2366,19 @@ fn specialize_one_pattern<'p, 'tcx>( let (alloc, offset, n, ty) = match value.ty.kind { ty::Array(t, n) => { let n = n.eval_usize(cx.tcx, cx.param_env); + // Shortcut for `n == 0` where no matter what `alloc` and `offset` we produce, + // the result would be exactly what we early return here. + if n == 0 { + if ctor_wild_subpatterns.len() as u64 == 0 { + return Some(PatStack::from_slice(&[])); + } else { + return None; + } + } match value.val { ty::ConstKind::Value(ConstValue::ByRef { offset, alloc, .. }) => { (Cow::Borrowed(alloc), offset, n, t) } - ty::ConstKind::Value(ConstValue::Scalar(Scalar::Raw { data, .. })) - if n == 0 => - { - let align = Align::from_bytes(data as u64).unwrap(); - // empty array - (Cow::Owned(Allocation::zst(align)), Size::ZERO, 0, t) - } _ => span_bug!(pat.span, "array pattern is {:?}", value,), } }