D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 9372 - Class member with @disabled ctor makes class ctor unusable
Summary: Class member with @disabled ctor makes class ctor unusable
Status: RESOLVED DUPLICATE of issue 9665
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-01-22 12:00 UTC by hsteoh
Modified: 2014-09-22 05:26 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 hsteoh 2013-01-22 12:00:45 UTC
Code:

-----------SNIP-------------
import std.stdio;

struct Trouble {
        @disable this();
        @disable this(this);

        int dummy;

        this(int x) {
                dummy = x;
        }
}

class Room {
        Trouble t;

        this() {
                t = Trouble(123);
        }
}

void main() {
        auto r = new Room; // this is line 23
}
-----------SNIP-------------

Compiler error:
-----------SNIP-------------
test.d(23): Error: default construction is disabled for type Room
-----------SNIP-------------

Commenting out the @disabled lines in Trouble makes the problem go away.

This behaviour makes no sense. The class ctor is a default ctor which calls the Trouble ctor with non-default arguments, so it should be allowed. I can see where this shouldn't be allowed (if Room is another struct for, example), but since Room is a class, this should be allowed.
Comment 1 Jacob Carlborg 2013-01-22 23:34:19 UTC
I don't know if this makes any sense but:

When the code "new Room" is run the runtime will first create a new instance of "Room" and initialize all fields. It will then run the constructor. Technically you don't need to run the constructor at all, I don't do that in my serialization library. The field "t" cannot be initialized because it has its default constructor disabled. I'm not sure if the default constructor of Trouble is the same as "Trouble.init". If not, I think this should compile. If it is the same, then this error perhaps makes sense.
Comment 2 hsteoh 2013-01-23 07:52:50 UTC
(In reply to comment #1)

Maybe it makes sense on some level, perhaps from the implementation POV, but to me, a language-level user, it makes no sense at all, because a ctor is supposed to be what initializes the class instance.

Furthermore, a class with a ctor that takes more than zero parameters causes "auto x = new Class;" to be rejected, because the ctor cannot be called with no arguments. So to me, this indicates that the ctor will always be called (as expected); the compiler never says "OK you called new with no parameters, I'll just default-initialize all your class fields". Since this is the case, it makes no sense to now suddenly require that all fields be default-initializable. It has always been the ctor's job to initialize all fields.

(In fact that's why I chose to use a class instead of a struct, since structs can be declared without calling any ctors.)
Comment 3 hsteoh 2013-01-23 07:55:10 UTC
P.S. Even Object.factory does not allow the creation of objects without a default ctor, so I'd argue that in no case should the implementation require that all class fields be default-initializable when there's already a ctor to do the job.
Comment 4 Jacob Carlborg 2013-01-23 08:02:58 UTC
(In reply to comment #3)
> P.S. Even Object.factory does not allow the creation of objects without a
> default ctor, so I'd argue that in no case should the implementation require
> that all class fields be default-initializable when there's already a ctor to
> do the job.

Then that will break a lot of the safety D provides with its default initialized variables. Example:

class Foo
{
    int a; // assume this is not initialized

    this ()
    {
        writeln(a); // writes garbage
    }
}
Comment 5 hsteoh 2013-01-23 08:14:47 UTC
It should be illegal to reference an uninitialized variable in the ctor. Something similar to how immutable fields are handled can be applied here: you're not allowed to read the value of an immutable field until it's initialized, and it can only be written to once.
Comment 6 Jacob Carlborg 2013-01-23 11:57:38 UTC
(In reply to comment #5)
> It should be illegal to reference an uninitialized variable in the ctor.
> Something similar to how immutable fields are handled can be applied here:
> you're not allowed to read the value of an immutable field until it's
> initialized, and it can only be written to once.

The following code with compiles DMD 2.061 and when it runs it prints "0".

import std.stdio;

class Foo
{
    immutable int a;

    this ()
    {
        writeln(a);
        a = 3;
    }
}

void main ()
{
    new Foo;
}
Comment 7 Maxim Fomin 2013-01-24 04:02:57 UTC
The issue is debatable, but code below should be supported;

import std.stdio;

struct Trouble {
        @disable this();
        @disable this(this);

        int dummy;

        this(int x) {
                dummy = x;
        }
}

class Room {
        Trouble t = Trouble(123); // this should work

        this() {
                //t = Trouble(123);
        }
}

void main() {
        auto r = new Room; // this is line 23
}

Current situation shows either limitation of new implementation (druntime) or design.
Comment 8 Jacob Carlborg 2013-01-24 04:43:05 UTC
(In reply to comment #7)
> The issue is debatable, but code below should be supported;
> 
> import std.stdio;
> 
> struct Trouble {
>         @disable this();
>         @disable this(this);
> 
>         int dummy;
> 
>         this(int x) {
>                 dummy = x;
>         }
> }
> 
> class Room {
>         Trouble t = Trouble(123); // this should work
> 
>         this() {
>                 //t = Trouble(123);
>         }
> }
> 
> void main() {
>         auto r = new Room; // this is line 23
> }
> 
> Current situation shows either limitation of new implementation (druntime) or
> design.

I think that should work. The constructor does not initialize the fields, it's done before the constructor is called. This works:

extern (C) Object _d_newclass(in ClassInfo);

class Room
{
    int i = 3;
    
    this (int f) {}
}

void main ()
{
    auto room = cast(Room) _d_newclass(Room.classinfo);
    assert(room.i == 3);
}

The constructor is not called but the field is initialized anyway.
Comment 9 Kenji Hara 2014-09-22 05:26:16 UTC
The original code has been working from 2.064, by fixing issue 9665.

(In reply to hsteoh from comment #0)
> Code:
> 
> -----------SNIP-------------
> import std.stdio;
> 
> struct Trouble {
>         @disable this();
>         @disable this(this);
> 
>         int dummy;
> 
>         this(int x) {
>                 dummy = x;
>         }
> }
> 
> class Room {
>         Trouble t;
> 
>         this() {
>                 t = Trouble(123);

By fixing issue 9665, this line is correctly handled as the initialization of the instance field 't'. Therefore the disabled default constructor won't be called on that.

>         }
> }
> 
> void main() {
>         auto r = new Room; // this is line 23
> }

*** This issue has been marked as a duplicate of issue 9665 ***