D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6611 - better error message for array post increment/decrement
Summary: better error message for array post increment/decrement
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 enhancement
Assignee: yebblies
URL:
Keywords: diagnostic, pull
Depends on:
Blocks:
 
Reported: 2011-09-06 03:17 UTC by bearophile_hugs
Modified: 2012-02-18 23:10 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 bearophile_hugs 2011-09-06 03:17:47 UTC
This code contains two operations that are refused by DMD2.055beta:

void main() {
    int[10] a;
    a[] += 1; // OK
    ++a[];    // OK
    --a[];    // OK
    a[]++;    // line 6, error
    a[]--;    // line 7, error
}


test2.d(6): Error: slice expression a[] is not a modifiable lvalue
test2.d(6): Error: 'a[]' is not a scalar, it is a int[]
test2.d(6): Error: cannot cast int to int[]
test2.d(7): Error: slice expression a[] is not a modifiable lvalue
test2.d(7): Error: 'a[]' is not a scalar, it is a int[]
test2.d(7): Error: cannot cast int to int[]


Maybe it's better to allow the last two lines too.
Comment 1 yebblies 2011-09-06 06:34:55 UTC
This line implies the following
a[]++ => (auto tmp = a[].dup, ++a[], tmp)

Do we really want it to be this easy to do?
Comment 2 bearophile_hugs 2011-09-06 10:22:34 UTC
(In reply to comment #1)
> This line implies the following
> a[]++ => (auto tmp = a[].dup, ++a[], tmp)
> 
> Do we really want it to be this easy to do?

I understand. If doing this is not good, then I suggest to turn this into a diagnostic enhancement request. So given code like this:

void main() {
    int[10] a;
    a[]++;
}


to raise a single error message that explains why that vector op is on purpose not supported.
Comment 3 yebblies 2011-09-06 10:41:35 UTC
(In reply to comment #2)
> I understand. If doing this is not good, then I suggest to turn this into a
> diagnostic enhancement request. So given code like this:
> 

It's probably not worth introducing yet another hidden allocation, or a special case as it can be easily written using pre-inc.

> void main() {
>     int[10] a;
>     a[]++;
> }
> 
> 
> to raise a single error message that explains why that vector op is on purpose
> not supported.

That sounds reasonable, the error message is terrible.
Comment 5 github-bugzilla 2012-02-18 23:07:27 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/176b8752c6f9d92e2570bcb47b87419bf2f0ccd2
Issue 6611 - better error message for array post increment/decrement

Detect attempts to use post increment/decrement on an array slice, and print a better error message.

https://github.com/D-Programming-Language/dmd/commit/d082916311aca3d14e13295eaafa9b6e52cd81b3
Merge pull request #717 from yebblies/issue6611

Issue 6611 - better error message for array post increment/decrement