Thread overview
[Issue 7664] New: Problem with fixed-sized associative array key assignment
Mar 24, 2012
Kenji Hara
Mar 24, 2012
Kenji Hara
March 07, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7664

           Summary: Problem with fixed-sized associative array key
                    assignment
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@eml.cc 2012-03-07 11:06:18 PST ---
I think this code has to be valid:



void main() {
    auto s = "hello";
    uint[immutable char[4]] aa;
    aa[s[0 .. 4]] = 0;
}


But DMD 2.059head gives:

test.d(4): Error: cannot implicitly convert expression (s[0u..4u]) of type
string to immutable(char[4u])

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7664



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2012-03-24 09:52:23 PDT ---
Compiler can calculate the length for s[0 .. 4] in compile time, because both lower bound and upper bound are constant expressions. So converting it to static array type is possible.

But, currently, the slice of an array expression is *always* typed as dynamic
array type.
Therefore it is a constant-folding enhancement, not a bug.

Bug 7665 will also settle down to the same mechanism.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7664


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement


--- Comment #2 from bearophile_hugs@eml.cc 2012-03-24 10:14:32 PDT ---
(In reply to comment #1)
> Compiler can calculate the length for s[0 .. 4] in compile time, because both lower bound and upper bound are constant expressions. So converting it to static array type is possible.
> 
> But, currently, the slice of an array expression is *always* typed as dynamic
> array type.
> Therefore it is a constant-folding enhancement, not a bug.

Thank you for your answers. I convert this to enhancement request then.

I think such constant-folding is useful to remove some run-time tests and speed up other code that uses slices.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 24, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=7664



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2012-03-24 10:26:02 PDT ---
(In reply to comment #2)
> Thank you for your answers. I convert this to enhancement request then.
> 
> I think such constant-folding is useful to remove some run-time tests and speed up other code that uses slices.

In really special case, D allows such conversion from dynamic array type to static array type. See following:

void foo(immutable(char)[4] s){}
void main()
{
    static assert(is(typeof("test") == immutable(char)[]));
    foo("test");            // immutable(char)[] to immutable(char)[4]
    foo(['t','e','s','t']); // immutable(char)[] to immutable(char)[4]
}

So there seems to be enough reasoning for this enhancement.

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