Commit 790309b
committed
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 #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](#115315 (comment)).
Cc `@rust-lang/wg-rfc-2229` `@ehuss`File tree
3 files changed
+22
-53
lines changed- compiler/rustc_hir_typeck/src
- tests/ui/closures/2229_closure_analysis
3 files changed
+22
-53
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
195 | 195 | | |
196 | 196 | | |
197 | 197 | | |
198 | | - | |
199 | 198 | | |
200 | 199 | | |
201 | 200 | | |
| |||
1607 | 1606 | | |
1608 | 1607 | | |
1609 | 1608 | | |
1610 | | - | |
1611 | | - | |
1612 | 1609 | | |
1613 | 1610 | | |
1614 | 1611 | | |
1615 | 1612 | | |
1616 | 1613 | | |
1617 | 1614 | | |
1618 | | - | |
| 1615 | + | |
1619 | 1616 | | |
1620 | 1617 | | |
1621 | 1618 | | |
1622 | | - | |
1623 | | - | |
1624 | | - | |
1625 | | - | |
1626 | | - | |
1627 | | - | |
1628 | | - | |
1629 | | - | |
1630 | | - | |
1631 | | - | |
1632 | | - | |
1633 | | - | |
1634 | | - | |
1635 | | - | |
1636 | | - | |
1637 | | - | |
| 1619 | + | |
| 1620 | + | |
| 1621 | + | |
| 1622 | + | |
1638 | 1623 | | |
1639 | 1624 | | |
1640 | 1625 | | |
| |||
1689 | 1674 | | |
1690 | 1675 | | |
1691 | 1676 | | |
1692 | | - | |
1693 | | - | |
1694 | | - | |
| 1677 | + | |
1695 | 1678 | | |
1696 | 1679 | | |
1697 | 1680 | | |
| |||
1725 | 1708 | | |
1726 | 1709 | | |
1727 | 1710 | | |
1728 | | - | |
| 1711 | + | |
1729 | 1712 | | |
1730 | 1713 | | |
1731 | 1714 | | |
| |||
1740 | 1723 | | |
1741 | 1724 | | |
1742 | 1725 | | |
1743 | | - | |
1744 | | - | |
1745 | | - | |
1746 | | - | |
1747 | | - | |
1748 | | - | |
| 1726 | + | |
1749 | 1727 | | |
1750 | 1728 | | |
1751 | 1729 | | |
| |||
1780 | 1758 | | |
1781 | 1759 | | |
1782 | 1760 | | |
1783 | | - | |
1784 | | - | |
1785 | | - | |
1786 | | - | |
1787 | | - | |
1788 | | - | |
| 1761 | + | |
| 1762 | + | |
1789 | 1763 | | |
1790 | 1764 | | |
1791 | 1765 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | | - | |
24 | | - | |
| 23 | + | |
| 24 | + | |
25 | 25 | | |
26 | 26 | | |
27 | 27 | | |
| |||
Lines changed: 7 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | 31 | | |
32 | 32 | | |
| |||
37 | 37 | | |
38 | 38 | | |
39 | 39 | | |
40 | | - | |
41 | | - | |
| 40 | + | |
| 41 | + | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | | - | |
| 45 | + | |
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
52 | | - | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
60 | 60 | | |
61 | 61 | | |
62 | 62 | | |
63 | | - | |
64 | | - | |
65 | | - | |
66 | | - | |
67 | | - | |
68 | | - | |
| 63 | + | |
69 | 64 | | |
70 | 65 | | |
71 | 66 | | |
| |||
0 commit comments