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

Salih Dincer salihdb at hotmail.com
Sun May 14 12:59:22 UTC 2023


On Saturday, 13 May 2023 at 19:21:30 UTC, Cecil Ward wrote:
> 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.

Where is the bug? What do you mean by CTFE? If you do not add 2 
more elements as below, the snippet will not be compiled due to 
an error. It should be that simple...

```d
void main()
{
   auto test()
   {
     immutable int[] s = [0, 1, 2];
     return s[1..2];
   }

   auto arr = test(); // compilation error:
} // core.exception.ArraySliceError at onlineapp.d(6)
//slice [1 .. 2] extends past source array of length 1
```

SDB at 79


More information about the Digitalmars-d mailing list