Opened 5 years ago

Closed 5 years ago

#9422 closed bug (duplicate)

EPT caching on --make can make spurious instances visible

Reported by: ezyang Owned by:
Priority: normal Milestone:
Component: Compiler (Type checker) Version: 7.9
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: GHC accepts invalid program Test Case:
Blocked By: Blocking:
Related Tickets: 8427 Differential Rev(s):
Wiki Page:


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.

Change History (2)

comment:1 Changed 5 years ago by ezyang

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.

comment:2 Changed 5 years ago by ezyang

Resolution: duplicate
Status: newclosed
Note: See TracTickets for help on using tickets.