@@ -498,8 +498,8 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
498498
499499 fn visit_rvalue ( & mut self , rvalue : & Rvalue < ' tcx > , location : Location ) {
500500 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 ) {
503503 self . fail( location, format!( $text, $t) ) ;
504504 }
505505 } ;
@@ -527,6 +527,25 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
527527 use BinOp :: * ;
528528 let a = vals. 0 . ty ( & self . body . local_decls , self . tcx ) ;
529529 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+
530549 match op {
531550 Offset => {
532551 check_kinds ! ( a, "Cannot offset non-pointer type {:?}" , ty:: RawPtr ( ..) ) ;
@@ -538,7 +557,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
538557 for x in [ a, b] {
539558 check_kinds ! (
540559 x,
541- "Cannot compare type {:?}" ,
560+ "Cannot {op:?} compare type {:?}" ,
542561 ty:: Bool
543562 | ty:: Char
544563 | ty:: Int ( ..)
@@ -548,19 +567,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
548567 | ty:: FnPtr ( ..)
549568 )
550569 }
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- }
558570 }
559- Shl | ShlUnchecked | Shr | ShrUnchecked => {
571+ AddUnchecked | SubUnchecked | MulUnchecked | Shl | ShlUnchecked | Shr
572+ | ShrUnchecked => {
560573 for x in [ a, b] {
561574 check_kinds ! (
562575 x,
563- "Cannot shift non-integer type {:?}" ,
576+ "Cannot {op:?} non-integer type {:?}" ,
564577 ty:: Uint ( ..) | ty:: Int ( ..)
565578 )
566579 }
@@ -569,55 +582,19 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
569582 for x in [ a, b] {
570583 check_kinds ! (
571584 x,
572- "Cannot perform bitwise op on type {:?}" ,
585+ "Cannot perform bitwise op {op:?} on type {:?}" ,
573586 ty:: Uint ( ..) | ty:: Int ( ..) | ty:: Bool
574587 )
575588 }
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- }
585589 }
586590 Add | Sub | Mul | Div | Rem => {
587591 for x in [ a, b] {
588592 check_kinds ! (
589593 x,
590- "Cannot perform arithmetic on type {:?}" ,
594+ "Cannot perform arithmetic {op:?} on type {:?}" ,
591595 ty:: Uint ( ..) | ty:: Int ( ..) | ty:: Float ( ..)
592596 )
593597 }
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- }
621598 }
622599 }
623600 }
0 commit comments