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 =
64            orig_expected.try_structurally_resolve_and_adjust_for_branches(self, expr.span);
65        debug!(?expected);
66
67        let mut coercion = {
68            let 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).
74                Expectation::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        };
79
80        let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
81        let mut prior_arm = None;
82        for arm in arms {
83            self.diverges.set(Diverges::Maybe);
84
85            if let Some(e) = &arm.guard {
86                self.check_expr_has_type_or_error(e, tcx.types.bool, |_| {});
87
88                // 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            }
93
94            // 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.
97
98            let arm_ty = self.check_expr_with_expectation(arm.body, expected);
99            all_arms_diverge &= self.diverges.get();
100            let tail_defines_return_position_impl_trait =
101                self.return_position_impl_trait_from_match_expectation(orig_expected);
102
103            let (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            };
108
109            let 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.
112                None => ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src),
113                Some((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            };
129            let cause = self.cause(arm_span, code);
130
131            // 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.
135            coercion.coerce_inner(
136                self,
137                &cause,
138                Some(arm.body),
139                arm_ty,
140                |err| {
141                    self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
142                },
143                false,
144            );
145
146            if !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.
150                prior_arm = Some((arm_block_id, arm_ty, arm_span));
151
152                prior_non_diverging_arms.push(arm_span);
153                if prior_non_diverging_arms.len() > 5 {
154                    prior_non_diverging_arms.remove(0);
155                }
156            }
157        }
158
159        // 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
165        if 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        }
173
174        // We won't diverge unless the scrutinee or all arms diverge.
175        self.diverges.set(scrut_diverges | all_arms_diverge);
176
177        coercion.complete(self)
178    }
179
180    fn 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    ) {
188        if 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        {
194            err.span_label(
195                expr.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            );
201            self.suggest_mismatched_types_on_tail(
202                err,
203                expr,
204                arm_ty,
205                prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
206                expr.hir_id,
207            );
208        }
209        self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
210    }
211
212    fn 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.
220        let Some(body) = self.tcx.hir_maybe_body_owned_by(self.body_id) else {
221            return;
222        };
223        let hir::ExprKind::Block(block, _) = body.value.kind else {
224            return;
225        };
226        let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), span: semi_span, .. }) =
227            block.innermost_block().stmts.last()
228        else {
229            return;
230        };
231        if last_expr.hir_id != expr.hir_id {
232            return;
233        }
234
235        // Next, make sure that we have no type expectation.
236        let Some(ret) =
237            self.tcx.hir_node_by_def_id(self.body_id).fn_decl().map(|decl| decl.output.span())
238        else {
239            return;
240        };
241
242        let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
243            Some(ret_coercion) => {
244                let ret_ty = ret_coercion.borrow().expected_ty();
245                let ret_ty = self.infcx.shallow_resolve(ret_ty);
246                self.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        };
253        if !can_coerce_to_return_ty {
254            return;
255        }
256
257        let semi = expr.span.shrink_to_hi().with_hi(semi_span.hi());
258        let sugg = crate::errors::RemoveSemiForCoerce { expr: expr.span, ret, semi };
259        diag.subdiagnostic(sugg);
260    }
261
262    /// When the previously checked expression (the scrutinee) diverges,
263    /// warn the user about the match arms being unreachable.
264    fn warn_arms_when_scrutinee_diverges(&self, arms: &'tcx [hir::Arm<'tcx>]) {
265        for arm in arms {
266            self.warn_if_unreachable(arm.body.hir_id, arm.body.span, "arm");
267        }
268    }
269
270    /// 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.
273    pub(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)
282        let hir_id = self.tcx.parent_hir_id(self.tcx.parent_hir_id(then_expr.hir_id));
283        let ret_reason = self.maybe_get_coercion_reason(hir_id, if_span);
284        let cause = self.cause(if_span, ObligationCauseCode::IfExpressionWithNoElse);
285        let mut error = false;
286        coercion.coerce_forced_unit(
287            self,
288            &cause,
289            |err| self.explain_if_expr(err, ret_reason, if_span, cond_expr, then_expr, &mut error),
290            false,
291        );
292        error
293    }
294
295    /// Check if the span comes from an assert-like macro expansion.
296    fn is_from_assert_macro(&self, span: Span) -> bool {
297        span.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!(
299                self.tcx.get_diagnostic_name(def_id),
300                Some(
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    }
311
312    /// 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;`.
314    fn 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    ) {
323        let is_assert_macro = self.is_from_assert_macro(if_span);
324
325        if let Some((if_span, msg)) = ret_reason {
326            err.span_label(if_span, msg);
327        } else if let ExprKind::Block(block, _) = then_expr.kind
328            && let Some(expr) = block.expr
329        {
330            err.span_label(expr.span, "found here");
331        }
332
333        if is_assert_macro {
334            err.code(E0308);
335            err.primary_message("mismatched types");
336        } else {
337            err.note("`if` expressions without `else` evaluate to `()`");
338            err.help("consider adding an `else` block that evaluates to the expected type");
339        }
340        *error = true;
341        if 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) = qpath
347        {
348            match 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)
354                    if self
355                        .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            }
366
367            let 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            ];
371            match (block.stmts, block.expr) {
372                ([first, ..], Some(expr)) => {
373                    let padding = self
374                        .tcx
375                        .sess
376                        .source_map()
377                        .indentation_before(first.span)
378                        .unwrap_or_else(|| String::new());
379                    sugg.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)) => {
385                    let padding = self
386                        .tcx
387                        .sess
388                        .source_map()
389                        .indentation_before(expr.span)
390                        .unwrap_or_else(|| String::new());
391                    sugg.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            }
400            err.multipart_suggestion(
401                "consider using an irrefutable `let` binding instead",
402                sugg,
403                Applicability::MaybeIncorrect,
404            );
405        }
406    }
407
408    pub(crate) fn maybe_get_coercion_reason(
409        &self,
410        hir_id: hir::HirId,
411        sp: Span,
412    ) -> Option<(Span, String)> {
413        let node = self.tcx.hir_node(hir_id);
414        if let hir::Node::Block(block) = node {
415            // check that the body's parent is an fn
416            let parent = self.tcx.parent_hir_node(self.tcx.parent_hir_id(block.hir_id));
417            if 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
421                if expr.span == sp {
422                    return self.get_fn_decl(hir_id).map(|(_, fn_decl)| {
423                        let (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        }
432        if let hir::Node::LetStmt(hir::LetStmt { ty: Some(_), pat, .. }) = node {
433            return Some((pat.span, "expected because of this assignment".to_string()));
434        }
435        None
436    }
437
438    pub(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> {
444        let error_sp = self.find_block_span_from_hir_id(else_expr.hir_id);
445
446        // Finally construct the cause:
447        self.cause(
448            error_sp,
449            ObligationCauseCode::IfExpression { expr_id, tail_defines_return_position_impl_trait },
450        )
451    }
452
453    pub(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.
511        if let Some(m) = contains_ref_bindings {
512            self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
513        } else if no_arms {
514            self.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.
519            let scrut_ty = self.next_ty_var(scrut.span);
520            self.check_expr_has_type_or_error(scrut, scrut_ty, |_| {});
521            scrut_ty
522        }
523    }
524
525    // 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.
529    pub(crate) fn return_position_impl_trait_from_match_expectation(
530        &self,
531        expectation: Expectation<'tcx>,
532    ) -> Option<LocalDefId> {
533        let expected_ty = expectation.to_option(self)?;
534        let (def_id, args) = match *expected_ty.kind() {
535            // FIXME: Could also check that the RPIT is not defined
536            ty::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.
538            ty::Infer(ty::TyVar(_)) => self
539                .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        };
547        let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
548            self.tcx.local_opaque_ty_origin(def_id)
549        else {
550            return None;
551        };
552        if &args[0..self.tcx.generics_of(parent_def_id).count()]
553            != ty::GenericArgs::identity_for_item(self.tcx, parent_def_id).as_slice()
554        {
555            return None;
556        }
557        Some(def_id)
558    }
559}
560
561fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
562    arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
563}