Requires `-de` compile flag.
Testcase:
```
ulong foo() {
return 1;
}
void compileCheck(const(ClusterInfoJohan) src, ClusterInfoJohan tgt) {
tgt = src;
}
struct ClusterInfoJohan {
UUID guid;
ulong oiuoi = 512;
ulong asdasdasd = {
foo();
return 1;
}();
}
struct UUID {
@safe @nogc
opAssign(UUID) { }
}
```
`dmd -de -o- test.d` gives:
test.d(6): Error: generated function `test.ClusterInfoJohan.opAssign` cannot be used because it is annotated with `@disable`
Compiles fine with DMD 2.101 or older.
Digger says it is caused by this commit: 9fc7c8ff643dbc88f84d95eb8e5b8956bcd29edb
https://github.com/dlang/dmd/pull/14483
Comment #1 by razvan.nitu1305 — 2023-06-19T15:50:01Z
The problem stems from the fact that `asdasdasd` is a system variable because it calls `foo` which is system. When the compiler tries to generate opAssign for ClusterInfoJohan it looks at the attributes of the potential opAssign/postblits/destructors it might call to deduce the attributes of the generated opAssign. Since UUID.opAssign is nogc and safe and the rest of the assignments (for oiuoi and asdasdasd) do not call any other user defined operators, the compiler wrongfully thinks that the generated opAssign should be nogc and safe. However, since we are accessing a system variable, it will issue a deprecation (which is silent normally, because errors are gagged when analyzing generated functions) which is catched when compiled with -de.
A workaround is to make `foo` @safe. That will make the code to compile.
Patch incoming.
Comment #2 by dlang-bot — 2023-06-20T14:35:44Z
@RazvanN7 created dlang/dmd pull request #15332 "Fix Issue 23964 - [REG2.102] inccorect error opAssign cannot be used …" fixing this issue:
- Fix Issue 23964 - [REG2.102] inccorect error opAssign cannot be used ... @disable
https://github.com/dlang/dmd/pull/15332
Comment #3 by johanengelen — 2024-04-29T13:18:12Z
> A workaround is to make `foo` @safe. That will make the code to compile.
Thanks, this indeed works and helps me out again now. A big problem with this bug is that it is _very_ hard to find the reason for the @disabling of opAssign in a normal codebase. So it is highly non-trivial to find out that setting @safe on a function somewhere is going to fix the compile error...
Comment #4 by robert.schadek — 2024-12-13T19:29:30Z