import TcRnMonad
import Type ( Kind, openTypeKind, liftedTypeKind,
unliftedTypeKind, mkArrowKind, splitTyConApp,
- mkTyVarTys, mkGenTyConApp, mkTyVarTys, ThetaType )
+ mkTyVarTys, mkGenTyConApp, mkTyVarTys, ThetaType, pprClassPred )
import TypeRep ( Type(..), PredType(..) )
import TyCon ( TyCon, tyConName )
import HscTypes ( ExternalPackageState(..), PackageInstEnv, PackageRuleBase,
HscEnv, TyThing(..), implicitTyThings, typeEnvIds,
ModIface(..), ModDetails(..), InstPool, ModGuts,
TypeEnv, mkTypeEnv, extendTypeEnvList, lookupTypeEnv,
- DeclPool, RulePool, Pool(..), Gated, addRuleToPool )
+ RulePool, Pool(..) )
import InstEnv ( extendInstEnv )
import CoreSyn
-import PprType ( pprClassPred )
import PprCore ( pprIdRules )
import Rules ( extendRuleBaseList )
import CoreUtils ( exprType )
| otherwise = do -- The normal case, not wired in
{ -- Get the decl from the pool
- decl <- updateEps (\ eps ->
- let
- (decls', decl) = selectDecl (eps_decls eps) name
- in
- (eps { eps_decls = decls' }, decl))
-
- -- Typecheck it
- -- Side-effects EPS by faulting in any needed decls
- -- (via nested calls to tcImportDecl)
- ; initIfaceLcl (nameModuleName name) (tcIfaceDecl decl) }
-
+ mb_decl <- updateEps (\ eps -> selectDecl eps name)
+
+ ; case mb_decl of
+ Just decl -> initIfaceLcl (nameModuleName name) (tcIfaceDecl decl)
+ -- Typecheck it
+ -- Side-effects EPS by faulting in any needed decls
+ -- (via nested calls to tcImportDecl)
+
+
+ Nothing -> do { ioToIOEnv (printErrs (msg defaultErrStyle)); failM }
+ -- Declaration not found
+ -- No errors-var to accumulate errors in, so just
+ -- print out the error right now
+
+ }
+ where
+ msg = hang (ptext SLIT("Can't find interface-file declaration for") <+> ppr (nameParent name))
+ 2 (vcat [ptext SLIT("Probable cause: bug in .hi-boot file, or inconsistent .hi file"),
+ ptext SLIT("Use -ddump-if-trace to get an idea of which file caused the error")])
-selectDecl :: DeclPool -> Name -> (DeclPool, IfaceDecl)
+selectDecl :: ExternalPackageState -> Name -> (ExternalPackageState, Maybe IfaceDecl)
-- Use nameParent to get the parent name of the thing
-selectDecl (Pool decls_map n_in n_out) name
- = (Pool decls' n_in (n_out+1), decl)
+selectDecl eps@(EPS { eps_decls = Pool decls_map n_in n_out}) name
+ = case lookupNameEnv decls_map main_name of
+ Nothing -> (eps, Nothing)
+ Just decl -> (eps {eps_decls = Pool decls' n_in (n_out+1)}, Just decl)
where
main_name = nameParent name
- decl = case lookupNameEnv decls_map main_name of
- Nothing -> pprPanic "selectDecl" (ppr main_name <+> ppr name) ;
- Just decl -> decl
-
- decls' = delFromNameEnv decls_map main_name
+ decls' = delFromNameEnv decls_map main_name
\end{code}
%************************************************************************
if we are trying to resolve (C x [y]), we need *both* (b) and (c),
even though T is not involved yet, so that we spot the overlap.
+
+NOTE: if you use an instance decl with NO type constructors
+ instance C a where ...
+and look up an Inst that only has type variables such as (C (n o))
+then GHC won't necessarily suck in the instances that overlap with this.
+
+
\begin{code}
loadImportedInsts :: Class -> [Type] -> TcM PackageInstEnv
loadImportedInsts cls tys
-- Suck in the instances
; let { (inst_pool', iface_insts)
- = selectInsts (eps_insts eps) cls_gate tc_gates }
+ = WARN( null tc_gates, ptext SLIT("Interesting! No tycons in Inst:")
+ <+> pprClassPred cls tys )
+ selectInsts (eps_insts eps) cls_gate tc_gates }
-- Empty => finish up rapidly, without writing to eps
; if null iface_insts then
(insts', iface_insts)
= case lookupNameEnv insts cls of {
Nothing -> (insts, []) ;
- Just gated_insts ->
+ Just gated_insts ->
- case foldl choose ([],[]) gated_insts of {
+ case choose1 gated_insts of {
(_, []) -> (insts, []) ; -- None picked
(gated_insts', iface_insts') ->
(extendNameEnv insts cls gated_insts', iface_insts') }}
+ choose1 gated_insts
+ | null tycons -- Bizarre special case of C (a b); then there are no tycons
+ = ([], map snd gated_insts) -- Just grab all the instances, no real alternative
+ | otherwise -- Normal case
+ = foldl choose2 ([],[]) gated_insts
+
-- Reverses the gated decls, but that doesn't matter
- choose (gis, decls) (gates, decl)
- | any (`elem` tycons) gates = (gis, decl:decls)
- | otherwise = ((gates,decl) : gis, decls)
+ choose2 (gis, decls) (gates, decl)
+ | null gates -- Happens when we have 'instance T a where ...'
+ || any (`elem` tycons) gates = (gis, decl:decls)
+ | otherwise = ((gates,decl) : gis, decls)
\end{code}
%************************************************************************
| null gates' = (pool, rule:if_rules)
| otherwise = ((gates',rule) : pool, if_rules)
where
- gates' = filter (`elemNameEnv` type_env) gates
+ gates' = filter (not . (`elemNameEnv` type_env)) gates
tcIfaceRule :: IfaceRule -> IfL IdCoreRule