[Issue 8603] Member access operator doesn't always dereference the pointer it's operating on

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Aug 31 09:50:18 PDT 2012


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


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

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


--- Comment #1 from Maxim Fomin <maxim at maxim-fomin.ru> 2012-08-31 09:50:15 PDT ---
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: -------


More information about the Digitalmars-d-bugs mailing list