8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use self :: ConstVal :: * ;
12
11
pub use rustc_const_math:: ConstInt ;
13
12
14
- use hir;
15
- use hir:: def:: Def ;
16
13
use hir:: def_id:: DefId ;
17
- use traits:: Reveal ;
18
14
use ty:: { self , TyCtxt , layout} ;
19
15
use ty:: subst:: Substs ;
20
- use util:: common:: ErrorReported ;
21
16
use rustc_const_math:: * ;
22
17
23
18
use graphviz:: IntoCow ;
24
19
use errors:: DiagnosticBuilder ;
20
+ use serialize:: { self , Encodable , Encoder , Decodable , Decoder } ;
25
21
use syntax:: symbol:: InternedString ;
26
22
use syntax:: ast;
27
23
use syntax_pos:: Span ;
28
24
29
25
use std:: borrow:: Cow ;
30
- use std:: collections:: BTreeMap ;
31
- use std:: rc:: Rc ;
32
26
33
- pub type EvalResult < ' tcx > = Result < ConstVal < ' tcx > , ConstEvalErr < ' tcx > > ;
27
+ pub type EvalResult < ' tcx > = Result < & ' tcx ty :: Const < ' tcx > , ConstEvalErr < ' tcx > > ;
34
28
35
- #[ derive( Clone , Debug , Hash , RustcEncodable , RustcDecodable , Eq , PartialEq ) ]
29
+ #[ derive( Copy , Clone , Debug , Hash , RustcEncodable , RustcDecodable , Eq , PartialEq ) ]
36
30
pub enum ConstVal < ' tcx > {
37
- Float ( ConstFloat ) ,
38
31
Integral ( ConstInt ) ,
32
+ Float ( ConstFloat ) ,
39
33
Str ( InternedString ) ,
40
- ByteStr ( Rc < Vec < u8 > > ) ,
34
+ ByteStr ( ByteArray < ' tcx > ) ,
41
35
Bool ( bool ) ,
42
36
Char ( char ) ,
43
37
Variant ( DefId ) ,
44
38
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 ) ,
39
+ Aggregate ( ConstAggregate < ' tcx > ) ,
40
+ Unevaluated ( DefId , & ' tcx Substs < ' tcx > ) ,
49
41
}
50
42
51
- impl < ' tcx > ConstVal < ' tcx > {
52
- pub fn description ( & self ) -> & ' static str {
53
- match * self {
54
- Float ( f) => f. description ( ) ,
55
- Integral ( i) => i. description ( ) ,
56
- Str ( _) => "string literal" ,
57
- ByteStr ( _) => "byte string literal" ,
58
- Bool ( _) => "boolean" ,
59
- Char ( ..) => "char" ,
60
- Variant ( _) => "enum variant" ,
61
- Struct ( _) => "struct" ,
62
- Tuple ( _) => "tuple" ,
63
- Function ( ..) => "function definition" ,
64
- Array ( ..) => "array" ,
65
- Repeat ( ..) => "repeat" ,
66
- }
43
+ #[ derive( Copy , Clone , Debug , Hash , RustcEncodable , Eq , PartialEq ) ]
44
+ pub struct ByteArray < ' tcx > {
45
+ pub data : & ' tcx [ u8 ] ,
46
+ }
47
+
48
+ impl < ' tcx > serialize:: UseSpecializedDecodable for ByteArray < ' tcx > { }
49
+
50
+ #[ derive( Copy , Clone , Debug , Hash , Eq , PartialEq ) ]
51
+ pub enum ConstAggregate < ' tcx > {
52
+ Struct ( & ' tcx [ ( ast:: Name , & ' tcx ty:: Const < ' tcx > ) ] ) ,
53
+ Tuple ( & ' tcx [ & ' tcx ty:: Const < ' tcx > ] ) ,
54
+ Array ( & ' tcx [ & ' tcx ty:: Const < ' tcx > ] ) ,
55
+ Repeat ( & ' tcx ty:: Const < ' tcx > , u64 ) ,
56
+ }
57
+
58
+ impl < ' tcx > Encodable for ConstAggregate < ' tcx > {
59
+ fn encode < S : Encoder > ( & self , _: & mut S ) -> Result < ( ) , S :: Error > {
60
+ bug ! ( "should never encode ConstAggregate::{:?}" , self )
61
+ }
62
+ }
63
+
64
+ impl < ' tcx > Decodable for ConstAggregate < ' tcx > {
65
+ fn decode < D : Decoder > ( _: & mut D ) -> Result < Self , D :: Error > {
66
+ bug ! ( "should never decode ConstAggregate" )
67
67
}
68
+ }
68
69
70
+ impl < ' tcx > ConstVal < ' tcx > {
69
71
pub fn to_const_int ( & self ) -> Option < ConstInt > {
70
72
match * self {
71
73
ConstVal :: Integral ( i) => Some ( i) ,
@@ -86,8 +88,6 @@ pub struct ConstEvalErr<'tcx> {
86
88
pub enum ErrKind < ' tcx > {
87
89
CannotCast ,
88
90
MissingStructField ,
89
- NegateOn ( ConstVal < ' tcx > ) ,
90
- NotOn ( ConstVal < ' tcx > ) ,
91
91
92
92
NonConstPath ,
93
93
UnimplementedConstVal ( & ' static str ) ,
@@ -146,9 +146,6 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
146
146
147
147
match self . kind {
148
148
CannotCast => simple ! ( "can't cast this type" ) ,
149
- NegateOn ( ref const_val) => simple ! ( "negate on {}" , const_val. description( ) ) ,
150
- NotOn ( ref const_val) => simple ! ( "not on {}" , const_val. description( ) ) ,
151
-
152
149
MissingStructField => simple ! ( "nonexistent struct field" ) ,
153
150
NonConstPath => simple ! ( "non-constant path in constant expression" ) ,
154
151
UnimplementedConstVal ( what) =>
@@ -221,37 +218,3 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
221
218
self . struct_error ( tcx, primary_span, primary_kind) . emit ( ) ;
222
219
}
223
220
}
224
-
225
- /// Returns the value of the length-valued expression
226
- pub fn eval_length ( tcx : TyCtxt ,
227
- count : hir:: BodyId ,
228
- reason : & str )
229
- -> Result < usize , ErrorReported >
230
- {
231
- let count_expr = & tcx. hir . body ( count) . value ;
232
- let count_def_id = tcx. hir . body_owner_def_id ( count) ;
233
- let param_env = ty:: ParamEnv :: empty ( Reveal :: UserFacing ) ;
234
- let substs = Substs :: identity_for_item ( tcx. global_tcx ( ) , count_def_id) ;
235
- match tcx. at ( count_expr. span ) . const_eval ( param_env. and ( ( count_def_id, substs) ) ) {
236
- Ok ( Integral ( Usize ( count) ) ) => {
237
- let val = count. as_u64 ( tcx. sess . target . uint_type ) ;
238
- assert_eq ! ( val as usize as u64 , val) ;
239
- Ok ( val as usize )
240
- } ,
241
- Ok ( _) |
242
- Err ( ConstEvalErr { kind : ErrKind :: TypeckError , .. } ) => Err ( ErrorReported ) ,
243
- Err ( err) => {
244
- let mut diag = err. struct_error ( tcx, count_expr. span , reason) ;
245
-
246
- if let hir:: ExprPath ( hir:: QPath :: Resolved ( None , ref path) ) = count_expr. node {
247
- if let Def :: Local ( ..) = path. def {
248
- diag. note ( & format ! ( "`{}` is a variable" ,
249
- tcx. hir. node_to_pretty_string( count_expr. id) ) ) ;
250
- }
251
- }
252
-
253
- diag. emit ( ) ;
254
- Err ( ErrorReported )
255
- }
256
- }
257
- }
0 commit comments