News for hackers. Stuff that matters.
Re:AI is forcing us to pay off 40 years of technical debt by goodmythical on Wednesday November 04, 2026 @06:24PM (#50372859)
>But in real life compilers can still detect infinite loops in your code unless you try really hard to fool them.

Having never written compiled code, this intriques me. I often write one offs that I actually want to loop infinitely until I tell them to stop.

How does the compiler know whether it's just really long or infinite? Like, if I write a progressive approximation of pi or prime search algorithm, both are never going to halt because we're certain there are infinite digits of pi and prime numbers. The program could technically halt, but not as a function of the program but rather the hardware.

Or, being silly, if I write a "flip bits in target directory until all bits are flipped" and target /dev/null as a user of the software, it wouldn't halt, so the compiler would detect that?

Is there some --don't-mind-the-recursion flag for developing a program like ycruncher? I get the feeling that there's a list of "this typically causes infinities" that it checks against and if your use isn't on there well then... So it's just the mouse/mouse trap thing but in a compiler?

Or is there some unique CS going on that the compiler employs to determine certain classes of loops?

Re:AI is forcing us to pay off 40 years of technical debt by pillmillipedes on Wednesday November 04, 2026 @08:01PM (#50373656)
to my knowledge, they detect some common intentional and unintentional infinite loop patterns, but not much more:

    for(;;) // intentional

    for(unsigned i=10;i>=0;i--)
    for(int i=0;j<10;i++)
    for(unsigned i=0;i!=100;i+=8)
    // unintentional
in C infinite loops with no side effects are UB, so detecting them is useful for diagnostics.

guaranteed termination also is needed for some optimizations. but since side-effect-less infinite loops are UB in C, a compiler doesn't have to prove that such a loop terminates, it can just assume it does. other languages may differ, but I don't think any of them prove convergence of algorithms.

a don't-mind-the-recursion flag can be `-fno-finite-loops` on clang, or a read or write to a `volatile` variable in standard C.

Re:AI is forcing us to pay off 40 years of technical debt by tarsierscripter on Wednesday November 04, 2026 @08:04PM (#50373688)
How does the compiler know whether it's just really long or infinite?

One straightforward approach is to use Hoare logic [1], specifically the method of loop invariants and loop conditions. If you have a loop where the conditions are expressed only in terms of invariants then the conditions will never change truth values during iteration of the loop, thus you have an infinite loop.

Now, some loops are written deliberately like this, i.e. "while (true)", and so there you need to look at whether the body of the loop contains any side effects (reading input, printing, etc). Then you have what is called a "productive infinite loop", such as an event loop or a REPL. Of course you can't prove this sort of loop will ever halt, since the user may never elect to issue the quit command, but these sorts of loops often tend to spend most of their time with the process sleeping until the operating system wakes them up to handle an event.

[1] https://en.wikipedia.org/wiki/Hoare_logic

Re:AI is forcing us to pay off 40 years of technical debt by zamadatix on Wednesday November 04, 2026 @08:59PM (#50374132)
I think it's worth pointing out the compiler usually won't do much (if any) of this kind of check unless you tell it to.

When you do tell it to, it's usually to warn you that you have accidentally made some really dumb mistake which resulted in a loop which will run forever while doing nothing externally useful to the loop.

Whenever you do something useful with an infinite loop, like come up with additional digits of pi or progressing the search for the next prime number, it results in some form of useful output which touches the environment outside the loop (e.g. print the number to the screen) and so it won't trigger this kind of thing.

Even then, it won't catch every such case. It just so happens that the vast majority of these cases are really easy to show in practice. Something like a loop which does absolutely nothing but loop until it calculates the last digit of pi would be an example of a useless infinite loop that the compiler might not catch, but also an example of one you'd kinda have to go out of your way to make fail in such a way in the first place.

Re:AI is forcing us to pay off 40 years of technical debt by wrs on Thursday November 05, 2026 @12:16AM (#50375493)
Interestingly, in C++, a trivial infinite loop is undefined behavior. So the compiler not only doesn’t have to detect it, it is allowed to assume you didn’t do it. [0] This is inconvenient when you actually want a trivial infinite loop. (C++26 was going to fix this, but it seems we may not ever get a C++26…)

[0] https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2023/p28...

Re:AI is forcing us to pay off 40 years of technical debt by stogot on Wednesday November 04, 2026 @09:10PM (#50374220)
That’s the point of the halting problem You can’t know if it will halt absolutely