Skip to content

Commit 0566644

Browse files
committed
Auto merge of #15625 - jDomantas:domantas/fix-15623, r=HKalbasi
fix: Don't skip closure captures after let-else As I understand that `return` was left there by accident. It caused capture analysis to skip the rest of the block after a let-else, and then missed captures caused incorrect results in borrowck, closure hints, layout calculation, etc. Fixes #15623 I didn't understand why I using the example from #15623 as-is doesn't work - I don't get the warnings unless I remove the `call_me()` call, even on the same commit as my own RA version which does show those warnings.
2 parents 9d0ccf0 + a961068 commit 0566644

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

crates/hir-ty/src/infer/closure.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,13 @@ impl InferenceContext<'_> {
469469
Statement::Let { pat, type_ref: _, initializer, else_branch } => {
470470
if let Some(else_branch) = else_branch {
471471
self.consume_expr(*else_branch);
472-
if let Some(initializer) = initializer {
473-
self.consume_expr(*initializer);
474-
}
475-
return;
476472
}
477473
if let Some(initializer) = initializer {
478-
self.walk_expr(*initializer);
474+
if else_branch.is_some() {
475+
self.consume_expr(*initializer);
476+
} else {
477+
self.walk_expr(*initializer);
478+
}
479479
if let Some(place) = self.place_of_expr(*initializer) {
480480
self.consume_with_pat(place, *pat);
481481
}

crates/hir-ty/src/layout/tests/closure.rs

+14
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,17 @@ fn ellipsis_pattern() {
255255
}
256256
}
257257
}
258+
259+
#[test]
260+
fn regression_15623() {
261+
size_and_align_expr! {
262+
let a = 2;
263+
let b = 3;
264+
let c = 5;
265+
move || {
266+
let 0 = a else { return b; };
267+
let y = c;
268+
y
269+
}
270+
}
271+
}

crates/ide-diagnostics/src/handlers/mutability_errors.rs

+23
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,29 @@ fn f() {
11701170
loop {}
11711171
for _ in 0..2 {}
11721172
}
1173+
"#,
1174+
);
1175+
}
1176+
1177+
#[test]
1178+
fn regression_15623() {
1179+
check_diagnostics(
1180+
r#"
1181+
//- minicore: fn
1182+
1183+
struct Foo;
1184+
1185+
impl Foo {
1186+
fn needs_mut(&mut self) {}
1187+
}
1188+
1189+
fn foo(mut foo: Foo) {
1190+
let mut call_me = || {
1191+
let 0 = 1 else { return };
1192+
foo.needs_mut();
1193+
};
1194+
call_me();
1195+
}
11731196
"#,
11741197
);
11751198
}

0 commit comments

Comments
 (0)