www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - [D1] assert failure expression.c

reply %u <e ee.com> writes:
Incomplete mixin expression + char[] to char assignment = crash :)

--
char[] foo()
{
  char[2] res = "1 ";
  res[1] = ";"; // should be a char
  return res;
}

struct S(T)
{
  int i = mixin( foo() ); // incomplete mixin expression
}

S!(int) s;
--
Assertion failure: '0' on line 1342 in file 'expression.c'

And while you are reading this, could you maybe tell me why this needs a .dup?
--
char[] bar()
{
  char[8] res = "int i=0;";
  return res.dup;// without: main.d(/here/): Error: escaping reference to
local res
}
mixin ( bar() );
--
Oct 09 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
%u:

 Incomplete mixin expression + char[] to char assignment = crash :)
You (or me or someone else) may add this to Bugzilla: char[2] foo() { char[2] code; code[1] = ""; return code; } struct Bar { int i = mixin(foo()); } void main() {}
 And while you are reading this, could you maybe tell me why this needs a .dup?
 --
 char[] bar()
 {
   char[8] res = "int i=0;";
   return res.dup;// without: main.d(/here/): Error: escaping reference to
 local res
 }
 mixin ( bar() );
If bar() returns a char[], it means it returns a dynamic array. char[8] inside bar() is allocated on the stack, so if you convert a static array to a dynamic array, it doesn't get copied (no return by value), the dynamic array is just a reference to the stack memory, and this is a bug. If bar returns a char[8] there is no error in D2 because fixed-sized arrays get copied by value. Bye, bearophile
Oct 09 2010
next sibling parent bearophile <bearophileHUGS lycos.com> writes:
 You (or me or someone else) may add this to Bugzilla:
 
 char[2] foo() {
     char[2] code;
     code[1] = "";
     return code;
 }
 struct Bar {
     int i = mixin(foo());
 }
 void main() {}
Further reduced: void foo() { char[0] code; code[0] = ""; } struct Bar { int i = mixin(foo()); } void main() {}
Oct 09 2010
prev sibling parent %u <e ee.com> writes:
Done & Thanks!

== Quote from bearophile (bearophileHUGS lycos.com)'s article
 %u:
 Incomplete mixin expression + char[] to char assignment = crash :)
You (or me or someone else) may add this to Bugzilla: char[2] foo() { char[2] code; code[1] = ""; return code; } struct Bar { int i = mixin(foo()); } void main() {}
 And while you are reading this, could you maybe tell me why this needs a .dup?
 --
 char[] bar()
 {
   char[8] res = "int i=0;";
   return res.dup;// without: main.d(/here/): Error: escaping reference to
 local res
 }
 mixin ( bar() );
If bar() returns a char[], it means it returns a dynamic array. char[8] inside
bar() is allocated on the stack, so if you convert a static array to a dynamic array, it doesn't get copied (no return by value), the dynamic array is just a reference to the stack memory, and this is a bug. If bar returns a char[8] there is no error in D2 because fixed-sized arrays get copied by value.
 Bye,
 bearophile
Oct 09 2010