mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 11:20:54 +00:00
Merge pull request #20161 from ShoyuVanilla/closure-capture
fix: Incorrect closure capturing for let exprs
This commit is contained in:
commit
87940a999f
@ -1229,10 +1229,11 @@ impl InferenceContext<'_> {
|
|||||||
self.select_from_expr(*expr);
|
self.select_from_expr(*expr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expr::Let { pat: _, expr } => {
|
Expr::Let { pat, expr } => {
|
||||||
self.walk_expr(*expr);
|
self.walk_expr(*expr);
|
||||||
let place = self.place_of_expr(*expr);
|
if let Some(place) = self.place_of_expr(*expr) {
|
||||||
self.ref_expr(*expr, place);
|
self.consume_with_pat(place, *pat);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Expr::UnaryOp { expr, op: _ }
|
Expr::UnaryOp { expr, op: _ }
|
||||||
| Expr::Array(Array::Repeat { initializer: expr, repeat: _ })
|
| Expr::Array(Array::Repeat { initializer: expr, repeat: _ })
|
||||||
|
@ -446,7 +446,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn let_binding_is_a_ref_capture() {
|
fn let_binding_is_a_ref_capture_in_ref_binding() {
|
||||||
check_closure_captures(
|
check_closure_captures(
|
||||||
r#"
|
r#"
|
||||||
//- minicore:copy
|
//- minicore:copy
|
||||||
@ -454,12 +454,36 @@ struct S;
|
|||||||
fn main() {
|
fn main() {
|
||||||
let mut s = S;
|
let mut s = S;
|
||||||
let s_ref = &mut s;
|
let s_ref = &mut s;
|
||||||
|
let mut s2 = S;
|
||||||
|
let s_ref2 = &mut s2;
|
||||||
let closure = || {
|
let closure = || {
|
||||||
if let ref cb = s_ref {
|
if let ref cb = s_ref {
|
||||||
|
} else if let ref mut cb = s_ref2 {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect!["83..135;49..54;112..117 ByRef(Shared) s_ref &'? &'? mut S"],
|
expect![[r#"
|
||||||
|
129..225;49..54;149..155 ByRef(Shared) s_ref &'? &'? mut S
|
||||||
|
129..225;93..99;188..198 ByRef(Mut { kind: Default }) s_ref2 &'? mut &'? mut S"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn let_binding_is_a_value_capture_in_binding() {
|
||||||
|
check_closure_captures(
|
||||||
|
r#"
|
||||||
|
//- minicore:copy, option
|
||||||
|
struct Box(i32);
|
||||||
|
fn main() {
|
||||||
|
let b = Some(Box(0));
|
||||||
|
let closure = || {
|
||||||
|
if let Some(b) = b {
|
||||||
|
let _move = b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect!["73..149;37..38;103..104 ByValue b Option<Box>"],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -236,6 +236,24 @@ impl S {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn regression_20155() {
|
||||||
|
check_diagnostics(
|
||||||
|
r#"
|
||||||
|
//- minicore: copy, option
|
||||||
|
struct Box(i32);
|
||||||
|
fn test() {
|
||||||
|
let b = Some(Box(0));
|
||||||
|
|| {
|
||||||
|
if let Some(b) = b {
|
||||||
|
let _move = b;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user