Skip to main content

rustc_hir_typeck/
_match.rs

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