While reviewing #1480 (closed), it was found that there are corner cases that are not easy to handle.
This task is about fixing TH module reification to reify direct import list, not usages. For this we may need to extend the current ModIface.
This task also includes cleaning up the comments and code in related areas, to quote simonpj: "we need more precise commentary on the fields of HscTypes.Dependencies, TcRnTypes.ImportAvails, and mi_usages of ModIface. For example, I wonder whether the mi_usages field could be part of the Dependencies".
Thomie, unfortunately this is not done yet and that is totally on me :(
The main task here is to "fix TH module reification to reify direct import list, not usages"
So just having more comments is not enought, actual coding (and probably some refactoring) has to happen. On the other hand, I am pretty sure that the better comments will help me to get back up to speed more quickly when I sit down to handle this.
I still plan to work on this, so feel free to leave me as owner unless someone else wants to tackle this before me in which case I am happy to discuss more if questions come up.
A good first step would be to write a precise specification for the change you propose. What will change? Is is just refactoring or will (TH) programmers see a difference? What will the difference be? I have no idea at the moment.
Once the specification is clear, and agreed, the implementation can follow. But the latter should not precede the former.
Simon, yes of course, it is important to first agree on what we want.
On the other hand, this is a much smaller change than my changes from previous year, so I do not think that we need a whole wiki page again.
Let me put it here what I think the issue is and then you can point it out where I was not precise enough and we can complete this to a "specification".
Take the following program:
{-# LANGUAGE TemplateHaskell #-}importLanguage.Haskell.THimportLanguage.Haskell.TH.Syntax$(doletmod=Module(PkgName"binary-0.7.1.0")(ModName"Data.Binary")ModuleInfoinfo<-reifyModulemodrunIO$mapM_printinforeturn[])main=return()
So from the programmer point of view, this change should make the ModuleInfo list returned by module reification only contain the actual direct imports.
So this is my goal, before discussing implementation details inside GHC, may I ask if this description is clear enough and if others agree with the goal?
reifyModule :: Module -> Q ModuleInfodata ModuleInfo = ModuleInfo [Module] -- Contains the import list of the module
The "import list of the module" M presumably means the list of modules that M imports directly. (Clarifying the comment would be good.)
But the program above shows that reifyModule reports a much longer list. (NB: A smaller test case would make easier.)
So the specification is simply to make reifyModudule behave as advertised.
Now I understand, it's easy enough to implement, at least for modules in the same package as M. Just pick modules whose Usage has Just _ in its usg_exports field. Sadly, for modules from other pacakges we simply don't record the necessary information in interface files at all. It would not be hard to fix this.
If I remember correctly the difference is not the package boundary, but the compilation unit boundary. So if you compile your modules with different GHC invocations by using -c instead of --make, then you will not have the usg_exports either, am I right?
If that is the case I prefer us to do the right fix and include this information in the interfaces files and do not introduce different behavior depending on compilation technique in the meantime.
I don't understand how the explicit imports list of a module would be helpful to another module. I've never grokked the usecase for this.
I suggest just changing the documentation to indicate that it is a list of usages rather than imports, to resolve this. How's that sound? Assigning this ticket to myself, to implement via documentation.
Alternatively, this usages information could just get removed. Worth checking if there are uses of it on hackage. I am considering implementing reification of exports (https://ghc.haskell.org/trac/ghc/ticket/10391), which is what I think most people would want in module reification as that would be quite useful.
Using mi_usages for this field is wrong, (as pointed out in #1480 (closed)), mi_usages is ONLY for recompilation checking.
I find the interface for reifyModule distasteful as it requires doing a deep traversal of transitive dependencies through all package boundaries. This can be thousands of modules which don't bring anything into scope in the current module.
With #22188 then the mi_usages comes back to bite us more thoroughly, I have made it emit a warning if you try and traverse through a package which lacks the correct mi_usages information.