@@ -41,6 +41,8 @@ pub enum Constant {
4141 Repeat ( Box < Constant > , u64 ) ,
4242 /// A tuple of constants.
4343 Tuple ( Vec < Constant > ) ,
44+ /// A raw pointer.
45+ RawPtr ( u128 ) ,
4446 /// A literal with syntax error.
4547 Err ( Symbol ) ,
4648}
@@ -109,6 +111,9 @@ impl Hash for Constant {
109111 c. hash ( state) ;
110112 l. hash ( state) ;
111113 } ,
114+ Constant :: RawPtr ( u) => {
115+ u. hash ( state) ;
116+ } ,
112117 Constant :: Err ( ref s) => {
113118 s. hash ( state) ;
114119 } ,
@@ -192,7 +197,7 @@ pub fn constant_simple<'c, 'cc>(
192197 constant ( lcx, tables, e) . and_then ( |( cst, res) | if res { None } else { Some ( cst) } )
193198}
194199
195- /// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`
200+ /// Creates a `ConstEvalLateContext` from the given `LateContext` and `TypeckTables`.
196201pub fn constant_context < ' c , ' cc > (
197202 lcx : & LateContext < ' c , ' cc > ,
198203 tables : & ' c ty:: TypeckTables < ' cc > ,
@@ -215,7 +220,7 @@ pub struct ConstEvalLateContext<'a, 'tcx: 'a> {
215220}
216221
217222impl < ' 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.
219224 pub fn expr ( & mut self , e : & Expr ) -> Option < Constant > {
220225 match e. node {
221226 ExprKind :: Path ( ref qpath) => self . fetch_path ( qpath, e. hir_id ) ,
@@ -238,7 +243,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
238243 } ) ,
239244 ExprKind :: Binary ( op, ref left, ref right) => self . binop ( op, left, right) ,
240245 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.
242247 if_chain ! {
243248 if args. is_empty( ) ;
244249 if let ExprKind :: Path ( qpath) = & callee. node;
@@ -262,7 +267,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
262267 }
263268 }
264269 } ,
265- // TODO: add other expressions
270+ // TODO: add other expressions.
266271 _ => None ,
267272 }
268273 }
@@ -304,13 +309,13 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
304309 }
305310 }
306311
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.
309314 fn multi ( & mut self , vec : & [ Expr ] ) -> Option < Vec < Constant > > {
310315 vec. iter ( ) . map ( |elem| self . expr ( elem) ) . collect :: < Option < _ > > ( )
311316 }
312317
313- /// lookup a possibly constant expression from a ExprKind::Path
318+ /// Lookup a possibly constant expression from a ExprKind::Path.
314319 fn fetch_path ( & mut self , qpath : & QPath , id : HirId ) -> Option < Constant > {
315320 use rustc:: mir:: interpret:: GlobalId ;
316321
@@ -334,14 +339,14 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
334339 if ret. is_some ( ) {
335340 self . needed_resolution = true ;
336341 }
337- return ret;
342+ ret
338343 } ,
339- _ => { } ,
344+ // FIXME: cover all useable cases.
345+ _ => None ,
340346 }
341- None
342347 }
343348
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.
345350 fn block ( & mut self , block : & Block ) -> Option < Constant > {
346351 if block. stmts . is_empty ( ) {
347352 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<'
467472 ty:: Float ( FloatTy :: F64 ) => Some ( Constant :: F64 ( f64:: from_bits (
468473 b. try_into ( ) . expect ( "invalid f64 bit representation" ) ,
469474 ) ) ) ,
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.
471482 _ => None ,
472483 } ,
473484 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<'
484495 } ,
485496 _ => None ,
486497 } ,
487- // FIXME: implement other conversions
498+ // FIXME: implement other conversions.
488499 _ => None ,
489500 }
490501}
0 commit comments