X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Ftypecheck%2FTcRnTypes.lhs;h=3c23921207adb680755cbf2e408d089b347b4f80;hp=b335b54391b5271db23dec4ffeb3a81ffd900243;hb=72e37dedee9e8a109ebda4b13e49b7133b530591;hpb=5ad61e1470db6dbc8279569c5ad1cc093f753ac0 diff --git a/compiler/typecheck/TcRnTypes.lhs b/compiler/typecheck/TcRnTypes.lhs index b335b54..3c23921 100644 --- a/compiler/typecheck/TcRnTypes.lhs +++ b/compiler/typecheck/TcRnTypes.lhs @@ -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 @@ -305,11 +306,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 @@ -469,7 +474,8 @@ of whether the imported things are actually used or not It is used * when processing the export list * when constructing usage info for the inteface file * to identify the list of directly imported modules - for initialisation purposes + for initialisation purposes and + for optimsed overlap checking of family instances * when figuring out what things are really unused \begin{code} @@ -593,6 +599,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 @@ -637,7 +661,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 @@ -660,6 +685,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} @@ -678,10 +711,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} @@ -699,14 +742,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 @@ -736,27 +793,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}