[Issue 21285] Delegate covariance broken between 2.092 and 2.094 (git master).

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Oct 18 08:03:24 UTC 2021


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

--- Comment #7 from Mathias LANG <pro.mathias.lang at gmail.com> ---
I looked a bit more into this. The issue is that `scope` is inferred on:
>         "preset": (string arg) {

Which is wrong, but since the function is not `@safe`, it shouldn't matter.
However, DMD also does something very stupid: It tries to find a common type
among all initializers without looking at the array type! This can be seen in
the following code:

```
void main ()
{
    void function()[] arr = [ &f1, &f2 ];
}

int f1 () { return 42; }
string f2 () { return "42"; }
```

Which errors out with:
```
cov.d(3): Error: incompatible types for `(& f1) : (& f2)`: `int function()` and
`string function()`
```

Now there are maybe good reasons to reject this code (we got co and
contravariance wrong in the past), but the fact that they error out between one
another is just wrong.

What the FE essentially do is:
- Get all elements;
- Make an array of all those elements;
- Try to convert this array to the LHS array;

The stack trace I got while aborting on the above code was:
```
core.exception.AssertError at src/dmd/expression.d(4019): Assertion failure
----------------
??:? _d_assertp [0x10a86a91c]
src/dmd/dinifile.d:177 dmd.globals.MATCH
dmd.expression.FuncExp.matchType(dmd.mtype.Type, dmd.dscope.Scope*,
dmd.expression.FuncExp*, int) [0x10a6213c7]
src/dmd/dinifile.d:177 _ZN14implicitCastTo14ImplicitCastTo5visitEP7FuncExp
[0x10a553de6]
src/dmd/dinifile.d:177 _ZN7FuncExp6acceptEP7Visitor [0x10a621535]
src/dmd/dinifile.d:177 dmd.expression.Expression
dmd.dcast.implicitCastTo(dmd.expression.Expression, dmd.dscope.Scope*,
dmd.mtype.Type) [0x10a5538c7]
src/dmd/dinifile.d:177 _ZN10Expression14implicitCastToEP5ScopeP4Type
[0x10a6194d8]
src/dmd/dinifile.d:177 dmd.mtype.Type
dmd.expressionsem.arrayExpressionToCommonType(dmd.dscope.Scope*, ref
dmd.root.array.Array!(dmd.expression.Expression).Array) [0x10a62e240]
src/dmd/dinifile.d:177
_ZN25ExpressionSemanticVisitor5visitEP20AssocArrayLiteralExp [0x10a6330e1]
src/dmd/dinifile.d:177 _ZN20AssocArrayLiteralExp6acceptEP7Visitor [0x10a61ef15]
```

As can be seen, the error happens in a child of `arrayExpressionToCommonType`.

--


More information about the Digitalmars-d-bugs mailing list