From 984542c4e229bbb5be070d1e276022f0b68513ec Mon Sep 17 00:00:00 2001 From: pommicket Date: Wed, 15 Oct 2025 15:43:43 -0400 Subject: [PATCH] Don't highlight `let` expressions as having type `bool` --- compiler/rustc_hir_typeck/src/demand.rs | 4 +++- tests/ui/binop/let-chain-type-issue-147665.rs | 7 +++++++ tests/ui/binop/let-chain-type-issue-147665.stderr | 9 +++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 tests/ui/binop/let-chain-type-issue-147665.rs create mode 100644 tests/ui/binop/let-chain-type-issue-147665.stderr diff --git a/compiler/rustc_hir_typeck/src/demand.rs b/compiler/rustc_hir_typeck/src/demand.rs index fb6ebe066a8f2..880b0ca2fb1fe 100644 --- a/compiler/rustc_hir_typeck/src/demand.rs +++ b/compiler/rustc_hir_typeck/src/demand.rs @@ -792,7 +792,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Binary(_, lhs, rhs), .. }), Some(TypeError::Sorts(ExpectedFound { expected, .. })), ) if rhs.hir_id == expr.hir_id - && self.typeck_results.borrow().expr_ty_adjusted_opt(lhs) == Some(expected) => + && self.typeck_results.borrow().expr_ty_adjusted_opt(lhs) == Some(expected) + // let expressions being marked as `bool` is confusing (see issue #147665) + && !matches!(lhs.kind, hir::ExprKind::Let(..)) => { err.span_label(lhs.span, format!("expected because this is `{expected}`")); } diff --git a/tests/ui/binop/let-chain-type-issue-147665.rs b/tests/ui/binop/let-chain-type-issue-147665.rs new file mode 100644 index 0000000000000..4a6b40c6f9877 --- /dev/null +++ b/tests/ui/binop/let-chain-type-issue-147665.rs @@ -0,0 +1,7 @@ +// Shouldn't highlight `let x = 1` as having type bool. +//@ edition:2024 + +fn main() { + if let x = 1 && 2 {} + //~^ ERROR mismatched types +} diff --git a/tests/ui/binop/let-chain-type-issue-147665.stderr b/tests/ui/binop/let-chain-type-issue-147665.stderr new file mode 100644 index 0000000000000..b2b82228eaebf --- /dev/null +++ b/tests/ui/binop/let-chain-type-issue-147665.stderr @@ -0,0 +1,9 @@ +error[E0308]: mismatched types + --> $DIR/let-chain-type-issue-147665.rs:5:21 + | +LL | if let x = 1 && 2 {} + | ^ expected `bool`, found integer + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0308`.