Avoid direct access to compiler state
This is a meta-ticket about the avoidance of direct access to compiler state (
HscEnv, etc.). Many functions implicitly access
DynFlags to query command line options, package state, linker state, etc. making the code not modular.
As a stronger motivation: I want to fix #14335 (support for compiler plugins with cross-compilation). It should have been pretty straightforward to use two package states instead of one (one for the host compiler and another for the target code). Sadly this isn't currently possible because the package state stored in
DynFlags is accessed very indirectly (e.g. to get the effective wired-in unit ids on disk when pretty-printing messages for the user, etc.). There are a lot of other examples of this kind.
All the work done in this direction will be beneficial to make GHC multi-target, multi-session and to improve cross-compilation support (cf https://github.com/hsyl20/ghc-cross-compilation for what we would like to improve).
Three different but related goals regarding DynFlags:
- Make it clearer which bits of DynFlags are used where. Eg pass smaller record to SDoc. Ameliorates the symptoms of not having (2); and is generally a good idea.
- Stop making DynFlags depend on TcM etc. It should just be an abstract syntax tree. Move mutable state, caches &c, to Session (= HscEnv).
- Remove mutable state from DynFlags, and keep it somewhere else. But where? HscEnv?
SDocContext: #10143 (closed)
Add SDocContext instead of directly accessing
Don't access the (target) package state when pretty-printing
DynFlagsin code generators
Fix handling of
Outputableinstances for things that need some context to be printed
Fix binary blob handling (
- Don't store state into DynFlags. Use HscEnv or another env instead.