D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 23054 - importC: struct compound-literal assigned by pointer has wrong storage duration
Summary: importC: struct compound-literal assigned by pointer has wrong storage duration
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords: ImportC, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2022-04-24 23:06 UTC by duser
Modified: 2022-05-14 09:07 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description duser 2022-04-24 23:06:47 UTC
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)
Comment 1 Walter Bright 2022-05-12 07:05:10 UTC
test1: I didn't realize that 6.5.2.5 Compound Literals could be non-static. This will require some significant work to implement.
Comment 2 Walter Bright 2022-05-12 19:55:22 UTC
test3 is checking the location of the temporary. C11 does not specify the location. Whether the address matches or not in the scope in the loop does not matter, as the lifetime of the temporary does not survive the scope.

In any case, the temporary is allocated on the stack, not statically and not on the GC heap.
Comment 3 Dlang Bot 2022-05-12 20:01:37 UTC
@WalterBright created dlang/dmd pull request #14118 "fix Issue 23054 - importC: struct compound-literal assigned by pointe…" fixing this issue:

- fix Issue 23054 - importC: struct compound-literal assigned by pointer has wrong storage duration

https://github.com/dlang/dmd/pull/14118
Comment 4 Dlang Bot 2022-05-14 09:07:07 UTC
dlang/dmd pull request #14118 "fix Issue 23054 - importC: struct compound-literal assigned by pointe…" was merged into master:

- 2b9ee9918bd4b8d160622df662413111bdb841ed by Walter Bright:
  fix Issue 23054 - importC: struct compound-literal assigned by pointer has wrong storage duration

https://github.com/dlang/dmd/pull/14118