Bug 5163 – meaningless error message with front() applied to void[]
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-11-04T08:08:00Z
Last change time
2010-11-08T02:26:24Z
Keywords
diagnostic
Assigned to
nobody
Creator
hoganmeier
Comments
Comment #0 by hoganmeier — 2010-11-04T08:08:33Z
import std.array;
void main()
{
void[] a = cast(void[]) [ 1, 2, 3 ];
a.front;
}
yields:
..\src\phobos\std\array.d(419): Error: [i] has no effect in expression (a[0u])
..\src\phobos\std\array.d(5): Error: template instance std.array.front!(void[]) error instantiating
Originally I used std.file.read(), hence the void array.
- 1st message is completely senseless, what "[i]"?
- 2nd message shows line 5 which is the line of "a.front;" but the filename is wrong.
Comment #1 by bugzilla — 2010-11-05T04:38:32Z
This is actually two separate issues.
The first is a Phobos issue, namely that there should be a template constraint on std.array.front() that prevents it from being instantiated on void[] arrays. front() is defined as something like
T front(T)(T[] array) { return array[0]; }
You cannot index into a void[] array, hence the first error message. This also means that void[] cannot be a range.
The second is the issue of the wrong line number, which is a bug in DMD. I'm guessing it's just bug 1913 again, so I'm marking this one as a Phobos issue.
In the case of a void[] returned from std.file.read(), you need to cast it to ubyte[] before using it as a range.
Comment #2 by bugzilla — 2010-11-05T05:18:35Z
This fixes the Phobos bug:
http://www.dsource.org/projects/phobos/changeset/2123
The error message is now:
test.d(6): Error: template std.array.front(A) if (is(typeof(A[0])) && !isNarrowString!(A) && !is(typeof(A[0]) : const(void))) does not match any function template declaration
test.d(6): Error: template std.array.front(A) if (is(typeof(A[0])) && !isNarrowString!(A) && !is(typeof(A[0]) : const(void))) cannot deduce template function from argument types !()(void[])
Arguably less readable, but more useful since it refers to the point of error in user code instead of in Phobos code.
Comment #3 by bearophile_hugs — 2010-11-05T05:36:36Z
(In reply to comment #2)
> Arguably less readable, but more useful since it refers to the point of error
> in user code instead of in Phobos code.
Too bad there is no handy way to add error messages to template constraints...
Comment #4 by hoganmeier — 2010-11-05T09:37:28Z
Thx for the fix, but I still think that strange [i] in the error message is a diagnostic bug, reopen it?
Comment #5 by bugzilla — 2010-11-08T02:26:24Z
I think '[i]' is just a compact way of saying 'indexing operation'. I guess it could just as well have been '[#]', '[index]', or whatever. (Even better would be a message saying that you can't index into void[] arrays.)
Anyway, I suggest you open a new bug report about it. It's better not to have two bugs in one report -- especially when one is a Phobos bug and the other is a DMD bug.