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

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Sep 9 14:41:12 UTC 2024


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

Nick Treleaven <nick at geany.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nick at geany.org

--- Comment #3 from Nick Treleaven <nick at geany.org> ---
> This might be a bug, DIP1000 should be catching this I think.

If `&e` is the address of an int inside the `Foo.foo` array, then that is fine
so long as the array is null or GC allocated.

However, if Foo.foo is changed to `int[1] foo;`, it still won't error and it
should:

```d
import std.stdio;

struct Foo
{
    int[1] foo;

    @safe
    int* foobar()
    {
        int* f;
        foreach(ref e; foo)
            f = &e; // escaping address of foo[0]
        return f;
    }
}

void main() @safe
{
    int* p;
    {
        Foo foo = Foo([1]);
        //writeln(&foo.foo[0]);
        p = foo.foobar();
    }
    p.writeln();
    (*p).writeln();
}
```

--


More information about the Digitalmars-d-bugs mailing list