Issue 23450 - Unexpected constructor behaviour in DMD in rare cases
Summary: Unexpected constructor behaviour in DMD in rare cases
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: No Owner
URL:
Keywords: backend, wrong-code
Depends on:
Blocks:
 
Reported: 2022-11-01 16:07 UTC by Keivan
Modified: 2022-12-17 10:37 UTC (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Keivan 2022-11-01 16:07:43 UTC
Today I came across a strange bug while using D with dmd. I have still not been able to figure out under what conditions does it happen but it seems to be a DMD related bug to me. Here is a reproducible snippet of the code

```
import std;

alias DG = void delegate();

class TType
{
}

class MyClass
{
    this(TType t1, TType, double, double[2], double, double, DG, TType, TType,
            DG, DG, DG, double, double, double, double, double, ulong, bool)
    {
        assert(t1 is null); // I am passing null so should be null!
        // NOTE: Seems to work in LDC but fails in DMD.
        writeln("No Bug!");
    }
}

void main()
{
    auto tt = new TType;

    new MyClass(null, tt, 0, [0, 0], 0, 0, null, null, null, null, null, null,
            0, 0, 0, 0, 0, 0, false);
}
```

The code gives an assertion failure on the current versions of dmd (reproducible on run.dlang.io as well) and does not happen when using LDC. The bug seems to be sensitive to the number of arguments and their types making it reproducible only in very limited cases. I have tried my best to reduce it to minimum but still does require these many arguments. The end results seems to me like variables are shifted i.e. variable 1 gets value of variable 2 and so on, but don't have enough proof to support this.
Comment 1 Steven Schveighoffer 2022-11-02 10:39:31 UTC
If you add a name for the second parameter in the function, you can see that the second parameter is null, and the first parameter is assigned `tt`.

Even -vcg-ast thinks that it's passing them in the right order. I think this is a codegen bug.
Comment 2 Dennis 2022-11-02 11:04:39 UTC
Might be the same as issue 21301.

There's something about long parameter lists with arrays/small structs of floats/doubles where DMD messes up the register/stack assignment.
Comment 3 Keivan 2022-11-02 11:13:00 UTC
Also based on the discussion on the forums(https://forum.dlang.org/post/lxnogwxpuiwlxvruqlrs@forum.dlang.org), the issue might also be same as issue 22583