]> git.lizzy.rs Git - rust.git/commit
2229: Reduce the size of closures with `capture_disjoint_fields`
authorAman Arora <me@aman-arora.com>
Mon, 28 Jun 2021 01:19:39 +0000 (21:19 -0400)
committerAman Arora <me@aman-arora.com>
Tue, 29 Jun 2021 07:16:43 +0000 (03:16 -0400)
commit23c0334fd3549c1e3a701c2669babbe272504251
treeb66a512b079fbd338aecd229d57bbdab65026863
parentfecc65a19763364b8dafbbf1d23be562268bd387
2229: Reduce the size of closures with `capture_disjoint_fields`

One key observation while going over the closure size profile of rustc
was that we are disjointly capturing one or more fields starting at an
immutable reference.

Disjoint capture over immutable reference doesn't add too much value
because the fields can either be borrowed immutably or copied.

One possible edge case of the optimization is when a fields of a struct
have a longer lifetime than the structure, therefore we can't completely
get rid of all the accesses on top of sharef refs, only the rightmost
one. Here is a possible example:

```rust
struct MyStruct<'a> {
   a: &'static A,
   b: B,
   c: C<'a>,
}

fn foo<'a, 'b>(m: &'a MyStruct<'b>) -> impl FnMut() + 'static {
    let c = || drop(&*m.a.field_of_a);
    // Here we really do want to capture `*m.a` because that outlives `'static`

    // If we capture `m`, then the closure no longer outlives `'static'
    // it is constrained to `'a`
}
```
compiler/rustc_typeck/src/check/upvar.rs
src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.rs
src/test/ui/closures/2229_closure_analysis/diagnostics/cant-mutate-imm-borrow.stderr
src/test/ui/closures/2229_closure_analysis/move_closure.rs
src/test/ui/closures/2229_closure_analysis/move_closure.stderr
src/test/ui/closures/2229_closure_analysis/optimization/edge_case.rs [new file with mode: 0644]
src/test/ui/closures/2229_closure_analysis/optimization/edge_case.stderr [new file with mode: 0644]
src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.rs [new file with mode: 0644]
src/test/ui/closures/2229_closure_analysis/optimization/edge_case_run_pass.stderr [new file with mode: 0644]