D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6797 - Fake changes to enum array
Summary: Fake changes to enum array
Status: RESOLVED INVALID
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P3 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
 
Reported: 2011-10-08 19:17 UTC by bearophile_hugs
Modified: 2023-02-11 07:52 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 bearophile_hugs 2011-10-08 19:17:03 UTC
Maybe this was reported already, because it looks like a basic bug.

This compiles and runs with no errors (DMD 2.056head):


void main() {
    enum int[][] array = [[0, 1], [2, 3]];
    foreach (r; array)
        r[0] *= 10;
    foreach (ref r; array)
        r[0] *= 10;
    assert(array == [[0, 1], [2, 3]]);
}


This is a big hole. I think the implementation of compile-time constants need a lot of debugging.
Comment 1 SomeDude 2012-05-01 15:08:48 UTC
This simpler case actually fails:

void main() {
    enum int[] array = [2, 1];
    foreach (r; array){
        r *= 10;          // should be rejected
        writeln(array);
    }
    foreach (ref r; array){
        r *= 10;          // should be rejected
        writeln(array);
    }
    assert(array == [20, 10]);
}

Changing enum to immutable gives the following compilation errors:
bug.d(8): Error: variable bug.main.r cannot modify immutable
bug.d(12): Error: variable bug.main.r cannot modify const

But this 

void main() {
    enum int[] array = [2, 1];
    array[0] = 5;
}

is correctly caught by the compiler:
bug.d(7): Error: constant [2,1][0] is not an lvalue
Comment 2 Andrej Mitrovic 2012-12-02 10:33:38 UTC
There is no "hole" here, what you get here are separate arrays which are allocated for you wherever you use them. For example:

import std.stdio;

void main()
{
    enum int[] array = [1, 2];

    foreach (i, _; array)
        writeln(&array[i]);

    foreach (i, ref _; array)
        writeln(&array[i]);
}

Prints:

972FE0
972FD4

972FB0
972FA4

Those are two separate arrays.