Skip to content

Commit dfe519b

Browse files
committed
Auto merge of #82738 - estebank:tail-expr-check-is-too-slow, r=oli-obk
Move check only relevant in error case out of critical path Move the check for potentially forgotten `return` in a tail expression of arbitrary expressions into the coercion error branch to avoid computing unncessary coercion checks on successful code. Follow up to #81458.
2 parents 51748a8 + c6c243a commit dfe519b

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

compiler/rustc_typeck/src/check/_match.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -207,17 +207,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
207207
),
208208
};
209209
let cause = self.cause(span, code);
210-
let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
211-
Some(ret_coercion) if self.in_tail_expr => {
212-
let ret_ty = ret_coercion.borrow().expected_ty();
213-
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
214-
self.can_coerce(arm_ty, ret_ty)
215-
&& prior_arm_ty.map_or(true, |t| self.can_coerce(t, ret_ty))
216-
// The match arms need to unify for the case of `impl Trait`.
217-
&& !matches!(ret_ty.kind(), ty::Opaque(..))
218-
}
219-
_ => false,
220-
};
221210

222211
// This is the moral equivalent of `coercion.coerce(self, cause, arm.body, arm_ty)`.
223212
// We use it this way to be able to expand on the potential error and detect when a
@@ -229,6 +218,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
229218
Some(&arm.body),
230219
arm_ty,
231220
Some(&mut |err: &mut DiagnosticBuilder<'_>| {
221+
let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
222+
Some(ret_coercion) if self.in_tail_expr => {
223+
let ret_ty = ret_coercion.borrow().expected_ty();
224+
let ret_ty = self.inh.infcx.shallow_resolve(ret_ty);
225+
self.can_coerce(arm_ty, ret_ty)
226+
&& prior_arm_ty.map_or(true, |t| self.can_coerce(t, ret_ty))
227+
// The match arms need to unify for the case of `impl Trait`.
228+
&& !matches!(ret_ty.kind(), ty::Opaque(..))
229+
}
230+
_ => false,
231+
};
232232
if let (Expectation::IsLast(stmt), Some(ret), true) =
233233
(orig_expected, self.ret_type_span, can_coerce_to_return_ty)
234234
{

0 commit comments

Comments
 (0)