Thread overview
[Issue 12420] New: [AA] Can't set associative array with array as key value using key type
Mar 20, 2014
Denis Shelomovskij
Mar 30, 2014
Kenji Hara
Mar 30, 2014
Kenji Hara
Mar 30, 2014
Denis Shelomovskij
March 20, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12420

           Summary: [AA] Can't set associative array with array as key
                    value using key type
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: verylonglogin.reg@gmail.com


--- Comment #0 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2014-03-20 14:02:44 MSK ---
This code should compile:
---
void main()
{
    int[char[]] aa;
    aa[new char[1]] = 5; // line 4
}
---
main.d(4): Error: associative arrays can only be assigned values with immutable
keys, not char[]
---

Also the error message is wrong. The compiler expects an array of not-mutable
elements (yes, `cast(const)` will work). This incorrect compiler hack [1] was
introduced trying to fix Issue 2954 (see Comment 8 of that issue). I propose to
remove the hack.

And yes, it's a regression from 2010. )


[1] http://www.dsource.org/projects/dmd/changeset/749

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12420



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2014-03-29 20:21:09 PDT ---
(In reply to comment #0)
> This code should compile:
> ---
> void main()
> {
>     int[char[]] aa;
>     aa[new char[1]] = 5; // line 4
> }
> ---
> main.d(4): Error: associative arrays can only be assigned values with immutable
> keys, not char[]
> ---

I agree with the conclusion, the reason in my thought is:

`new char[1]` makes unique data and have no foreign reference, so it can be implicitly convertible to immutable(char[]). Therefore directly using it in the index part of the AA element setting will be allowed.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12420


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2014-03-29 20:51:15 PDT ---
https://github.com/D-Programming-Language/dmd/pull/3408

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 30, 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12420



--- Comment #3 from Denis Shelomovskij <verylonglogin.reg@gmail.com> 2014-03-30 12:27:43 MSK ---
(In reply to comment #1)
> I agree with the conclusion, the reason in my thought is:
> 
> `new char[1]` makes unique data and have no foreign reference, so it can be implicitly convertible to immutable(char[]). Therefore directly using it in the index part of the AA element setting will be allowed.

Looks like you misunderstood me. The word `immutable` shouldn't be here, it's just an incorrect error message. Currently we have no rules disallowing non-`immutable` associative array keys. I filed Issue 12491 for this.

This issue will be auto-fixed if Issue 2954 will be fixed by applying a correct check to ensure key expression can be implicitly converted to AA key type in case of setting AA value.

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