@@ -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;
@@ -987,6 +987,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
987
987
return ;
988
988
}
989
989
let self_ty = trait_ref. self_ty ( ) ;
990
+ let found_ty = trait_ref. args . get ( 1 ) . and_then ( |a| a. as_type ( ) ) ;
990
991
991
992
let mut prev_ty = self . resolve_vars_if_possible (
992
993
typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
@@ -1012,17 +1013,76 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
1012
1013
1013
1014
// The following logic is simlar to `point_at_chain`, but that's focused on associated types
1014
1015
let mut expr = expr;
1015
- while let hir:: ExprKind :: MethodCall ( _path_segment , rcvr_expr, _args , span) = expr. kind {
1016
+ while let hir:: ExprKind :: MethodCall ( path_segment , rcvr_expr, args , span) = expr. kind {
1016
1017
// Point at every method call in the chain with the `Result` type.
1017
1018
// let foo = bar.iter().map(mapper)?;
1018
1019
// ------ -----------
1019
1020
expr = rcvr_expr;
1020
1021
chain. push ( ( span, prev_ty) ) ;
1021
1022
1022
- prev_ty = self . resolve_vars_if_possible (
1023
+ let next_ty = self . resolve_vars_if_possible (
1023
1024
typeck. expr_ty_adjusted_opt ( expr) . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1024
1025
) ;
1025
1026
1027
+ let is_diagnostic_item = |symbol : Symbol , ty : Ty < ' tcx > | {
1028
+ let ty:: Adt ( def, _) = ty. kind ( ) else {
1029
+ return false ;
1030
+ } ;
1031
+ self . tcx . is_diagnostic_item ( symbol, def. did ( ) )
1032
+ } ;
1033
+ // For each method in the chain, see if this is `Result::map_err` or
1034
+ // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1035
+ // trailing `;`.
1036
+ // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1037
+ // accurate check, but we are in the wrong stage to do that and looking for
1038
+ // `Result::map_err` by checking the Self type and the path segment is enough.
1039
+ // sym::ok_or_else
1040
+ if let Some ( ty) = get_e_type ( prev_ty)
1041
+ && let Some ( found_ty) = found_ty
1042
+ && (
1043
+ (
1044
+ path_segment. ident . name == sym:: map_err
1045
+ && is_diagnostic_item ( sym:: Result , next_ty)
1046
+ ) || (
1047
+ path_segment. ident . name == sym:: ok_or_else
1048
+ && is_diagnostic_item ( sym:: Option , next_ty)
1049
+ )
1050
+ )
1051
+ && [ sym:: map_err, sym:: ok_or_else] . contains ( & path_segment. ident . name )
1052
+ && let ty:: Tuple ( tys) = found_ty. kind ( )
1053
+ && tys. is_empty ( )
1054
+ && self . can_eq ( obligation. param_env , ty, found_ty)
1055
+ && args. len ( ) == 1
1056
+ && let Some ( arg) = args. get ( 0 )
1057
+ && let hir:: ExprKind :: Closure ( closure) = arg. kind
1058
+ && let body = self . tcx . hir ( ) . body ( closure. body )
1059
+ && let hir:: ExprKind :: Block ( block, _) = body. value . kind
1060
+ && let None = block. expr
1061
+ && let [ .., stmt] = block. stmts
1062
+ && let hir:: StmtKind :: Semi ( expr) = stmt. kind
1063
+ && let expr_ty = self . resolve_vars_if_possible (
1064
+ typeck. expr_ty_adjusted_opt ( expr)
1065
+ . unwrap_or ( Ty :: new_misc_error ( self . tcx ) ) ,
1066
+ )
1067
+ && self
1068
+ . infcx
1069
+ . type_implements_trait (
1070
+ self . tcx . get_diagnostic_item ( sym:: From ) . unwrap ( ) ,
1071
+ [ self_ty, expr_ty] ,
1072
+ obligation. param_env ,
1073
+ )
1074
+ . must_apply_modulo_regions ( )
1075
+ {
1076
+ err. span_suggestion_short (
1077
+ stmt. span . with_lo ( expr. span . hi ( ) ) ,
1078
+ "remove this semicolon" ,
1079
+ String :: new ( ) ,
1080
+ Applicability :: MachineApplicable ,
1081
+ ) ;
1082
+ }
1083
+
1084
+ prev_ty = next_ty;
1085
+
1026
1086
if let hir:: ExprKind :: Path ( hir:: QPath :: Resolved ( None , path) ) = expr. kind
1027
1087
&& let hir:: Path { res : hir:: def:: Res :: Local ( hir_id) , .. } = path
1028
1088
&& let Some ( hir:: Node :: Pat ( binding) ) = self . tcx . hir ( ) . find ( * hir_id)
0 commit comments