www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3731] New: Immutable class may be changed when inherits from mutable parent

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731

           Summary: Immutable class may be changed when inherits from
                    mutable parent
           Product: D
           Version: 2.039
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: tomeksowi gmail.com


--- Comment #0 from Tomasz Sowiński <tomeksowi gmail.com> 2010-01-20 12:44:48
PST ---
This merrily compiles:

class Zmienna {
    int a;
    this(int a) {
        this.a = a;
    }
}

immutable class Stala : Zmienna {
    this(int a) {
        super(a);
    }
}

void main() {
    auto st = new Stala(5);
    Zmienna zm = st;
    zm.a = 666;
//  st.a = 666; // fails
    assert (st.a == 666);
}


The above shows that an immutable class shouldn't be allowed to derive from
mutable classes. Unfortunately it won't work because Object is not (and can't
be) immutable. Not sure how to bite this one, perhaps make an opt out that says
mutable parents with no fields are OK? Or an exception only for Object?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 20 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy yahoo.com


--- Comment #1 from Steven Schveighoffer <schveiguy yahoo.com> 2010-01-21
05:43:28 PST ---
The solution would be to make it illegal to have a mutable class reference to
the base class.

In your example, this line should be an error:

Zmienna zm = st; // error, must use immutable(Zmienna) or const(Zmienna)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #2 from Tomasz Sowiński <tomeksowi gmail.com> 2010-01-21 11:03:00
PST ---
(In reply to comment #1)
 The solution would be to make it illegal to have a mutable class reference to
 the base class.
 
 In your example, this line should be an error:
 
 Zmienna zm = st; // error, must use immutable(Zmienna) or const(Zmienna)
Thanks, that makes sense. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 21 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |accepts-invalid, patch
                 CC|                            |yebblies gmail.com
            Summary|Immutable class may be      |Can implicitly cast an
                   |changed when inherits from  |immutable reference to a
                   |mutable parent              |derived class


--- Comment #3 from yebblies <yebblies gmail.com> 2011-06-13 22:59:03 PDT ---
https://github.com/D-Programming-Language/dmd/pull/125

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #4 from Kenji Hara <k.hara.pg gmail.com> 2011-09-16 03:24:37 PDT ---
I think issue 3731 is same as issue 5080, and the yebblies patch is a bit
better than mine.

See also Don and Steven's comments in 5080.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 16 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |rayerd.wiz gmail.com


--- Comment #5 from yebblies <yebblies gmail.com> 2011-12-13 16:23:28 EST ---
*** Issue 6863 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 12 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #6 from github-bugzilla puremagic.com 2012-01-27 20:24:11 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/7bed81961aefc5327b57be0862514b78d84355db
The fix for issue 3731 shows a couple of places in phobos that rely on this
bug.  std.xml is going away, so work around the issue for now (all problems are
caused by opEquals and opCmp not being const correct) and in std.datetime, AA
values must be rebindable.

https://github.com/D-Programming-Language/phobos/commit/472c2a984391a97fd6fda5b77d082e184b828c87
Merge pull request #408 from yebblies/issue3731

Remove implicit casts to mutable super class

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |k.hara.pg gmail.com


--- Comment #7 from yebblies <yebblies gmail.com> 2012-01-31 13:13:15 EST ---
*** Issue 5080 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 30 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


dawg dawgfoto.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dawg dawgfoto.de


--- Comment #8 from dawg dawgfoto.de 2012-03-04 05:19:41 PST ---
*** Issue 7636 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 04 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Stewart Gordon <smjg 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 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: -------
Mar 25 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #10 from Steven Schveighoffer <schveiguy yahoo.com> 2012-03-26
03:11:50 PDT ---
(In reply to comment #9)
 (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.
I think you misunderstand. I don't mean because Stala exists, Zmienna should be prevented from being mutable, even for cases where Stala is not involved. I mean, it should be illegal to have a mutable Stala reference point at a Zmienna. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #11 from Stewart Gordon <smjg iname.com> 2012-03-26 03:57:16 PDT ---
(In reply to comment #10)
 I think you misunderstand.  I don't mean because Stala exists, 
 Zmienna should be prevented from being mutable, even for cases 
 where Stala is not involved.  I mean, it should be illegal to have 
 a mutable Stala reference point at a Zmienna.
I haven't misunderstood. There's no such thing as a mutable Stala reference. Because Stala is an immutable class, any reference to a Stala is automatically immutable. See for yourself: ----- void main() { Stala st = new Stala(5); pragma(msg, typeof(st)); // immutable(Stala) st = new Stala(4); // errors, because st is immutable Zmienna zm = st; // accepts-invalid } ----- The bug is that DMD allows the implicit conversion of immutable(Stala) to Zmienna. It's just mb_id in my last example. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |doob me.com


--- Comment #13 from Steven Schveighoffer <schveiguy yahoo.com> 2012-04-16
05:30:03 PDT ---
*** Issue 7920 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 16 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Steven Schveighoffer <schveiguy yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg gmx.com


--- Comment #14 from Steven Schveighoffer <schveiguy yahoo.com> 2012-04-19
04:52:11 PDT ---
*** Issue 7939 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 19 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731



--- Comment #15 from github-bugzilla puremagic.com 2012-07-18 00:08:12 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/65f12c80d383bd655aec1f53510fd4ae201b3b79
Fix Issue 3731 - Can implicitly cast an immutable reference to a derived class
to a mutable reference to a base class.

Ensure the modifier conversion is valid before allowing an implicit conversion
to a base class.

https://github.com/D-Programming-Language/dmd/commit/bf982fac38051e34d365d972ff847983cb73848a
Merge pull request #125 from yebblies/issue3731

Issue 3731 - Can implicitly cast an immutable reference to a derived class

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 18 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3731


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |clugdbug yahoo.com.au
         Resolution|                            |FIXED


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