www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4511] New: Contravariance problem

reply d-bugmail puremagic.com writes:
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



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: -------
Jul 26 2010
next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4511


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

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



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: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4511


Sobirari Muhomori <dfj1esp02 sneakemail.com> changed:

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



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: -------
Dec 09 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jun 17 2011
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Nov 08 2011