August 24, 2023
https://issues.dlang.org/show_bug.cgi?id=24107

          Issue ID: 24107
           Summary: The error for exceeding the CTFE recursion limit
                    bypasses speculative compilation.
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: contact@harrygillanders.com

Exceeding the CTFE recursion limit (which is one-thousand recursive calls)
causes the compiler to emit an error.
The issue is that this error is emitted even when the compiler is compiling
speculatively, such as when evaluating an argument for `__traits(compiles,
...)`.


This code will replicate the issue:
> bool recurse ()
> {
> 	return recurse();
> }
>
> pragma(msg, "This should not output an error message: ", __traits(compiles, {enum bool r = recurse();}));

which outputs:
> This should not output an error message: .\ctfeRecursionError.d(1): Error: function `ctfeRecursionError.recurse` CTFE recursion limit exceeded
> .\ctfeRecursionError.d(3):        called from here: `recurse()`
> .\ctfeRecursionError.d(1):        1000 recursive calls to function `recurse`
> .\ctfeRecursionError.d(6):        called from here: `recurse()`
> false

when it should output:
> This should not output an error message: false


The offending bit of code, in the compiler, that causes this seems to be in
`dmd.dinterpret`, wherein `global.gag` is set to zero:
https://github.com/dlang/dmd/blob/0b937d671023a1bf86985b2452096a64aae85f07/compiler/src/dmd/dinterpret.d#L638

--