17184, "mppf", "overriden method can return unmanaged subclass but not owned subclass", "2021-02-18T17:58:55Z"
It appears that, in an overriden method, we can return a subclass:
class C {
var x: int;
proc returnCopyOfMe() {
return new unmanaged C(x);
}
}
class D: C {
var y: int;
override proc returnCopyOfMe() {
return new unmanaged D(x,y);
}
}
proc main() {
var a = new C(1);
var b = new D(2,3);
var c: owned C = new D(4,5);
writeln(a.returnCopyOfMe());
writeln(b.returnCopyOfMe());
writeln(c.returnCopyOfMe());
}
However the equivalent program not using unmanaged
but rather owned
does not compile:
class C {
var x: int;
proc returnCopyOfMe() {
return new owned C(x);
}
}
class D: C {
var y: int;
override proc returnCopyOfMe() {
return new owned D(x,y);
}
}
proc main() {
var a = new C(1);
var b = new D(2,3);
var c: owned C = new D(4,5);
writeln(a.returnCopyOfMe());
writeln(b.returnCopyOfMe());
writeln(c.returnCopyOfMe());
}
It seems like we intend to support covariant return types but have not correctly handled the case when they are owned/shared.