Bug 8589 – Incorrect conversion of function returning `typeof(null)` to function returning an array

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-08-26T02:56:00Z
Last change time
2013-03-10T21:02:31Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
verylonglogin.reg

Comments

Comment #0 by verylonglogin.reg — 2012-08-26T02:56:44Z
--- void f(int[] function() del) { assert(!del()); } typeof(null) g() { return null; } void main() { f(&g); f(() => null); } --- As a result `f(() => null)` will trigger this issue too. This makes lambda expressions returning null very dangerous.
Comment #1 by bearophile_hugs — 2012-08-26T04:38:12Z
This compiles: f(() => null); This causes no assert to fire: foo(() => (int[]).init); While this doesn't even compile: foo(() => []); Error: function test.foo (int[] function() del) is not callable using argument types (void[] function() pure nothrow @safe) See also Issue 7007 Since lot of time in D dynamic arrays are not pointers, so generally accepting "null" as empty array literal is a wrong design decision, especially since the "[]" literal is available.
Comment #2 by k.hara.pg — 2012-09-16T04:06:36Z
https://github.com/D-Programming-Language/dmd/pull/1119 Runtime representation of typeof(null) is same as void*, then delegate and dynamic array type should not be covariant with typeof(null). It's limitation.
Comment #3 by github-bugzilla — 2013-03-06T14:28:14Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/043d6926ef4f3d0e8f25c1f0d69891bf7f39bdd1 fix Issue 8589 - Incorrect conversion of function returning `typeof(null)` to function returning an array https://github.com/D-Programming-Language/dmd/commit/f2b02836d7680aa3d1c92f2541c98638dcb0f0f9 Merge pull request #1119 from 9rnsr/fix8589 Issue 8589 - Incorrect conversion of function returning `typeof(null)` to function returning an array
Comment #4 by andrej.mitrovich — 2013-03-10T16:36:13Z
Kenji, is this a parser bug? f(() => int[].init); test.d(11): Error: found '[' when expecting '.' following int test.d(11): Error: found ']' when expecting identifier following 'int.' You have to use: f(() => (int[]).init); But that's not very convenient. Also this won't work because '[]' will be typed as 'void[]': f(() => []); But I guess we'll have to live with that for now..
Comment #5 by k.hara.pg — 2013-03-10T20:55:04Z
(In reply to comment #4) > Kenji, is this a parser bug? > > f(() => int[].init); > > test.d(11): Error: found '[' when expecting '.' following int > test.d(11): Error: found ']' when expecting identifier following 'int.' > > You have to use: > > f(() => (int[]).init); > > But that's not very convenient. No, it is not allowed in current grammar. So it is not a bug, but a limitation. > Also this won't work because '[]' will be typed as 'void[]': That would be a type inference bug.
Comment #6 by k.hara.pg — 2013-03-10T21:02:31Z
(In reply to comment #5) > > Also this won't work because '[]' will be typed as 'void[]': > > That would be a type inference bug. Oh.. sorry, it is a today's limitation, not a bug.