@@ -498,8 +498,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
498
498
499
499
fn visit_rvalue ( & mut self , rvalue : & Rvalue < ' tcx > , location : Location ) {
500
500
macro_rules! check_kinds {
501
- ( $t: expr, $text: literal, $( $patterns : tt ) * ) => {
502
- if !matches!( ( $t) . kind( ) , $( $patterns ) * ) {
501
+ ( $t: expr, $text: literal, $typat : pat ) => {
502
+ if !matches!( ( $t) . kind( ) , $typat ) {
503
503
self . fail( location, format!( $text, $t) ) ;
504
504
}
505
505
} ;
@@ -527,6 +527,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
527
527
use BinOp :: * ;
528
528
let a = vals. 0 . ty ( & self . body . local_decls , self . tcx ) ;
529
529
let b = vals. 1 . ty ( & self . body . local_decls , self . tcx ) ;
530
+ if crate :: util:: binop_right_homogeneous ( * op) {
531
+ if let Eq | Lt | Le | Ne | Ge | Gt = op {
532
+ // The function pointer types can have lifetimes
533
+ if !self . mir_assign_valid_types ( a, b) {
534
+ self . fail (
535
+ location,
536
+ format ! ( "Cannot {op:?} compare incompatible types {a:?} and {b:?}" ) ,
537
+ ) ;
538
+ }
539
+ } else if a != b {
540
+ self . fail (
541
+ location,
542
+ format ! (
543
+ "Cannot perform binary op {op:?} on unequal types {a:?} and {b:?}"
544
+ ) ,
545
+ ) ;
546
+ }
547
+ }
548
+
530
549
match op {
531
550
Offset => {
532
551
check_kinds ! ( a, "Cannot offset non-pointer type {:?}" , ty:: RawPtr ( ..) ) ;
@@ -538,7 +557,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
538
557
for x in [ a, b] {
539
558
check_kinds ! (
540
559
x,
541
- "Cannot compare type {:?}" ,
560
+ "Cannot {op:?} compare type {:?}" ,
542
561
ty:: Bool
543
562
| ty:: Char
544
563
| ty:: Int ( ..)
@@ -548,19 +567,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
548
567
| ty:: FnPtr ( ..)
549
568
)
550
569
}
551
- // The function pointer types can have lifetimes
552
- if !self . mir_assign_valid_types ( a, b) {
553
- self . fail (
554
- location,
555
- format ! ( "Cannot compare unequal types {:?} and {:?}" , a, b) ,
556
- ) ;
557
- }
558
570
}
559
- Shl | ShlUnchecked | Shr | ShrUnchecked => {
571
+ AddUnchecked | SubUnchecked | MulUnchecked | Shl | ShlUnchecked | Shr
572
+ | ShrUnchecked => {
560
573
for x in [ a, b] {
561
574
check_kinds ! (
562
575
x,
563
- "Cannot shift non-integer type {:?}" ,
576
+ "Cannot {op:?} non-integer type {:?}" ,
564
577
ty:: Uint ( ..) | ty:: Int ( ..)
565
578
)
566
579
}
@@ -569,55 +582,19 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
569
582
for x in [ a, b] {
570
583
check_kinds ! (
571
584
x,
572
- "Cannot perform bitwise op on type {:?}" ,
585
+ "Cannot perform bitwise op {op:?} on type {:?}" ,
573
586
ty:: Uint ( ..) | ty:: Int ( ..) | ty:: Bool
574
587
)
575
588
}
576
- if a != b {
577
- self . fail (
578
- location,
579
- format ! (
580
- "Cannot perform bitwise op on unequal types {:?} and {:?}" ,
581
- a, b
582
- ) ,
583
- ) ;
584
- }
585
589
}
586
590
Add | Sub | Mul | Div | Rem => {
587
591
for x in [ a, b] {
588
592
check_kinds ! (
589
593
x,
590
- "Cannot perform arithmetic on type {:?}" ,
594
+ "Cannot perform arithmetic {op:?} on type {:?}" ,
591
595
ty:: Uint ( ..) | ty:: Int ( ..) | ty:: Float ( ..)
592
596
)
593
597
}
594
- if a != b {
595
- self . fail (
596
- location,
597
- format ! (
598
- "Cannot perform arithmetic on unequal types {:?} and {:?}" ,
599
- a, b
600
- ) ,
601
- ) ;
602
- }
603
- }
604
- AddUnchecked | SubUnchecked | MulUnchecked => {
605
- for x in [ a, b] {
606
- check_kinds ! (
607
- x,
608
- "Cannot perform unchecked arithmetic on type {:?}" ,
609
- ty:: Uint ( ..) | ty:: Int ( ..)
610
- )
611
- }
612
- if a != b {
613
- self . fail (
614
- location,
615
- format ! (
616
- "Cannot perform unchecked arithmetic on unequal types {:?} and {:?}" ,
617
- a, b
618
- ) ,
619
- ) ;
620
- }
621
598
}
622
599
}
623
600
}
0 commit comments