@safe:
void main()
{
auto arr = new int[](5);
int* p;
foreach (i, ref n; arr)
{
p = &arr[i]; // OK
p = &n; // not OK
}
}
DMD 2.111.0 says:
test.d(10): Error: taking the address of stack-allocated local variable `n` is not allowed in a `@safe` function
p = &n; // not OK
^
However, n is not stack-allocated, it is on the GC heap.
Probably related: #18267
(However, the wording of the error has regressed further in the latest version.)