@@ -237,11 +237,11 @@ use super::{FieldPat, Pat, PatKind, PatRange};
237237
238238use rustc:: hir:: def_id:: DefId ;
239239use rustc:: hir:: { HirId , RangeEnd } ;
240- use rustc:: ty:: layout:: { Integer , IntegerExt , Size , VariantIdx } ;
240+ use rustc:: ty:: layout:: { Integer , IntegerExt , Size , VariantIdx , Align } ;
241241use rustc:: ty:: { self , Const , Ty , TyCtxt , TypeFoldable , VariantDef } ;
242242
243243use rustc:: lint;
244- use rustc:: mir:: interpret:: { truncate, AllocId , ConstValue , Pointer , Scalar } ;
244+ use rustc:: mir:: interpret:: { truncate, AllocId , ConstValue , Pointer , Scalar , Allocation } ;
245245use rustc:: mir:: Field ;
246246use rustc:: util:: captures:: Captures ;
247247use rustc:: util:: common:: ErrorReported ;
@@ -258,13 +258,15 @@ use std::fmt;
258258use std:: iter:: { FromIterator , IntoIterator } ;
259259use std:: ops:: RangeInclusive ;
260260use std:: u128;
261+ use std:: borrow:: Cow ;
261262
262263pub fn expand_pattern < ' a , ' tcx > ( cx : & MatchCheckCtxt < ' a , ' tcx > , pat : Pat < ' tcx > ) -> Pat < ' tcx > {
263- LiteralExpander { tcx : cx. tcx } . fold_pattern ( & pat)
264+ LiteralExpander { tcx : cx. tcx , param_env : cx . param_env } . fold_pattern ( & pat)
264265}
265266
266267struct LiteralExpander < ' tcx > {
267268 tcx : TyCtxt < ' tcx > ,
269+ param_env : ty:: ParamEnv < ' tcx > ,
268270}
269271
270272impl LiteralExpander < ' tcx > {
@@ -284,9 +286,23 @@ impl LiteralExpander<'tcx> {
284286 debug ! ( "fold_const_value_deref {:?} {:?} {:?}" , val, rty, crty) ;
285287 match ( val, & crty. kind , & rty. kind ) {
286288 // the easy case, deref a reference
287- ( ConstValue :: Scalar ( Scalar :: Ptr ( p) ) , x, y) if x == y => {
288- let alloc = self . tcx . alloc_map . lock ( ) . unwrap_memory ( p. alloc_id ) ;
289- ConstValue :: ByRef { alloc, offset : p. offset }
289+ ( ConstValue :: Scalar ( p) , x, y) if x == y => {
290+ match p {
291+ Scalar :: Ptr ( p) => {
292+ let alloc = self . tcx . alloc_map . lock ( ) . unwrap_memory ( p. alloc_id ) ;
293+ ConstValue :: ByRef { alloc, offset : p. offset }
294+ } ,
295+ Scalar :: Raw { .. } => {
296+ let layout = self . tcx . layout_of ( self . param_env . and ( rty) ) . unwrap ( ) ;
297+ if layout. is_zst ( ) {
298+ // Deref of a reference to a ZST is a nop.
299+ ConstValue :: Scalar ( Scalar :: zst ( ) )
300+ } else {
301+ // FIXME(oli-obk): this is reachable for `const FOO: &&&u32 = &&&42;`
302+ bug ! ( "cannot deref {:#?}, {} -> {}" , val, crty, rty) ;
303+ }
304+ }
305+ }
290306 }
291307 // unsize array to slice if pattern is array but match value or other patterns are slice
292308 ( ConstValue :: Scalar ( Scalar :: Ptr ( p) ) , ty:: Array ( t, n) , ty:: Slice ( u) ) => {
@@ -2347,16 +2363,27 @@ fn specialize_one_pattern<'p, 'tcx>(
23472363 // just integers. The only time they should be pointing to memory
23482364 // is when they are subslices of nonzero slices.
23492365 let ( alloc, offset, n, ty) = match value. ty . kind {
2350- ty:: Array ( t, n) => match value. val {
2351- ty:: ConstKind :: Value ( ConstValue :: ByRef { offset, alloc, .. } ) => {
2352- ( alloc, offset, n. eval_usize ( cx. tcx , cx. param_env ) , t)
2366+ ty:: Array ( t, n) => {
2367+ let n = n. eval_usize ( cx. tcx , cx. param_env ) ;
2368+ match value. val {
2369+ ty:: ConstKind :: Value ( ConstValue :: ByRef { offset, alloc, .. } ) => {
2370+ ( Cow :: Borrowed ( alloc) , offset, n, t)
2371+ }
2372+ ty:: ConstKind :: Value ( ConstValue :: Scalar ( Scalar :: Raw { data, .. } ) )
2373+ if n == 0 => {
2374+ let align = Align :: from_bytes ( data as u64 ) . unwrap ( ) ;
2375+ // empty array
2376+ ( Cow :: Owned ( Allocation :: zst ( align) ) , Size :: ZERO , 0 , t)
2377+ }
2378+ _ => span_bug ! ( pat. span, "array pattern is {:?}" , value, ) ,
23532379 }
2354- _ => span_bug ! ( pat. span, "array pattern is {:?}" , value, ) ,
23552380 } ,
23562381 ty:: Slice ( t) => {
23572382 match value. val {
23582383 ty:: ConstKind :: Value ( ConstValue :: Slice { data, start, end } ) => {
2359- ( data, Size :: from_bytes ( start as u64 ) , ( end - start) as u64 , t)
2384+ let offset = Size :: from_bytes ( start as u64 ) ;
2385+ let n = ( end - start) as u64 ;
2386+ ( Cow :: Borrowed ( data) , offset, n, t)
23602387 }
23612388 ty:: ConstKind :: Value ( ConstValue :: ByRef { .. } ) => {
23622389 // FIXME(oli-obk): implement `deref` for `ConstValue`
0 commit comments