Direct recursion detection possible?

Cecil Ward cecil at cecilward.com
Thu May 25 15:56:43 UTC 2023


On Wednesday, 24 May 2023 at 23:04:53 UTC, Cecil Ward wrote:
> Due to some stupidity of mine involving misuse of templates I 
> ended up with a routine that simply called itself infinitely. 
> Directly, that is, and in this case the routine was optimised 
> down into a simple infinite loop with no loop body content.
>
> Is it possible for our compilers to detect direct infinite 
> recursion ? I’m ignoring the indirect call case, and I’m also 
> ignoring anything involving function pointers, so given those 
> restrictions, we will be able to see that the generated code of 
> a routine is calling its own address, because that address is a 
> literal and we know what the current function is that we’re 
> currently in.
>
> And while we’re at it, is it possible to detect all kinds of 
> straightforward infinite loops (not coming from recursion)? 
> That is where the generated code, perhaps as a result of 
> optimisation, is a straight loop with no body. Can we implement 
> that?
>
> It ought to be possible because the backend can spot a pattern 
> like "L1:  jmp  L1".
>
> I promise I’m not trying to solve the Halting Problem :)
>
> Cecil Ward.

Is it a nightmare to get template generation to detect a 
call-to-self, one line function (‘direct’ recursion bar the 
mangling)? My stupidity was trying to ‘forward’ a call to a 
particular template specialisation on to the routine for the 
general case, and getting it all wrong. I wouldn’t want to make 
more work compared to the L1: jmp L1 case, but if anyone did feel 
like doing a check at template instantiation-related code 
emission time then a very nice specific and meaningful message 
could be output. Indeed there are some very worthwhile diagnostic 
longwinded template-related error messages already when someone 
has an error relating to multiple possible template expansion 
possibilities. (What is that called, that I’m thinking of?)


More information about the Digitalmars-d mailing list