www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - swap doesn't work in CTFE?

reply Johann MacDonagh <johann.macdonagh..no spam..gmail.com> writes:
Although CTFE supports ref parameters, swap doesn't appear to work. This 
casues dmd to segfault in 2.053 and the current dmd master.

import std.algorithm;
import std.stdio;

string ctfeRef(ref string a, ref string b)
{
	return a;
}

string ctfeSort()
{
     auto x = [ "a", "c", "b" ];
     swap(x[1], x[0]);           // This is the problem here
     return ctfeRef(x[1], x[0]);
}

void main()
{
     enum xx = ctfeSort();
     writeln(xx);
}

This means things like sort won't work in CTFE. Does anyone know if this 
is a known bug? My searching didn't bring anything up.
Jul 04 2011
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"Johann MacDonagh" <johann.macdonagh..no spam..gmail.com> wrote in message 
news:iusp80$vnr$1 digitalmars.com...
 Although CTFE supports ref parameters, swap doesn't appear to work. This 
 casues dmd to segfault in 2.053 and the current dmd master.

 import std.algorithm;
 import std.stdio;

 string ctfeRef(ref string a, ref string b)
 {
 return a;
 }

 string ctfeSort()
 {
     auto x = [ "a", "c", "b" ];
     swap(x[1], x[0]);           // This is the problem here
     return ctfeRef(x[1], x[0]);
 }

 void main()
 {
     enum xx = ctfeSort();
     writeln(xx);
 }

 This means things like sort won't work in CTFE. Does anyone know if this 
 is a known bug? My searching didn't bring anything up.
Same thing happens with pointers. Reduced: void swap(int[]* lhs, int[]* rhs) { *lhs = *rhs; *rhs = *lhs; } int ctfeSort() { int[][2] x; swap(&x[0], &x[1]); return 0; } void main() { enum x = ctfeSort(); }
Jul 04 2011
parent reply bearophile <bearophileHUGS lycos.com> writes:
Daniel Murphy:

 Same thing happens with pointers.  Reduced:
Pointers to structs in CTFE will work in DMD 2.054 :-) Bye, bearophile
Jul 04 2011
parent reply "Daniel Murphy" <yebblies nospamgmail.com> writes:
"bearophile" <bearophileHUGS lycos.com> wrote in message 
news:iut093$1bjg$1 digitalmars.com...
 Daniel Murphy:

 Same thing happens with pointers.  Reduced:
Pointers to structs in CTFE will work in DMD 2.054 :-)
When they don't crash the compiler, that is. http://d.puremagic.com/issues/show_bug.cgi?id=6250
Jul 04 2011
next sibling parent reply Ary Manzana <ary esperanto.org.ar> writes:
On 7/4/11 11:39 PM, Daniel Murphy wrote:
 "bearophile"<bearophileHUGS lycos.com>  wrote in message
 news:iut093$1bjg$1 digitalmars.com...
 Daniel Murphy:

 Same thing happens with pointers.  Reduced:
Pointers to structs in CTFE will work in DMD 2.054 :-)
When they don't crash the compiler, that is. http://d.puremagic.com/issues/show_bug.cgi?id=6250
Is there any point in implementing CTFE in the compiler when LLVM already allows you to define functions and JIT compile them? Why write yet another "JIT compiler" that in fact doesn't optimize anything and just interprets everything as it comes? I'm sure with LLVM you can do all of what DMD currently does and more.
Jul 05 2011
parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On 2011-07-05 12:10, Ary Manzana wrote:
 On 7/4/11 11:39 PM, Daniel Murphy wrote:
 "bearophile"<bearophileHUGS lycos.com>  wrote in message
 news:iut093$1bjg$1 digitalmars.com...
 
 Daniel Murphy:
 Same thing happens with pointers.  Reduced:
Pointers to structs in CTFE will work in DMD 2.054 :-)
When they don't crash the compiler, that is. http://d.puremagic.com/issues/show_bug.cgi?id=6250
Is there any point in implementing CTFE in the compiler when LLVM already allows you to define functions and JIT compile them? Why write yet another "JIT compiler" that in fact doesn't optimize anything and just interprets everything as it comes? I'm sure with LLVM you can do all of what DMD currently does and more.
CTFE has a major impact on how programs are compiled. It wouldn't make any sense to try and take it out the compiler itself. Not only does it affect what you can initialize module and static variables to, but you can use it in conditional compilation and template instantiation. It's an integral part of the language itself and an integral part of the compilation process. Splitting it out wouldn't work. Not to mention, CTFE happens in the _frontend_ of the compiler, not the backend, and LLVM is the backend of LDC. dmd, gdc, and LDC all share the same frontend. So, unless the JIT compiler were in the frontend, it wouldn't matter on whit how good it is, because it couldn't be used in the frontend - which is where CTFE needs to happen. - Jonathan M Davis
Jul 05 2011
prev sibling parent Johann MacDonagh <johann.macdonagh..no spam..gmail.com> writes:
On 7/4/2011 10:39 PM, Daniel Murphy wrote:
 "bearophile"<bearophileHUGS lycos.com>  wrote in message
 news:iut093$1bjg$1 digitalmars.com...
 Daniel Murphy:

 Same thing happens with pointers.  Reduced:
Pointers to structs in CTFE will work in DMD 2.054 :-)
When they don't crash the compiler, that is. http://d.puremagic.com/issues/show_bug.cgi?id=6250
Perfect! Thank you. I verified the exception is a stack overflow for the ref case as well (but since you know the compiler internals I'm sure you knew that already ;) ).
Jul 05 2011