Bug 8830 – [CTFE] Incorrect slicing with pointer from sliced array
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-10-16T06:56:00Z
Last change time
2015-06-09T05:11:45Z
Assigned to
nobody
Creator
monarchdodra
Comments
Comment #0 by monarchdodra — 2012-10-16T06:56:32Z
Minimal test case:
//----
import std.stdio;
string[] foo(string s)
{
auto ss = s[1..$];
auto l = ss.length;
string s2 = ss.ptr[0..2];
return [ss, s2];
}
void main()
{
enum bar = foo("hello");
writeln(bar);
}
//----
Creates:
//----
[
"ello", //Sliced 1..$ of "hello"
"hel" // *should* be "ello"[0..2], but is actually "hello"[0..2]
]
//----
It would appear that when slicing a pointer extracted from a previously sliced, array, it will slice from the first index of that *original* array.
Comment #1 by monarchdodra — 2012-10-16T06:57:02Z
(In reply to comment #0)
> Minimal test case:
>
> //----
> import std.stdio;
>
> string[] foo(string s)
> {
> auto ss = s[1..$];
> auto l = ss.length;
> string s2 = ss.ptr[0..2];
> return [ss, s2];
>
> }
>
> void main()
> {
> enum bar = foo("hello");
> writeln(bar);
> }
> //----
>
> Creates:
>
> //----
> [
> "ello", //Sliced 1..$ of "hello"
> "hel" // *should* be "ello"[0..2], but is actually "hello"[0..2]
> ]
> //----
>
> It would appear that when slicing a pointer extracted from a previously sliced,
> array, it will slice from the first index of that *original* array.
What is strange though, is that ss.ptr *does* point to the right element:
//----
string[] foo(string s)
{
auto ss = s[1..$];
auto l = ss.length;
string s2 = [*ss.ptr];
return [ss, s2];
}
void main()
{
enum bar = foo("hello");
writeln(bar);
}
//----
[
"ello",
"e" //extracted pointer points to the right element.
]
//----
Comment #2 by github-bugzilla — 2012-10-20T17:18:49Z