D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 3652 - Allow explicit and implicit casting of dynamic array slices of known size to static array
Summary: Allow explicit and implicit casting of dynamic array slices of known size to ...
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 enhancement
Assignee: No Owner
URL:
Keywords: pull
: 8843 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-12-26 14:17 UTC by Witold Baryluk
Modified: 2020-05-15 16:11 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Witold Baryluk 2009-12-26 14:17:17 UTC
Consider this:

void foo(float[4] x) {
   return x[0]+x[1]*x[2]-x[3];
}


void main() {
    float[] xs = read_floats_from_file();

    foreach (i; 0 .. xs.length-4) {
         foo(xs[ i .. i+4]);
    }
}

or simpler thing to calculate in compile time:

void main() {
    float[] xs = read_floats_from_file();
    foo(xs[ 0 .. 4]);
}

In both cases compiler will say:
   cannot implicitly convert expression (_adDupT((& _D11TypeInfo_Af6__initZ),qs[0u..4u])) of type float[] to float[4u]
or something similar. It is hard to see why this is really needed. 

How about allowing (and performing) implicit cast to static array if compiler knows (or can easly infere) size of the slice and it aggree with target type? Of course there will be situations where we have no knowledge about it:


void main() {
    float[] xs = read_floats_from_file();
    uint a = rand() % xs.length;
    foo(xs[ 0 .. a ]);
}

But allowing implicit cast also can detect other errors:
    foreach (i; 0 .. xs.length-4) {
         foo(xs[ i .. i+3]);
    }

Compiler in such situation will complain not because we are using float[] to float[4] conversion, but because we are trying to call float[4] function using float[3] slice.
Comment 1 Witold Baryluk 2009-12-26 14:18:34 UTC
So essentially problem is that slices are just dynamic arrays. So bringing back the topic of making slices different/distinctive types.
Comment 2 Witold Baryluk 2009-12-26 14:21:40 UTC
It is even worse that i was innitially thinking:

explicit casting doesn't work:
series2.d(113): Error: e2ir: cannot cast qs[0u..1u] of type float[] to type float[1u]
Comment 3 Kenji Hara 2013-03-01 02:30:36 UTC
Partial implementation:
https://github.com/D-Programming-Language/dmd/pull/1705

(In reply to comment #0)
>     float[] xs = read_floats_from_file();
>     foreach (i; 0 .. xs.length-4) {
>          foo(xs[i .. i+4]);
>     }

In this case, detecting length == 4 in compile time is difficult with complicated cases. Consider:

   foo(xs[(i*4+10)/2 .. (i*8>>1)/2+9]);
   // lwr = (i*4 + 10)/2 = i*4/2 + 10/2            = (i*2+5)
   // upr = (i*8>>1)/2 + 5 = (i*4/2) + 5 = i*2 + 9 = (i*2+5) + 4

So this is not supported in my pull request.
Comment 4 github-bugzilla 2013-03-02 11:12:56 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/82b79c071c9ca13785e16c9faea901fa2c45531e
partial fix Issue 3652 - Allow explicit and implicit casting of dynamic array slices of known size to static array

If and only if both side of slice boundaries can be constant-folded, the conversion will succeed.

Limitation:
- Does not run CTFE.
- There is no deformation of formula.

https://github.com/D-Programming-Language/dmd/commit/e74670edbf1e013da748342a83909e2bce65d993
Merge pull request #1705 from 9rnsr/ct_slice

partial fix Issue 3652 - Allow explicit and implicit casting of dynamic array slices of known size to static array
Comment 5 bearophile_hugs 2013-03-02 12:00:57 UTC
(In reply to comment #3)

> So this is not supported in my pull request.

Your pull request is not perfect, but it's a significant improvement.

Maybe related: Issue 8277 and Issue 9165
Comment 6 yebblies 2013-07-27 23:37:52 UTC
*** Issue 8843 has been marked as a duplicate of this issue. ***
Comment 7 Kenji Hara 2014-12-10 09:19:46 UTC
See also: issue 13700
Comment 8 github-bugzilla 2015-01-01 12:53:40 UTC
Commits pushed to master at https://github.com/D-Programming-Language/dlang.org

https://github.com/D-Programming-Language/dlang.org/commit/243a97c3ebde92cd3c3f99963e39591fc307fff3
Issue 3652 - Allow explicit and implicit casting of dynamic array slices of known size to static array

https://github.com/D-Programming-Language/dlang.org/commit/900cd04b373192feea7d682150eabe10e34b793f
Merge pull request #725 from 9rnsr/fix3652

Issue 3652 - Allow explicit and implicit casting of dynamic array slices of known size to static array
Comment 9 Witold Baryluk 2020-05-15 16:11:38 UTC
It does appear way better indeed.


https://godbolt.org/z/taxeAR

One way to emulate, the foo(xs[i .. i+4]), is by doing foo(xs[i .. i+4][0 .. 4]).

It is hard to support arbitrary expression. Not only it would be very hard to write specification to indicate what kind of expressions are supported by compiler.

in general function equality is equivalent to halting problem, so only some poorly defined subset (by algorithm) can be supported. compiler is not full symbolic algebra system, so it is probably not worth implementing or specifying.

Thanks for the patches, Kenji.