gracelang / minigrace

Self-hosting compiler for the Grace programming language
39 stars 22 forks source link

Cyclic imports cause minigrace to run forever (command line only) #312

Open apblack opened 4 years ago

apblack commented 4 years ago

If module A imports module B, and module B imports module A, minigrace will run forever — or until the host OS runs out of processes. It will start compiling A, which will launch a compile of B, which will launch a compile of A, and so on.

There used to be logic in xmodule that attempted to detect his situation — it was removed in commit 0cf0402 because it did not work. It used the modules entry of the compiled code's gct dictionary to detect imports, but because this entry is not available until after compilation has finished (and compilation never finished), the cycle-detection code was never run.

Instead, I believe that we need to build the imports graph before compilation starts. This graph could then be searched for cycles. As an added benefit, if no cycles are found, a topological sort of the graph will yield the dependencies for compiling the graph of modules that may enable some of them to be compiled in parallel.