Files
carbon-lang/toolchain/testing
Christopher Di Bella 05537a618e Add tests for trivial destruction involving Carbon-only types (#8139)
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. Trivial destruction requires both `Core.Destroy.Op` and
`Core.SubobjectDestroy.Op` are trivial for an object and all of its
subobjects.

`Core.TrivialDestroy` requires types implement `Core.Destroy`, which in
turn, requires types implement `Core.SubobjectDestroy`. There isn't a
way to automatically check this, like with `Core.SubobjectDestroy`, so
we need to explicitly test that types implement `Core.Destroy` and
`Core.TrivialDestroy`.

Test cases for trivial destruction are designed to check that:

1. Complete Carbon-only types that have trivial destruction always
implement `Core.TrivialDestroy`.
2. Carbon-only types that do not have trivial destruction do not
implement `Core.TrivialDestroy`.
3. Types that have trivial destruction generate a no-op for
`Core.SubobjectDestroy.Op`. Types that don't have trivial destruction
shouldn't output SemIR dumps.
4. `Core.SelfDestruct` isn't present in the SemIR dumps.

## 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/trivial/`, because most
of the salient SemIR is concerned with interfaces that have custom
witnesses.
2026-11-24 17:06:21 +00:00
..