@@ -41,7 +41,7 @@ use rustc_session::config::{DumpSolverProofTree, TraitSolver};
41
41
use rustc_session:: Limit ;
42
42
use rustc_span:: def_id:: LOCAL_CRATE ;
43
43
use rustc_span:: symbol:: sym;
44
- use rustc_span:: { BytePos , ExpnKind , Span , DUMMY_SP } ;
44
+ use rustc_span:: { BytePos , ExpnKind , Span , Symbol , DUMMY_SP } ;
45
45
use std:: borrow:: Cow ;
46
46
use std:: fmt;
47
47
use std:: iter;
@@ -1004,6 +1004,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1004
1004
return ;
1005
1005
}
1006
1006
let self_ty = trait_ref. self_ty ( ) ;
1007
+ let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
1007
1008
1008
1009
let mut prev_ty = self . resolve_vars_if_possible (
1009
1010
typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
@@ -1029,17 +1030,76 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1029
1030
1030
1031
// The following logic is simlar to `point_at_chain`, but that's focused on associated types
1031
1032
let mut expr = expr;
1032
- while let hir:: ExprKind :: MethodCall ( _path_segment , rcvr_expr, _args , span) = expr. kind {
1033
+ while let hir:: ExprKind :: MethodCall ( path_segment , rcvr_expr, args , span) = expr. kind {
1033
1034
// Point at every method call in the chain with the `Result` type.
1034
1035
// let foo = bar.iter().map(mapper)?;
1035
1036
// ------ -----------
1036
1037
expr = rcvr_expr;
1037
1038
chain. push ( ( span, prev_ty) ) ;
1038
1039
1039
- prev_ty = self . resolve_vars_if_possible (
1040
+ let next_ty = self . resolve_vars_if_possible (
1040
1041
typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1041
1042
) ;
1042
1043
1044
+ let is_diagnostic_item = |symbol : Symbol , ty : Ty < ' tcx > | {
1045
+ let ty:: Adt ( def, _) = ty. kind ( ) else {
1046
+ return false ;
1047
+ } ;
1048
+ self . tcx . is_diagnostic_item ( symbol, def. did ( ) )
1049
+ } ;
1050
+ // For each method in the chain, see if this is `Result::map_err` or
1051
+ // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1052
+ // trailing `;`.
1053
+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1054
+ // accurate check, but we are in the wrong stage to do that and looking for
1055
+ // `Result::map_err` by checking the Self type and the path segment is enough.
1056
+ // sym::ok_or_else
1057
+ if let Some ( ty) = get_e_type ( prev_ty)
1058
+ && let Some ( found_ty) = found_ty
1059
+ && (
1060
+ (
1061
+ path_segment. ident . name == sym:: map_err
1062
+ && is_diagnostic_item ( sym:: Result , next_ty)
1063
+ ) || (
1064
+ path_segment. ident . name == sym:: ok_or_else
1065
+ && is_diagnostic_item ( sym:: Option , next_ty)
1066
+ )
1067
+ )
1068
+ && [ sym:: map_err, sym:: ok_or_else] . contains ( & path_segment. ident . name )
1069
+ && let ty:: Tuple ( tys) = found_ty. kind ( )
1070
+ && tys. is_empty ( )
1071
+ && self . can_eq ( obligation. param_env , ty, found_ty)
1072
+ && args. len ( ) == 1
1073
+ && let Some ( arg) = args. get ( 0 )
1074
+ && let hir:: ExprKind :: Closure ( closure) = arg. kind
1075
+ && let body = self . tcx . hir ( ) . body ( closure. body )
1076
+ && let hir:: ExprKind :: Block ( block, _) = body. value . kind
1077
+ && let None = block. expr
1078
+ && let [ .., stmt] = block. stmts
1079
+ && let hir:: StmtKind :: Semi ( expr) = stmt. kind
1080
+ && let expr_ty = self . resolve_vars_if_possible (
1081
+ typeck. expr_ty_adjusted_opt ( expr)
1082
+ . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1083
+ )
1084
+ && self
1085
+ . infcx
1086
+ . type_implements_trait (
1087
+ self . tcx . get_diagnostic_item ( sym:: From ) . unwrap ( ) ,
1088
+ [ self_ty, expr_ty] ,
1089
+ obligation. param_env ,
1090
+ )
1091
+ . must_apply_modulo_regions ( )
1092
+ {
1093
+ err. span_suggestion_short (
1094
+ stmt. span . with_lo ( expr. span . hi ( ) ) ,
1095
+ "remove this semicolon" ,
1096
+ String :: new ( ) ,
1097
+ Applicability :: MachineApplicable ,
1098
+ ) ;
1099
+ }
1100
+
1101
+ prev_ty = next_ty;
1102
+
1043
1103
if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = expr. kind
1044
1104
&& let hir:: Path { res : hir:: def:: Res :: Local ( hir_id) , .. } = path
1045
1105
&& let Some ( hir:: Node :: Pat ( binding) ) = self . tcx . hir ( ) . find ( * hir_id)
0 commit comments