Commit 8697549
committed
fix heap-cell cleanup calling the wrong destructor
Summary
=======
Fix `ref`s to objects calling the wrong destructor upon the refcount
reaching zero in some narrow circumstances involving generic types and
`sink` type modifiers.
Details
=======
Due to the sighash-based canonicalization used prior to type-bound op
lifting ignoring `sink` modifiers, the same hook was lifted for both
`ref Obj[proc(x: sink T)]` and `ref Obj[proc(x: T)]`, which led to the
wrong destructor being called on cleanup when the object type is final.
In a very narrow situation, where all of the following apply:
* the `ref` type itself is generic
* the object type is never used outside the `ref` type
* the object type has no user-defined destructor, only an implicit one
* the object type is non-final
* the types reach the MIR phase in the wrong order
the canonicalization led to the object not having its destructor called
at all, due to the object's destructor not being lifted, thus
potentially *leaking memory* if the object type requires destruction
for some of its fields.
Two types for which `sameType(a, b)` results in `false` should produce
different sighashes, and so hashing is changed to not ignore `sink`
types, fixing both issues.
In addition, the scanning for types to lift the ops for in `sempass2`
is fixed to handle generic ref types used as the constructor in object
construction expressions, which makes the code work as intended.1 parent e05448b commit 8697549
File tree
3 files changed
+22
-5
lines changed- compiler/sem
- tests/lang_objects/destructor
3 files changed
+22
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1345 | 1345 | | |
1346 | 1346 | | |
1347 | 1347 | | |
1348 | | - | |
1349 | | - | |
1350 | | - | |
| 1348 | + | |
| 1349 | + | |
| 1350 | + | |
1351 | 1351 | | |
1352 | 1352 | | |
1353 | 1353 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
135 | | - | |
| 135 | + | |
136 | 136 | | |
137 | 137 | | |
138 | 138 | | |
| |||
179 | 179 | | |
180 | 180 | | |
181 | 181 | | |
182 | | - | |
| 182 | + | |
183 | 183 | | |
184 | 184 | | |
185 | 185 | | |
| |||
Lines changed: 17 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
0 commit comments