Bug 24323 – x[0 .. 2] cast to Ty[2], x[$-2 .. $] does not?
Status
RESOLVED
Resolution
DUPLICATE
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-01-08T03:41:54Z
Last change time
2024-01-08T11:14:16Z
Assigned to
No Owner
Creator
Manu
Comments
Comment #0 by turkeyman — 2024-01-08T03:41:54Z
void fn(ref const ubyte[2]);
void test(ubyte[] data)
{
fn(data[0 .. 2]); // <- call successfully
fn(data[$-2 .. $]); // <- fails: not callable using argument types `ubyte[]`
// this also fails:
size_t i = data.length - 2;
fn(data[i .. i+2]);
// this works:
fn(data[$-2 .. $][0 .. 2]);
}
I feel like the language should be able to determine that `$ - ($-2) == 2`. The workaround is pretty dumb.
I can't possibly be the first one to report this, what's the reason? Just hard to implement the bit of algebra required?
As long as there is just one variable term in a slice expression, and the rest of the terms are all known at compile time, this equation can be easily solved.
I sure hope that $ symbol is only ever evaluated once inside a slice expression, even in the presence of a custom opDollar?
Comment #1 by turkeyman — 2024-01-08T04:01:16Z
Actually, the number of variable terms is irrelevant; the equation just needs to be simplified and the variable terms will naturally be cancelled out.
Comment #2 by dkorpel — 2024-01-08T10:38:52Z
> I can't possibly be the first one to report this, what's the reason? Just hard to implement the bit of algebra required?
You're right: https://github.com/dlang/dmd/pull/4209
The difficulty is determining how this would fit with the rest of constant folding.
*** This issue has been marked as a duplicate of issue 13700 ***
Comment #3 by turkeyman — 2024-01-08T11:14:16Z
Difficult or otherwise, it's a pretty lame hole, and it's been that way for decades. It should be done.