os-rust/tests/ui/closures/2229_closure_analysis
bors 790309b102 Auto merge of #115315 - RalfJung:field-capture-packed-alignment, r=oli-obk
closure field capturing: don't depend on alignment of packed fields

This fixes the closure field capture part of https://github.com/rust-lang/rust/issues/115305: field capturing always stops at projections into packed structs, no matter the alignment of the field. This means changing a private field type from `u8` to `u64` can never change how closures capture fields, which is probably what we want.

Here's an example where, before this PR, changing the type of a private field in a repr(Rust) struct can change the output of a program:

```rust
#![allow(dead_code)]

mod m {
    // before patch
    #[derive(Default)]
    pub struct S1(u8);
    // after patch
    #[derive(Default)]
    pub struct S2(u64);
}

struct NoisyDrop;
impl Drop for NoisyDrop {
    fn drop(&mut self) {
        eprintln!("dropped!");
    }
}

#[repr(packed)]
struct MyType {
    field: m::S1, // output changes when this becomes S2
    other_field: NoisyDrop,
    third_field: Vec<()>,
}

fn test(r: MyType) {
    let c = || {
        let _val = std::ptr::addr_of!(r.field);
        let _val = r.third_field;
    };
    drop(c);
    eprintln!("before dropping");
}

fn main() {
    test(MyType {
        field: Default::default(),
        other_field: NoisyDrop,
        third_field: Vec::new(),
    });
}
```

Of course this is a breaking change for the same reason that doing field capturing in the first place was a breaking change. Packed fields are relatively rare and depending on drop order is relatively rare, so I don't expect this to have much impact, but it's hard to be sure and even a crater run will only tell us so much.

Also see the [nomination comment](https://github.com/rust-lang/rust/pull/115315#issuecomment-1702807825).

Cc `@rust-lang/wg-rfc-2229` `@ehuss`
2023-09-16 05:29:23 +00:00
..
diagnostics tweak "make mut" spans when assigning to locals 2023-05-05 22:40:04 +12:00
match Improve clarity of diagnostic message on non-exhaustive matches 2023-09-03 19:55:11 +08:00
migrations Rename drop_ref lint to dropping_references 2023-05-21 14:16:41 +02:00
optimization Rename drop_ref lint to dropping_references 2023-05-21 14:16:41 +02:00
run_pass s/drain_filter/extract_if/ for Vec, Btree{Map,Set} and LinkedList 2023-06-14 09:28:54 +02:00
array_subslice.rs Fix subslice capture in closure 2023-03-27 22:26:30 +01:00
array_subslice.stderr Fix subslice capture in closure 2023-03-27 22:26:30 +01:00
arrays-completely-captured.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
arrays-completely-captured.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
bad-pattern.rs Bail out of MIR construction if check_match fails 2023-04-30 19:17:40 +01:00
bad-pattern.stderr Bail out of MIR construction if check_match fails 2023-04-30 19:17:40 +01:00
by_value.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
by_value.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-1.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-1.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-2.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-3.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-3.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-4.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-analysis-4.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-disjoint-field-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-disjoint-field-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-disjoint-field-tuple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-disjoint-field-tuple.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-enum-field.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-enums.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
capture-enums.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deep-multilevel-struct.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deep-multilevel-struct.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deep-multilevel-tuple.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
deep-multilevel-tuple.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
destructure_patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
destructure_patterns.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
feature-gate-capture_disjoint_fields.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
feature-gate-capture_disjoint_fields.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
filter-on-struct-member.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
filter-on-struct-member.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-87378.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-87378.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-87987.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-87987.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-88118-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-88118-2.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-88476.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-88476.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-89606.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-90465.fixed Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-90465.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-90465.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue-92724-needsdrop-query-cycle.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
issue_88118.rs Remove the capture_disjoint_fields feature 2023-02-28 01:21:15 +00:00
move_closure.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
move_closure.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
multilevel-path-1.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
multilevel-path-1.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
multilevel-path-2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
multilevel-path-2.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
nested-closure.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
nested-closure.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
path-with-array-access.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
path-with-array-access.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
preserve_field_drop_order.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
preserve_field_drop_order.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
preserve_field_drop_order2.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
preserve_field_drop_order2.twenty_eighteen.run.stdout Move /src/test to /tests 2023-01-11 09:32:08 +00:00
preserve_field_drop_order2.twenty_twentyone.run.stdout Move /src/test to /tests 2023-01-11 09:32:08 +00:00
repr_packed.rs closure field capturing: don't depend on alignment of packed fields 2023-08-28 18:25:37 +02:00
repr_packed.stderr closure field capturing: don't depend on alignment of packed fields 2023-08-28 18:25:37 +02:00
simple-struct-min-capture.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
simple-struct-min-capture.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unique-borrows-are-invariant-1.rs add tests 2023-05-29 18:37:53 +02:00
unique-borrows-are-invariant-1.stderr add tests 2023-05-29 18:37:53 +02:00
unique-borrows-are-invariant-2.rs add tests 2023-05-29 18:37:53 +02:00
unique-borrows-are-invariant-2.stderr add tests 2023-05-29 18:37:53 +02:00
unsafe_ptr.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
unsafe_ptr.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00
wild_patterns.rs Move /src/test to /tests 2023-01-11 09:32:08 +00:00
wild_patterns.stderr Move /src/test to /tests 2023-01-11 09:32:08 +00:00