www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5363] New: const + alias this = wrong code

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

           Summary: const + alias this = wrong code
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: dsimcha yahoo.com


--- Comment #0 from David Simcha <dsimcha yahoo.com> 2010-12-21 19:30:58 PST ---
Happens on at least 2.050, 2.051.

import core.stdc.stdio;  // Pollutes asm less than std.stdio.

struct Foo {
    uint dummy = 0;
    uint k = 0;

    alias dummy this;  // Yes, this line is necessary for reproducing the bug.

    // This needs to be a function to reproduce the bug.  Setting k directly
    // doesn't cut it.
    void put(uint element) {
        k += element;
    }

    // rhs must be const to reproduce the bug.
    void put(const Foo rhs) {
        k += rhs.k;
    }
}

void main() {
    Foo foo1, foo2;
    foo1.put(10);
    foo2.put(42);
    foo1.put(foo2);

    printf("%d\n", foo1.k);  // 10, should be 52
}

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


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

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


--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2011-06-21 19:36:13 PDT ---
More simple case:
----
struct S
{
    int dummy;
    alias dummy this;
}
int foo(int){ return 1; }
int foo(const(S)){ return 2; }
void main()
{
    S s;
    assert(foo(s) == 2);
}
----

It is the reason of this problem that the type matching levels through normal
and alias this have same basis.

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


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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #2 from Kenji Hara <k.hara.pg gmail.com> 2011-07-14 04:31:02 PDT ---
https://github.com/D-Programming-Language/dmd/pull/173

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


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com


--- Comment #3 from SomeDude <lovelydear mailmetrash.com> 2012-04-22 09:30:41
PDT ---
Bug still here in 2.059.
Kenji, what happened to your pull ?

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


jens.k.mueller gmx.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jens.k.mueller gmx.de
         OS/Version|Windows                     |All
           Severity|critical                    |blocker


--- Comment #4 from jens.k.mueller gmx.de 2012-11-13 02:36:38 PST ---
Today I ran into this bug. Unfortunately it tooks quite some time until I had a
minimal example that exemplified the problem. Then I could search the database
to find this report here.
I leave my minimal example here for reference.

struct A                                                                        
{                                                                               
    B b;                                                                        
    alias b this;                                                               
}                                                                               

struct B                                                                        
{                                                                               
    static struct Value                                                         
    {                                                                           
    }                                                                           
    Value value;                                                                

    B[] bs;                                                                     
    alias value this;                                                           

    bool opEquals(ref const B rhs) const // remove const in front of B and it
works
    {                                                                           
        return this.bs == rhs.bs;                                               
    }                                                                           
}                                                                               

unittest                                                                        
{                                                                               
    A a;                                                                        
    a.bs ~= B.init;                                                             
    B b;                                                                        
    b.bs ~= B.init;                                                             
    assert(b.opEquals(a)); // fails                                
}

This is Linux and dmd2.060.
This issue is a blocker for me because I can't use const and alias this
anymore. I have to remove the const which makes my code error-prone.

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