Skip to content

Incorrect claim that a foreach-ref variable is "stack-allocated" #21341

@vpanteleev-sym

Description

@vpanteleev-sym
@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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions