Bug 12495 – CTFE slice cast can cause allocation

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-03-30T12:18:00Z
Last change time
2015-01-24T07:54:50Z
Keywords
CTFE, pull
Assigned to
nobody
Creator
monarchdodra

Comments

Comment #0 by monarchdodra — 2014-03-30T12:18:45Z
Casting a slice from "string" to "immutable(ubyte)[]" can cause allocation in CTFE (apparently). To trigger this, we must start from an "empty slice of immutable pointing to mutable data". /---- import std.string; void main() { string getStr() { char[1] dummyBuf = void; //dummy starting point. string slice = cast(string)dummyBuf[0 .. 0]; //empty slice, .ptr points mutable. slice ~= 'a'; //This should allocate. May or may not point immutable memory? return slice.idup; //This should allocate again, and definitly point immutable memory; } enum k = indexOf(getStr()); } auto indexOf(string s) { auto p1 = s.ptr; //auto p2 = (cast(immutable(ubyte)[])s).ptr; auto p2 = s.representation.ptr; //raw cast also reproduces //assert(cast(void*)p1 == cast(void*)p2); //Fails return cast(void*)p2 - cast(void*)p1; //cannot subtract pointers to two different memory blocks } /----
Comment #1 by k.hara.pg — 2015-01-21T12:58:09Z
Comment #2 by github-bugzilla — 2015-01-24T07:54:49Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7b825160b4d504b22668e2664229ae1a657282b5 fix Issue 12495 - CTFE slice cast can cause allocation https://github.com/D-Programming-Language/dmd/commit/74430a4869b97052c8dbfd86ec74e2edc45eb7a0 Merge pull request #4324 from 9rnsr/fix12495 Issue 12495 - CTFE slice cast can cause allocation