[Issue 22270] [DIP1000] class does infer scope in methods when assigned to a scope variable

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 3 21:40:05 UTC 2021


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

--- Comment #7 from João Lourenço <jlourenco5691 at gmail.com> ---
I was referring to:

```
scope bar1 = Bar();
auto  bar2 = Bar();
```

I thought `scope` only meant instantiating on the stack when used this way. But
I guess it also marks all fields as scope. If I understood correctly, the
instance `bar1` lifetime is the current scope and so are all its members, and
the instance `bar2` lifetime is the current scope but its members are assumed
to have infinite lifetime. And that's why if instantiate Bar normally without
scope I can do this in @safe code with dip1000:

```
struct Bar
{
    int[] arr;

    @safe Wrapper dummy() {
        Wrapper wrapper = { bar: &this };
        return wrapper;
    }
}

struct Wrapper { Bar* bar; }

@safe void main()
{
    auto bar = Bar();
    cast(void) bar.dummy;
}
```

--


More information about the Digitalmars-d-bugs mailing list