Skip to content

Commit

Permalink
Auto merge of rust-lang#86995 - sexxi-goose:rewrite, r=nikomatsakis
Browse files Browse the repository at this point in the history
2229: Rewrite/Refactor Closure Capture Analaysis

While handling all the differnet edge cases the code for the captur analysis got pretty compicated. Looking at the overall picture of the edge cases the rules can still be layed out simply.

Alogithm: https://hackmd.io/D3I_gwvuT-SPnJ22tgJumw

r? `@nikomatsakis`

Closes rust-lang/project-rfc-2229#52
Implementation part of rust-lang/project-rfc-2229#53
  • Loading branch information
bors committed Jul 11, 2021
2 parents 4581c4e + 5055569 commit 81053b9
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 224 deletions.
328 changes: 160 additions & 168 deletions compiler/rustc_typeck/src/check/upvar.rs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/test/ui/closures/2229_closure_analysis/by_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ fn big_box() {
//~^ First Pass analysis includes:
//~| Min Capture analysis includes:
let p = t.0.0;
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
//~| NOTE: Capturing t[(0, 0)] -> ByValue
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ByValue
//~| NOTE: Min Capture t[(0, 0)] -> ByValue
println!("{} {:?}", t.1, p);
//~^ NOTE: Capturing t[(1, 0)] -> ImmBorrow
Expand Down
11 changes: 3 additions & 8 deletions src/test/ui/closures/2229_closure_analysis/by_value.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,13 @@ LL | |
LL | | };
| |_____^
|
note: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
--> $DIR/by_value.rs:24:17
|
LL | let p = t.0.0;
| ^^^^^
note: Capturing t[(0, 0)] -> ByValue
note: Capturing t[(0, 0),Deref,(0, 0)] -> ByValue
--> $DIR/by_value.rs:24:17
|
LL | let p = t.0.0;
| ^^^^^
note: Capturing t[(1, 0)] -> ImmBorrow
--> $DIR/by_value.rs:28:29
--> $DIR/by_value.rs:27:29
|
LL | println!("{} {:?}", t.1, p);
| ^^^
Expand All @@ -53,7 +48,7 @@ note: Min Capture t[(0, 0)] -> ByValue
LL | let p = t.0.0;
| ^^^^^
note: Min Capture t[(1, 0)] -> ImmBorrow
--> $DIR/by_value.rs:28:29
--> $DIR/by_value.rs:27:29
|
LL | println!("{} {:?}", t.1, p);
| ^^^
Expand Down
52 changes: 44 additions & 8 deletions src/test/ui/closures/2229_closure_analysis/move_closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn simple_move_closure() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
t.0.0 = "new S".into();
//~^ NOTE: Capturing t[(0, 0),(0, 0)] -> ByValue
//~^ NOTE: Capturing t[(0, 0),(0, 0)] -> MutBorrow
//~| NOTE: Min Capture t[(0, 0),(0, 0)] -> ByValue
};
c();
Expand Down Expand Up @@ -78,7 +78,7 @@ fn struct_contains_ref_to_another_struct_2() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
let _t = t.0.0;
//~^ NOTE: Capturing t[(0, 0),Deref] -> ImmBorrow
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture t[(0, 0),Deref] -> ImmBorrow
};

Expand All @@ -100,8 +100,7 @@ fn struct_contains_ref_to_another_struct_3() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
let _t = t.0.0;
//~^ NOTE: Capturing t[(0, 0),Deref] -> ImmBorrow
//~| NOTE: Capturing t[(0, 0)] -> ByValue
//~^ NOTE: Capturing t[(0, 0),Deref,(0, 0)] -> ByValue
//~| NOTE: Min Capture t[(0, 0)] -> ByValue
};

Expand All @@ -122,8 +121,7 @@ fn truncate_box_derefs() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
let _t = b.0;
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ByValue
//~| NOTE: Capturing b[] -> ByValue
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture b[] -> ByValue
};

Expand All @@ -139,7 +137,7 @@ fn truncate_box_derefs() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
println!("{}", b.0);
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ByValue
//~^ NOTE: Capturing b[Deref,(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture b[] -> ByValue
};

Expand All @@ -156,16 +154,54 @@ fn truncate_box_derefs() {
//~^ ERROR: First Pass analysis includes:
//~| ERROR: Min Capture analysis includes:
println!("{}", t.1.0);
//~^ NOTE: Capturing t[(1, 0),Deref,(0, 0)] -> ByValue
//~^ NOTE: Capturing t[(1, 0),Deref,(0, 0)] -> ImmBorrow
//~| NOTE: Min Capture t[(1, 0)] -> ByValue
};
}

struct Foo { x: i32 }

// Ensure that even in move closures, if the data is not owned by the root variable
// then we don't truncate the derefs or a ByValue capture, rather do a reborrow
fn box_mut_1() {
let mut foo = Foo { x: 0 } ;

let p_foo = &mut foo;
let box_p_foo = Box::new(p_foo);

let c = #[rustc_capture_analysis] move || box_p_foo.x += 10;
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
//~| First Pass analysis includes:
//~| NOTE: Capturing box_p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
//~| Min Capture analysis includes:
//~| NOTE: Min Capture box_p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
}

// Ensure that even in move closures, if the data is not owned by the root variable
// then we don't truncate the derefs or a ByValue capture, rather do a reborrow
fn box_mut_2() {
let foo = Foo { x: 0 } ;

let mut box_foo = Box::new(foo);
let p_foo = &mut box_foo;

let c = #[rustc_capture_analysis] move || p_foo.x += 10;
//~^ ERROR: attributes on expressions are experimental
//~| NOTE: see issue #15701 <https://github.com/rust-lang/rust/issues/15701>
//~| First Pass analysis includes:
//~| NOTE: Capturing p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
//~| Min Capture analysis includes:
//~| NOTE: Min Capture p_foo[Deref,Deref,(0, 0)] -> UniqueImmBorrow
}

fn main() {
simple_move_closure();
simple_ref();
struct_contains_ref_to_another_struct_1();
struct_contains_ref_to_another_struct_2();
struct_contains_ref_to_another_struct_3();
truncate_box_derefs();
box_mut_2();
box_mut_1();
}
Loading

0 comments on commit 81053b9

Please sign in to comment.