Thread overview
[Issue 5123] New: Cannot assign null to a class with 'alias this'
Oct 26, 2010
osa8aso@gmail.com
Oct 26, 2010
osa8aso@gmail.com
Oct 23, 2011
Kenji Hara
October 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5123

           Summary: Cannot assign null to a class with 'alias this'
           Product: D
           Version: D2
          Platform: Other
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: osa8aso@gmail.com


--- Comment #0 from osa8aso@gmail.com 2010-10-26 13:34:54 PDT ---
If class has 'alias this' for a struct member, attempt to assign null to a class instance fails:
------
struct Foo {}
class Bar {
    Foo foo_;
    alias foo_ this;
}
void main() {
    Bar a;
    a = null;
}
------
aliasthis1.d(8): Error: cannot implicitly convert expression (null) of type
void* to Foo

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5123


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2010-10-26 13:43:16 PDT ---
Here alias this is doing its work of "let's pretend" a Bar instance is a Foo instance. To solve this problem you may need something more refined than alias this, something that allows a partial aliasing. I don't think it's an easy thing to do.

A related program shows a different outcome:

struct Foo {}
class Bar {
    Foo foo_;
    alias foo_ this;
}
void main() {
    Bar a;
    //a = null;
    Foo f;
    a = f; // Access violation!
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5123



--- Comment #2 from osa8aso@gmail.com 2010-10-26 15:17:04 PDT ---
> A related program shows a different outcome:
>     Bar a;
>     Foo f;
>     a = f; // Access violation!

This is expected. 'a' is an uninitialized, and attempt to assign to a Foo part of null instance causes access violation. This is no different from

  Bar a;
  a.foo_ = Foo();

"Partial" assignment of Foo to a valid Bar instance is fine and works as expected:
-----
struct Foo { int x; }
class Bar {
    Foo foo_;
    alias foo_ this;
    int y;
}
void main() {
    Bar a = new Bar;
    a.x = 1;
    a.y = 42;
    assert( a.foo_.x == 1 && a.y == 42 );
    a = Foo( 2 );
    assert( a.foo_.x == 2 && a.y == 42 );
}
-----

My problem is only with special treatment of null: I want to nullify local reference to a Bar instance, not change Bar or Foo inside it.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=5123



--- Comment #3 from bearophile_hugs@eml.cc 2010-10-26 15:36:13 PDT ---
(In reply to comment #2)
> This is expected. 'a' is an uninitialized, and attempt to assign to a Foo part of null instance causes access violation. This is no different from
> 
>   Bar a;
>   a.foo_ = Foo();

Right, sorry for the noise.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 23, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5123


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2011-10-23 04:15:28 PDT ---
This issue was already fixed by bug 2943.

And this was related to bug 6630.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------