Thread overview
[Issue 4511] New: Contravariance problem
Dec 09, 2010
Michal Minich
Dec 09, 2010
Sobirari Muhomori
Dec 09, 2010
Sobirari Muhomori
Jun 17, 2011
yebblies
Nov 08, 2011
Walter Bright
July 26, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4511

           Summary: Contravariance problem
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2010-07-26 13:35:11 PDT ---
A problem found by Jesse Phillips. This is Java program:


class Base {}

class Derived extends Base {}

abstract class Abstract {
    abstract Base foo();
}

class Concrete extends Abstract {
     @Override Derived foo() {
        return new Base();
    }
}

class Test {}



The javac compiler prints:


Test.java:11: incompatible types
        return new Base();
               ^
  required: Derived
  found:    Base
1 error



But dmd 2.047 compiles this D2 program with no errors:

class Base {}

class Derived : Base {}

abstract class Abstract {
    abstract Base foo();
}

class Concrete : Abstract {
    override Derived foo() {
        return new Base;
    }
}

void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4511


Michal Minich <michal.minich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |michal.minich@gmail.com


--- Comment #1 from Michal Minich <michal.minich@gmail.com> 2010-12-09 01:16:45 PST ---
Slightly extended example shows that the returned instance is of type derived!, but constructor is not called.

class Base {}

class Derived : Base { this () { x = 1; }  int x; }

abstract class Abstract {
    abstract Base foo();
}

class Concrete : Abstract {
    override Derived foo() {
        return new Base;
    }
}

void main() {
    auto c = new Concrete;
    auto x = c.foo();
    writeln (typeof(x).stringof); // prints Derived (even it is result of 'new
Base')
    writeln (x.x); // prints 0 (wich means Derived's constructor is not called)
    x.x = 2;
    writeln (x.x); // prints 2
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4511


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Blocks|                            |2573


--- Comment #2 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-12-09 11:27:03 PST ---
>     auto c = new Concrete;
>     auto x = c.foo();
>     writeln (typeof(x).stringof); // prints Derived (even it is result of 'new
> Base')

You're doing it wrong.
typeof(x) is evaluated at compile time and always gives the declared type even
if x==null

The correct code is
---
auto c = new Concrete;
auto x = c.foo();
writeln(x.classinfo.name); // Base
---

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 09, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4511


Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |critical


--- Comment #3 from Sobirari Muhomori <dfj1esp02@sneakemail.com> 2010-12-09 11:31:30 PST ---
>     x.x = 2;
>     writeln (x.x); // prints 2

This just writes to the memory after the allocated Base object, probably corrupting heap.

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


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |yebblies@gmail.com
           Platform|x86                         |All
         OS/Version|Windows                     |All


--- Comment #4 from yebblies <yebblies@gmail.com> 2011-06-17 07:02:54 PDT ---
https://github.com/D-Programming-Language/dmd/pull/137

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


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #5 from Walter Bright <bugzilla@digitalmars.com> 2011-11-08 02:20:51 PST ---
https://github.com/D-Programming-Language/dmd/commit/b3bbae5302ed7b68d98134d389dfc8d3cc735119

https://github.com/D-Programming-Language/dmd/commit/1209914a3cca53bd2026bedccbae6118026b666c

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