@@ -7,7 +7,9 @@ use rustc_middle::{bug, span_bug};
77use rustc_span:: symbol:: sym;
88use rustc_target:: abi:: Abi ;
99
10- use super :: { err_ub, throw_ub, throw_ub_custom, ImmTy , Immediate , InterpCx , Machine , PlaceTy } ;
10+ use super :: {
11+ err_ub, throw_ub, throw_ub_custom, ImmTy , Immediate , InterpCx , Machine , MemPlaceMeta , PlaceTy ,
12+ } ;
1113
1214use crate :: fluent_generated as fluent;
1315
@@ -479,11 +481,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
479481 use rustc_middle:: mir:: UnOp :: * ;
480482
481483 let layout = val. layout ;
482- let val = val. to_scalar ( ) ;
483484 trace ! ( "Running unary op {:?}: {:?} ({})" , un_op, val, layout. ty) ;
484485
485486 match layout. ty . kind ( ) {
486487 ty:: Bool => {
488+ let val = val. to_scalar ( ) ;
487489 let val = val. to_bool ( ) ?;
488490 let res = match un_op {
489491 Not => !val,
@@ -492,6 +494,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
492494 Ok ( ( ImmTy :: from_bool ( res, * self . tcx ) , false ) )
493495 }
494496 ty:: Float ( fty) => {
497+ let val = val. to_scalar ( ) ;
495498 // No NaN adjustment here, `-` is a bitwise operation!
496499 let res = match ( un_op, fty) {
497500 ( Neg , FloatTy :: F32 ) => Scalar :: from_f32 ( -val. to_f32 ( ) ?) ,
@@ -500,8 +503,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
500503 } ;
501504 Ok ( ( ImmTy :: from_scalar ( res, layout) , false ) )
502505 }
503- _ => {
504- assert ! ( layout . ty . is_integral ( ) ) ;
506+ _ if layout . ty . is_integral ( ) => {
507+ let val = val . to_scalar ( ) ;
505508 let val = val. to_bits ( layout. size ) ?;
506509 let ( res, overflow) = match un_op {
507510 Not => ( self . truncate ( !val, layout) , false ) , // bitwise negation, then truncate
@@ -516,9 +519,31 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
516519 let truncated = self . truncate ( res, layout) ;
517520 ( truncated, overflow || self . sign_extend ( truncated, layout) != res)
518521 }
522+ _ => span_bug ! ( self . cur_span( ) , "Invalid integer op {:?}" , un_op) ,
519523 } ;
520524 Ok ( ( ImmTy :: from_uint ( res, layout) , overflow) )
521525 }
526+ ty:: RawPtr ( ..) => {
527+ assert_eq ! ( un_op, PtrMetadata ) ;
528+ let ( _, meta) = val. to_scalar_and_meta ( ) ?;
529+ Ok ( (
530+ match meta {
531+ MemPlaceMeta :: Meta ( scalar) => {
532+ let ty = un_op. ty ( * self . tcx , val. layout . ty ) ;
533+ let layout = self . layout_of ( ty) ?;
534+ ImmTy :: from_scalar ( scalar, layout)
535+ }
536+ MemPlaceMeta :: None => {
537+ let unit_layout = self . layout_of ( self . tcx . types . unit ) ?;
538+ ImmTy :: uninit ( unit_layout)
539+ }
540+ } ,
541+ false ,
542+ ) )
543+ }
544+ _ => {
545+ bug ! ( "Unexpected unary op argument {val:?}" )
546+ }
522547 }
523548 }
524549
0 commit comments