@@ -252,6 +252,7 @@ use syntax_pos::{Span, DUMMY_SP};
252
252
use arena:: TypedArena ;
253
253
254
254
use smallvec:: { smallvec, SmallVec } ;
255
+ use std:: borrow:: Cow ;
255
256
use std:: cmp:: { self , max, min, Ordering } ;
256
257
use std:: convert:: TryInto ;
257
258
use std:: fmt;
@@ -260,11 +261,12 @@ use std::ops::RangeInclusive;
260
261
use std:: u128;
261
262
262
263
pub 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)
264
265
}
265
266
266
267
struct LiteralExpander < ' tcx > {
267
268
tcx : TyCtxt < ' tcx > ,
269
+ param_env : ty:: ParamEnv < ' tcx > ,
268
270
}
269
271
270
272
impl LiteralExpander < ' tcx > {
@@ -284,9 +286,23 @@ impl LiteralExpander<'tcx> {
284
286
debug ! ( "fold_const_value_deref {:?} {:?} {:?}" , val, rty, crty) ;
285
287
match ( val, & crty. kind , & rty. kind ) {
286
288
// 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
+ }
290
306
}
291
307
// unsize array to slice if pattern is array but match value or other patterns are slice
292
308
( ConstValue :: Scalar ( Scalar :: Ptr ( p) ) , ty:: Array ( t, n) , ty:: Slice ( u) ) => {
@@ -2348,16 +2364,30 @@ fn specialize_one_pattern<'p, 'tcx>(
2348
2364
// just integers. The only time they should be pointing to memory
2349
2365
// is when they are subslices of nonzero slices.
2350
2366
let ( alloc, offset, n, ty) = match value. ty . kind {
2351
- ty:: Array ( t, n) => match value. val {
2352
- ty:: ConstKind :: Value ( ConstValue :: ByRef { offset, alloc, .. } ) => {
2353
- ( alloc, offset, n. eval_usize ( cx. tcx , cx. param_env ) , t)
2367
+ ty:: Array ( t, n) => {
2368
+ 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
+ }
2354
2377
}
2355
- _ => span_bug ! ( pat. span, "array pattern is {:?}" , value, ) ,
2356
- } ,
2378
+ match value. val {
2379
+ ty:: ConstKind :: Value ( ConstValue :: ByRef { offset, alloc, .. } ) => {
2380
+ ( Cow :: Borrowed ( alloc) , offset, n, t)
2381
+ }
2382
+ _ => span_bug ! ( pat. span, "array pattern is {:?}" , value, ) ,
2383
+ }
2384
+ }
2357
2385
ty:: Slice ( t) => {
2358
2386
match value. val {
2359
2387
ty:: ConstKind :: Value ( ConstValue :: Slice { data, start, end } ) => {
2360
- ( data, Size :: from_bytes ( start as u64 ) , ( end - start) as u64 , t)
2388
+ let offset = Size :: from_bytes ( start as u64 ) ;
2389
+ let n = ( end - start) as u64 ;
2390
+ ( Cow :: Borrowed ( data) , offset, n, t)
2361
2391
}
2362
2392
ty:: ConstKind :: Value ( ConstValue :: ByRef { .. } ) => {
2363
2393
// FIXME(oli-obk): implement `deref` for `ConstValue`
0 commit comments