Skip to content

Commit 0e8b80a

Browse files
committed
add comments
1 parent 0712fde commit 0e8b80a

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1051,31 +1051,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10511051
// For each method in the chain, see if this is `Result::map_err` or
10521052
// `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
10531053
// trailing `;`.
1054-
// Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1055-
// accurate check, but we are in the wrong stage to do that and looking for
1056-
// `Result::map_err` by checking the Self type and the path segment is enough.
1057-
// sym::ok_or_else
10581054
if let Some(ty) = get_e_type(prev_ty)
10591055
&& let Some(found_ty) = found_ty
1056+
// Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1057+
// accurate check, but we are in the wrong stage to do that and looking for
1058+
// `Result::map_err` by checking the Self type and the path segment is enough.
1059+
// sym::ok_or_else
10601060
&& (
1061-
(
1061+
( // Result::map_err
10621062
path_segment.ident.name == sym::map_err
10631063
&& is_diagnostic_item(sym::Result, next_ty)
1064-
) || (
1064+
) || ( // Option::ok_or_else
10651065
path_segment.ident.name == sym::ok_or_else
10661066
&& is_diagnostic_item(sym::Option, next_ty)
10671067
)
10681068
)
1069-
&& [sym::map_err, sym::ok_or_else].contains(&path_segment.ident.name)
1069+
// Found `Result<_, ()>?`
10701070
&& let ty::Tuple(tys) = found_ty.kind()
10711071
&& tys.is_empty()
1072+
// The current method call returns `Result<_, ()>`
10721073
&& self.can_eq(obligation.param_env, ty, found_ty)
1074+
// There's a single argument in the method call and it is a closure
10731075
&& args.len() == 1
10741076
&& let Some(arg) = args.get(0)
10751077
&& let hir::ExprKind::Closure(closure) = arg.kind
1078+
// The closure has a block for its body with no tail expression
10761079
&& let body = self.tcx.hir().body(closure.body)
10771080
&& let hir::ExprKind::Block(block, _) = body.value.kind
10781081
&& let None = block.expr
1082+
// The last statement is of a type that can be converted to the return error type
10791083
&& let [.., stmt] = block.stmts
10801084
&& let hir::StmtKind::Semi(expr) = stmt.kind
10811085
&& let expr_ty = self.resolve_vars_if_possible(

0 commit comments

Comments
 (0)