D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6838 - Covariant return types don't work with static arrays
Summary: Covariant return types don't work with static arrays
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-10-22 09:07 UTC by Piotr Szturmaj
Modified: 2012-05-04 16:06 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Piotr Szturmaj 2011-10-22 09:07:48 UTC
This is an example from http://d-programming-language.org/function.html, but with changed return types:

class Foo {
  int[] test() { return null; }
}

class Bar : Foo {
  int[2] test() { return [1, 2]; } // overrides and is covariant with Foo.test()
}

// and here's additional test code:

void main() {
  Foo f = new Bar();
  auto arr = f.test();
  assert(arr.length == 2);
}

This code compiles but fails on assert. Returned arr.length is 1 and accessing the first element of that array results in access violation.

Given that static arrays are implicitly convertible to dynamic arrays, it should work. If not, it at least should not compile.
Comment 1 SomeDude 2012-04-20 17:00:57 UTC
This no longer compiles on 2.059 Win32:

PS E:\DigitalMars\dmd2\samples> rdmd bug.d
bug.d(6): Error: function bug.Bar.test of type int[2u]() overrides but is not covariant with bug.Foo.test of type int[]()
Failed: "dmd" "-v" "-o-" "bug.d" "-I."
PS E:\DigitalMars\dmd2\samples>
Comment 2 SomeDude 2012-05-01 15:28:26 UTC
Should be closed as FIXED.
Comment 3 SomeDude 2012-05-04 16:06:07 UTC
Code returns a sound compilation error.