@@ -208,7 +208,11 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
208
208
self . memory . write_bool ( ptr, b) ?;
209
209
Ok ( ptr)
210
210
}
211
- Char ( _c) => unimplemented ! ( ) ,
211
+ Char ( c) => {
212
+ let ptr = self . memory . allocate ( 4 ) ;
213
+ self . memory . write_uint ( ptr, c as u64 , 4 ) ?;
214
+ Ok ( ptr)
215
+ } ,
212
216
Struct ( _node_id) => unimplemented ! ( ) ,
213
217
Tuple ( _node_id) => unimplemented ! ( ) ,
214
218
Function ( _def_id) => unimplemented ! ( ) ,
@@ -402,11 +406,17 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
402
406
403
407
SwitchInt { ref discr, ref values, ref targets, .. } => {
404
408
let discr_ptr = self . eval_lvalue ( discr) ?. to_ptr ( ) ;
409
+ let discr_ty = self . lvalue_ty ( discr) ;
405
410
let discr_size = self
406
- . type_layout ( self . lvalue_ty ( discr ) )
411
+ . type_layout ( discr_ty )
407
412
. size ( & self . tcx . data_layout )
408
413
. bytes ( ) as usize ;
409
414
let discr_val = self . memory . read_uint ( discr_ptr, discr_size) ?;
415
+ if let ty:: TyChar = discr_ty. sty {
416
+ if :: std:: char:: from_u32 ( discr_val as u32 ) . is_none ( ) {
417
+ return Err ( EvalError :: InvalidChar ( discr_val as u32 ) ) ;
418
+ }
419
+ }
410
420
411
421
// Branch to the `otherwise` case by default, if no match is found.
412
422
let mut target_block = targets[ targets. len ( ) - 1 ] ;
@@ -1371,6 +1381,13 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1371
1381
use syntax:: ast:: { IntTy , UintTy } ;
1372
1382
let val = match ( self . memory . pointer_size , & ty. sty ) {
1373
1383
( _, & ty:: TyBool ) => PrimVal :: Bool ( self . memory . read_bool ( ptr) ?) ,
1384
+ ( _, & ty:: TyChar ) => {
1385
+ let c = self . memory . read_uint ( ptr, 4 ) ? as u32 ;
1386
+ match :: std:: char:: from_u32 ( c) {
1387
+ Some ( ch) => PrimVal :: Char ( ch) ,
1388
+ None => return Err ( EvalError :: InvalidChar ( c) ) ,
1389
+ }
1390
+ }
1374
1391
( _, & ty:: TyInt ( IntTy :: I8 ) ) => PrimVal :: I8 ( self . memory . read_int ( ptr, 1 ) ? as i8 ) ,
1375
1392
( 2 , & ty:: TyInt ( IntTy :: Is ) ) |
1376
1393
( _, & ty:: TyInt ( IntTy :: I16 ) ) => PrimVal :: I16 ( self . memory . read_int ( ptr, 2 ) ? as i16 ) ,
0 commit comments