www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12315] New: std.array.array at compile-time too

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315

           Summary: std.array.array at compile-time too
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: rejects-valid
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



import std.array: array;
struct Foo1 {
    int i;
}
struct Foo2 {
    immutable int i;
}
void main() {
    enum r1 = [Foo1(1)].array; // OK
    enum r2 = [Foo2(1)].array; // Error
}


dmd 2.066alpha gives:

...\dmd2\src\phobos\std\conv.d(3908,23): Error: memcpy cannot be interpreted at
compile time, because it has no available source code
...\dmd2\src\phobos\std\array.d(45,23):        called from here:
emplaceRef(result[i], e)
temp.d(10,24):        called from here: array([Foo2(1)])

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 08 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315


Peter Alexander <peter.alexander.au gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |CTFE
                 CC|                            |peter.alexander.au gmail.co
                   |                            |m
          Component|Phobos                      |DMD



05:00:05 PDT ---
Changing this to dmd CTFE bug. I can't think of any way to solve it in Phobos
without more compiler support.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315





 Changing this to dmd CTFE bug. I can't think of any way to solve it in Phobos
 without more compiler support.
This seems to work: import std.traits: ForeachType; ForeachType!R[] myArray(R)(R r) { if (__ctfe) { typeof(return) result; foreach (item; r) result ~= r; return result; } else { // ... assert(0); } } struct Foo1 { int i; } struct Foo2 { immutable int i; } void main() { enum r1 = [Foo1(1)].myArray; // OK enum r2 = [Foo2(1)].myArray; // OK } -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315


Peter Alexander <peter.alexander.au gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|DMD                         |Phobos



06:46:00 PDT ---


 Changing this to dmd CTFE bug. I can't think of any way to solve it in Phobos
 without more compiler support.
This seems to work:
... why didn't I think of that :-) I'll make the pull. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315






 I'll make the pull.
There are also assumeSafeAppend, reserve/capacity, and so on. I don't know if they speedup the code at compile-time too. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315




07:01:52 PDT ---
https://github.com/D-Programming-Language/phobos/pull/1986

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315






 There are also assumeSafeAppend, reserve/capacity, and so on. I don't know if
 they speedup the code at compile-time too.
It seems code like this doesn't yet work: import std.traits: ForeachType; import std.range: hasLength; ForeachType!R[] myArray(R)(R r) { if (__ctfe) { typeof(return) result; static if (hasLength!R) result.reserve = r.length; foreach (item; r) result ~= r; return result; } else { // ... assert(0); } } struct Foo1 { int i; } struct Foo2 { immutable int i; } void main() { enum r1 = [Foo1(1)].myArray; // OK enum r2 = [Foo2(1)].myArray; // OK } -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315




Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8d9479c3a4b1f3cb7738b27d90d92330c8d1f058
Fix Issue 12315 - `array` at compile-time with all types

Problem was that `emplaceRef` calls `memcpy` for some types, which is unsafe
and unavailable at compile time.

https://d.puremagic.com/issues/show_bug.cgi?id=12315

https://github.com/D-Programming-Language/phobos/commit/6b31fb8685b458f142db34ca21e5c9639e5cb38d


Fix Issue 12315 - `array` at compile-time with all types

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 09 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315


Peter Alexander <peter.alexander.au gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 09 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12315


monarchdodra gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |monarchdodra gmail.com
         Resolution|FIXED                       |



Reopening because I'm not sure it's worth filing something new, but `Appender`
will also CTFE fail on the same types.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 10 2014