[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 11:58:06 UTC 2021


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

Dennis <dkorpel at live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |dkorpel at live.nl
           Hardware|x86_64                      |All
         Resolution|---                         |INVALID
                 OS|Linux                       |All

--- Comment #1 from Dennis <dkorpel at live.nl> ---
Neither method gets `scope` inferred. If you remove the `new` in Bar (`scope
bar = Bar;`) you'll get the same error for the struct. What's happening is that
since `Bar` is a pointer, it gets dereferenced implicitly:

```
@safe
void main()
{
    scope Bar* bar = new Bar;
    (*bar).dummy; // dereference strips away the scope layer
}
```
`dummy` can't escape the pointer `bar` since `this` is passed by `ref`.
Escaping a pointer to a ref variable (`&this`) is fixed by
https://github.com/dlang/dmd/pull/12812.

The class case is not allowed since dummy can do this:

```
Foo globalFoo;
class Foo
{
    @safe void dummy() {
        globalFoo = this;
    }
}
```

So they are not consistent because `struct` is a value type and `class` a
reference type.

--


More information about the Digitalmars-d-bugs mailing list