9
9
// except according to those terms.
10
10
11
11
use self :: ConstVal :: * ;
12
+ use self :: ConstAggregate :: * ;
12
13
pub use rustc_const_math:: ConstInt ;
13
14
14
15
use hir;
@@ -22,30 +23,55 @@ use rustc_const_math::*;
22
23
23
24
use graphviz:: IntoCow ;
24
25
use errors:: DiagnosticBuilder ;
26
+ use serialize:: { self , Encodable , Encoder , Decodable , Decoder } ;
25
27
use syntax:: symbol:: InternedString ;
26
28
use syntax:: ast;
27
29
use syntax_pos:: Span ;
28
30
29
31
use std:: borrow:: Cow ;
30
- use std:: collections:: BTreeMap ;
31
- use std:: rc:: Rc ;
32
32
33
- pub type EvalResult < ' tcx > = Result < ConstVal < ' tcx > , ConstEvalErr < ' tcx > > ;
33
+ pub type EvalResult < ' tcx > = Result < & ' tcx ConstVal < ' tcx > , ConstEvalErr < ' tcx > > ;
34
34
35
- #[ derive( Clone , Debug , Hash , RustcEncodable , RustcDecodable , Eq , PartialEq ) ]
35
+ #[ derive( Copy , Clone , Debug , Hash , RustcEncodable , RustcDecodable , Eq , PartialEq ) ]
36
36
pub enum ConstVal < ' tcx > {
37
37
Float ( ConstFloat ) ,
38
38
Integral ( ConstInt ) ,
39
39
Str ( InternedString ) ,
40
- ByteStr ( Rc < Vec < u8 > > ) ,
40
+ ByteStr ( ByteArray < ' tcx > ) ,
41
41
Bool ( bool ) ,
42
42
Char ( char ) ,
43
43
Variant ( DefId ) ,
44
44
Function ( DefId , & ' tcx Substs < ' tcx > ) ,
45
- Struct ( BTreeMap < ast:: Name , ConstVal < ' tcx > > ) ,
46
- Tuple ( Vec < ConstVal < ' tcx > > ) ,
47
- Array ( Vec < ConstVal < ' tcx > > ) ,
48
- Repeat ( Box < ConstVal < ' tcx > > , u64 ) ,
45
+ Aggregate ( ConstAggregate < ' tcx > ) ,
46
+ }
47
+
48
+ impl < ' tcx > serialize:: UseSpecializedDecodable for & ' tcx ConstVal < ' tcx > { }
49
+
50
+ #[ derive( Copy , Clone , Debug , Hash , RustcEncodable , Eq , PartialEq ) ]
51
+ pub struct ByteArray < ' tcx > {
52
+ pub data : & ' tcx [ u8 ] ,
53
+ }
54
+
55
+ impl < ' tcx > serialize:: UseSpecializedDecodable for ByteArray < ' tcx > { }
56
+
57
+ #[ derive( Copy , Clone , Debug , Hash , Eq , PartialEq ) ]
58
+ pub enum ConstAggregate < ' tcx > {
59
+ Struct ( & ' tcx [ ( ast:: Name , & ' tcx ConstVal < ' tcx > ) ] ) ,
60
+ Tuple ( & ' tcx [ & ' tcx ConstVal < ' tcx > ] ) ,
61
+ Array ( & ' tcx [ & ' tcx ConstVal < ' tcx > ] ) ,
62
+ Repeat ( & ' tcx ConstVal < ' tcx > , u64 ) ,
63
+ }
64
+
65
+ impl < ' tcx > Encodable for ConstAggregate < ' tcx > {
66
+ fn encode < S : Encoder > ( & self , _: & mut S ) -> Result < ( ) , S :: Error > {
67
+ bug ! ( "should never encode ConstAggregate::{:?}" , self )
68
+ }
69
+ }
70
+
71
+ impl < ' tcx > Decodable for ConstAggregate < ' tcx > {
72
+ fn decode < D : Decoder > ( _: & mut D ) -> Result < Self , D :: Error > {
73
+ bug ! ( "should never decode ConstAggregate" )
74
+ }
49
75
}
50
76
51
77
impl < ' tcx > ConstVal < ' tcx > {
@@ -58,11 +84,11 @@ impl<'tcx> ConstVal<'tcx> {
58
84
Bool ( _) => "boolean" ,
59
85
Char ( ..) => "char" ,
60
86
Variant ( _) => "enum variant" ,
61
- Struct ( _) => "struct" ,
62
- Tuple ( _) => "tuple" ,
87
+ Aggregate ( Struct ( _) ) => "struct" ,
88
+ Aggregate ( Tuple ( _) ) => "tuple" ,
63
89
Function ( ..) => "function definition" ,
64
- Array ( ..) => "array" ,
65
- Repeat ( ..) => "repeat" ,
90
+ Aggregate ( Array ( ..) ) => "array" ,
91
+ Aggregate ( Repeat ( ..) ) => "repeat" ,
66
92
}
67
93
}
68
94
@@ -233,7 +259,7 @@ pub fn eval_length(tcx: TyCtxt,
233
259
let param_env = ty:: ParamEnv :: empty ( Reveal :: UserFacing ) ;
234
260
let substs = Substs :: identity_for_item ( tcx. global_tcx ( ) , count_def_id) ;
235
261
match tcx. at ( count_expr. span ) . const_eval ( param_env. and ( ( count_def_id, substs) ) ) {
236
- Ok ( Integral ( Usize ( count) ) ) => {
262
+ Ok ( & Integral ( Usize ( count) ) ) => {
237
263
let val = count. as_u64 ( tcx. sess . target . uint_type ) ;
238
264
assert_eq ! ( val as usize as u64 , val) ;
239
265
Ok ( val as usize )
0 commit comments