1use rustc_errors::codes::*;
2use rustc_errors::{Applicability, Diag};
3use rustc_hir::def::{CtorOf, DefKind, Res};
4use rustc_hir::def_id::LocalDefId;
5use rustc_hir::{selfas hir, ExprKind, HirId, PatKind};
6use rustc_hir_pretty::ty_to_string;
7use rustc_middle::ty::{self, Ty};
8use rustc_span::{Span, sym};
9use rustc_trait_selection::traits::{
10MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
11};
12use tracing::{debug, instrument};
1314use crate::coercion::CoerceMany;
15use crate::{Diverges, Expectation, FnCtxt, GatherLocalsVisitor, Needs};
1617impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18x;#[instrument(skip(self), level = "debug", ret)]19pub(crate) fn check_expr_match(
20&self,
21 expr: &'tcx hir::Expr<'tcx>,
22 scrut: &'tcx hir::Expr<'tcx>,
23 arms: &'tcx [hir::Arm<'tcx>],
24 orig_expected: Expectation<'tcx>,
25 match_src: hir::MatchSource,
26 ) -> Ty<'tcx> {
27let tcx = self.tcx;
2829let acrb = arms_contain_ref_bindings(arms);
30let scrutinee_ty = self.demand_scrutinee_type(scrut, acrb, arms.is_empty());
31debug!(?scrutinee_ty);
3233// If there are no arms, that is a diverging match; a special case.
34if arms.is_empty() {
35self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
36return tcx.types.never;
37 }
3839self.warn_arms_when_scrutinee_diverges(arms);
4041// Otherwise, we have to union together the types that the arms produce and so forth.
42let scrut_diverges = self.diverges.replace(Diverges::Maybe);
4344// #55810: Type check patterns first so we get types for all bindings.
45let scrut_span = scrut.span.find_ancestor_inside(expr.span).unwrap_or(scrut.span);
46for arm in arms {
47 GatherLocalsVisitor::gather_from_arm(self, arm);
4849self.check_pat_top(arm.pat, scrutinee_ty, Some(scrut_span), Some(scrut), None);
50 }
5152// Now typecheck the blocks.
53 //
54 // The result of the match is the common supertype of all the
55 // arms. Start out the value as bottom, since it's the, well,
56 // bottom the type lattice, and we'll be moving up the lattice as
57 // we process each arm. (Note that any match with 0 arms is matching
58 // on any empty type and is therefore unreachable; should the flow
59 // of execution reach it, we will panic, so bottom is an appropriate
60 // type in that case)
61let mut all_arms_diverge = Diverges::WarnedAlways;
6263let expected =
64 orig_expected.try_structurally_resolve_and_adjust_for_branches(self, expr.span);
65debug!(?expected);
6667let mut coercion = {
68let coerce_first = match expected {
69// We don't coerce to `()` so that if the match expression is a
70 // statement it's branches can have any consistent type. That allows
71 // us to give better error messages (pointing to a usually better
72 // arm for inconsistent arms or to the whole match when a `()` type
73 // is required).
74Expectation::ExpectHasType(ety) if ety != tcx.types.unit => ety,
75_ => self.next_ty_var(expr.span),
76 };
77 CoerceMany::with_capacity(coerce_first, arms.len())
78 };
7980let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
81let mut prior_arm = None;
82for arm in arms {
83self.diverges.set(Diverges::Maybe);
8485if let Some(e) = &arm.guard {
86self.check_expr_has_type_or_error(e, tcx.types.bool, |_| {});
8788// FIXME: If this is the first arm and the pattern is irrefutable,
89 // e.g. `_` or `x`, and the guard diverges, then the whole match
90 // may also be considered to diverge. We should warn on all subsequent
91 // arms, too, just like we do for diverging scrutinees above.
92}
9394// N.B. We don't reset diverges here b/c we want to warn in the arm
95 // if the guard diverges, like: `x if { loop {} } => f()`, and we
96 // also want to consider the arm to diverge itself.
9798let arm_ty = self.check_expr_with_expectation(arm.body, expected);
99 all_arms_diverge &= self.diverges.get();
100let tail_defines_return_position_impl_trait =
101self.return_position_impl_trait_from_match_expectation(orig_expected);
102103let (arm_block_id, arm_span) = if let hir::ExprKind::Block(blk, _) = arm.body.kind {
104 (Some(blk.hir_id), self.find_block_span(blk))
105 } else {
106 (None, arm.body.span)
107 };
108109let code = match prior_arm {
110// The reason for the first arm to fail is not that the match arms diverge,
111 // but rather that there's a prior obligation that doesn't hold.
112None => ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src),
113Some((prior_arm_block_id, prior_arm_ty, prior_arm_span)) => {
114 ObligationCauseCode::MatchExpressionArm(Box::new(MatchExpressionArmCause {
115 arm_block_id,
116 arm_span,
117 arm_ty,
118 prior_arm_block_id,
119 prior_arm_ty,
120 prior_arm_span,
121 scrut_span: scrut.span,
122 expr_span: expr.span,
123 source: match_src,
124 prior_non_diverging_arms: prior_non_diverging_arms.clone(),
125 tail_defines_return_position_impl_trait,
126 }))
127 }
128 };
129let cause = self.cause(arm_span, code);
130131// This is the moral equivalent of `coercion.coerce(self, cause, arm.body, arm_ty)`.
132 // We use it this way to be able to expand on the potential error and detect when a
133 // `match` tail statement could be a tail expression instead. If so, we suggest
134 // removing the stray semicolon.
135coercion.coerce_inner(
136self,
137&cause,
138Some(arm.body),
139 arm_ty,
140 |err| {
141self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
142 },
143false,
144 );
145146if !arm_ty.is_never() {
147// When a match arm has type `!`, then it doesn't influence the expected type for
148 // the following arm. If all of the prior arms are `!`, then the influence comes
149 // from elsewhere and we shouldn't point to any previous arm.
150prior_arm = Some((arm_block_id, arm_ty, arm_span));
151152 prior_non_diverging_arms.push(arm_span);
153if prior_non_diverging_arms.len() > 5 {
154 prior_non_diverging_arms.remove(0);
155 }
156 }
157 }
158159// If all of the arms in the `match` diverge,
160 // and we're dealing with an actual `match` block
161 // (as opposed to a `match` desugared from something else'),
162 // we can emit a better note. Rather than pointing
163 // at a diverging expression in an arbitrary arm,
164 // we can point at the entire `match` expression
165if let (Diverges::Always { .. }, hir::MatchSource::Normal) = (all_arms_diverge, match_src) {
166 all_arms_diverge = Diverges::Always {
167 span: expr.span,
168 custom_note: Some(
169"any code following this `match` expression is unreachable, as all arms diverge",
170 ),
171 };
172 }
173174// We won't diverge unless the scrutinee or all arms diverge.
175self.diverges.set(scrut_diverges | all_arms_diverge);
176177 coercion.complete(self)
178 }
179180fn explain_never_type_coerced_to_unit(
181&self,
182 err: &mut Diag<'_>,
183 arm: &hir::Arm<'tcx>,
184 arm_ty: Ty<'tcx>,
185 prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
186 expr: &hir::Expr<'tcx>,
187 ) {
188if let hir::ExprKind::Block(block, _) = arm.body.kind
189 && let Some(expr) = block.expr
190 && let arm_tail_ty = self.node_ty(expr.hir_id)
191 && arm_tail_ty.is_never()
192 && !arm_ty.is_never()
193 {
194err.span_label(
195expr.span,
196::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this expression is of type `!`, but it is coerced to `{0}` due to its surrounding expression",
arm_ty))
})format!(
197"this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
198 surrounding expression",
199 ),
200 );
201self.suggest_mismatched_types_on_tail(
202err,
203expr,
204arm_ty,
205prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
206expr.hir_id,
207 );
208 }
209self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
210 }
211212fn suggest_removing_semicolon_for_coerce(
213&self,
214 diag: &mut Diag<'_>,
215 expr: &hir::Expr<'tcx>,
216 arm_ty: Ty<'tcx>,
217 prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
218 ) {
219// First, check that we're actually in the tail of a function.
220let Some(body) = self.tcx.hir_maybe_body_owned_by(self.body_id) else {
221return;
222 };
223let hir::ExprKind::Block(block, _) = body.value.kind else {
224return;
225 };
226let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), span: semi_span, .. }) =
227block.innermost_block().stmts.last()
228else {
229return;
230 };
231if last_expr.hir_id != expr.hir_id {
232return;
233 }
234235// Next, make sure that we have no type expectation.
236let Some(ret) =
237self.tcx.hir_node_by_def_id(self.body_id).fn_decl().map(|decl| decl.output.span())
238else {
239return;
240 };
241242let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
243Some(ret_coercion) => {
244let ret_ty = ret_coercion.borrow().expected_ty();
245let ret_ty = self.infcx.shallow_resolve(ret_ty);
246self.may_coerce(arm_ty, ret_ty)
247 && prior_arm.is_none_or(|(_, ty, _)| self.may_coerce(ty, ret_ty))
248// The match arms need to unify for the case of `impl Trait`.
249 && !#[allow(non_exhaustive_omitted_patterns)] match ret_ty.kind() {
ty::Alias(ty::Opaque, ..) => true,
_ => false,
}matches!(ret_ty.kind(), ty::Alias(ty::Opaque, ..))250 }
251_ => false,
252 };
253if !can_coerce_to_return_ty {
254return;
255 }
256257let semi = expr.span.shrink_to_hi().with_hi(semi_span.hi());
258let sugg = crate::errors::RemoveSemiForCoerce { expr: expr.span, ret, semi };
259diag.subdiagnostic(sugg);
260 }
261262/// When the previously checked expression (the scrutinee) diverges,
263 /// warn the user about the match arms being unreachable.
264fn warn_arms_when_scrutinee_diverges(&self, arms: &'tcx [hir::Arm<'tcx>]) {
265for arm in arms {
266self.warn_if_unreachable(arm.body.hir_id, arm.body.span, "arm");
267 }
268 }
269270/// Handle the fallback arm of a desugared if(-let) like a missing else.
271 ///
272 /// Returns `true` if there was an error forcing the coercion to the `()` type.
273pub(super) fn if_fallback_coercion(
274&self,
275 if_span: Span,
276 cond_expr: &'tcx hir::Expr<'tcx>,
277 then_expr: &'tcx hir::Expr<'tcx>,
278 coercion: &mut CoerceMany<'tcx>,
279 ) -> bool {
280// If this `if` expr is the parent's function return expr,
281 // the cause of the type coercion is the return type, point at it. (#25228)
282let hir_id = self.tcx.parent_hir_id(self.tcx.parent_hir_id(then_expr.hir_id));
283let ret_reason = self.maybe_get_coercion_reason(hir_id, if_span);
284let cause = self.cause(if_span, ObligationCauseCode::IfExpressionWithNoElse);
285let mut error = false;
286coercion.coerce_forced_unit(
287self,
288&cause,
289 |err| self.explain_if_expr(err, ret_reason, if_span, cond_expr, then_expr, &mut error),
290false,
291 );
292error293 }
294295/// Check if the span comes from an assert-like macro expansion.
296fn is_from_assert_macro(&self, span: Span) -> bool {
297span.ctxt().outer_expn_data().macro_def_id.is_some_and(|def_id| {
298#[allow(non_exhaustive_omitted_patterns)] match self.tcx.get_diagnostic_name(def_id)
{
Some(sym::assert_macro | sym::debug_assert_macro | sym::assert_eq_macro |
sym::assert_ne_macro | sym::debug_assert_eq_macro |
sym::debug_assert_ne_macro) => true,
_ => false,
}matches!(
299self.tcx.get_diagnostic_name(def_id),
300Some(
301 sym::assert_macro
302 | sym::debug_assert_macro
303 | sym::assert_eq_macro
304 | sym::assert_ne_macro
305 | sym::debug_assert_eq_macro
306 | sym::debug_assert_ne_macro
307 )
308 )309 })
310 }
311312/// Explain why `if` expressions without `else` evaluate to `()` and detect likely irrefutable
313 /// `if let PAT = EXPR {}` expressions that could be turned into `let PAT = EXPR;`.
314fn explain_if_expr(
315&self,
316 err: &mut Diag<'_>,
317 ret_reason: Option<(Span, String)>,
318 if_span: Span,
319 cond_expr: &'tcx hir::Expr<'tcx>,
320 then_expr: &'tcx hir::Expr<'tcx>,
321 error: &mut bool,
322 ) {
323let is_assert_macro = self.is_from_assert_macro(if_span);
324325if let Some((if_span, msg)) = ret_reason {
326err.span_label(if_span, msg);
327 } else if let ExprKind::Block(block, _) = then_expr.kind
328 && let Some(expr) = block.expr
329 {
330err.span_label(expr.span, "found here");
331 }
332333if is_assert_macro {
334err.code(E0308);
335err.primary_message("mismatched types");
336 } else {
337err.note("`if` expressions without `else` evaluate to `()`");
338err.help("consider adding an `else` block that evaluates to the expected type");
339 }
340*error = true;
341if let ExprKind::Let(hir::LetExpr { span, pat, init, .. }) = cond_expr.kind
342 && let ExprKind::Block(block, _) = then_expr.kind
343// Refutability checks occur on the MIR, so we approximate it here by checking
344 // if we have an enum with a single variant or a struct in the pattern.
345&& let PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..) = pat.kind
346 && let hir::QPath::Resolved(_, path) = qpath347 {
348match path.res {
349 Res::Def(DefKind::Ctor(CtorOf::Struct, _), _) => {
350// Structs are always irrefutable. Their fields might not be, but we
351 // don't check for that here, it's only an approximation.
352}
353 Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id)
354if self355 .tcx
356 .adt_def(self.tcx.parent(self.tcx.parent(def_id)))
357 .variants()
358 .len()
359 == 1 =>
360 {
361// There's only a single variant in the `enum`, so we can suggest the
362 // irrefutable `let` instead of `if let`.
363}
364_ => return,
365 }
366367let mut sugg = <[_]>::into_vec(::alloc::boxed::box_new([(if_span.until(*span),
String::new())]))vec![
368// Remove the `if`
369(if_span.until(*span), String::new()),
370 ];
371match (block.stmts, block.expr) {
372 ([first, ..], Some(expr)) => {
373let padding = self374 .tcx
375 .sess
376 .source_map()
377 .indentation_before(first.span)
378 .unwrap_or_else(|| String::new());
379sugg.extend([
380 (init.span.between(first.span), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(";\n{0}", padding))
})format!(";\n{padding}")),
381 (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
382 ]);
383 }
384 ([], Some(expr)) => {
385let padding = self386 .tcx
387 .sess
388 .source_map()
389 .indentation_before(expr.span)
390 .unwrap_or_else(|| String::new());
391sugg.extend([
392 (init.span.between(expr.span), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(";\n{0}", padding))
})format!(";\n{padding}")),
393 (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
394 ]);
395 }
396// If there's no value in the body, then the `if` expression would already
397 // be of type `()`, so checking for those cases is unnecessary.
398(_, None) => return,
399 }
400err.multipart_suggestion(
401"consider using an irrefutable `let` binding instead",
402sugg,
403 Applicability::MaybeIncorrect,
404 );
405 }
406 }
407408pub(crate) fn maybe_get_coercion_reason(
409&self,
410 hir_id: hir::HirId,
411 sp: Span,
412 ) -> Option<(Span, String)> {
413let node = self.tcx.hir_node(hir_id);
414if let hir::Node::Block(block) = node {
415// check that the body's parent is an fn
416let parent = self.tcx.parent_hir_node(self.tcx.parent_hir_id(block.hir_id));
417if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. })) =
418 (&block.expr, parent)
419 {
420// check that the `if` expr without `else` is the fn body's expr
421if expr.span == sp {
422return self.get_fn_decl(hir_id).map(|(_, fn_decl)| {
423let (ty, span) = match fn_decl.output {
424 hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span),
425 hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span),
426 };
427 (span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected `{0}` because of this return type",
ty))
})format!("expected `{ty}` because of this return type"))
428 });
429 }
430 }
431 }
432if let hir::Node::LetStmt(hir::LetStmt { ty: Some(_), pat, .. }) = node {
433return Some((pat.span, "expected because of this assignment".to_string()));
434 }
435None436 }
437438pub(crate) fn if_cause(
439&self,
440 expr_id: HirId,
441 else_expr: &'tcx hir::Expr<'tcx>,
442 tail_defines_return_position_impl_trait: Option<LocalDefId>,
443 ) -> ObligationCause<'tcx> {
444let error_sp = self.find_block_span_from_hir_id(else_expr.hir_id);
445446// Finally construct the cause:
447self.cause(
448error_sp,
449 ObligationCauseCode::IfExpression { expr_id, tail_defines_return_position_impl_trait },
450 )
451 }
452453pub(super) fn demand_scrutinee_type(
454&self,
455 scrut: &'tcx hir::Expr<'tcx>,
456 contains_ref_bindings: Option<hir::Mutability>,
457 no_arms: bool,
458 ) -> Ty<'tcx> {
459// Not entirely obvious: if matches may create ref bindings, we want to
460 // use the *precise* type of the scrutinee, *not* some supertype, as
461 // the "scrutinee type" (issue #23116).
462 //
463 // arielb1 [writes here in this comment thread][c] that there
464 // is certainly *some* potential danger, e.g., for an example
465 // like:
466 //
467 // [c]: https://github.com/rust-lang/rust/pull/43399#discussion_r130223956
468 //
469 // ```
470 // let Foo(x) = f()[0];
471 // ```
472 //
473 // Then if the pattern matches by reference, we want to match
474 // `f()[0]` as a lexpr, so we can't allow it to be
475 // coerced. But if the pattern matches by value, `f()[0]` is
476 // still syntactically a lexpr, but we *do* want to allow
477 // coercions.
478 //
479 // However, *likely* we are ok with allowing coercions to
480 // happen if there are no explicit ref mut patterns - all
481 // implicit ref mut patterns must occur behind a reference, so
482 // they will have the "correct" variance and lifetime.
483 //
484 // This does mean that the following pattern would be legal:
485 //
486 // ```
487 // struct Foo(Bar);
488 // struct Bar(u32);
489 // impl Deref for Foo {
490 // type Target = Bar;
491 // fn deref(&self) -> &Bar { &self.0 }
492 // }
493 // impl DerefMut for Foo {
494 // fn deref_mut(&mut self) -> &mut Bar { &mut self.0 }
495 // }
496 // fn foo(x: &mut Foo) {
497 // {
498 // let Bar(z): &mut Bar = x;
499 // *z = 42;
500 // }
501 // assert_eq!(foo.0.0, 42);
502 // }
503 // ```
504 //
505 // FIXME(tschottdorf): don't call contains_explicit_ref_binding, which
506 // is problematic as the HIR is being scraped, but ref bindings may be
507 // implicit after #42640. We need to make sure that pat_adjustments
508 // (once introduced) is populated by the time we get here.
509 //
510 // See #44848.
511if let Some(m) = contains_ref_bindings {
512self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
513 } else if no_arms {
514self.check_expr(scrut)
515 } else {
516// ...but otherwise we want to use any supertype of the
517 // scrutinee. This is sort of a workaround, see note (*) in
518 // `check_pat` for some details.
519let scrut_ty = self.next_ty_var(scrut.span);
520self.check_expr_has_type_or_error(scrut, scrut_ty, |_| {});
521scrut_ty522 }
523 }
524525// Does the expectation of the match define an RPIT?
526 // (e.g. we're in the tail of a function body)
527 //
528 // Returns the `LocalDefId` of the RPIT, which is always identity-substituted.
529pub(crate) fn return_position_impl_trait_from_match_expectation(
530&self,
531 expectation: Expectation<'tcx>,
532 ) -> Option<LocalDefId> {
533let expected_ty = expectation.to_option(self)?;
534let (def_id, args) = match *expected_ty.kind() {
535// FIXME: Could also check that the RPIT is not defined
536ty::Alias(ty::Opaque, alias_ty) => (alias_ty.def_id.as_local()?, alias_ty.args),
537// FIXME(-Znext-solver=no): Remove this branch once `replace_opaque_types_with_infer` is gone.
538ty::Infer(ty::TyVar(_)) => self539 .inner
540 .borrow_mut()
541 .opaque_types()
542 .iter_opaque_types()
543 .find(|(_, v)| v.ty == expected_ty)
544 .map(|(k, _)| (k.def_id, k.args))?,
545_ => return None,
546 };
547let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
548self.tcx.local_opaque_ty_origin(def_id)
549else {
550return None;
551 };
552if &args[0..self.tcx.generics_of(parent_def_id).count()]
553 != ty::GenericArgs::identity_for_item(self.tcx, parent_def_id).as_slice()
554 {
555return None;
556 }
557Some(def_id)
558 }
559}
560561fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
562arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
563}