Potential recompilation avoidance improvement with TH
Apologies that this does not have the "bug" issue template, clicking that in the Template box isn't doing anything for me.
I'm investigated GHC recompilation avoidance behavior and I've found some very surprising behavior. I've documented the behavior in this repository.
To briefly recap, we have this module graph:
(GCB.Types.FooExplicit) --+
+-> (GCB.Explicit)
(GCB.Types.Quuz) ------+
GCB.Types.Quuz
defines two Template Haskell functions that are used in GCB.Explicit
, which has explicit imports for everything it uses.
GCB.Types.FooExplicit
has an explicit export list.
I'm finding that any modification of GCB.Types.FooExplicit
triggers a TH
recompilation of GCB.Explicit
. and I've tried these:
- A top-level term is added, but not exported.
- This seems reasonable - a top-level term could introduce an orphan instance or a type class instance, and there's no guarantee that the TH in
GCB.Explicit
doesn't reify type class information. This could be improved if we check that no additional class instance declarations are added - a value or internal type definition should not cause a problem here, as far as I can tell.
- This seems reasonable - a top-level term could introduce an orphan instance or a type class instance, and there's no guarantee that the TH in
- A top-level exported term has a
where x = 2 :: Int
clause added, even if that term is not imported by the dependent module, or even used by a term that is imported by the module.- This seems less reasonable to me. Modifying the definition of a function that is not even imported should not cause recompilation. I don't know if GHC tracks function-level modifications.
- A top-level non-exported term has a
where x = 2 :: Int
clause added.- The only way this makes sense is if the top-level non-exported term is used by a top-level exported term, and this can affect the result of the Template Haskell.
- A comment is added or removed.
- I can understand this with GHCi, which doesn't use object files. But with regular GHC, there's literally no change to the module, aside from a comment. This should definitely not cause recompilation.
I've tested this with GHC 8.6.5 and GHC 9.2, and the results are the same in both cases.
This problem is pretty severe - any change to any module that is imported by any module that uses any TH is a pretty massive burden. Is there anything we can do to improve this situation?