Skip to content

Commit 58d4b99

Browse files
authored
Merge pull request #20039 from ShoyuVanilla/let-bind-ref-capt
fix: Closure capturing for let exprs
2 parents e7971e4 + 70f376d commit 58d4b99

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/tools/rust-analyzer/crates/hir-ty/src/infer/closure.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,11 +1230,15 @@ impl InferenceContext<'_> {
12301230
self.select_from_expr(*expr);
12311231
}
12321232
}
1233+
Expr::Let { pat: _, expr } => {
1234+
self.walk_expr(*expr);
1235+
let place = self.place_of_expr(*expr);
1236+
self.ref_expr(*expr, place);
1237+
}
12331238
Expr::UnaryOp { expr, op: _ }
12341239
| Expr::Array(Array::Repeat { initializer: expr, repeat: _ })
12351240
| Expr::Await { expr }
12361241
| Expr::Loop { body: expr, label: _ }
1237-
| Expr::Let { pat: _, expr }
12381242
| Expr::Box { expr }
12391243
| Expr::Cast { expr, type_ref: _ } => {
12401244
self.consume_expr(*expr);

src/tools/rust-analyzer/crates/hir-ty/src/tests/closure_captures.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,3 +444,22 @@ fn main() {
444444
expect!["99..165;49..54;120..121,133..134 ByRef(Mut { kind: Default }) a &'? mut A"],
445445
);
446446
}
447+
448+
#[test]
449+
fn let_binding_is_a_ref_capture() {
450+
check_closure_captures(
451+
r#"
452+
//- minicore:copy
453+
struct S;
454+
fn main() {
455+
let mut s = S;
456+
let s_ref = &mut s;
457+
let closure = || {
458+
if let ref cb = s_ref {
459+
}
460+
};
461+
}
462+
"#,
463+
expect!["83..135;49..54;112..117 ByRef(Shared) s_ref &'? &'? mut S"],
464+
);
465+
}

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/moved_out_of_ref.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,23 @@ fn test() {
220220
"#,
221221
)
222222
}
223+
224+
#[test]
225+
fn regression_18201() {
226+
check_diagnostics(
227+
r#"
228+
//- minicore: copy
229+
struct NotCopy;
230+
struct S(NotCopy);
231+
impl S {
232+
fn f(&mut self) {
233+
|| {
234+
if let ref mut _cb = self.0 {
235+
}
236+
};
237+
}
238+
}
239+
"#,
240+
)
241+
}
223242
}

0 commit comments

Comments
 (0)