D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2276 - Error message missing line number on array operation
Summary: Error message missing line number on array operation
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Windows
: P2 normal
Assignee: Don
URL:
Keywords: diagnostic, patch
Depends on:
Blocks:
 
Reported: 2008-08-09 12:17 UTC by Simen Kjaeraas
Modified: 2014-03-01 00:36 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Simen Kjaeraas 2008-08-09 12:17:56 UTC
This program:

void main()
{
	int[] a = [0];
	int[] b = [1, 2];
	int[] c;
	c[] = a + b;
}

Gives the error message "Error: cannot implicitly convert expression (c0 + c1) of type int[] to int". While I agree the program is flawed, so is the error message.
Comment 1 Don 2009-06-08 19:17:38 UTC
This is much the same as bug 2277.
Comment 2 Don 2010-05-18 00:12:06 UTC
Arrays ops still need more work, but this patch at least gives reasonable error messages for everything it cannot handle, rather than generating errors on compiler-generated code. This also fixes bug 3548, which is largely the same as this one. 

PATCH:
arrayop.c.
----------------
Expression *BinExp::arrayOp(Scope *sc)
{
    //printf("BinExp::arrayOp() %s\n", toChars());

    if (type->toBasetype()->nextOf()->toBasetype()->ty == Tvoid)
    {
        error("Cannot perform array operations on void[] arrays");
        return new ErrorExp();
    }
+    if (!isArrayOpValid(e2))
+    {
+        e2->error("invalid array operation %s (did you forget a [] ?)", toChars());
+        return new ErrorExp();
+    }
----------------
and add the following function to the top of that file.
----------------
// Check that there are no uses of arrays without [].
bool isArrayOpValid(Expression *e)
{
    if (e->op == TOKslice)
        return true;
    Type *tb = e->type->toBasetype();
    
    if ( (tb->ty == Tarray) || (tb->ty == Tsarray) )
    {
        switch (e->op)
        {
            case TOKadd:
            case TOKmin:
            case TOKmul:
            case TOKdiv:
            case TOKmod:
            case TOKxor:
            case TOKand:
            case TOKor:
            case TOKpow:
                 return isArrayOpValid(((BinExp *)e)->e1) && isArrayOpValid(((BinExp *)e)->e2);
            case TOKcall:
                 return false; // TODO: Decide if [] is required after arrayop calls.
            case TOKneg:
            case TOKtilde:
                 return isArrayOpValid(((UnaExp *)e)->e1);
            default:
                return false;
        }
    }
    return true;
}
Comment 3 Walter Bright 2010-05-31 19:02:33 UTC
http://www.dsource.org/projects/dmd/changeset/509