www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 18598] New: cyclic constructor calls have undefined behavior


          Issue ID: 18598
           Summary: cyclic constructor calls have undefined behavior but
                    are accepted in  safe code
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: safe
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: ag0aep6g gmail.com

On class constructors, the spec says [1]:

 It is illegal for constructors to mutually call each other, although
 the compiler is not required to detect it. It will result in undefined
But DMD accepts this: ---- class C { this() safe { this(1); } this(int i) safe { this(); } } void main() safe { auto c = new C; } ---- According to the spec, the code has undefined behavior, so it shouldn't be accepted with the safe attribute. Also according to the spec, "the compiler is not required to detect" this, but that can't apply to safe code, because the compiler is required to ensure that there is "no possibility of undefined behavior" in safe code [2]. (As always, this can be fixed by letting DMD reject the code, or by changing the spec to give the code defined behavior.) [1] https://dlang.org/spec/class.html#constructors [2] https://dlang.org/spec/function.html#function-safety --
Mar 12 2018