Skip to content

Don't declare variables in ExprKind::Let in invalid positions #141876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2370,6 +2370,10 @@ impl Expr<'_> {
// Lang item paths cannot currently be local variables or statics.
ExprKind::Path(QPath::LangItem(..)) => false,

// Suppress errors for bad expressions.
ExprKind::Err(_guar)
| ExprKind::Let(&LetExpr { recovered: ast::Recovered::Yes(_guar), .. }) => true,

// Partially qualified paths in expressions can only legally
// refer to associated items which are always rvalues.
ExprKind::Path(QPath::TypeRelative(..))
Expand Down Expand Up @@ -2401,8 +2405,7 @@ impl Expr<'_> {
| ExprKind::Binary(..)
| ExprKind::Yield(..)
| ExprKind::Cast(..)
| ExprKind::DropTemps(..)
| ExprKind::Err(_) => false,
| ExprKind::DropTemps(..) => false,
}
}

Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4891,11 +4891,28 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
self.resolve_expr(e, Some(expr));
}

ExprKind::Let(ref pat, ref scrutinee, _, _) => {
ExprKind::Let(ref pat, ref scrutinee, _, Recovered::No) => {
self.visit_expr(scrutinee);
self.resolve_pattern_top(pat, PatternSource::Let);
}

ExprKind::Let(ref pat, ref scrutinee, _, Recovered::Yes(_)) => {
self.visit_expr(scrutinee);
// This is basically a tweaked, inlined `resolve_pattern_top`.
let mut bindings = smallvec![(PatBoundCtx::Product, Default::default())];
self.resolve_pattern(pat, PatternSource::Let, &mut bindings);
// We still collect the bindings in this `let` expression which is in
// an invalid position (and therefore shouldn't declare variables into
// its parent scope). To avoid unnecessary errors though, we do just
// reassign the resolutions to `Res::Err`.
for (_, bindings) in &mut bindings {
for (_, binding) in bindings {
*binding = Res::Err;
}
}
self.apply_pattern_bindings(bindings);
}

ExprKind::If(ref cond, ref then, ref opt_else) => {
self.with_rib(ValueNS, RibKind::Normal, |this| {
let old = this.diag_metadata.in_if_condition.replace(cond);
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/destructuring-assignment/bad-let-in-destructure.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Regression test for <https://github.com/rust-lang/rust/issues/141844>.

fn main() {
// The following expression gets desugared into something like:
// ```
// let (lhs,) = x; (let x = 1) = lhs;
// ```
// This used to ICE since we haven't yet declared the type for `x` when
// checking the first desugared statement, whose RHS resolved to `x` since
// in the AST, the `let` expression was visited first.
(let x = 1,) = x;
//~^ ERROR expected expression, found `let` statement
}
10 changes: 10 additions & 0 deletions tests/ui/destructuring-assignment/bad-let-in-destructure.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: expected expression, found `let` statement
--> $DIR/bad-let-in-destructure.rs:11:4
|
LL | (let x = 1,) = x;
| ^^^
|
= note: only supported directly in conditions of `if` and `while` expressions

error: aborting due to 1 previous error

Loading