@@ -1656,82 +1656,39 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
16561656 } ;
16571657
16581658 enum Similar < ' tcx > {
1659- Adts ( ty:: AdtDef < ' tcx > , ty:: AdtDef < ' tcx > ) ,
1660- PrimitiveFound ( Ty < ' tcx > , ty:: AdtDef < ' tcx > ) ,
1661- PrimitiveExpected ( ty:: AdtDef < ' tcx > , Ty < ' tcx > ) ,
1662- }
1663-
1664- let primitive_sym = |kind : & _ | match kind {
1665- ty:: Bool => Some ( sym:: bool) ,
1666- ty:: Char => Some ( sym:: char) ,
1667- ty:: Float ( f) => match f {
1668- ty:: FloatTy :: F32 => Some ( sym:: f32) ,
1669- ty:: FloatTy :: F64 => Some ( sym:: f64) ,
1670- } ,
1671- ty:: Int ( f) => match f {
1672- ty:: IntTy :: Isize => Some ( sym:: isize) ,
1673- ty:: IntTy :: I8 => Some ( sym:: i8) ,
1674- ty:: IntTy :: I16 => Some ( sym:: i16) ,
1675- ty:: IntTy :: I32 => Some ( sym:: i32) ,
1676- ty:: IntTy :: I64 => Some ( sym:: i64) ,
1677- ty:: IntTy :: I128 => Some ( sym:: i128) ,
1678- } ,
1679- ty:: Uint ( f) => match f {
1680- ty:: UintTy :: Usize => Some ( sym:: usize) ,
1681- ty:: UintTy :: U8 => Some ( sym:: u8) ,
1682- ty:: UintTy :: U16 => Some ( sym:: u16) ,
1683- ty:: UintTy :: U32 => Some ( sym:: u32) ,
1684- ty:: UintTy :: U64 => Some ( sym:: u64) ,
1685- ty:: UintTy :: U128 => Some ( sym:: u128) ,
1686- } ,
1687- _ => None ,
1688- } ;
1689-
1690- let similarity = |e : ExpectedFound < Ty < ' tcx > > | {
1691- let ( fk, ek) = ( e. found . kind ( ) , e. expected . kind ( ) ) ;
1692- match ( fk, ek) {
1693- (
1694- ty:: Adt ( adt, _) ,
1695- ty:: Bool | ty:: Char | ty:: Int ( _) | ty:: Uint ( _) | ty:: Float ( _) ,
1696- ) => {
1697- let path = self . tcx . def_path ( adt. did ( ) ) . data ;
1698- let name = path. last ( ) . unwrap ( ) . data . get_opt_name ( ) ;
1699- let prim_sym = primitive_sym ( ek) ;
1659+ Adts { expected : ty:: AdtDef < ' tcx > , found : ty:: AdtDef < ' tcx > } ,
1660+ PrimitiveFound { expected : ty:: AdtDef < ' tcx > , found : Ty < ' tcx > } ,
1661+ PrimitiveExpected { expected : Ty < ' tcx > , found : ty:: AdtDef < ' tcx > } ,
1662+ }
17001663
1701- if name == prim_sym {
1702- return Some ( Similar :: PrimitiveExpected ( * adt, e. expected ) ) ;
1703- }
1704- None
1664+ let similarity = |ExpectedFound { expected, found } : ExpectedFound < Ty < ' tcx > > | {
1665+ if let ty:: Adt ( expected, _) = expected. kind ( ) && let Some ( primitive) = found. primitive_symbol ( ) {
1666+ let path = self . tcx . def_path ( expected. did ( ) ) . data ;
1667+ let name = path. last ( ) . unwrap ( ) . data . get_opt_name ( ) ;
1668+ if name == Some ( primitive) {
1669+ return Some ( Similar :: PrimitiveFound { expected : * expected, found } ) ;
17051670 }
1706- (
1707- ty:: Bool | ty:: Char | ty:: Int ( _) | ty:: Uint ( _) | ty:: Float ( _) ,
1708- ty:: Adt ( adt, _) ,
1709- ) => {
1710- let path = self . tcx . def_path ( adt. did ( ) ) . data ;
1711- let name = path. last ( ) . unwrap ( ) . data . get_opt_name ( ) ;
1712- let prim_sym = primitive_sym ( fk) ;
1713-
1714- if name == prim_sym {
1715- return Some ( Similar :: PrimitiveFound ( e. expected , * adt) ) ;
1716- }
1717- None
1671+ } else if let Some ( primitive) = expected. primitive_symbol ( ) && let ty:: Adt ( found, _) = found. kind ( ) {
1672+ let path = self . tcx . def_path ( found. did ( ) ) . data ;
1673+ let name = path. last ( ) . unwrap ( ) . data . get_opt_name ( ) ;
1674+ if name == Some ( primitive) {
1675+ return Some ( Similar :: PrimitiveExpected { expected, found : * found } ) ;
17181676 }
1719- ( ty:: Adt ( f, _) , ty:: Adt ( e, _) ) => {
1720- if !f. did ( ) . is_local ( ) && f. did ( ) . krate == e. did ( ) . krate {
1721- // Most likely types from different versions of the same crate
1722- // are in play, in which case this message isn't so helpful.
1723- // A "perhaps two different versions..." error is already emitted for that.
1724- return None ;
1725- }
1726- let e_path = self . tcx . def_path ( e. did ( ) ) . data ;
1727- let f_path = self . tcx . def_path ( f. did ( ) ) . data ;
1728- if let ( Some ( e_last) , Some ( f_last) ) = ( e_path. last ( ) , f_path. last ( ) ) && e_last == f_last {
1729- return Some ( Similar :: Adts ( * f, * e) ) ;
1730- }
1731- None
1677+ } else if let ty:: Adt ( expected, _) = expected. kind ( ) && let ty:: Adt ( found, _) = found. kind ( ) {
1678+ if !expected. did ( ) . is_local ( ) && expected. did ( ) . krate == found. did ( ) . krate {
1679+ // Most likely types from different versions of the same crate
1680+ // are in play, in which case this message isn't so helpful.
1681+ // A "perhaps two different versions..." error is already emitted for that.
1682+ return None ;
1683+ }
1684+ let f_path = self . tcx . def_path ( found. did ( ) ) . data ;
1685+ let e_path = self . tcx . def_path ( expected. did ( ) ) . data ;
1686+
1687+ if let ( Some ( e_last) , Some ( f_last) ) = ( e_path. last ( ) , f_path. last ( ) ) && e_last == f_last {
1688+ return Some ( Similar :: Adts { expected : * expected, found : * found} ) ;
17321689 }
1733- _ => None ,
17341690 }
1691+ None
17351692 } ;
17361693
17371694 match terr {
@@ -1759,8 +1716,8 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17591716 } ;
17601717
17611718 let diagnose_adts =
1762- |found_adt : ty:: AdtDef < ' tcx > ,
1763- expected_adt : ty:: AdtDef < ' tcx > ,
1719+ |expected_adt : ty:: AdtDef < ' tcx > ,
1720+ found_adt : ty:: AdtDef < ' tcx > ,
17641721 diagnostic : & mut Diagnostic | {
17651722 let found_name = values. found . sort_string ( self . tcx ) ;
17661723 let expected_name = values. expected . sort_string ( self . tcx ) ;
@@ -1792,14 +1749,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
17921749 } ;
17931750
17941751 match s {
1795- Similar :: Adts ( found_adt , expected_adt ) => {
1796- diagnose_adts ( found_adt , expected_adt , diag)
1752+ Similar :: Adts { expected , found } => {
1753+ diagnose_adts ( expected , found , diag)
17971754 }
1798- Similar :: PrimitiveFound ( prim , e ) => {
1799- diagnose_primitive ( prim, values. expected , e . did ( ) , diag)
1755+ Similar :: PrimitiveFound { expected , found : prim } => {
1756+ diagnose_primitive ( prim, values. expected , expected . did ( ) , diag)
18001757 }
1801- Similar :: PrimitiveExpected ( f , prim) => {
1802- diagnose_primitive ( prim, values. found , f . did ( ) , diag)
1758+ Similar :: PrimitiveExpected { expected : prim, found } => {
1759+ diagnose_primitive ( prim, values. found , found . did ( ) , diag)
18031760 }
18041761 }
18051762 }
0 commit comments