Description

This is perhaps best demonstrated by way of an example. Suppose we have a package a with the following two modules, with A defining a type class and data type, and I defining an orphan instance.

moduleAwheredataA=AclassC a where
cop :: a -> a

moduleIwhereimportAinstanceCAwhere
cop = id

Then, when compiling this module:

moduleNwhereimportAx= cop A

I might reasonably expect to be told that the instance C is not in scope (since I didn't import I, nor did any of my dependencies import I). However, suppose I have another module:

moduleMwhereimportI

and I use --make to compile both of these modules in one go, with M being compiled first (ghc --make N.hs M.hs does that for me), then N will incorrectly type check and compile! This should not happen.

The reason for this behavior is that we do not clear the EPT (external package table) between compilations of a file in batch compilation mode; this saves us from having to repeatedly read instances from the external files. Thus, M, having loaded the instances into the EPT when type-checking, N will automatically pick them up. Note that I and A have to be in a separate package; otherwise --make puts them in the HPT (home package table) and everything works fine. (Note also that one-shot compilation puts home modules in the EPT, but since that gets thrown out everything is fine as well, although perhaps a user of the GHC API could get bitten by this.)

As for the fix, it's probably not right to clear the EPT either, since we get quite good time savings from not having to parse interface files repeatedly. Perhaps we could do something clever by partitioning up the EPT, but maybe it's not worth it and we should keep this bug as a known infelicity until the next time this part of the compiler gets rewritten. I'll submit a test-case (marked as failing) anyhoo.

Oldest firstNewest firstThreaded

Show commentsShow property changes

Change History (2)

In our most recent Backpack information, SPJ described a nice and simple way of solving this problem. Continue to maintain a global EPT, but with every type class instance, record the module it originated from. Now, when we do a lookup into the EPT table, we need to check if that module is transitively reachable from the current module; if it is not, then it is discarded. The transitivity check can be done lazily, so, hopefully, when this bad behavior doesn't occur, we can avoid doing all of the work of slurping in all the imports to figure out what type instances are available and which are not.