[Issue 18598] New: cyclic constructor calls have undefined behavior but are accepted in @safe code

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Mar 12 10:29:36 UTC 2018


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

          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 at puremagic.com
          Reporter: ag0aep6g at 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
> behavior.

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

--


More information about the Digitalmars-d-bugs mailing list