www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 19192] New: DMD generates invalid code for covariants

https://issues.dlang.org/show_bug.cgi?id=19192

          Issue ID: 19192
           Summary: DMD generates invalid code for covariants involving
                    template classes
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: blocker
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: puneet coverify.org

tested with DMD 2.081.2 and with LDC 1.10

$ rdmd covariant.d
[]
$ rdmd -version=COVARIANT covariant.d
[1635151715, 1851877746, 1631727220, 1965564274, 1735290732, 774462811,
7496002, 0, 4802768, 0, 4492600, 0, 4492620, 0, 4492632, 0, 4492772, 0,
4448724, 0, 4448744, 0, 0, 0, 4505724, 0, 0]
core.exception.AssertError covariant.d(55): Assertion failure

// covariant.d
interface Foo {
  int[] vars();
  Foo troll();
}
class Frop: Foo {
  int[] vars() {
    assert(false);
  }
  Foo troll() {
    assert(false);
  }
}
abstract class Barbee : Foo {}
class Bar(V): Barbee {
  Barber!(V) _barber;
  this(Barber!(V) barber) {
    _barber = barber;
  }
  int[] vars() {
    return _barber.vars;
  }
  version (COVARIANT) alias COVARTYPE = typeof(this);
  else alias COVARTYPE = Foo;
  COVARTYPE troll() {
    return _barber.troll()[0];
  }
}
class Barber(V) {
  Bar!(V)[] _elems;
  int[] vars() {
    return [];          
  }
  auto troll() {
    return this;
  }
  void build(size_t v) {
    _elems.length = v;
    for (size_t i; i!=v; ++i) _elems[i] = new Bar!(V)(this);
  }
  Bar!(V) opIndex(Foo indx) {
    return new Bar!(V)(this);
  }
  Bar!(V) opIndex(size_t n) {
    build(n+1);
    return _elems[n];
  }
}
void main() {
  import std.stdio;
  auto barber = new Barber!(ulong[])();
  auto frop = new Frop();
  Foo foo = barber[frop];
  Foo fool = foo.troll();
  auto ll = fool.vars; writeln(ll);
  assert (ll.length == 0);
}

--
Aug 26 2018