Skip to content

Commit 23fa94f

Browse files
committed
add comments
1 parent 422aace commit 23fa94f

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1092,31 +1092,35 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
10921092
// For each method in the chain, see if this is `Result::map_err` or
10931093
// `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
10941094
// trailing `;`.
1095-
// Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1096-
// accurate check, but we are in the wrong stage to do that and looking for
1097-
// `Result::map_err` by checking the Self type and the path segment is enough.
1098-
// sym::ok_or_else
10991095
if let Some(ty) = get_e_type(prev_ty)
11001096
&& let Some(found_ty) = found_ty
1097+
// Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1098+
// accurate check, but we are in the wrong stage to do that and looking for
1099+
// `Result::map_err` by checking the Self type and the path segment is enough.
1100+
// sym::ok_or_else
11011101
&& (
1102-
(
1102+
( // Result::map_err
11031103
path_segment.ident.name == sym::map_err
11041104
&& is_diagnostic_item(sym::Result, next_ty)
1105-
) || (
1105+
) || ( // Option::ok_or_else
11061106
path_segment.ident.name == sym::ok_or_else
11071107
&& is_diagnostic_item(sym::Option, next_ty)
11081108
)
11091109
)
1110-
&& [sym::map_err, sym::ok_or_else].contains(&path_segment.ident.name)
1110+
// Found `Result<_, ()>?`
11111111
&& let ty::Tuple(tys) = found_ty.kind()
11121112
&& tys.is_empty()
1113+
// The current method call returns `Result<_, ()>`
11131114
&& self.can_eq(obligation.param_env, ty, found_ty)
1115+
// There's a single argument in the method call and it is a closure
11141116
&& args.len() == 1
11151117
&& let Some(arg) = args.get(0)
11161118
&& let hir::ExprKind::Closure(closure) = arg.kind
1119+
// The closure has a block for its body with no tail expression
11171120
&& let body = self.tcx.hir().body(closure.body)
11181121
&& let hir::ExprKind::Block(block, _) = body.value.kind
11191122
&& let None = block.expr
1123+
// The last statement is of a type that can be converted to the return error type
11201124
&& let [.., stmt] = block.stmts
11211125
&& let hir::StmtKind::Semi(expr) = stmt.kind
11221126
&& let expr_ty = self.resolve_vars_if_possible(

0 commit comments

Comments
 (0)