@@ -41,6 +41,8 @@ pub enum Constant {
41
41
Repeat ( Box < Constant > , u64 ) ,
42
42
/// A tuple of constants.
43
43
Tuple ( Vec < Constant > ) ,
44
+ /// A raw pointer.
45
+ RawPtr ( u128 ) ,
44
46
/// A literal with syntax error.
45
47
Err ( Symbol ) ,
46
48
}
@@ -109,6 +111,9 @@ impl Hash for Constant {
109
111
c. hash ( state) ;
110
112
l. hash ( state) ;
111
113
} ,
114
+ Constant :: RawPtr ( u) => {
115
+ u. hash ( state) ;
116
+ } ,
112
117
Constant :: Err ( ref s) => {
113
118
s. hash ( state) ;
114
119
} ,
@@ -192,7 +197,7 @@ pub fn constant_simple<'c, 'cc>(
192
197
constant ( lcx, tables, e) . and_then ( |( cst, res) | if res { None } else { Some ( cst) } )
193
198
}
194
199
195
- /// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`
200
+ /// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`.
196
201
pub fn constant_context < ' c , ' cc > (
197
202
lcx : & LateContext < ' c , ' cc > ,
198
203
tables : & ' c ty:: TypeckTables < ' cc > ,
@@ -215,7 +220,7 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
215
220
}
216
221
217
222
impl < ' c , ' cc > ConstEvalLateContext < ' c , ' cc > {
218
- /// simple constant folding: Insert an expression, get a constant or none.
223
+ /// Simple constant folding: Insert an expression, get a constant or none.
219
224
pub fn expr ( & mut self , e : & Expr ) -> Option < Constant > {
220
225
match e. node {
221
226
ExprKind :: Path ( ref qpath) => self . fetch_path ( qpath, e. hir_id ) ,
@@ -238,7 +243,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
238
243
} ) ,
239
244
ExprKind :: Binary ( op, ref left, ref right) => self . binop ( op, left, right) ,
240
245
ExprKind :: Call ( ref callee, ref args) => {
241
- // We only handle a few const functions for now
246
+ // We only handle a few const functions for now.
242
247
if_chain ! {
243
248
if args. is_empty( ) ;
244
249
if let ExprKind :: Path ( qpath) = & callee. node;
@@ -262,7 +267,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
262
267
}
263
268
}
264
269
} ,
265
- // TODO: add other expressions
270
+ // TODO: add other expressions.
266
271
_ => None ,
267
272
}
268
273
}
@@ -304,13 +309,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
304
309
}
305
310
}
306
311
307
- /// create `Some(Vec![..])` of all constants, unless there is any
308
- /// non-constant part
312
+ /// Create `Some(Vec![..])` of all constants, unless there is any
313
+ /// non-constant part.
309
314
fn multi ( & mut self , vec : & [ Expr ] ) -> Option < Vec < Constant > > {
310
315
vec. iter ( ) . map ( |elem| self . expr ( elem) ) . collect :: < Option < _ > > ( )
311
316
}
312
317
313
- /// lookup a possibly constant expression from a ExprKind::Path
318
+ /// Lookup a possibly constant expression from a ExprKind::Path.
314
319
fn fetch_path ( & mut self , qpath : & QPath , id : HirId ) -> Option < Constant > {
315
320
use rustc:: mir:: interpret:: GlobalId ;
316
321
@@ -334,14 +339,14 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
334
339
if ret. is_some ( ) {
335
340
self . needed_resolution = true ;
336
341
}
337
- return ret;
342
+ ret
338
343
} ,
339
- _ => { } ,
344
+ // FIXME: cover all useable cases.
345
+ _ => None ,
340
346
}
341
- None
342
347
}
343
348
344
- /// A block can only yield a constant if it only has one constant expression
349
+ /// A block can only yield a constant if it only has one constant expression.
345
350
fn block ( & mut self , block : & Block ) -> Option < Constant > {
346
351
if block. stmts . is_empty ( ) {
347
352
block. expr . as_ref ( ) . and_then ( |b| self . expr ( b) )
@@ -467,7 +472,13 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
467
472
ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits (
468
473
b. try_into ( ) . expect ( "invalid f64 bit representation" ) ,
469
474
) ) ) ,
470
- // FIXME: implement other conversion
475
+ ty:: RawPtr ( type_and_mut) => {
476
+ if let ty:: Uint ( _) = type_and_mut. ty . sty {
477
+ return Some ( Constant :: RawPtr ( b) ) ;
478
+ }
479
+ None
480
+ } ,
481
+ // FIXME: implement other conversions.
471
482
_ => None ,
472
483
} ,
473
484
ConstValue :: Slice ( Scalar :: Ptr ( ptr) , n) => match result. ty . sty {
@@ -484,7 +495,7 @@ pub fn miri_to_const<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, result: &ty::Const<'
484
495
} ,
485
496
_ => None ,
486
497
} ,
487
- // FIXME: implement other conversions
498
+ // FIXME: implement other conversions.
488
499
_ => None ,
489
500
}
490
501
}
0 commit comments