[Issue 24754] cannot take address of a member array in a ref foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 13 11:56:52 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24754

--- Comment #12 from Nick Treleaven <nick at geany.org> ---
(In reply to Dennis from comment #8)
> Reduces to:
> 
> ```D
> void main() @safe
> {
>     foreach(ref e; new int[1])
>         int* f = &e;
> }
> ```

The compiler doesn't know (without optimization or -dip1000) that the program
is equivalent to that. It doesn't know below that `f` doesn't escape the scope
of foobar somehow.

    @safe
    void foobar()
    {
        int* f;
        foreach(ref e; foo)
            f = &e;
    }

So it seems counter-intuitive to the way D's semantic analysis works to allow
this case without -dip1000.

--


More information about the Digitalmars-d-bugs mailing list