Bug 13610 – bringToFront does not work with CTFE

Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2014-10-13T11:17:00Z
Last change time
2014-12-07T03:28:44Z
Assigned to
nobody
Creator
jens.k.mueller

Comments

Comment #0 by jens.k.mueller — 2014-10-13T11:17:50Z
bringToFront does not work with CTFE. I don't know whether it is supposed to but it certainly is surprising. import std.algorithm : bringToFront; // at running time { auto array = [1, 2, 3, 4]; bringToFront(array[0 .. $-1], array[$-1 .. $]); assert(array == [4, 1, 2, 3]); } // at compile time { enum array = [1, 2, 3, 4]; bringToFront(array[0 .. $-1], array[$-1 .. $]); static assert(array == [4, 1, 2, 3]); // fails } says Error: static assert ([1, 2, 3, 4] == [4, 1, 2, 3]) is false I used dmd v2.066.0 on Linux.
Comment #1 by clugdbug — 2014-10-14T10:50:06Z
(In reply to jens.k.mueller from comment #0) > bringToFront does not work with CTFE. I don't know whether it is supposed to > but > it certainly is surprising. > > import std.algorithm : bringToFront; > // at running time > { > auto array = [1, 2, 3, 4]; > bringToFront(array[0 .. $-1], array[$-1 .. $]); > assert(array == [4, 1, 2, 3]); > } > // at compile time > { > enum array = [1, 2, 3, 4]; > bringToFront(array[0 .. $-1], array[$-1 .. $]); > static assert(array == [4, 1, 2, 3]); // fails Of course it fails. array is an enum, it can never change. That's the point of enum! The confusing thing is that bringToFront compiles when passed a manifest constant.
Comment #2 by jens.k.mueller — 2014-10-14T13:15:32Z
That makes perfect sense. So it should not compile then.
Comment #3 by yebblies — 2014-12-07T03:28:44Z
(In reply to Don from comment #1) > > Of course it fails. array is an enum, it can never change. That's the point > of enum! > > The confusing thing is that bringToFront compiles when passed a manifest > constant. The 'bug' is that you can have mutable array manifest constant. The code expands to: bringToFront([1, 2, 3, 4][0 .. $-1], [1, 2, 3, 4][$-1 .. $]); which is valid, because an array literal is a valid range. The mutable manifest constant and implicit dup, are unfortunately working as designed. See issue 9953.