[Issue 21175] opAssign should be allowed to return void and let the compiler take care of chained assignments

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Nov 18 15:23:35 UTC 2021


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

Dennis <dkorpel at live.nl> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dkorpel at live.nl

--- Comment #2 from Dennis <dkorpel at live.nl> ---
N.b. this is relevant to dip1000, since this works:

```
@safe:
struct S {
        int* x;
        void opAssign(return scope S other) scope {
                this.x = other.x;
        }
}

void main() {
        scope S b;
        scope S a;
        a = b;
}
```

But this doesn't:
```
@safe:
struct S {
        int* x;
        ref S opAssign(return scope S other) return scope {
                this.x = other.x;
                return this;
        }
}

void main() {
        scope S b;
        scope S a;
        a = b;
}
```

source/apps/retscope.d(7): Error: scope variable `other` assigned to `this`
with longer lifetime

The return destination of `return scope S other` was `this` when `opAssign`
returned `void`, but now the return destination is the return value. Example in
Phobos:

https://github.com/dlang/phobos/blob/62780daf85c4c57bbc805e1e6499a33a852a2802/std/datetime/systime.d#L696

The only reason this compiles currently is because of issue 20149

--


More information about the Digitalmars-d-bugs mailing list