EPT caching on --make can make spurious instances visible
|Reported by:||ezyang||Owned by:|
|Component:||Compiler (Type checker)||Version:||7.9|
|Type of failure:||GHC accepts invalid program||Test Case:|
|Related Tickets:||8427||Differential Revisions:|
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.
module A where data A = A class C a where cop :: a -> a
module I where import A instance C A where cop = id
Then, when compiling this module:
module N where import A x = 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:
module M where import I
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.