Bug 23144 – False circular reference error when the field name of a struct or class is the same as the type name
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-05-29T00:51:44Z
Last change time
2022-06-01T09:41:55Z
Keywords
diagnostic
Assigned to
No Owner
Creator
godmyoh
Comments
Comment #0 by menodinulla — 2022-05-29T00:51:44Z
False circular reference error occurs when the field name of a struct or class is the same as the type name of the field.
This error doesn't occur on local variables.
---
module example;
struct S { }
struct OtherStruct
{
S s; // allowed
S S; // Error: circular reference to variable `example.OtherStruct.S`
}
class C { }
class OtherClass
{
C c; // allowed
C C; // Error: circular reference to variable `example.OtherClass.C`
}
int main()
{
S S; // allowed
C C; // allowed
return 0;
}
Comment #1 by b2.temp — 2022-05-29T04:11:53Z
The message is not wrong: a lookup for `S` in `OtherStruct` begins in the `OtherStruct` scope so it finds `S` as a member, which is not ready yet.
The module scope operator should be used here or a fully qualified type.
```
struct S { }
struct OtherStruct
{
.S s;
.S S;
}
```
For local variables that works without qualifier because out of order declarations are not supported and the lookup is only valid for everything that is declared before the variable S (and then in the parent scopes)
```
int main()
{
S S;
return 0;
}
```
I suggest to close a invalid is the explanations are correct.
Comment #2 by b2.temp — 2022-05-29T04:41:56Z
however the error message should rather be "S is used as a type", in that case the issue is valid.