@@ -7,7 +7,9 @@ use rustc_middle::{bug, span_bug};
7
7
use rustc_span:: symbol:: sym;
8
8
use rustc_target:: abi:: Abi ;
9
9
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
+ } ;
11
13
12
14
use crate :: fluent_generated as fluent;
13
15
@@ -479,11 +481,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
479
481
use rustc_middle:: mir:: UnOp :: * ;
480
482
481
483
let layout = val. layout ;
482
- let val = val. to_scalar ( ) ;
483
484
trace ! ( "Running unary op {:?}: {:?} ({})" , un_op, val, layout. ty) ;
484
485
485
486
match layout. ty . kind ( ) {
486
487
ty:: Bool => {
488
+ let val = val. to_scalar ( ) ;
487
489
let val = val. to_bool ( ) ?;
488
490
let res = match un_op {
489
491
Not => !val,
@@ -492,6 +494,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
492
494
Ok ( ( ImmTy :: from_bool ( res, * self . tcx ) , false ) )
493
495
}
494
496
ty:: Float ( fty) => {
497
+ let val = val. to_scalar ( ) ;
495
498
// No NaN adjustment here, `-` is a bitwise operation!
496
499
let res = match ( un_op, fty) {
497
500
( 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> {
500
503
} ;
501
504
Ok ( ( ImmTy :: from_scalar ( res, layout) , false ) )
502
505
}
503
- _ => {
504
- assert ! ( layout . ty . is_integral ( ) ) ;
506
+ _ if layout . ty . is_integral ( ) => {
507
+ let val = val . to_scalar ( ) ;
505
508
let val = val. to_bits ( layout. size ) ?;
506
509
let ( res, overflow) = match un_op {
507
510
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> {
516
519
let truncated = self . truncate ( res, layout) ;
517
520
( truncated, overflow || self . sign_extend ( truncated, layout) != res)
518
521
}
522
+ _ => span_bug ! ( self . cur_span( ) , "Invalid integer op {:?}" , un_op) ,
519
523
} ;
520
524
Ok ( ( ImmTy :: from_uint ( res, layout) , overflow) )
521
525
}
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
+ }
522
547
}
523
548
}
524
549
0 commit comments