www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8603] New: Member access operator doesn't always dereference the pointer it's operating on

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

           Summary: Member access operator doesn't always dereference the
                    pointer it's operating on
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: tommitissari hotmail.com



struct S1
{
    int _value = 42;

    void fun()
    {
        ++_value;
    }
}

void fun(ref S1 s1)
{
    s1._value += 1000;
}

void fun(ref S1* ptr1)
{
    ++ptr1;
}
/////////////////////////////
struct S2
{
    int _value = 42;
}

void fun(ref S2 s2)
{
    ++s2._value;
}

void fun(ref S2* ptr2)
{
    ++ptr2;
}
/////////////////////////////
struct S3
{
    int _value = 42;
}

void fun(ref S3 s3)
{
    ++s3._value;
}


void main()
{
    auto ptr1 = new S1();
    ptr1.fun(); // OK: calls member (*ptr1).fun()

    auto ptr2 = new S2();
    ptr2.fun(); // WRONG: calls .fun(ptr2)
                // should call .fun(*ptr2) 

    auto ptr3 = new S3();
    ptr3.fun(); // ERROR: function main.fun (ref S3 s3) is
                // not callable using argument types (S3*)
                // Should call .fun(*ptr3)
}

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


Maxim Fomin <maxim maxim-fomin.ru> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |maxim maxim-fomin.ru



---
I agree with current dmd behavior described here. UFCS purpose was to rewrite
expression "obj.method(args)" to "method(obj, args)" and it was not intended to
allow to call "void fun(ref S2 s2)" instead of "void fun(ref S1* ptr1)" with
pointer argument. Equivalence of accessing member through pointer or directly
through object (absence of -> operator) should happen only in cases when some
actual member is accessed (to emphasize: equivalence of access, not equivalence
of pointer and not-pointer types). In case of UFCS there is no member accessing
and semantic need not be same.

If your request to change behavior would be accepted, following problem arise:

import std.stdio;

struct S {}

void foo(S s)
{
    writeln(".");
}

void foo(S* ptr)
{
    writeln("->");
}

void main()
{
    auto s = new S();
    s.foo(); // currently prints "->", would print "."
}

If first function argument would be changed, the output would be also
unintentionally changed. This is unexpected and contradicts to usual
overloading rules. What I am afraid of, is that it would be possible to call
function expected object value with implicitly converted (dereferenced) to
value pointer. Moreover, this implicit pointer to type conversion would occur
in some limited cases where function is called if it is a member of that type.

Consider following code:

import std.stdio;

void foo(int i)
{
    writeln(".");
}

void foo(int* i)
{
    writeln("->");
}

void main()
{
    int* i;
    foo(i);  // good
    i.foo(); // bug
}

In this case what actually would happen is: (unexpected) conversion of pointer
type to non-pointer type requires (unintentional) pointer dereference which
would result in segfault.

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


Maxim Fomin <maxim maxim-fomin.ru> changed:

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



---
*** Issue 8616 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: -------
Sep 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8603




PDT ---
 If your request to change behavior would be accepted, following problem arise:
That's fixed by simply making using UFCS illegal when there function is overloaded on both S and S*. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Sep 04 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=8603


Jonathan M Davis <jmdavisProg gmx.com> changed:

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



PDT ---

when I created it), but I think that the description in 8616 is much better, so
I'm going to close this one in favor of that one.

*** This issue has been marked as a duplicate of issue 8616 ***

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