[Issue 3731] Derived class implicitly convertible to base class with arbitrary change of constancy

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sun Mar 25 07:52:40 PDT 2012


http://d.puremagic.com/issues/show_bug.cgi?id=3731


Stewart Gordon <smjg at iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Can implicitly cast an      |Derived class implicitly
                   |immutable reference to a    |convertible to base class
                   |derived class               |with arbitrary change of
                   |                            |constancy


--- Comment #9 from Stewart Gordon <smjg at iname.com> 2012-03-25 07:52:59 PDT ---
(In reply to comment #1)
> The solution would be to make it illegal to have a mutable class reference to
> the base class.

No, because a Zmienna is perfectly allowed to be mutable.  It's Stala that
isn't.

> In your example, this line should be an error:
> 
> Zmienna zm = st; // error, must use immutable(Zmienna) or const(Zmienna)

Correct, since because Stala is immutable, any object reference of type Stala
is actually of type immutable(Stala).

The bug is that constancy is ignored when converting from a derived class to a
base class, as this code shows (DMD 2.058, Win32):
----------
class Base           { int x; }
class Derived : Base { int y; }

void main() {
    Derived            md;
    const(Derived)     cd;
    immutable(Derived) id;

    Base               mb_md = md;
    const(Base)        cb_md = md;
    immutable(Base)    ib_md = md; // accepts-invalid

    Base               mb_cd = cd; // accepts-invalid
    const(Base)        cb_cd = cd;
    immutable(Base)    ib_cd = cd; // accepts-invalid

    Base               mb_id = id; // accepts-invalid
    const(Base)        cb_id = id;
    immutable(Base)    ib_id = id;
}

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


More information about the Digitalmars-d-bugs mailing list