www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Error: circular reference to variable cause by order (bugs?)

reply Dakota <dakota gmail.com> writes:
I am trying to translate some c code into d, get this error:

```d
struct A {
	void* sub;
}

struct B {
	void* subs;
}

__gshared {
	const A[1] a0 = [
		{ &b1_ptr },
	];
	const A[2] a1 = [
		{ &b1_ptr },
	];
	const B b1 = {a1.ptr};
	const b1_ptr = &b1;

	const A[1] a2 = [
		{ &b1_ptr },
	];
	const B b2 = {a2.ptr};
	const b2_ptr = &b2;
}

```

```sh
  Error: circular reference to variable `tests.b1_ptr`
```

If I move `a0` after `b2_ptr`, no error any more.

The c code has circular reference type, and compile ok. and there 
is a lot type I am not able to adjust order by hand to guess 
right order.

the translate code still need to work with C interface, so I can 
not change the struct layout.  is this a bug of d?
Jul 18
parent monkyyy <crazymonkyyy gmail.com> writes:
On Thursday, 18 July 2024 at 12:25:37 UTC, Dakota wrote:
 I am trying to translate some c code into d, get this error:

 ```d
 struct A {
 	void* sub;
 }

 struct B {
 	void* subs;
 }

 __gshared {
 	const A[1] a0 = [
 		{ &b1_ptr },
 	];
 	const A[2] a1 = [
 		{ &b1_ptr },
 	];
 	const B b1 = {a1.ptr};
 	const b1_ptr = &b1;

 	const A[1] a2 = [
 		{ &b1_ptr },
 	];
 	const B b2 = {a2.ptr};
 	const b2_ptr = &b2;
 }

 ```

 ```sh
  Error: circular reference to variable `tests.b1_ptr`
 ```

 If I move `a0` after `b2_ptr`, no error any more.

 The c code has circular reference type, and compile ok. and 
 there is a lot type I am not able to adjust order by hand to 
 guess right order.

 the translate code still need to work with C interface, so I 
 can not change the struct layout.  is this a bug of d?
if it was a compiler bug it probably be a crash; and the c code may have had a header file of some sort to make the linker happy that the traslator skipped that is just confusing cyclic code
Jul 18