rustc_hir_typeck/
_match.rs

1use rustc_errors::{Applicability, Diag};
2use rustc_hir::def::{CtorOf, DefKind, Res};
3use rustc_hir::def_id::LocalDefId;
4use rustc_hir::{self as hir, ExprKind, PatKind};
5use rustc_hir_pretty::ty_to_string;
6use rustc_middle::ty::{self, Ty};
7use rustc_span::Span;
8use rustc_trait_selection::traits::{
9    IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
10};
11use tracing::{debug, instrument};
12
13use crate::coercion::{AsCoercionSite, CoerceMany};
14use crate::{Diverges, Expectation, FnCtxt, Needs};
15
16impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17    #[instrument(skip(self), level = "debug", ret)]
18    pub(crate) fn check_expr_match(
19        &self,
20        expr: &'tcx hir::Expr<'tcx>,
21        scrut: &'tcx hir::Expr<'tcx>,
22        arms: &'tcx [hir::Arm<'tcx>],
23        orig_expected: Expectation<'tcx>,
24        match_src: hir::MatchSource,
25    ) -> Ty<'tcx> {
26        let tcx = self.tcx;
27
28        let acrb = arms_contain_ref_bindings(arms);
29        let scrutinee_ty = self.demand_scrutinee_type(scrut, acrb, arms.is_empty());
30        debug!(?scrutinee_ty);
31
32        // If there are no arms, that is a diverging match; a special case.
33        if arms.is_empty() {
34            self.diverges.set(self.diverges.get() | Diverges::always(expr.span));
35            return tcx.types.never;
36        }
37
38        self.warn_arms_when_scrutinee_diverges(arms);
39
40        // Otherwise, we have to union together the types that the arms produce and so forth.
41        let scrut_diverges = self.diverges.replace(Diverges::Maybe);
42
43        // #55810: Type check patterns first so we get types for all bindings.
44        let scrut_span = scrut.span.find_ancestor_inside(expr.span).unwrap_or(scrut.span);
45        for arm in arms {
46            self.check_pat_top(arm.pat, scrutinee_ty, Some(scrut_span), Some(scrut), None);
47        }
48
49        // Now typecheck the blocks.
50        //
51        // The result of the match is the common supertype of all the
52        // arms. Start out the value as bottom, since it's the, well,
53        // bottom the type lattice, and we'll be moving up the lattice as
54        // we process each arm. (Note that any match with 0 arms is matching
55        // on any empty type and is therefore unreachable; should the flow
56        // of execution reach it, we will panic, so bottom is an appropriate
57        // type in that case)
58        let mut all_arms_diverge = Diverges::WarnedAlways;
59
60        let expected =
61            orig_expected.try_structurally_resolve_and_adjust_for_branches(self, expr.span);
62        debug!(?expected);
63
64        let mut coercion = {
65            let coerce_first = match expected {
66                // We don't coerce to `()` so that if the match expression is a
67                // statement it's branches can have any consistent type. That allows
68                // us to give better error messages (pointing to a usually better
69                // arm for inconsistent arms or to the whole match when a `()` type
70                // is required).
71                Expectation::ExpectHasType(ety) if ety != tcx.types.unit => ety,
72                _ => self.next_ty_var(expr.span),
73            };
74            CoerceMany::with_coercion_sites(coerce_first, arms)
75        };
76
77        let mut prior_non_diverging_arms = vec![]; // Used only for diagnostics.
78        let mut prior_arm = None;
79        for arm in arms {
80            self.diverges.set(Diverges::Maybe);
81
82            if let Some(e) = &arm.guard {
83                self.check_expr_has_type_or_error(e, tcx.types.bool, |_| {});
84
85                // FIXME: If this is the first arm and the pattern is irrefutable,
86                // e.g. `_` or `x`, and the guard diverges, then the whole match
87                // may also be considered to diverge. We should warn on all subsequent
88                // arms, too, just like we do for diverging scrutinees above.
89            }
90
91            // N.B. We don't reset diverges here b/c we want to warn in the arm
92            // if the guard diverges, like: `x if { loop {} } => f()`, and we
93            // also want to consider the arm to diverge itself.
94
95            let arm_ty = self.check_expr_with_expectation(arm.body, expected);
96            all_arms_diverge &= self.diverges.get();
97            let tail_defines_return_position_impl_trait =
98                self.return_position_impl_trait_from_match_expectation(orig_expected);
99
100            let (arm_block_id, arm_span) = if let hir::ExprKind::Block(blk, _) = arm.body.kind {
101                (Some(blk.hir_id), self.find_block_span(blk))
102            } else {
103                (None, arm.body.span)
104            };
105
106            let code = match prior_arm {
107                // The reason for the first arm to fail is not that the match arms diverge,
108                // but rather that there's a prior obligation that doesn't hold.
109                None => ObligationCauseCode::BlockTailExpression(arm.body.hir_id, match_src),
110                Some((prior_arm_block_id, prior_arm_ty, prior_arm_span)) => {
111                    ObligationCauseCode::MatchExpressionArm(Box::new(MatchExpressionArmCause {
112                        arm_block_id,
113                        arm_span,
114                        arm_ty,
115                        prior_arm_block_id,
116                        prior_arm_ty,
117                        prior_arm_span,
118                        scrut_span: scrut.span,
119                        expr_span: expr.span,
120                        source: match_src,
121                        prior_non_diverging_arms: prior_non_diverging_arms.clone(),
122                        tail_defines_return_position_impl_trait,
123                    }))
124                }
125            };
126            let cause = self.cause(arm_span, code);
127
128            // This is the moral equivalent of `coercion.coerce(self, cause, arm.body, arm_ty)`.
129            // We use it this way to be able to expand on the potential error and detect when a
130            // `match` tail statement could be a tail expression instead. If so, we suggest
131            // removing the stray semicolon.
132            coercion.coerce_inner(
133                self,
134                &cause,
135                Some(arm.body),
136                arm_ty,
137                |err| {
138                    self.explain_never_type_coerced_to_unit(err, arm, arm_ty, prior_arm, expr);
139                },
140                false,
141            );
142
143            if !arm_ty.is_never() {
144                // When a match arm has type `!`, then it doesn't influence the expected type for
145                // the following arm. If all of the prior arms are `!`, then the influence comes
146                // from elsewhere and we shouldn't point to any previous arm.
147                prior_arm = Some((arm_block_id, arm_ty, arm_span));
148
149                prior_non_diverging_arms.push(arm_span);
150                if prior_non_diverging_arms.len() > 5 {
151                    prior_non_diverging_arms.remove(0);
152                }
153            }
154        }
155
156        // If all of the arms in the `match` diverge,
157        // and we're dealing with an actual `match` block
158        // (as opposed to a `match` desugared from something else'),
159        // we can emit a better note. Rather than pointing
160        // at a diverging expression in an arbitrary arm,
161        // we can point at the entire `match` expression
162        if let (Diverges::Always { .. }, hir::MatchSource::Normal) = (all_arms_diverge, match_src) {
163            all_arms_diverge = Diverges::Always {
164                span: expr.span,
165                custom_note: Some(
166                    "any code following this `match` expression is unreachable, as all arms diverge",
167                ),
168            };
169        }
170
171        // We won't diverge unless the scrutinee or all arms diverge.
172        self.diverges.set(scrut_diverges | all_arms_diverge);
173
174        coercion.complete(self)
175    }
176
177    fn explain_never_type_coerced_to_unit(
178        &self,
179        err: &mut Diag<'_>,
180        arm: &hir::Arm<'tcx>,
181        arm_ty: Ty<'tcx>,
182        prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
183        expr: &hir::Expr<'tcx>,
184    ) {
185        if let hir::ExprKind::Block(block, _) = arm.body.kind
186            && let Some(expr) = block.expr
187            && let arm_tail_ty = self.node_ty(expr.hir_id)
188            && arm_tail_ty.is_never()
189            && !arm_ty.is_never()
190        {
191            err.span_label(
192                expr.span,
193                format!(
194                    "this expression is of type `!`, but it is coerced to `{arm_ty}` due to its \
195                     surrounding expression",
196                ),
197            );
198            self.suggest_mismatched_types_on_tail(
199                err,
200                expr,
201                arm_ty,
202                prior_arm.map_or(arm_tail_ty, |(_, ty, _)| ty),
203                expr.hir_id,
204            );
205        }
206        self.suggest_removing_semicolon_for_coerce(err, expr, arm_ty, prior_arm)
207    }
208
209    fn suggest_removing_semicolon_for_coerce(
210        &self,
211        diag: &mut Diag<'_>,
212        expr: &hir::Expr<'tcx>,
213        arm_ty: Ty<'tcx>,
214        prior_arm: Option<(Option<hir::HirId>, Ty<'tcx>, Span)>,
215    ) {
216        let hir = self.tcx.hir();
217
218        // First, check that we're actually in the tail of a function.
219        let Some(body) = 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                    && !matches!(ret_ty.kind(), ty::Alias(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<T>(
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, '_, T>,
278    ) -> bool
279    where
280        T: AsCoercionSite,
281    {
282        // If this `if` expr is the parent's function return expr,
283        // the cause of the type coercion is the return type, point at it. (#25228)
284        let hir_id = self.tcx.parent_hir_id(self.tcx.parent_hir_id(then_expr.hir_id));
285        let ret_reason = self.maybe_get_coercion_reason(hir_id, if_span);
286        let cause = self.cause(if_span, ObligationCauseCode::IfExpressionWithNoElse);
287        let mut error = false;
288        coercion.coerce_forced_unit(
289            self,
290            &cause,
291            |err| self.explain_if_expr(err, ret_reason, if_span, cond_expr, then_expr, &mut error),
292            false,
293        );
294        error
295    }
296
297    /// Explain why `if` expressions without `else` evaluate to `()` and detect likely irrefutable
298    /// `if let PAT = EXPR {}` expressions that could be turned into `let PAT = EXPR;`.
299    fn explain_if_expr(
300        &self,
301        err: &mut Diag<'_>,
302        ret_reason: Option<(Span, String)>,
303        if_span: Span,
304        cond_expr: &'tcx hir::Expr<'tcx>,
305        then_expr: &'tcx hir::Expr<'tcx>,
306        error: &mut bool,
307    ) {
308        if let Some((if_span, msg)) = ret_reason {
309            err.span_label(if_span, msg);
310        } else if let ExprKind::Block(block, _) = then_expr.kind
311            && let Some(expr) = block.expr
312        {
313            err.span_label(expr.span, "found here");
314        }
315        err.note("`if` expressions without `else` evaluate to `()`");
316        err.help("consider adding an `else` block that evaluates to the expected type");
317        *error = true;
318        if let ExprKind::Let(hir::LetExpr { span, pat, init, .. }) = cond_expr.kind
319            && let ExprKind::Block(block, _) = then_expr.kind
320            // Refutability checks occur on the MIR, so we approximate it here by checking
321            // if we have an enum with a single variant or a struct in the pattern.
322            && let PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..) = pat.kind
323            && let hir::QPath::Resolved(_, path) = qpath
324        {
325            match path.res {
326                Res::Def(DefKind::Ctor(CtorOf::Struct, _), _) => {
327                    // Structs are always irrefutable. Their fields might not be, but we
328                    // don't check for that here, it's only an approximation.
329                }
330                Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id)
331                    if self
332                        .tcx
333                        .adt_def(self.tcx.parent(self.tcx.parent(def_id)))
334                        .variants()
335                        .len()
336                        == 1 =>
337                {
338                    // There's only a single variant in the `enum`, so we can suggest the
339                    // irrefutable `let` instead of `if let`.
340                }
341                _ => return,
342            }
343
344            let mut sugg = vec![
345                // Remove the `if`
346                (if_span.until(*span), String::new()),
347            ];
348            match (block.stmts, block.expr) {
349                ([first, ..], Some(expr)) => {
350                    let padding = self
351                        .tcx
352                        .sess
353                        .source_map()
354                        .indentation_before(first.span)
355                        .unwrap_or_else(|| String::new());
356                    sugg.extend([
357                        (init.span.between(first.span), format!(";\n{padding}")),
358                        (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
359                    ]);
360                }
361                ([], Some(expr)) => {
362                    let padding = self
363                        .tcx
364                        .sess
365                        .source_map()
366                        .indentation_before(expr.span)
367                        .unwrap_or_else(|| String::new());
368                    sugg.extend([
369                        (init.span.between(expr.span), format!(";\n{padding}")),
370                        (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
371                    ]);
372                }
373                // If there's no value in the body, then the `if` expression would already
374                // be of type `()`, so checking for those cases is unnecessary.
375                (_, None) => return,
376            }
377            err.multipart_suggestion(
378                "consider using an irrefutable `let` binding instead",
379                sugg,
380                Applicability::MaybeIncorrect,
381            );
382        }
383    }
384
385    pub(crate) fn maybe_get_coercion_reason(
386        &self,
387        hir_id: hir::HirId,
388        sp: Span,
389    ) -> Option<(Span, String)> {
390        let node = self.tcx.hir_node(hir_id);
391        if let hir::Node::Block(block) = node {
392            // check that the body's parent is an fn
393            let parent = self.tcx.parent_hir_node(self.tcx.parent_hir_id(block.hir_id));
394            if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. })) =
395                (&block.expr, parent)
396            {
397                // check that the `if` expr without `else` is the fn body's expr
398                if expr.span == sp {
399                    return self.get_fn_decl(hir_id).map(|(_, fn_decl)| {
400                        let (ty, span) = match fn_decl.output {
401                            hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span),
402                            hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span),
403                        };
404                        (span, format!("expected `{ty}` because of this return type"))
405                    });
406                }
407            }
408        }
409        if let hir::Node::LetStmt(hir::LetStmt { ty: Some(_), pat, .. }) = node {
410            return Some((pat.span, "expected because of this assignment".to_string()));
411        }
412        None
413    }
414
415    pub(crate) fn if_cause(
416        &self,
417        span: Span,
418        cond_span: Span,
419        then_expr: &'tcx hir::Expr<'tcx>,
420        else_expr: &'tcx hir::Expr<'tcx>,
421        then_ty: Ty<'tcx>,
422        else_ty: Ty<'tcx>,
423        tail_defines_return_position_impl_trait: Option<LocalDefId>,
424    ) -> ObligationCause<'tcx> {
425        let mut outer_span = if self.tcx.sess.source_map().is_multiline(span) {
426            // The `if`/`else` isn't in one line in the output, include some context to make it
427            // clear it is an if/else expression:
428            // ```
429            // LL |      let x = if true {
430            //    | _____________-
431            // LL ||         10i32
432            //    ||         ----- expected because of this
433            // LL ||     } else {
434            // LL ||         10u32
435            //    ||         ^^^^^ expected `i32`, found `u32`
436            // LL ||     };
437            //    ||_____- `if` and `else` have incompatible types
438            // ```
439            Some(span)
440        } else {
441            // The entire expression is in one line, only point at the arms
442            // ```
443            // LL |     let x = if true { 10i32 } else { 10u32 };
444            //    |                       -----          ^^^^^ expected `i32`, found `u32`
445            //    |                       |
446            //    |                       expected because of this
447            // ```
448            None
449        };
450
451        let (error_sp, else_id) = if let ExprKind::Block(block, _) = &else_expr.kind {
452            let block = block.innermost_block();
453
454            // Avoid overlapping spans that aren't as readable:
455            // ```
456            // 2 |        let x = if true {
457            //   |   _____________-
458            // 3 |  |         3
459            //   |  |         - expected because of this
460            // 4 |  |     } else {
461            //   |  |____________^
462            // 5 | ||
463            // 6 | ||     };
464            //   | ||     ^
465            //   | ||_____|
466            //   | |______if and else have incompatible types
467            //   |        expected integer, found `()`
468            // ```
469            // by not pointing at the entire expression:
470            // ```
471            // 2 |       let x = if true {
472            //   |               ------- `if` and `else` have incompatible types
473            // 3 |           3
474            //   |           - expected because of this
475            // 4 |       } else {
476            //   |  ____________^
477            // 5 | |
478            // 6 | |     };
479            //   | |_____^ expected integer, found `()`
480            // ```
481            if block.expr.is_none()
482                && block.stmts.is_empty()
483                && let Some(outer_span) = &mut outer_span
484                && let Some(cond_span) = cond_span.find_ancestor_inside(*outer_span)
485            {
486                *outer_span = outer_span.with_hi(cond_span.hi())
487            }
488
489            (self.find_block_span(block), block.hir_id)
490        } else {
491            (else_expr.span, else_expr.hir_id)
492        };
493
494        let then_id = if let ExprKind::Block(block, _) = &then_expr.kind {
495            let block = block.innermost_block();
496            // Exclude overlapping spans
497            if block.expr.is_none() && block.stmts.is_empty() {
498                outer_span = None;
499            }
500            block.hir_id
501        } else {
502            then_expr.hir_id
503        };
504
505        // Finally construct the cause:
506        self.cause(
507            error_sp,
508            ObligationCauseCode::IfExpression(Box::new(IfExpressionCause {
509                else_id,
510                then_id,
511                then_ty,
512                else_ty,
513                outer_span,
514                tail_defines_return_position_impl_trait,
515            })),
516        )
517    }
518
519    pub(super) fn demand_scrutinee_type(
520        &self,
521        scrut: &'tcx hir::Expr<'tcx>,
522        contains_ref_bindings: Option<hir::Mutability>,
523        no_arms: bool,
524    ) -> Ty<'tcx> {
525        // Not entirely obvious: if matches may create ref bindings, we want to
526        // use the *precise* type of the scrutinee, *not* some supertype, as
527        // the "scrutinee type" (issue #23116).
528        //
529        // arielb1 [writes here in this comment thread][c] that there
530        // is certainly *some* potential danger, e.g., for an example
531        // like:
532        //
533        // [c]: https://github.com/rust-lang/rust/pull/43399#discussion_r130223956
534        //
535        // ```
536        // let Foo(x) = f()[0];
537        // ```
538        //
539        // Then if the pattern matches by reference, we want to match
540        // `f()[0]` as a lexpr, so we can't allow it to be
541        // coerced. But if the pattern matches by value, `f()[0]` is
542        // still syntactically a lexpr, but we *do* want to allow
543        // coercions.
544        //
545        // However, *likely* we are ok with allowing coercions to
546        // happen if there are no explicit ref mut patterns - all
547        // implicit ref mut patterns must occur behind a reference, so
548        // they will have the "correct" variance and lifetime.
549        //
550        // This does mean that the following pattern would be legal:
551        //
552        // ```
553        // struct Foo(Bar);
554        // struct Bar(u32);
555        // impl Deref for Foo {
556        //     type Target = Bar;
557        //     fn deref(&self) -> &Bar { &self.0 }
558        // }
559        // impl DerefMut for Foo {
560        //     fn deref_mut(&mut self) -> &mut Bar { &mut self.0 }
561        // }
562        // fn foo(x: &mut Foo) {
563        //     {
564        //         let Bar(z): &mut Bar = x;
565        //         *z = 42;
566        //     }
567        //     assert_eq!(foo.0.0, 42);
568        // }
569        // ```
570        //
571        // FIXME(tschottdorf): don't call contains_explicit_ref_binding, which
572        // is problematic as the HIR is being scraped, but ref bindings may be
573        // implicit after #42640. We need to make sure that pat_adjustments
574        // (once introduced) is populated by the time we get here.
575        //
576        // See #44848.
577        if let Some(m) = contains_ref_bindings {
578            self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
579        } else if no_arms {
580            self.check_expr(scrut)
581        } else {
582            // ...but otherwise we want to use any supertype of the
583            // scrutinee. This is sort of a workaround, see note (*) in
584            // `check_pat` for some details.
585            let scrut_ty = self.next_ty_var(scrut.span);
586            self.check_expr_has_type_or_error(scrut, scrut_ty, |_| {});
587            scrut_ty
588        }
589    }
590
591    // Does the expectation of the match define an RPIT?
592    // (e.g. we're in the tail of a function body)
593    //
594    // Returns the `LocalDefId` of the RPIT, which is always identity-substituted.
595    pub(crate) fn return_position_impl_trait_from_match_expectation(
596        &self,
597        expectation: Expectation<'tcx>,
598    ) -> Option<LocalDefId> {
599        let expected_ty = expectation.to_option(self)?;
600        let (def_id, args) = match *expected_ty.kind() {
601            // FIXME: Could also check that the RPIT is not defined
602            ty::Alias(ty::Opaque, alias_ty) => (alias_ty.def_id.as_local()?, alias_ty.args),
603            // FIXME(-Znext-solver): Remove this branch once `replace_opaque_types_with_infer` is gone.
604            ty::Infer(ty::TyVar(_)) => self
605                .inner
606                .borrow()
607                .iter_opaque_types()
608                .find(|(_, v)| v.ty == expected_ty)
609                .map(|(k, _)| (k.def_id, k.args))?,
610            _ => return None,
611        };
612        let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
613            self.tcx.local_opaque_ty_origin(def_id)
614        else {
615            return None;
616        };
617        if &args[0..self.tcx.generics_of(parent_def_id).count()]
618            != ty::GenericArgs::identity_for_item(self.tcx, parent_def_id).as_slice()
619        {
620            return None;
621        }
622        Some(def_id)
623    }
624}
625
626fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
627    arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
628}