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 // First, check that we're actually in the tail of a function.
217 let Some(body) = self.tcx.hir_maybe_body_owned_by(self.body_id) else {
218 return;
219 };
220 let hir::ExprKind::Block(block, _) = body.value.kind else {
221 return;
222 };
223 let Some(hir::Stmt { kind: hir::StmtKind::Semi(last_expr), span: semi_span, .. }) =
224 block.innermost_block().stmts.last()
225 else {
226 return;
227 };
228 if last_expr.hir_id != expr.hir_id {
229 return;
230 }
231
232 // Next, make sure that we have no type expectation.
233 let Some(ret) =
234 self.tcx.hir_node_by_def_id(self.body_id).fn_decl().map(|decl| decl.output.span())
235 else {
236 return;
237 };
238
239 let can_coerce_to_return_ty = match self.ret_coercion.as_ref() {
240 Some(ret_coercion) => {
241 let ret_ty = ret_coercion.borrow().expected_ty();
242 let ret_ty = self.infcx.shallow_resolve(ret_ty);
243 self.may_coerce(arm_ty, ret_ty)
244 && prior_arm.is_none_or(|(_, ty, _)| self.may_coerce(ty, ret_ty))
245 // The match arms need to unify for the case of `impl Trait`.
246 && !matches!(ret_ty.kind(), ty::Alias(ty::Opaque, ..))
247 }
248 _ => false,
249 };
250 if !can_coerce_to_return_ty {
251 return;
252 }
253
254 let semi = expr.span.shrink_to_hi().with_hi(semi_span.hi());
255 let sugg = crate::errors::RemoveSemiForCoerce { expr: expr.span, ret, semi };
256 diag.subdiagnostic(sugg);
257 }
258
259 /// When the previously checked expression (the scrutinee) diverges,
260 /// warn the user about the match arms being unreachable.
261 fn warn_arms_when_scrutinee_diverges(&self, arms: &'tcx [hir::Arm<'tcx>]) {
262 for arm in arms {
263 self.warn_if_unreachable(arm.body.hir_id, arm.body.span, "arm");
264 }
265 }
266
267 /// Handle the fallback arm of a desugared if(-let) like a missing else.
268 ///
269 /// Returns `true` if there was an error forcing the coercion to the `()` type.
270 pub(super) fn if_fallback_coercion<T>(
271 &self,
272 if_span: Span,
273 cond_expr: &'tcx hir::Expr<'tcx>,
274 then_expr: &'tcx hir::Expr<'tcx>,
275 coercion: &mut CoerceMany<'tcx, '_, T>,
276 ) -> bool
277 where
278 T: AsCoercionSite,
279 {
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 /// Explain why `if` expressions without `else` evaluate to `()` and detect likely irrefutable
296 /// `if let PAT = EXPR {}` expressions that could be turned into `let PAT = EXPR;`.
297 fn explain_if_expr(
298 &self,
299 err: &mut Diag<'_>,
300 ret_reason: Option<(Span, String)>,
301 if_span: Span,
302 cond_expr: &'tcx hir::Expr<'tcx>,
303 then_expr: &'tcx hir::Expr<'tcx>,
304 error: &mut bool,
305 ) {
306 if let Some((if_span, msg)) = ret_reason {
307 err.span_label(if_span, msg);
308 } else if let ExprKind::Block(block, _) = then_expr.kind
309 && let Some(expr) = block.expr
310 {
311 err.span_label(expr.span, "found here");
312 }
313 err.note("`if` expressions without `else` evaluate to `()`");
314 err.help("consider adding an `else` block that evaluates to the expected type");
315 *error = true;
316 if let ExprKind::Let(hir::LetExpr { span, pat, init, .. }) = cond_expr.kind
317 && let ExprKind::Block(block, _) = then_expr.kind
318 // Refutability checks occur on the MIR, so we approximate it here by checking
319 // if we have an enum with a single variant or a struct in the pattern.
320 && let PatKind::TupleStruct(qpath, ..) | PatKind::Struct(qpath, ..) = pat.kind
321 && let hir::QPath::Resolved(_, path) = qpath
322 {
323 match path.res {
324 Res::Def(DefKind::Ctor(CtorOf::Struct, _), _) => {
325 // Structs are always irrefutable. Their fields might not be, but we
326 // don't check for that here, it's only an approximation.
327 }
328 Res::Def(DefKind::Ctor(CtorOf::Variant, _), def_id)
329 if self
330 .tcx
331 .adt_def(self.tcx.parent(self.tcx.parent(def_id)))
332 .variants()
333 .len()
334 == 1 =>
335 {
336 // There's only a single variant in the `enum`, so we can suggest the
337 // irrefutable `let` instead of `if let`.
338 }
339 _ => return,
340 }
341
342 let mut sugg = vec![
343 // Remove the `if`
344 (if_span.until(*span), String::new()),
345 ];
346 match (block.stmts, block.expr) {
347 ([first, ..], Some(expr)) => {
348 let padding = self
349 .tcx
350 .sess
351 .source_map()
352 .indentation_before(first.span)
353 .unwrap_or_else(|| String::new());
354 sugg.extend([
355 (init.span.between(first.span), format!(";\n{padding}")),
356 (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
357 ]);
358 }
359 ([], Some(expr)) => {
360 let padding = self
361 .tcx
362 .sess
363 .source_map()
364 .indentation_before(expr.span)
365 .unwrap_or_else(|| String::new());
366 sugg.extend([
367 (init.span.between(expr.span), format!(";\n{padding}")),
368 (expr.span.shrink_to_hi().with_hi(block.span.hi()), String::new()),
369 ]);
370 }
371 // If there's no value in the body, then the `if` expression would already
372 // be of type `()`, so checking for those cases is unnecessary.
373 (_, None) => return,
374 }
375 err.multipart_suggestion(
376 "consider using an irrefutable `let` binding instead",
377 sugg,
378 Applicability::MaybeIncorrect,
379 );
380 }
381 }
382
383 pub(crate) fn maybe_get_coercion_reason(
384 &self,
385 hir_id: hir::HirId,
386 sp: Span,
387 ) -> Option<(Span, String)> {
388 let node = self.tcx.hir_node(hir_id);
389 if let hir::Node::Block(block) = node {
390 // check that the body's parent is an fn
391 let parent = self.tcx.parent_hir_node(self.tcx.parent_hir_id(block.hir_id));
392 if let (Some(expr), hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. })) =
393 (&block.expr, parent)
394 {
395 // check that the `if` expr without `else` is the fn body's expr
396 if expr.span == sp {
397 return self.get_fn_decl(hir_id).map(|(_, fn_decl)| {
398 let (ty, span) = match fn_decl.output {
399 hir::FnRetTy::DefaultReturn(span) => ("()".to_string(), span),
400 hir::FnRetTy::Return(ty) => (ty_to_string(&self.tcx, ty), ty.span),
401 };
402 (span, format!("expected `{ty}` because of this return type"))
403 });
404 }
405 }
406 }
407 if let hir::Node::LetStmt(hir::LetStmt { ty: Some(_), pat, .. }) = node {
408 return Some((pat.span, "expected because of this assignment".to_string()));
409 }
410 None
411 }
412
413 pub(crate) fn if_cause(
414 &self,
415 span: Span,
416 cond_span: Span,
417 then_expr: &'tcx hir::Expr<'tcx>,
418 else_expr: &'tcx hir::Expr<'tcx>,
419 then_ty: Ty<'tcx>,
420 else_ty: Ty<'tcx>,
421 tail_defines_return_position_impl_trait: Option<LocalDefId>,
422 ) -> ObligationCause<'tcx> {
423 let mut outer_span = if self.tcx.sess.source_map().is_multiline(span) {
424 // The `if`/`else` isn't in one line in the output, include some context to make it
425 // clear it is an if/else expression:
426 // ```
427 // LL | let x = if true {
428 // | _____________-
429 // LL || 10i32
430 // || ----- expected because of this
431 // LL || } else {
432 // LL || 10u32
433 // || ^^^^^ expected `i32`, found `u32`
434 // LL || };
435 // ||_____- `if` and `else` have incompatible types
436 // ```
437 Some(span)
438 } else {
439 // The entire expression is in one line, only point at the arms
440 // ```
441 // LL | let x = if true { 10i32 } else { 10u32 };
442 // | ----- ^^^^^ expected `i32`, found `u32`
443 // | |
444 // | expected because of this
445 // ```
446 None
447 };
448
449 let (error_sp, else_id) = if let ExprKind::Block(block, _) = &else_expr.kind {
450 let block = block.innermost_block();
451
452 // Avoid overlapping spans that aren't as readable:
453 // ```
454 // 2 | let x = if true {
455 // | _____________-
456 // 3 | | 3
457 // | | - expected because of this
458 // 4 | | } else {
459 // | |____________^
460 // 5 | ||
461 // 6 | || };
462 // | || ^
463 // | ||_____|
464 // | |______if and else have incompatible types
465 // | expected integer, found `()`
466 // ```
467 // by not pointing at the entire expression:
468 // ```
469 // 2 | let x = if true {
470 // | ------- `if` and `else` have incompatible types
471 // 3 | 3
472 // | - expected because of this
473 // 4 | } else {
474 // | ____________^
475 // 5 | |
476 // 6 | | };
477 // | |_____^ expected integer, found `()`
478 // ```
479 if block.expr.is_none()
480 && block.stmts.is_empty()
481 && let Some(outer_span) = &mut outer_span
482 && let Some(cond_span) = cond_span.find_ancestor_inside(*outer_span)
483 {
484 *outer_span = outer_span.with_hi(cond_span.hi())
485 }
486
487 (self.find_block_span(block), block.hir_id)
488 } else {
489 (else_expr.span, else_expr.hir_id)
490 };
491
492 let then_id = if let ExprKind::Block(block, _) = &then_expr.kind {
493 let block = block.innermost_block();
494 // Exclude overlapping spans
495 if block.expr.is_none() && block.stmts.is_empty() {
496 outer_span = None;
497 }
498 block.hir_id
499 } else {
500 then_expr.hir_id
501 };
502
503 // Finally construct the cause:
504 self.cause(
505 error_sp,
506 ObligationCauseCode::IfExpression(Box::new(IfExpressionCause {
507 else_id,
508 then_id,
509 then_ty,
510 else_ty,
511 outer_span,
512 tail_defines_return_position_impl_trait,
513 })),
514 )
515 }
516
517 pub(super) fn demand_scrutinee_type(
518 &self,
519 scrut: &'tcx hir::Expr<'tcx>,
520 contains_ref_bindings: Option<hir::Mutability>,
521 no_arms: bool,
522 ) -> Ty<'tcx> {
523 // Not entirely obvious: if matches may create ref bindings, we want to
524 // use the *precise* type of the scrutinee, *not* some supertype, as
525 // the "scrutinee type" (issue #23116).
526 //
527 // arielb1 [writes here in this comment thread][c] that there
528 // is certainly *some* potential danger, e.g., for an example
529 // like:
530 //
531 // [c]: https://github.com/rust-lang/rust/pull/43399#discussion_r130223956
532 //
533 // ```
534 // let Foo(x) = f()[0];
535 // ```
536 //
537 // Then if the pattern matches by reference, we want to match
538 // `f()[0]` as a lexpr, so we can't allow it to be
539 // coerced. But if the pattern matches by value, `f()[0]` is
540 // still syntactically a lexpr, but we *do* want to allow
541 // coercions.
542 //
543 // However, *likely* we are ok with allowing coercions to
544 // happen if there are no explicit ref mut patterns - all
545 // implicit ref mut patterns must occur behind a reference, so
546 // they will have the "correct" variance and lifetime.
547 //
548 // This does mean that the following pattern would be legal:
549 //
550 // ```
551 // struct Foo(Bar);
552 // struct Bar(u32);
553 // impl Deref for Foo {
554 // type Target = Bar;
555 // fn deref(&self) -> &Bar { &self.0 }
556 // }
557 // impl DerefMut for Foo {
558 // fn deref_mut(&mut self) -> &mut Bar { &mut self.0 }
559 // }
560 // fn foo(x: &mut Foo) {
561 // {
562 // let Bar(z): &mut Bar = x;
563 // *z = 42;
564 // }
565 // assert_eq!(foo.0.0, 42);
566 // }
567 // ```
568 //
569 // FIXME(tschottdorf): don't call contains_explicit_ref_binding, which
570 // is problematic as the HIR is being scraped, but ref bindings may be
571 // implicit after #42640. We need to make sure that pat_adjustments
572 // (once introduced) is populated by the time we get here.
573 //
574 // See #44848.
575 if let Some(m) = contains_ref_bindings {
576 self.check_expr_with_needs(scrut, Needs::maybe_mut_place(m))
577 } else if no_arms {
578 self.check_expr(scrut)
579 } else {
580 // ...but otherwise we want to use any supertype of the
581 // scrutinee. This is sort of a workaround, see note (*) in
582 // `check_pat` for some details.
583 let scrut_ty = self.next_ty_var(scrut.span);
584 self.check_expr_has_type_or_error(scrut, scrut_ty, |_| {});
585 scrut_ty
586 }
587 }
588
589 // Does the expectation of the match define an RPIT?
590 // (e.g. we're in the tail of a function body)
591 //
592 // Returns the `LocalDefId` of the RPIT, which is always identity-substituted.
593 pub(crate) fn return_position_impl_trait_from_match_expectation(
594 &self,
595 expectation: Expectation<'tcx>,
596 ) -> Option<LocalDefId> {
597 let expected_ty = expectation.to_option(self)?;
598 let (def_id, args) = match *expected_ty.kind() {
599 // FIXME: Could also check that the RPIT is not defined
600 ty::Alias(ty::Opaque, alias_ty) => (alias_ty.def_id.as_local()?, alias_ty.args),
601 // FIXME(-Znext-solver): Remove this branch once `replace_opaque_types_with_infer` is gone.
602 ty::Infer(ty::TyVar(_)) => self
603 .inner
604 .borrow()
605 .iter_opaque_types()
606 .find(|(_, v)| v.ty == expected_ty)
607 .map(|(k, _)| (k.def_id, k.args))?,
608 _ => return None,
609 };
610 let hir::OpaqueTyOrigin::FnReturn { parent: parent_def_id, .. } =
611 self.tcx.local_opaque_ty_origin(def_id)
612 else {
613 return None;
614 };
615 if &args[0..self.tcx.generics_of(parent_def_id).count()]
616 != ty::GenericArgs::identity_for_item(self.tcx, parent_def_id).as_slice()
617 {
618 return None;
619 }
620 Some(def_id)
621 }
622}
623
624fn arms_contain_ref_bindings<'tcx>(arms: &'tcx [hir::Arm<'tcx>]) -> Option<hir::Mutability> {
625 arms.iter().filter_map(|a| a.pat.contains_explicit_ref_binding()).max()
626}