D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7664 - Problem with fixed-sized associative array key assignment
Summary: Problem with fixed-sized associative array key assignment
Status: RESOLVED WORKSFORME
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 enhancement
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2012-03-07 11:06 UTC by bearophile_hugs
Modified: 2020-03-21 03:56 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-03-07 11:06:18 UTC
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])
Comment 1 Kenji Hara 2012-03-24 09:52:23 UTC
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.
Comment 2 bearophile_hugs 2012-03-24 10:14:32 UTC
(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.
Comment 3 Kenji Hara 2012-03-24 10:26:02 UTC
(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.