@@ -916,16 +916,14 @@ fn report_trait_method_mismatch<'tcx>(
916
916
// When the `impl` receiver is an arbitrary self type, like `self: Box<Self>`, the
917
917
// span points only at the type `Box<Self`>, but we want to cover the whole
918
918
// argument pattern and type.
919
- let span = match tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind {
920
- ImplItemKind :: Fn ( ref sig, body) => tcx
921
- . hir ( )
922
- . body_param_names ( body)
923
- . zip ( sig. decl . inputs . iter ( ) )
924
- . map ( |( param, ty) | param. span . to ( ty. span ) )
925
- . next ( )
926
- . unwrap_or ( impl_err_span) ,
927
- _ => bug ! ( "{:?} is not a method" , impl_m) ,
928
- } ;
919
+ let ImplItemKind :: Fn ( ref sig, body) = tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind else { bug ! ( "{impl_m:?} is not a method" ) } ;
920
+ let span = tcx
921
+ . hir ( )
922
+ . body_param_names ( body)
923
+ . zip ( sig. decl . inputs . iter ( ) )
924
+ . map ( |( param, ty) | param. span . to ( ty. span ) )
925
+ . next ( )
926
+ . unwrap_or ( impl_err_span) ;
929
927
930
928
diag. span_suggestion (
931
929
span,
@@ -938,22 +936,21 @@ fn report_trait_method_mismatch<'tcx>(
938
936
if trait_sig. inputs ( ) . len ( ) == * i {
939
937
// Suggestion to change output type. We do not suggest in `async` functions
940
938
// to avoid complex logic or incorrect output.
941
- match tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind {
942
- ImplItemKind :: Fn ( ref sig, _) if !sig. header . asyncness . is_async ( ) => {
943
- let msg = "change the output type to match the trait" ;
944
- let ap = Applicability :: MachineApplicable ;
945
- match sig. decl . output {
946
- hir:: FnRetTy :: DefaultReturn ( sp) => {
947
- let sugg = format ! ( "-> {} " , trait_sig. output( ) ) ;
948
- diag. span_suggestion_verbose ( sp, msg, sugg, ap) ;
949
- }
950
- hir:: FnRetTy :: Return ( hir_ty) => {
951
- let sugg = trait_sig. output ( ) ;
952
- diag. span_suggestion ( hir_ty. span , msg, sugg, ap) ;
953
- }
954
- } ;
955
- }
956
- _ => { }
939
+ if let ImplItemKind :: Fn ( sig, _) = & tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind
940
+ && !sig. header . asyncness . is_async ( )
941
+ {
942
+ let msg = "change the output type to match the trait" ;
943
+ let ap = Applicability :: MachineApplicable ;
944
+ match sig. decl . output {
945
+ hir:: FnRetTy :: DefaultReturn ( sp) => {
946
+ let sugg = format ! ( "-> {} " , trait_sig. output( ) ) ;
947
+ diag. span_suggestion_verbose ( sp, msg, sugg, ap) ;
948
+ }
949
+ hir:: FnRetTy :: Return ( hir_ty) => {
950
+ let sugg = trait_sig. output ( ) ;
951
+ diag. span_suggestion ( hir_ty. span , msg, sugg, ap) ;
952
+ }
953
+ } ;
957
954
} ;
958
955
} else if let Some ( trait_ty) = trait_sig. inputs ( ) . get ( * i) {
959
956
diag. span_suggestion (
@@ -1080,25 +1077,18 @@ fn extract_spans_for_error_reporting<'tcx>(
1080
1077
trait_m : & ty:: AssocItem ,
1081
1078
) -> ( Span , Option < Span > ) {
1082
1079
let tcx = infcx. tcx ;
1083
- let mut impl_args = match tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind {
1084
- ImplItemKind :: Fn ( ref sig, _) => {
1085
- sig. decl . inputs . iter ( ) . map ( |t| t. span ) . chain ( iter:: once ( sig. decl . output . span ( ) ) )
1086
- }
1087
- _ => bug ! ( "{:?} is not a method" , impl_m) ,
1080
+ let mut impl_args = {
1081
+ let ImplItemKind :: Fn ( sig, _) = & tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind else { bug ! ( "{:?} is not a method" , impl_m) } ;
1082
+ sig. decl . inputs . iter ( ) . map ( |t| t. span ) . chain ( iter:: once ( sig. decl . output . span ( ) ) )
1088
1083
} ;
1089
- let trait_args =
1090
- trait_m. def_id . as_local ( ) . map ( |def_id| match tcx. hir ( ) . expect_trait_item ( def_id) . kind {
1091
- TraitItemKind :: Fn ( ref sig, _) => {
1092
- sig. decl . inputs . iter ( ) . map ( |t| t. span ) . chain ( iter:: once ( sig. decl . output . span ( ) ) )
1093
- }
1094
- _ => bug ! ( "{:?} is not a TraitItemKind::Fn" , trait_m) ,
1095
- } ) ;
1084
+
1085
+ let trait_args = trait_m. def_id . as_local ( ) . map ( |def_id| {
1086
+ let TraitItemKind :: Fn ( sig, _) = & tcx. hir ( ) . expect_trait_item ( def_id) . kind else { bug ! ( "{:?} is not a TraitItemKind::Fn" , trait_m) } ;
1087
+ sig. decl . inputs . iter ( ) . map ( |t| t. span ) . chain ( iter:: once ( sig. decl . output . span ( ) ) )
1088
+ } ) ;
1096
1089
1097
1090
match terr {
1098
- TypeError :: ArgumentMutability ( i) => {
1099
- ( impl_args. nth ( i) . unwrap ( ) , trait_args. and_then ( |mut args| args. nth ( i) ) )
1100
- }
1101
- TypeError :: ArgumentSorts ( ExpectedFound { .. } , i) => {
1091
+ TypeError :: ArgumentMutability ( i) | TypeError :: ArgumentSorts ( ExpectedFound { .. } , i) => {
1102
1092
( impl_args. nth ( i) . unwrap ( ) , trait_args. and_then ( |mut args| args. nth ( i) ) )
1103
1093
}
1104
1094
_ => ( cause. span ( ) , tcx. hir ( ) . span_if_local ( trait_m. def_id ) ) ,
@@ -1158,8 +1148,7 @@ fn compare_self_type<'tcx>(
1158
1148
} else {
1159
1149
err. note_trait_signature ( trait_m. name , trait_m. signature ( tcx) ) ;
1160
1150
}
1161
- let reported = err. emit ( ) ;
1162
- return Err ( reported) ;
1151
+ return Err ( err. emit ( ) ) ;
1163
1152
}
1164
1153
1165
1154
( true , false ) => {
@@ -1178,8 +1167,8 @@ fn compare_self_type<'tcx>(
1178
1167
} else {
1179
1168
err. note_trait_signature ( trait_m. name , trait_m. signature ( tcx) ) ;
1180
1169
}
1181
- let reported = err . emit ( ) ;
1182
- return Err ( reported ) ;
1170
+
1171
+ return Err ( err . emit ( ) ) ;
1183
1172
}
1184
1173
}
1185
1174
@@ -1361,41 +1350,41 @@ fn compare_number_of_method_arguments<'tcx>(
1361
1350
let trait_m_fty = tcx. fn_sig ( trait_m. def_id ) ;
1362
1351
let trait_number_args = trait_m_fty. inputs ( ) . skip_binder ( ) . len ( ) ;
1363
1352
let impl_number_args = impl_m_fty. inputs ( ) . skip_binder ( ) . len ( ) ;
1353
+
1364
1354
if trait_number_args != impl_number_args {
1365
- let trait_span = if let Some ( def_id) = trait_m. def_id . as_local ( ) {
1366
- match tcx. hir ( ) . expect_trait_item ( def_id) . kind {
1367
- TraitItemKind :: Fn ( ref trait_m_sig, _) => {
1368
- let pos = if trait_number_args > 0 { trait_number_args - 1 } else { 0 } ;
1369
- if let Some ( arg) = trait_m_sig. decl . inputs . get ( pos) {
1370
- Some ( if pos == 0 {
1371
- arg. span
1372
- } else {
1373
- arg. span . with_lo ( trait_m_sig. decl . inputs [ 0 ] . span . lo ( ) )
1374
- } )
1355
+ let trait_span = trait_m
1356
+ . def_id
1357
+ . as_local ( )
1358
+ . and_then ( |def_id| {
1359
+ let TraitItemKind :: Fn ( trait_m_sig, _) = & tcx. hir ( ) . expect_trait_item ( def_id) . kind else { bug ! ( "{:?} is not a method" , impl_m) } ;
1360
+ let pos = trait_number_args. saturating_sub ( 1 ) ;
1361
+ trait_m_sig. decl . inputs . get ( pos) . map ( |arg| {
1362
+ if pos == 0 {
1363
+ arg. span
1375
1364
} else {
1376
- trait_item_span
1365
+ arg . span . with_lo ( trait_m_sig . decl . inputs [ 0 ] . span . lo ( ) )
1377
1366
}
1378
- }
1379
- _ => bug ! ( "{:?} is not a method" , impl_m) ,
1380
- }
1381
- } else {
1382
- trait_item_span
1383
- } ;
1384
- let impl_span = match tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind {
1385
- ImplItemKind :: Fn ( ref impl_m_sig, _) => {
1386
- let pos = if impl_number_args > 0 { impl_number_args - 1 } else { 0 } ;
1387
- if let Some ( arg) = impl_m_sig. decl . inputs . get ( pos) {
1367
+ } )
1368
+ } )
1369
+ . or ( trait_item_span) ;
1370
+
1371
+ let impl_span = {
1372
+ let ImplItemKind :: Fn ( impl_m_sig, _) = & tcx. hir ( ) . expect_impl_item ( impl_m. def_id . expect_local ( ) ) . kind else { bug ! ( "{:?} is not a method" , impl_m) } ;
1373
+ let pos = impl_number_args. saturating_sub ( 1 ) ;
1374
+ impl_m_sig
1375
+ . decl
1376
+ . inputs
1377
+ . get ( pos)
1378
+ . map ( |arg| {
1388
1379
if pos == 0 {
1389
1380
arg. span
1390
1381
} else {
1391
1382
arg. span . with_lo ( impl_m_sig. decl . inputs [ 0 ] . span . lo ( ) )
1392
1383
}
1393
- } else {
1394
- impl_m_span
1395
- }
1396
- }
1397
- _ => bug ! ( "{:?} is not a method" , impl_m) ,
1384
+ } )
1385
+ . unwrap_or ( impl_m_span)
1398
1386
} ;
1387
+
1399
1388
let mut err = struct_span_err ! (
1400
1389
tcx. sess,
1401
1390
impl_span,
@@ -1406,6 +1395,7 @@ fn compare_number_of_method_arguments<'tcx>(
1406
1395
tcx. def_path_str( trait_m. def_id) ,
1407
1396
trait_number_args
1408
1397
) ;
1398
+
1409
1399
if let Some ( trait_span) = trait_span {
1410
1400
err. span_label (
1411
1401
trait_span,
@@ -1417,6 +1407,7 @@ fn compare_number_of_method_arguments<'tcx>(
1417
1407
} else {
1418
1408
err. note_trait_signature ( trait_m. name , trait_m. signature ( tcx) ) ;
1419
1409
}
1410
+
1420
1411
err. span_label (
1421
1412
impl_span,
1422
1413
format ! (
@@ -1425,8 +1416,8 @@ fn compare_number_of_method_arguments<'tcx>(
1425
1416
impl_number_args
1426
1417
) ,
1427
1418
) ;
1428
- let reported = err . emit ( ) ;
1429
- return Err ( reported ) ;
1419
+
1420
+ return Err ( err . emit ( ) ) ;
1430
1421
}
1431
1422
1432
1423
Ok ( ( ) )
@@ -1515,10 +1506,9 @@ fn compare_synthetic_generics<'tcx>(
1515
1506
let _: Option < _ > = try {
1516
1507
let impl_m = impl_m. def_id . as_local ( ) ?;
1517
1508
let impl_m = tcx. hir ( ) . expect_impl_item ( impl_m) ;
1518
- let input_tys = match impl_m. kind {
1519
- hir:: ImplItemKind :: Fn ( ref sig, _) => sig. decl . inputs ,
1520
- _ => unreachable ! ( ) ,
1521
- } ;
1509
+ let hir:: ImplItemKind :: Fn ( sig, _) = & impl_m. kind else { unreachable ! ( ) } ;
1510
+ let input_tys = sig. decl . inputs ;
1511
+
1522
1512
struct Visitor ( Option < Span > , hir:: def_id:: LocalDefId ) ;
1523
1513
impl < ' v > intravisit:: Visitor < ' v > for Visitor {
1524
1514
fn visit_ty ( & mut self , ty : & ' v hir:: Ty < ' v > ) {
@@ -1532,6 +1522,7 @@ fn compare_synthetic_generics<'tcx>(
1532
1522
}
1533
1523
}
1534
1524
}
1525
+
1535
1526
let mut visitor = Visitor ( None , impl_def_id) ;
1536
1527
for ty in input_tys {
1537
1528
intravisit:: Visitor :: visit_ty ( & mut visitor, ty) ;
@@ -1556,8 +1547,7 @@ fn compare_synthetic_generics<'tcx>(
1556
1547
}
1557
1548
_ => unreachable ! ( ) ,
1558
1549
}
1559
- let reported = err. emit ( ) ;
1560
- error_found = Some ( reported) ;
1550
+ error_found = Some ( err. emit ( ) ) ;
1561
1551
}
1562
1552
}
1563
1553
if let Some ( reported) = error_found { Err ( reported) } else { Ok ( ( ) ) }
@@ -1717,10 +1707,8 @@ pub(super) fn compare_impl_const_raw(
1717
1707
) ;
1718
1708
1719
1709
// Locate the Span containing just the type of the offending impl
1720
- match tcx. hir ( ) . expect_impl_item ( impl_const_item_def) . kind {
1721
- ImplItemKind :: Const ( ref ty, _) => cause. span = ty. span ,
1722
- _ => bug ! ( "{:?} is not a impl const" , impl_const_item) ,
1723
- }
1710
+ let ImplItemKind :: Const ( ty, _) = tcx. hir ( ) . expect_impl_item ( impl_const_item_def) . kind else { bug ! ( "{impl_const_item:?} is not a impl const" ) } ;
1711
+ cause. span = ty. span ;
1724
1712
1725
1713
let mut diag = struct_span_err ! (
1726
1714
tcx. sess,
@@ -1732,10 +1720,8 @@ pub(super) fn compare_impl_const_raw(
1732
1720
1733
1721
let trait_c_span = trait_const_item_def. as_local ( ) . map ( |trait_c_def_id| {
1734
1722
// Add a label to the Span containing just the type of the const
1735
- match tcx. hir ( ) . expect_trait_item ( trait_c_def_id) . kind {
1736
- TraitItemKind :: Const ( ref ty, _) => ty. span ,
1737
- _ => bug ! ( "{:?} is not a trait const" , trait_const_item) ,
1738
- }
1723
+ let TraitItemKind :: Const ( ty, _) = tcx. hir ( ) . expect_trait_item ( trait_c_def_id) . kind else { bug ! ( "{trait_const_item:?} is not a trait const" ) } ;
1724
+ ty. span
1739
1725
} ) ;
1740
1726
1741
1727
infcx. err_ctxt ( ) . note_type_err (
0 commit comments