CTFE for array / string / slice operations, constant propagation and bug detection at compile time

Cecil Ward cecil at cecilward.com
Sat May 13 19:21:30 UTC 2023


Here’s a snippet of code which was compiled with both GDC and 
LDC, latest versions currently present on the godbolt Compiler 
Explorer site, target = x86-64, command line options -O3 -release 
/ -frelease

immutable (int)[]
test() {
        immutable int[] s = [1];
	
	return s[1..2];
	}

There is no CTFE here with either compiler, and the bug in the 
return statement is not spotted by the compilers at compile time. 
It’s clearer to see in the LDC compiler, the return value is an 
address off the end of the initialiser data array and a length of 
1. If the compiler had CTFE’d s, had then done constant 
propagation and CTFE’d the retval then it could have spotted the 
bug.

Would it be a nightmare to implement CTFE for arrays, slices and 
strings? Optimisation of basic operations on these types, in 
respect of concatenation, slicing and many more operations would 
be extremely welcome. Would there possibly be a chance to catch 
many bugs at compile-time?


More information about the Digitalmars-d mailing list