www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 7940] New: CTFE breaks

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

           Summary: CTFE breaks
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: iteronvexor gmail.com



DMD 2.059 64-bit Gnu/Linux

----------------8<----------------8<----------------
import std.typecons  : Tuple;
import std.array     : appender;
import std.algorithm : sort;
import std.stdio     : writeln;

alias double Real;

struct S {

  Tuple!(uint, Real)[] _data;
  alias _data this;
}

private auto gen(K, V)(V[K] data) {
  alias Tuple!(K, V) T;
  auto app = appender!(T[]);
  foreach(k, v; data)
    app.put(T(k, v));
  sort(app.data);
  return app.data;
}

auto s(Real[uint] data) {
  return S(gen!(uint, Real)(data));
}

static S s1 = s([1: 12.0, 5: 4.6, 3: 9.99]);

void main() {

  S s2 = s([1: 12.0, 5: 4.6, 3: 9.99]);

  // prints [Tuple!(uint,double)(0, nan), Tuple!(uint,double)(0, nan),
Tuple!(uint,double)(0, nan)]
  writeln(s1);

  // prints [Tuple!(uint,double)(1, 12), Tuple!(uint,double)(3, 9.99),
Tuple!(uint,double)(5, 4.6)]
  writeln(s2);
}
---------------->8---------------->8----------------

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 18 2012
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940


SomeDude <lovelydear mailmetrash.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lovelydear mailmetrash.com
           Platform|x86_64                      |All
         OS/Version|Linux                       |All



PDT ---
Same code as above, very slightly simplified.
-----------------
import std.typecons  : Tuple;
import std.array     : appender;
import std.stdio     : writeln;

struct S {
  Tuple!(uint, double)[] _data;
  alias _data this;
}

private auto gen(K, V)(V[K] data) {
  alias Tuple!(K, V) T;
  auto app = appender!(T[]);
  foreach(k, v; data) app.put(T(k, v));
  return app.data;
}

auto s(double[uint] data) {
  return S(gen!(uint, double)(data));
}

void main() {
  static S s1 = s([1: 12.0, 5: 4.6, 3: 9.99]);
  S s2        = s([1: 12.0, 5: 4.6, 3: 9.99]);
  writeln(s1);
  writeln(s2);
}
------------------
PS E:\DigitalMars\dmd2\samples> rdmd -g bug.d
[Tuple!(uint,double)(0, nan), Tuple!(uint,double)(0, nan),
Tuple!(uint,double)(0, nan)]
[Tuple!(uint,double)(1, 12), Tuple!(uint,double)(3, 9.99),
Tuple!(uint,double)(5, 4.6)]

If we remove the static, it works.

If we replace the appender by an array in:

import std.typecons  : Tuple;
import std.array     : appender;
import std.stdio     : writeln;

struct S {
  Tuple!(uint, double)[] _data;
  alias _data this;
}

private auto gen(K, V)(V[K] data) {
  alias Tuple!(K, V) T;
  T[] app;
  foreach(k, v; data) app ~= T(k, v);
  return app;
}

auto s(double[uint] data) {
  return S(gen!(uint, double)(data));
}

void main() {
  static S s1 = s([1: 12.0, 5: 4.6, 3: 9.99]);
  S s2        = s([1: 12.0, 5: 4.6, 3: 9.99]);
  writeln(s1);
  writeln(s2);
}
PS E:\DigitalMars\dmd2\samples> rdmd -g bug.d
[Tuple!(uint,double)(1, 12), Tuple!(uint,double)(5, 4.6),
Tuple!(uint,double)(3, 9.99)]
[Tuple!(uint,double)(1, 12), Tuple!(uint,double)(3, 9.99),
Tuple!(uint,double)(5, 4.6)]

If we remove the static:
PS E:\DigitalMars\dmd2\samples> rdmd -g bug.d
[Tuple!(uint,double)(1, 12), Tuple!(uint,double)(3, 9.99),
Tuple!(uint,double)(5, 4.6)]
[Tuple!(uint,double)(1, 12), Tuple!(uint,double)(3, 9.99),
Tuple!(uint,double)(5, 4.6)]

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 21 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |CTFE, wrong-code
                 CC|                            |clugdbug yahoo.com.au
            Summary|CTFE breaks                 |CTFE wrong-code for
                   |                            |opAssign and
                   |                            |std.array.Appender



Could be a CTFE compiler bug, but more likely to
be a bug in the if (__ctfe) code in std.array.Appender.
I don't trust array.Appender anyway. I see no reason for it not be  safe.
It shouldn't be using pointers at all.

Reduced test case:
-----------------
import std.array     : appender;

struct Bug7940 {
    int field0;

    void opAssign(Bug7940 rhs) {
        field0 = rhs.field0;
    }
}

Bug7940[] gen() {
  alias Bug7940 T;
  auto app = appender!(T[]);
  app.put(T(12));
  assert(app.data[0].field0 == 12);
  return app.data;
}

void main() {
  static const ww = gen();
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 26 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940




Reduced test case shows it's a subtle CTFE bug, involving assignment to a
member of a struct which is an element of a slice inside a struct.
----------

struct Bug7940 {
    int m;
}

struct App7940 {
   Bug7940[] x;
}

int bug7940() {
  Bug7940[2] y;
  App7940 app;
  app.x = y[0..1];
  app.x[0].m = 12;
  assert(y[0].m == 12);
  assert(app.x[0].m == 12); // also fails
  return 1;
}

static assert(bug7940());

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 06 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/7aa03a9c79e996884dbe0febd88c2f51bf928084
Fix issue 7940 CTFE wrong-code for opAssign and std.array.Appender

There was an erroneous copy. Array literals don't need to be copied (they are
reference types).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940




Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/bf3a4c05952e31c9a00687280c413d033b08784f
fix issue 7940 - CTFE wrong-code for opAssign and std.array.Appender

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=7940


Walter Bright <bugzilla digitalmars.com> changed:

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


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 07 2012