[clang] [Clang] Mark this pointer in destructors dead_on_return (PR #166276)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 2 18:56:13 PST 2025


zygoloid wrote:

Is it correct to mark destructors' `this` pointers as `dead_on_return`? What about this case:

```c++
struct X {
  int n;
  ~X() {
    this[n].n = 0;
  }
};
void f() {
  X xs[] = {42, 0};
}
```

I think that's valid -- you can use a pointer to an array element to reach other elements of the array, even if the pointer is the `this` pointer -- but if the `this[n].n = 0` store is treated as dead, then destroying `xs[1]` won't set `xs[0].n` to `0`, so destroying `xs[0]` will store outside the array.

I think we'd need to add a size to the `dead_on_return` attribute to indicate how many bytes are dead; we can't assume all memory reachable through `this` is dead.

https://github.com/llvm/llvm-project/pull/166276


More information about the cfe-commits mailing list