[Issue 22621] New: static real array not passed correctly to function as r-value

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Dec 22 20:52:15 UTC 2021


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

          Issue ID: 22621
           Summary: static real array not passed correctly to function as
                    r-value
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: tedgin at cyverse.org

A static array of reals passed to a function as an r-value may have an
incorrect value inside the function. The correctness depends on what is defined
before the array in the caller's stack frame. Here's an example.

```
// bug.d

import std.stdio;

void f(real[1] x) 
do {
    writefln("x[0] = %e", x[0]);
    assert(x[0] == 2.0L, "x[0] != 2");
}

void main() 
do {
    version (succeeds) {
        int _ = 1;
    } else {
        double _ = 1.0;
    }

    real[1] y = [ 2.0L ];
    f(y);
}
```

If an int is defined prior to the array, the program works as expected, but if
a double is defined, the program fails.  

```
? rdmd -version=succeeds bug.d
x[0] = 2.000000e+00

? rdmd -version=fails bug.d
x[0] = -4.344603e-713
core.exception.AssertError at bug.d(8): x[0] != 2
----------------
??:? _d_assert_msg [0x55558abc29e4]
??:? void bug.f(real[1]) [0x55558ab95921]
??:? _Dmain [0x55558ab9596b]

```

I'm using dmd version 2.098.0 and rdmd build 20211010.

--


More information about the Digitalmars-d-bugs mailing list