Error: non-constant expression...

bearophile bearophileHUGS at lycos.com
Fri Nov 26 13:32:16 PST 2010


> In my mind there is some confusion about all this. Other people may give you better answers.

A solution in such situations is to take a look at the asm, that has confirmed my ideas:


string first_function(string k) {
    immutable string[string] map1 = ["bar" : "spam"];
    return map1[k];
}
string second_function(string k) {
    const string[string] map2 = ["bar" : "spam"];
    return map2[k];
}
string third_function(string k) {
    enum string[string] map3 = ["bar" : "spam"];
    return map3[k];
}
string fourth_function(string k) {
    static enum string[string] map4 = ["bar" : "spam"];
    return map4[k];
}
immutable string[string] map5;
static this() {
    map5 = ["bar" : "spam"];
}
string fifth_function(string k) {
    return map5[k];
}
void main() {}


Compiled with:
DMD 2.050, -O -release -inline

_D5test314first_functionFAyaZAya    comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
        mov ECX,offset FLAT:_D16TypeInfo_HAyayAa6__initZ
        push    dword ptr 0Ch[ESP]
        push    dword ptr 0Ch[ESP]
        push    8
        push    EAX
        push    dword ptr FLAT:_DATA[01Ch]
        push    dword ptr FLAT:_DATA[018h]
        push    dword ptr FLAT:_DATA[0Ch]
        push    dword ptr FLAT:_DATA[08h]
        push    1
        push    ECX
        call    near ptr __d_assocarrayliteralT
        add ESP,018h
        push    EAX
        call    near ptr __aaGetRvalue
        mov EDX,4[EAX]
        mov EAX,[EAX]
        add ESP,014h
        pop ECX
        ret 8

_D5test315second_functionFAyaZAya   comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
        mov ECX,offset FLAT:_D17TypeInfo_HAyaxAya6__initZ
        push    dword ptr 0Ch[ESP]
        push    dword ptr 0Ch[ESP]
        push    8
        push    EAX
        push    dword ptr FLAT:_DATA[01Ch]
        push    dword ptr FLAT:_DATA[018h]
        push    dword ptr FLAT:_DATA[0Ch]
        push    dword ptr FLAT:_DATA[08h]
        push    1
        push    ECX
        call    near ptr __d_assocarrayliteralT
        add ESP,018h
        push    EAX
        call    near ptr __aaGetRvalue
        mov EDX,4[EAX]
        mov EAX,[EAX]
        add ESP,014h
        pop ECX
        ret 8

_D5test314third_functionFAyaZAya    comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
        mov ECX,offset FLAT:_D16TypeInfo_HAyaAya6__initZ
        push    dword ptr 0Ch[ESP]
        push    dword ptr 0Ch[ESP]
        push    8
        push    EAX
        push    dword ptr FLAT:_DATA[01Ch]
        push    dword ptr FLAT:_DATA[018h]
        push    dword ptr FLAT:_DATA[0Ch]
        push    dword ptr FLAT:_DATA[08h]
        push    1
        push    ECX
        call    near ptr __d_assocarrayliteralT
        add ESP,018h
        push    EAX
        call    near ptr __aaGetRvalue
        mov EDX,4[EAX]
        mov EAX,[EAX]
        add ESP,014h
        pop ECX
        ret 8

_D5test315fourth_functionFAyaZAya   comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
        mov ECX,offset FLAT:_D16TypeInfo_HAyaAya6__initZ
        push    dword ptr 0Ch[ESP]
        push    dword ptr 0Ch[ESP]
        push    8
        push    EAX
        push    dword ptr FLAT:_DATA[01Ch]
        push    dword ptr FLAT:_DATA[018h]
        push    dword ptr FLAT:_DATA[0Ch]
        push    dword ptr FLAT:_DATA[08h]
        push    1
        push    ECX
        call    near ptr __d_assocarrayliteralT
        add ESP,018h
        push    EAX
        call    near ptr __aaGetRvalue
        mov EDX,4[EAX]
        mov EAX,[EAX]
        add ESP,014h
        pop ECX
        ret 8

_D5test314fifth_functionFAyaZAya    comdat
L0:     push    EAX
        mov EAX,offset FLAT:_D11TypeInfo_Aa6__initZ
        push    dword ptr 0Ch[ESP]
        push    dword ptr 0Ch[ESP]
        push    8
        push    EAX
        push    dword ptr _D5test34map5yHAyaAa
        call    near ptr __aaGetRvalue
        mov EDX,4[EAX]
        mov EAX,[EAX]
        add ESP,014h
        pop ECX
        ret 8


I may add something about this in bugzilla (the problem with enum AAs is already present there).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list