Thread overview
[Issue 5690] New: Struct const member makes struct const
Mar 03, 2011
Tom
Mar 03, 2011
Stewart Gordon
Jan 24, 2012
Walter Bright
Jan 24, 2012
Kenji Hara
Jan 24, 2012
Kenji Hara
Jan 24, 2012
Kenji Hara
March 03, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=5690

           Summary: Struct const member makes struct const
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: tarossi@gmail.com


--- Comment #0 from Tom <tarossi@gmail.com> 2011-03-03 05:34:25 PST ---
int main(string[] args) {
    auto s1 = f(); // MH MH
    auto s2 = g(); // OK
    s2.c = null; // OK
    return 0;
}

class C {}

struct StructWithConstMember {
    this(int i, C c) { this.i=i; this.c=c; }
    int i;
    const(C) c;
}

struct StructWithoutConstMember {
    this(int i, C c) { this.i=i; this.c=c; }
    int i;
    C c;
}

ref StructWithConstMember f() {
    return * new StructWithConstMember(1, new C); // ERROR
}

ref StructWithoutConstMember g() {
    return * new StructWithoutConstMember(1, new C); // OK
}


src\main.d(27): Error: *new StructWithConstMember(1,new C) is not mutable

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


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |spec
                 CC|                            |smjg@iname.com


--- Comment #1 from Stewart Gordon <smjg@iname.com> 2011-03-03 07:24:09 PST ---
It only makes sense that it should make it const - to reassign the struct would violate the constancy of the const member.  Though the MBNR approach (see issue 2625) would also work.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5690


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |INVALID


--- Comment #2 from Walter Bright <bugzilla@digitalmars.com> 2012-01-24 00:06:54 PST ---
Right, this is not a compiler bug.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5690


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-01-24 05:20:27 PST ---
(In reply to comment #2)
> Right, this is not a compiler bug.

No, this is a compiler bug.

(In reply to comment #1)
> It only makes sense that it should make it const - to reassign the struct would violate the constancy of the const member.

A returned value from f() is *mutable* object, so to reassign the part of it is
still valid.

  f().i = 10;  // field i is mutable so this code should compile.

This issue is a dup of bug 6366, that has more better summary "Issue 6336 - Can't return ref T where T has const/immutable members".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5690



--- Comment #4 from Kenji Hara <k.hara.pg@gmail.com> 2012-01-24 05:22:57 PST ---
Notice:

A NewExpression like (new StructWithConstMember(1, new C)) makes rvalue, but
dereferencing it (* new StructWithConstMember(1, new C)) makes *lvalue*.
So returning it from function by ref is valid.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=5690


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |DUPLICATE


--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> 2012-01-24 05:23:21 PST ---
*** This issue has been marked as a duplicate of issue 6336 ***

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