[Issue 23054] New: importC: struct compound-literal assigned by pointer has wrong storage duration

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Apr 24 23:06:47 UTC 2022


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

          Issue ID: 23054
           Summary: importC: struct compound-literal assigned by pointer
                    has wrong storage duration
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: ImportC, wrong-code
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: duser at neet.fi
                CC: duser at neet.fi

test program:

struct S { int x; };
int test1(int x)
{
        // compile error because it's static (but currently this segfaults when
compiling)
        // using a local variable assigned to x, it gives "Error: non-constant
expression `y = x`"
        struct S *b = &(struct S){x};
        return 0;
}
int test2(int x)
{
        struct S *s = &(struct S){0};
        s->x = x;
        if (x != 0)
        {
                test2(0);
                // detect if it's static instead of automatic
                // the recursive call shouldn't have affected this copy
                if (s->x != x) return 2;
        }
        return 0;
}
int test3(void)
{
        // detect if it's GC instead of static or automatic
        // this is true in CTFE
        void *prev;
        for (int i = 0; i < 2; i++)
        {
                void *curr = &(struct S){0}; // should have the same address on
both loop iterations
                if (i == 0)
                        prev = curr;
                else
                        if (curr != prev) return 3;
        }
        return 0;
}
_Static_assert(test1(1) == 0, "1");
_Static_assert(test2(1) == 0, "2");
_Static_assert(test3() == 0, "3"); // fails
int main()
{
        int rv;
        if (rv = test1(1)) return rv; // function fails to compile
        if (rv = test2(1)) return rv; // fails
        if (rv = test3()) return rv;
        return 0;
}

"&(struct S){0}" here should work like taking the address of a local variable

outside CTFE, it works like a static variable, in CTFE it's allocated using GC
(which is also wrong)

--


More information about the Digitalmars-d-bugs mailing list