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 #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.