Bug 8221 – typeof(null) rejected as return type for covariant overrides

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-06-11T08:20:00Z
Last change time
2012-06-22T22:17:08Z
Keywords
pull, rejects-valid
Assigned to
nobody
Creator
timon.gehr

Comments

Comment #0 by timon.gehr — 2012-06-11T08:20:54Z
DMD 2.059: class A{ A foo(){return this;} } class B: A{ override typeof(null) foo(){return null;} // error } The code should compile.
Comment #1 by k.hara.pg — 2012-06-11T20:16:59Z
Comment #2 by bugzilla — 2012-06-13T22:50:28Z
Why should this compile? What is the compelling reason?
Comment #3 by k.hara.pg — 2012-06-13T23:05:57Z
(In reply to comment #2) > Why should this compile? What is the compelling reason? typeof(null) is a subtype of array/class/pointer types. By return type covariance rule, B.foo should be able to override A.foo.
Comment #4 by bugzilla — 2012-06-14T00:39:47Z
(In reply to comment #3) > typeof(null) is a subtype of array/class/pointer types. By return type > covariance rule, B.foo should be able to override A.foo. Since A is not a base class of null, I do not think this is valid. foo could return B, and that will work, because A is a base class of B.
Comment #5 by k.hara.pg — 2012-06-14T01:13:26Z
(In reply to comment #4) > (In reply to comment #3) > > typeof(null) is a subtype of array/class/pointer types. By return type > > covariance rule, B.foo should be able to override A.foo. > > Since A is not a base class of null, I do not think this is valid. foo could > return B, and that will work, because A is a base class of B. No. Indeed A is not a base class of null _liteal_, but A is a base _type_ of typeof(null). Current dmd compiles following tests: static assert(is(typeof(null) : int[])); // int[] is a base type of typeof(null) static assert(is(typeof(null) : Object)); // Object is a base type of typeof(null) static assert(is(typeof(null) : int*)); // int* is a base type of typeof(null) I have implemented typeof(null) as like Scala's Nothing type, so this is expected result. In other words, typeof(null) is the most derived class type from all of class types. So dmd should compile the original code.
Comment #6 by github-bugzilla — 2012-06-22T21:31:29Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/bc55af9d3d38fd18f3a2c1f323cd1a4f49dbd673 fix Issue 8221 - typeof(null) rejected as return type for covariant overrides https://github.com/D-Programming-Language/dmd/commit/ad7637f3d3c2cdf98ff7de8d715315e8557ee223 Merge pull request #1001 from 9rnsr/fix8221 Issue 8221 - typeof(null) rejected as return type for covariant overrides