www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6238] New: Cannot define global immutable AA

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6238

           Summary: Cannot define global immutable AA
           Product: D
           Version: D2
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: johann.macdonagh gmail.com



21:54:32 PDT ---
I *believe* this bug has been discussed and reported before, but a search
didn't turn up anything. This code fails to compile:

immutable test = // auto, const, etc.. also fail
[
    'a' : 1,
    'b' : 2,
];

void main()
{
}

On 2.053 we get:
main.d(2): Error: non-constant expression ['a':1,'b':2]

A workaround is to do:

enum test =
[
    'a' : 1,
    'b' : 2,
];

void main()
{
    auto localTest = test;

    // Use either test or localTest
}

But of course that will cause test to be copied each time it is referenced
(which, besides all the drama in issue 4397 I believe to be correct).

The reason this matters is because of CTFE. If I have an AA which I want to be
able to use in CTFE and at runtime without causing copies to be made at runtime
I'd have to use an immutable AA.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 01 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6238


hsteoh quickfur.ath.cx changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hsteoh quickfur.ath.cx
           Severity|normal                      |major



It gets worse. The following doesn't compile either:

// (package scope)
auto hash = [ "abc":1, "def":2, "ghi":3 ];

Neither does this compile:

int[string] hash = [ "abc":1, "def":2, "ghi":3 ];

This happens on both dmd 2.057 (Linux) and gdc 4.6.2 (Linux).

It seems that initializing associative arrays with literals only works in
function scope. IMHO this is a major bug. It completely breaks CTFE for
associative arrays (trying to assign to an assoc array in a CTFE function
triggers the same error), and greatly limits the usefulness of having a literal
in the first place.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6238


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |yebblies gmail.com



 It completely breaks CTFE for
 associative arrays (trying to assign to an assoc array in a CTFE function
 triggers the same error)
Can you please give an example of this? As far as I know AAs are usable in ctfe. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Feb 03 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6238




int[string] initHash(in string[] words) {
        int[string] h;

        for (auto i=0; i < words.length; i++) {
                h[words[i]] = i; // Compiler points to this line and says:
Error: non-constant expression ["abc":0,"def":1,"ghi":2]
        }

        return h;
}

int[string] hash3 = initHash(["abc", "def", "ghi"]);


Or is this a case of the compiler trying to be helpful and giving a misleading
error message? (I.e. the error is supposed to be on the line that initializes
hash3, but the compiler is pointing to the cause of the supposed non-constancy
of the value.)

I tried changing initHash() to construct a hash literal as a string and using
mixin() to initialize hash3, but that didn't work either.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6238


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|Other                       |All
         OS/Version|Windows                     |All



Yes, it's a line number bug.  The AA works perfectly well in ctfe, but once
evaluated it becomes:

int[string] hash3 = ["abc":0,"def":1,"ghi":2];

The wrong line is because that was the line it was last modified at, and the
interpreter forgets to correct it.

The error message is because of the same bug, and occurs after ctfe has
finished.
Doing everything except trying to initialize a global should be working in
ctfe.

I've opened a new bug report for the line number bug: issue 7434

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Feb 03 2012