Files
carbon-lang/toolchain/check
Christopher Di Bella 7f576a0415 Add tests for non-trivial destruction involving Carbon-only types (#8137)
The tests dedicated to the Carbon destruction model are incomplete. This
is one of two changes to improve robustness when testing destruction. It
adds coverage for the destruction model in its current state and changes
that will be applied in the near future. The changes have been broken up
into two smaller fragments to make them easier to review.

## How these tests work

The test harness for destruction is to create an object, and then exit
the scope the object was introduced in. The toolchain automatically
calls `Core.SelfDestruct` on objects that have non-trivial destruction,
and shouldn't automatically call it on objects that have trivial
destruction. Non-trivial destruction implies that either
`Core.Destroy.Op` or `Core.SubobjectDestroy.Op` is non-trivial. Both may
be non-trivial, but that's not required.

Test cases for non-trivial destruction are designed to check that:

1. Complete Carbon-only types always implement `Core.SubobjectDestroy`.
2. The SemIR for `Core.SubobjectDestroy.Op` is correct.
a. For objects with non-trivial destruction: `Core.SubobjectDestroy.Op`
calls `Core.SelfDestruct` on each of its objects that have non-trivial
destruction.
b. For objects with trivial destruction: `Core.SubobjectDestroy.Op` is
implemented as a no-op.
3. The SemIR for `Core.SelfDestruct` is correct.
a. For objects with non-trivial destruction: the toolchain automatically
calls `Core.SelfDestruct` on the objects, as they go out of scope.
b. For objects with trivial destruction: `Core.SelfDestruct` shouldn't
be called automatically.

## Known limitations

* Most tests are marked using `todo_`, since trivial destruction is
still WIP, and is only partially correct at the moment.
* Some tests expose cases that aren't yet accounted for in our
implementation. They're commented out to avoid crashes.

## How these tests are organised

There isn't a single obvious place for these tests to reside. The
destruction model consists of four interfaces and `Core.SelfDestruct`.
Carbon has three destruction models: trivial destruction, non-trivial
destruction, and dynamic destruction.

The interfaces and `Core.SelfDestruct` are interdependent. As such, I've
found it difficult to write tests that focus only on one interface or
`Core.SelfDestruct`. I've found it much easier to tease them out by
writing tests that treat destruction as a unit. The tests are in
`check/testdata/impl/custom_witness/destruction/non_trivial/`, because
most of the salient SemIR is concerned with interfaces that have custom
witnesses.
2026-11-24 17:16:18 +00:00
..
2026-11-13 01:17:20 +00:00
2026-09-03 17:06:22 +00:00
2026-08-25 18:43:52 +00:00
2026-08-17 20:49:23 +00:00