D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5163 - meaningless error message with front() applied to void[]
Summary: meaningless error message with front() applied to void[]
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2010-11-04 08:08 UTC by Trass3r
Modified: 2010-11-08 02:26 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 Trass3r 2010-11-04 08:08:33 UTC
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 Lars T. Kyllingstad 2010-11-05 04:38:32 UTC
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 Lars T. Kyllingstad 2010-11-05 05:18:35 UTC
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 bearophile_hugs 2010-11-05 05:36:36 UTC
(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 Trass3r 2010-11-05 09:37:28 UTC
Thx for the fix, but I still think that strange [i] in the error message is a diagnostic bug, reopen it?
Comment 5 Lars T. Kyllingstad 2010-11-08 02:26:24 UTC
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.