X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcRnTypes.lhs;h=4785a49ee2d1bdb717ec1314e9ee1556f5bd07d2;hp=37f1eab752d8af093873ed39b7d4952e7fd8b8d9;hb=c1681a73fa4ca4cf8758264ae387ac09a9e900d8;hpb=4287edeb7f329529149d8c95597d5e418388265f diff --git a/compiler/typecheck/TcRnTypes.lhs b/compiler/typecheck/TcRnTypes.lhs index 37f1eab..4785a49 100644 --- a/compiler/typecheck/TcRnTypes.lhs +++ b/compiler/typecheck/TcRnTypes.lhs @@ -13,7 +13,7 @@ module TcRnTypes( IfGblEnv(..), IfLclEnv(..), -- Ranamer types - ErrCtxt, + ErrCtxt, RecFieldEnv, ImportAvails(..), emptyImportAvails, plusImportAvails, WhereFrom(..), mkModDeps, @@ -28,9 +28,9 @@ module TcRnTypes( ArrowCtxt(NoArrowCtxt), newArrowScope, escapeArrowScope, -- Insts - Inst(..), InstOrigin(..), InstLoc(..), pprInstLoc, - instLocSrcLoc, instLocSrcSpan, - LIE, emptyLIE, unitLIE, plusLIE, consLIE, + Inst(..), InstOrigin(..), InstLoc(..), + pprInstLoc, pprInstArising, instLocSpan, instLocOrigin, + LIE, emptyLIE, unitLIE, plusLIE, consLIE, instLoc, instSpan, plusLIEs, mkLIE, isEmptyLIE, lieToList, listToLIE, -- Misc other types @@ -44,6 +44,7 @@ import HscTypes import Packages import Type import TcType +import TcGadt import InstEnv import FamInstEnv import IOEnv @@ -132,7 +133,8 @@ data TcGblEnv tcg_default :: Maybe [Type], -- Types used for defaulting -- Nothing => no 'default' decl - tcg_fix_env :: FixityEnv, -- Just for things in this module + tcg_fix_env :: FixityEnv, -- Just for things in this module + tcg_field_env :: RecFieldEnv, -- Just for things in this module tcg_type_env :: TypeEnv, -- Global type env for the module we are compiling now -- All TyCons and Classes (for this module) end up in here right away, @@ -224,8 +226,17 @@ data TcGblEnv tcg_fords :: [LForeignDecl Id], -- ...Foreign import & exports tcg_doc :: Maybe (HsDoc Name), -- Maybe Haddock documentation - tcg_hmi :: HaddockModInfo Name -- Haddock module information + tcg_hmi :: HaddockModInfo Name, -- Haddock module information + tcg_hpc :: AnyHpcUsage -- True if any part of the prog uses hpc instrumentation. } + +type RecFieldEnv = NameEnv [Name] -- Maps a constructor name *in this module* + -- to the fields for that constructor + -- This is used when dealing with ".." notation in record + -- construction and pattern matching. + -- The FieldEnv deals *only* with constructors defined in + -- *thie* module. For imported modules, we get the same info + -- from the TypeEnv \end{code} %************************************************************************ @@ -305,11 +316,15 @@ data TcLclEnv -- Changes as we move inside an expression -- Maintained during renaming, of course, but also during -- type checking, solely so that when renaming a Template-Haskell -- splice we have the right environment for the renamer. + -- + -- Used only for names bound within a value binding (bound by + -- lambda, case, where, let etc), but *not* for top-level names. + -- + -- Does *not* include global name envt; may shadow it + -- Includes both ordinary variables and type variables; + -- they are kept distinct because tyvar have a different + -- occurrence contructor (Name.TvOcc) -- - -- Does *not* include global name envt; may shadow it - -- Includes both ordinary variables and type variables; - -- they are kept distinct because tyvar have a different - -- occurrence contructor (Name.TvOcc) -- We still need the unsullied global name env so that -- we can look up record field names @@ -594,6 +609,24 @@ data Inst tci_loc :: InstLoc } + | ImplicInst { -- An implication constraint + -- forall tvs. (reft, given) => wanted + tci_name :: Name, + tci_tyvars :: [TcTyVar], -- Quantified type variables + -- Includes coercion variables + -- mentioned in tci_reft + tci_reft :: Refinement, + tci_given :: [Inst], -- Only Dicts + -- (no Methods, LitInsts, ImplicInsts) + tci_wanted :: [Inst], -- Only Dicts and ImplicInsts + -- (no Methods or LitInsts) + + tci_loc :: InstLoc + } + -- NB: the tci_given are not necessarily rigid, + -- although they will be if the tci_reft is non-trivial + -- NB: the tci_reft is already applied to tci_given and tci_wanted + | Method { tci_id :: TcId, -- The Id for the Inst @@ -638,7 +671,8 @@ data Inst @Insts@ are ordered by their class/type info, rather than by their unique. This allows the context-reduction mechanism to use standard finite -maps to do their stuff. +maps to do their stuff. It's horrible that this code is here, rather +than with the Avails handling stuff in TcSimplify \begin{code} instance Ord Inst where @@ -661,6 +695,14 @@ cmpInst (LitInst {}) (Dict {}) = GT cmpInst (LitInst {}) (Method {}) = GT cmpInst l1@(LitInst {}) l2@(LitInst {}) = (tci_lit l1 `compare` tci_lit l2) `thenCmp` (tci_ty l1 `tcCmpType` tci_ty l2) +cmpInst (LitInst {}) other = LT + + -- Implication constraints are compared by *name* + -- not by type; that is, we make no attempt to do CSE on them +cmpInst (ImplicInst {}) (Dict {}) = GT +cmpInst (ImplicInst {}) (Method {}) = GT +cmpInst (ImplicInst {}) (LitInst {}) = GT +cmpInst i1@(ImplicInst {}) i2@(ImplicInst {}) = tci_name i1 `compare` tci_name i2 \end{code} @@ -679,10 +721,20 @@ emptyLIE = emptyBag unitLIE inst = unitBag inst mkLIE insts = listToBag insts plusLIE lie1 lie2 = lie1 `unionBags` lie2 -consLIE inst lie = inst `consBag` lie plusLIEs lies = unionManyBags lies lieToList = bagToList listToLIE = listToBag + +consLIE inst lie = lie `snocBag` inst +-- Putting the new Inst at the *end* of the bag is a half-hearted attempt +-- to ensure that we tend to report the *leftmost* type-constraint error +-- E.g. f :: [a] +-- f = [1,2,3] +-- we'd like to complain about the '1', not the '3'. +-- +-- "Half-hearted" because the rest of the type checker makes no great +-- claims for retaining order in the constraint set. Still, this +-- seems to improve matters slightly. Exampes: mdofail001, tcfail015 \end{code} @@ -700,14 +752,28 @@ It appears in TcMonad because there are a couple of error-message-generation functions that deal with it. \begin{code} +------------------------------------------- data InstLoc = InstLoc InstOrigin SrcSpan ErrCtxt -instLocSrcLoc :: InstLoc -> SrcLoc -instLocSrcLoc (InstLoc _ src_span _) = srcSpanStart src_span +instLoc :: Inst -> InstLoc +instLoc inst = tci_loc inst + +instSpan :: Inst -> SrcSpan +instSpan wanted = instLocSpan (instLoc wanted) + +instLocSpan :: InstLoc -> SrcSpan +instLocSpan (InstLoc _ s _) = s -instLocSrcSpan :: InstLoc -> SrcSpan -instLocSrcSpan (InstLoc _ src_span _) = src_span +instLocOrigin :: InstLoc -> InstOrigin +instLocOrigin (InstLoc o _ _) = o +pprInstArising :: Inst -> SDoc +pprInstArising loc = ptext SLIT("arising from") <+> pprInstLoc (tci_loc loc) + +pprInstLoc :: InstLoc -> SDoc +pprInstLoc (InstLoc orig span _) = sep [ppr orig, text "at" <+> ppr span] + +------------------------------------------- data InstOrigin = SigOrigin SkolemInfo -- Pattern, class decl, inst decl etc; -- Places that bind type variables and introduce @@ -737,27 +803,23 @@ data InstOrigin | DefaultOrigin -- Typechecking a default decl | DoOrigin -- Arising from a do expression | ProcOrigin -- Arising from a proc expression -\end{code} - -\begin{code} -pprInstLoc :: InstLoc -> SDoc -pprInstLoc (InstLoc orig locn _) - = sep [text "arising from" <+> pp_orig orig, - text "at" <+> ppr locn] - where - pp_orig (OccurrenceOf name) = hsep [ptext SLIT("use of"), quotes (ppr name)] - pp_orig (IPOccOrigin name) = hsep [ptext SLIT("use of implicit parameter"), quotes (ppr name)] - pp_orig (IPBindOrigin name) = hsep [ptext SLIT("binding for implicit parameter"), quotes (ppr name)] - pp_orig RecordUpdOrigin = ptext SLIT("a record update") - pp_orig (LiteralOrigin lit) = hsep [ptext SLIT("the literal"), quotes (ppr lit)] - pp_orig (ArithSeqOrigin seq) = hsep [ptext SLIT("the arithmetic sequence"), quotes (ppr seq)] - pp_orig (PArrSeqOrigin seq) = hsep [ptext SLIT("the parallel array sequence"), quotes (ppr seq)] - pp_orig InstSigOrigin = ptext SLIT("instantiating a type signature") - pp_orig InstScOrigin = ptext SLIT("the superclasses of an instance declaration") - pp_orig DerivOrigin = ptext SLIT("the 'deriving' clause of a data type declaration") - pp_orig StandAloneDerivOrigin = ptext SLIT("a 'deriving' declaration") - pp_orig DefaultOrigin = ptext SLIT("a 'default' declaration") - pp_orig DoOrigin = ptext SLIT("a do statement") - pp_orig ProcOrigin = ptext SLIT("a proc expression") - pp_orig (SigOrigin info) = pprSkolInfo info + | ImplicOrigin SDoc -- An implication constraint + +instance Outputable InstOrigin where + ppr (OccurrenceOf name) = hsep [ptext SLIT("a use of"), quotes (ppr name)] + ppr (IPOccOrigin name) = hsep [ptext SLIT("a use of implicit parameter"), quotes (ppr name)] + ppr (IPBindOrigin name) = hsep [ptext SLIT("a binding for implicit parameter"), quotes (ppr name)] + ppr RecordUpdOrigin = ptext SLIT("a record update") + ppr (LiteralOrigin lit) = hsep [ptext SLIT("the literal"), quotes (ppr lit)] + ppr (ArithSeqOrigin seq) = hsep [ptext SLIT("the arithmetic sequence"), quotes (ppr seq)] + ppr (PArrSeqOrigin seq) = hsep [ptext SLIT("the parallel array sequence"), quotes (ppr seq)] + ppr InstSigOrigin = ptext SLIT("instantiating a type signature") + ppr InstScOrigin = ptext SLIT("the superclasses of an instance declaration") + ppr DerivOrigin = ptext SLIT("the 'deriving' clause of a data type declaration") + ppr StandAloneDerivOrigin = ptext SLIT("a 'deriving' declaration") + ppr DefaultOrigin = ptext SLIT("a 'default' declaration") + ppr DoOrigin = ptext SLIT("a do statement") + ppr ProcOrigin = ptext SLIT("a proc expression") + ppr (ImplicOrigin doc) = doc + ppr (SigOrigin info) = pprSkolInfo info \end{code}