@@ -53,21 +53,6 @@ pub enum Constant {
53
53
Tuple ( Vec < Constant > ) ,
54
54
}
55
55
56
- impl Constant {
57
- /// Convert to `u64` if possible.
58
- ///
59
- /// # panics
60
- ///
61
- /// If the constant could not be converted to `u64` losslessly.
62
- fn as_u64 ( & self ) -> u64 {
63
- if let Constant :: Int ( val) = * self {
64
- val. to_u64 ( ) . expect ( "negative constant can't be casted to `u64`" )
65
- } else {
66
- panic ! ( "Could not convert a `{:?}` to `u64`" , self ) ;
67
- }
68
- }
69
- }
70
-
71
56
impl PartialEq for Constant {
72
57
fn eq ( & self , other : & Constant ) -> bool {
73
58
match ( self , other) {
@@ -266,11 +251,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
266
251
ExprLit ( ref lit) => Some ( lit_to_constant ( & lit. node , self . tcx , self . tables . expr_ty ( e) ) ) ,
267
252
ExprArray ( ref vec) => self . multi ( vec) . map ( Constant :: Vec ) ,
268
253
ExprTup ( ref tup) => self . multi ( tup) . map ( Constant :: Tuple ) ,
269
- ExprRepeat ( ref value, number_id) => {
270
- let val = & self . tcx . hir . body ( number_id) . value ;
271
- self . binop_apply ( value,
272
- val,
273
- |v, n| Some ( Constant :: Repeat ( Box :: new ( v) , n. as_u64 ( ) as usize ) ) )
254
+ ExprRepeat ( ref value, _) => {
255
+ let n = match self . tables . expr_ty ( e) . sty {
256
+ ty:: TyArray ( _, n) => n,
257
+ _ => span_bug ! ( e. span, "typeck error" ) ,
258
+ } ;
259
+ self . expr ( value) . map ( |v| Constant :: Repeat ( Box :: new ( v) , n) )
274
260
} ,
275
261
ExprUnary ( op, ref operand) => {
276
262
self . expr ( operand) . and_then ( |o| match op {
@@ -375,15 +361,4 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
375
361
_ => None ,
376
362
}
377
363
}
378
-
379
-
380
- fn binop_apply < F > ( & mut self , left : & Expr , right : & Expr , op : F ) -> Option < Constant >
381
- where F : Fn ( Constant , Constant ) -> Option < Constant >
382
- {
383
- if let ( Some ( lc) , Some ( rc) ) = ( self . expr ( left) , self . expr ( right) ) {
384
- op ( lc, rc)
385
- } else {
386
- None
387
- }
388
- }
389
364
}
0 commit comments