Simon Peyton Jones pushed to branch wip/T27078 at Glasgow Haskell Compiler / GHC

Commits:

1 changed file:

Changes:

  • compiler/GHC/Core/Lint.hs
    ... ... @@ -179,65 +179,7 @@ Note [Linting function types]
    179 179
     All saturated applications of funTyCon are represented with the FunTy constructor.
    
    180 180
     See Note [Function type constructors and FunTy] in GHC.Builtin.Types.Prim
    
    181 181
     
    
    182
    - We check this invariant in lintType.
    
    183
    -
    
    184
    -Note [Linting type lets]
    
    185
    -~~~~~~~~~~~~~~~~~~~~~~~~
    
    186
    -In the desugarer, it's very very convenient to be able to say (in effect)
    
    187
    -        let a = Type Bool in
    
    188
    -        let x::a = True in <body>
    
    189
    -That is, use a type let.  See Note [Core type and coercion invariant] in "GHC.Core".
    
    190
    -One place it is used is in mkWwBodies; see Note [Join points and beta-redexes]
    
    191
    -in GHC.Core.Opt.WorkWrap.Utils.  (Maybe there are other "clients" of this feature; I'm not sure).
    
    192
    -
    
    193
    -* Hence when linting <body> we need to remember that a=Int, else we
    
    194
    -  might reject a correct program.  So we carry a type substitution (in
    
    195
    -  this example [a -> Bool]) and apply this substitution before
    
    196
    -  comparing types. In effect, in Lint, type equality is always
    
    197
    -  equality-modulo-le-subst.  This is in the le_subst field of
    
    198
    -  LintEnv.  But nota bene:
    
    199
    -
    
    200
    -  (SI1) The le_subst substitution is applied to types and coercions only
    
    201
    -
    
    202
    -  (SI2) The result of that substitution is used only to check for type
    
    203
    -        equality, to check well-typed-ness, /but is then discarded/.
    
    204
    -        The result of substitution does not outlive the CoreLint pass.
    
    205
    -
    
    206
    -  (SI3) The InScopeSet of le_subst includes only TyVar and CoVar binders.
    
    207
    -
    
    208
    -* The function
    
    209
    -        lintInTy :: Type -> LintM (Type, Kind)
    
    210
    -  returns a substituted type.
    
    211
    -
    
    212
    -* When we encounter a binder (like x::a) we must apply the substitution
    
    213
    -  to the type of the binding variable.  lintBinders does this.
    
    214
    -
    
    215
    -* Clearly we need to clone tyvar binders as we go.
    
    216
    -
    
    217
    -* But take care (#17590)! We must also clone CoVar binders:
    
    218
    -    let a = TYPE (ty |> cv)
    
    219
    -    in \cv -> blah
    
    220
    -  blindly substituting for `a` might capture `cv`.
    
    221
    -
    
    222
    -* Alas, when cloning a coercion variable we might choose a unique
    
    223
    -  that happens to clash with an inner Id, thus
    
    224
    -      \cv_66 -> let wild_X7 = blah in blah
    
    225
    -  We decide to clone `cv_66` because it's already in scope.  Fine,
    
    226
    -  choose a new unique.  Aha, X7 looks good.  So we check the lambda
    
    227
    -  body with le_subst of [cv_66 :-> cv_X7]
    
    228
    -
    
    229
    -  This is all fine, even though we use the same unique as wild_X7.
    
    230
    -  As (SI2) says, we do /not/ return a new lambda
    
    231
    -     (\cv_X7 -> let wild_X7 = blah in ...)
    
    232
    -  We simply use the le_subst substitution in types/coercions only, when
    
    233
    -  checking for equality.
    
    234
    -
    
    235
    -* We still need to check that Id occurrences are bound by some
    
    236
    -  enclosing binding.  We do /not/ use the InScopeSet for the le_subst
    
    237
    -  for this purpose -- it contains only TyCoVars.  Instead we have a separate
    
    238
    -  le_ids for the in-scope Id binders.
    
    239
    -
    
    240
    -Sigh.  We might want to explore getting rid of type-let!
    
    182
    +We check this invariant in lintType.
    
    241 183
     
    
    242 184
     Note [Bad unsafe coercion]
    
    243 185
     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -564,11 +506,11 @@ Check a core binding, returning the list of variables bound.
    564 506
     -- Let
    
    565 507
     
    
    566 508
     lintRecBindings :: TopLevelFlag -> [(Id, CoreExpr)]
    
    567
    -                -> ([OutId] -> LintM a) -> LintM (a, [UsageEnv])
    
    509
    +                -> LintM a -> LintM (a, [UsageEnv])
    
    568 510
     lintRecBindings top_lvl pairs thing_inside
    
    569
    -  = lintIdBndrs top_lvl bndrs $ \ bndrs' ->
    
    570
    -    do { ues <- zipWithM lint_pair bndrs' rhss
    
    571
    -       ; a <- thing_inside bndrs'
    
    511
    +  = lintIdBndrs top_lvl bndrs $
    
    512
    +    do { ues <- zipWithM lint_pair bndrs rhss
    
    513
    +       ; a <- thing_inside
    
    572 514
            ; return (a, ues) }
    
    573 515
       where
    
    574 516
         (bndrs, rhss) = unzip pairs
    
    ... ... @@ -578,14 +520,14 @@ lintRecBindings top_lvl pairs thing_inside
    578 520
                ; lintLetBind top_lvl Recursive bndr' rhs rhs_ty
    
    579 521
                ; return ue }
    
    580 522
     
    
    581
    -lintLetBody :: LintLocInfo -> [OutId] -> CoreExpr -> LintM (OutType, UsageEnv)
    
    523
    +lintLetBody :: LintLocInfo -> [Id] -> CoreExpr -> LintM (Type, UsageEnv)
    
    582 524
     lintLetBody loc bndrs body
    
    583 525
       = do { (body_ty, body_ue) <- addLoc loc (lintCoreExpr body)
    
    584 526
            ; mapM_ (lintJoinBndrType body_ty) bndrs
    
    585 527
            ; return (body_ty, body_ue) }
    
    586 528
     
    
    587
    -lintLetBind :: TopLevelFlag -> RecFlag -> OutId
    
    588
    -              -> CoreExpr -> OutType -> LintM ()
    
    529
    +lintLetBind :: TopLevelFlag -> RecFlag -> Id
    
    530
    +            -> CoreExpr -> Type -> LintM ()
    
    589 531
     -- Binder's type, and the RHS, have already been linted
    
    590 532
     -- This function checks other invariants
    
    591 533
     lintLetBind top_lvl rec_flag binder rhs rhs_ty
    
    ... ... @@ -676,7 +618,7 @@ lintLetBind top_lvl rec_flag binder rhs rhs_ty
    676 618
     -- join point.
    
    677 619
     --
    
    678 620
     -- See Note [Checking StaticPtrs].
    
    679
    -lintRhs :: Id -> CoreExpr -> LintM (OutType, UsageEnv)
    
    621
    +lintRhs :: Id -> CoreExpr -> LintM (Type, UsageEnv)
    
    680 622
     -- NB: the Id can be Linted or not -- it's only used for
    
    681 623
     --     its OccInfo and join-pointer-hood
    
    682 624
     lintRhs bndr rhs
    
    ... ... @@ -691,7 +633,7 @@ lintRhs _bndr rhs = fmap lf_check_static_ptrs getLintFlags >>= go
    691 633
       where
    
    692 634
         -- Allow occurrences of 'makeStatic' at the top-level but produce errors
    
    693 635
         -- otherwise.
    
    694
    -    go :: StaticPtrCheck -> LintM (OutType, UsageEnv)
    
    636
    +    go :: StaticPtrCheck -> LintM (Type, UsageEnv)
    
    695 637
         go AllowAtTopLevel
    
    696 638
           | (binders0, rhs') <- collectTyBinders rhs
    
    697 639
           , Just (fun, t, info, e) <- collectMakeStaticArgs rhs'
    
    ... ... @@ -708,7 +650,7 @@ lintRhs _bndr rhs = fmap lf_check_static_ptrs getLintFlags >>= go
    708 650
     
    
    709 651
     -- | Lint the RHS of a join point with expected join arity of @n@ (see Note
    
    710 652
     -- [Join points] in "GHC.Core").
    
    711
    -lintJoinLams :: JoinArity -> Maybe Id -> CoreExpr -> LintM (OutType, UsageEnv)
    
    653
    +lintJoinLams :: JoinArity -> Maybe Id -> CoreExpr -> LintM (Type, UsageEnv)
    
    712 654
     lintJoinLams join_arity enforce rhs
    
    713 655
       = go join_arity rhs
    
    714 656
       where
    
    ... ... @@ -896,13 +838,8 @@ suspicious and worth investigating if you have a seg-fault or bizarre behaviour.
    896 838
     ************************************************************************
    
    897 839
     -}
    
    898 840
     
    
    899
    -lintCoreExpr :: InExpr -> LintM (OutType, UsageEnv)
    
    900
    --- The returned type has the substitution from the monad
    
    901
    --- already applied to it:
    
    902
    ---      lintCoreExpr e subst = exprType (subst e)
    
    903
    ---
    
    904
    --- The returned "type" can be a kind, if the expression is (Type ty)
    
    905
    -
    
    841
    +lintCoreExpr :: CoreExpr -> LintM (Type, UsageEnv)
    
    842
    +-- The returned type is the type of the expression
    
    906 843
     -- If you edit this function, you may need to update the GHC formalism
    
    907 844
     -- See Note [GHC Formalism]
    
    908 845
     
    
    ... ... @@ -929,7 +866,7 @@ lintCoreExpr (Cast expr co)
    929 866
     
    
    930 867
            ; lintCoercion co
    
    931 868
            ; lintRole co Representational (coercionRole co)
    
    932
    -       ; Pair from_ty to_ty <- substCoKindM co
    
    869
    +       ; let Pair from_ty to_ty = coercionKind co
    
    933 870
            ; checkValueType (typeKind to_ty) $
    
    934 871
              text "target of cast" <+> quotes (ppr co)
    
    935 872
            ; ensureEqTys from_ty expr_ty (mkCastErr expr co from_ty expr_ty)
    
    ... ... @@ -944,13 +881,12 @@ lintCoreExpr (Tick tickish expr)
    944 881
     lintCoreExpr (Let (NonRec tv (Type ty)) body)
    
    945 882
       | isTyVar tv
    
    946 883
       =     -- See Note [Linting type lets]
    
    947
    -    do  { ty' <- lintTypeAndSubst ty
    
    948
    -        ; lintTyCoBndr tv              $ \ tv' ->
    
    949
    -    do  { addLoc (RhsOf tv) $ lintTyKind tv' ty'
    
    884
    +    do  { lintType ty
    
    885
    +        ; lintTyCoBndr tv              $
    
    886
    +    do  { addLoc (RhsOf tv) $ lintTyKind tv ty
    
    950 887
                     -- Now extend the substitution so we
    
    951 888
                     -- take advantage of it in the body
    
    952
    -        ; -- extendTvSubstL tv ty' $
    
    953
    -          addLoc (BodyOfLet tv) $
    
    889
    +        ; addLoc (BodyOfLet tv) $
    
    954 890
               lintCoreExpr body } }
    
    955 891
     
    
    956 892
     lintCoreExpr (Let (NonRec bndr rhs) body)
    
    ... ... @@ -960,10 +896,10 @@ lintCoreExpr (Let (NonRec bndr rhs) body)
    960 896
     
    
    961 897
               -- See Note [Multiplicity of let binders] in Var
    
    962 898
              -- Now lint the binder
    
    963
    -       ; lintBinder LetBind bndr $ \bndr' ->
    
    964
    -    do { lintLetBind NotTopLevel NonRecursive bndr' rhs rhs_ty
    
    965
    -       ; addAliasUE bndr' let_ue $
    
    966
    -         lintLetBody (BodyOfLet bndr') [bndr'] body } }
    
    899
    +       ; lintBinder LetBind bndr $
    
    900
    +    do { lintLetBind NotTopLevel NonRecursive bndr rhs rhs_ty
    
    901
    +       ; addAliasUE bndr let_ue $
    
    902
    +         lintLetBody (BodyOfLet bndr) [bndr] body } }
    
    967 903
     
    
    968 904
       | otherwise
    
    969 905
       = failWithL (mkLetErr bndr rhs)       -- Not quite accurate
    
    ... ... @@ -982,8 +918,8 @@ lintCoreExpr e@(Let (Rec pairs) body)
    982 918
     
    
    983 919
               -- See Note [Multiplicity of let binders] in Var
    
    984 920
             ; ((body_type, body_ue), ues) <-
    
    985
    -            lintRecBindings NotTopLevel pairs $ \ bndrs' ->
    
    986
    -            lintLetBody (BodyOfLetRec bndrs') bndrs' body
    
    921
    +            lintRecBindings NotTopLevel pairs $
    
    922
    +            lintLetBody (BodyOfLetRec bndrs) bndrs body
    
    987 923
             ; return (body_type, body_ue  `addUE` scaleUE ManyTy (foldr1WithDefault zeroUE addUE ues)) }
    
    988 924
       where
    
    989 925
         bndrs = map fst pairs
    
    ... ... @@ -995,7 +931,7 @@ lintCoreExpr e@(App _ _)
    995 931
         -- N.B. we may have an over-saturated application of the form:
    
    996 932
         --   runRW (\s -> \x -> ...) y
    
    997 933
       , ty_arg1 : ty_arg2 : cont_arg : rest <- args
    
    998
    -  = do { let lint_rw_cont :: CoreArg -> Mult -> UsageEnv -> LintM (OutType, UsageEnv)
    
    934
    +  = do { let lint_rw_cont :: CoreArg -> Mult -> UsageEnv -> LintM (Type, UsageEnv)
    
    999 935
                  lint_rw_cont expr@(Lam _ _) mult fun_ue
    
    1000 936
                     = do { (arg_ty, arg_ue) <- lintJoinLams 1 (Just fun) expr
    
    1001 937
                          ; let app_ue = addUE fun_ue (scaleUE mult arg_ue)
    
    ... ... @@ -1045,53 +981,42 @@ lintCoreExpr (Type ty)
    1045 981
     lintCoreExpr (Coercion co)
    
    1046 982
       -- See Note [Coercions in terms]
    
    1047 983
       = do { addLoc (InCo co) $ lintCoercion co
    
    1048
    -       ; ty <- substTyM (coercionType co)
    
    984
    +       ; let ty = coercionType co
    
    1049 985
            ; return (ty, zeroUE) }
    
    1050 986
     
    
    1051 987
     ----------------------
    
    1052
    -lintIdOcc :: InId -> Int -- Number of arguments (type or value) being passed
    
    1053
    -          -> LintM (OutType, UsageEnv) -- returns type of the *variable*
    
    1054
    -lintIdOcc in_id nargs
    
    1055
    -  = addLoc (OccOf in_id) $
    
    1056
    -    do  { checkL (isNonCoVarId in_id)
    
    1057
    -                 (text "Non term variable" <+> ppr in_id)
    
    988
    +lintIdOcc :: Id -> Int -- Number of arguments (type or value) being passed
    
    989
    +          -> LintM (Type, UsageEnv) -- returns type of the *variable*
    
    990
    +lintIdOcc id nargs
    
    991
    +  = addLoc (OccOf id) $
    
    992
    +    do  { checkL (isNonCoVarId id)
    
    993
    +                 (text "Non term variable" <+> ppr id)
    
    1058 994
                      -- See GHC.Core Note [Variable occurrences in Core]
    
    1059 995
     
    
    1060
    -        -- Check that the type of the occurrence is the same
    
    1061
    -        -- as the type of the binding site.  The inScopeIds are
    
    1062
    -        -- /un-substituted/, so this checks that the occurrence type
    
    1063
    -        -- is identical to the binder type.
    
    1064
    -        -- This makes things much easier for things like:
    
    1065
    -        --    /\a. \(x::Maybe a). /\a. ...(x::Maybe a)...
    
    1066
    -        -- The "::Maybe a" on the occurrence is referring to the /outer/ a.
    
    1067
    -        -- If we compared /substituted/ types we'd risk comparing
    
    1068
    -        -- (Maybe a) from the binding site with bogus (Maybe a1) from
    
    1069
    -        -- the occurrence site.  Comparing un-substituted types finesses
    
    1070
    -        -- this altogether
    
    1071
    -        ; out_ty <- lintVarOcc in_id
    
    996
    +        ; lintVarOcc id
    
    1072 997
     
    
    1073 998
               -- Check for a nested occurrence of the StaticPtr constructor.
    
    1074 999
               -- See Note [Checking StaticPtrs].
    
    1075 1000
             ; when (nargs /= 0) $
    
    1076
    -          checkL (idName in_id /= makeStaticName) $
    
    1001
    +          checkL (idName id /= makeStaticName) $
    
    1077 1002
               text "Found makeStatic nested in an expression"
    
    1078 1003
     
    
    1079
    -        ; checkDeadIdOcc in_id
    
    1004
    +        ; checkDeadIdOcc id
    
    1080 1005
     
    
    1081
    -        ; case isDataConId_maybe in_id of
    
    1006
    +        ; case isDataConId_maybe id of
    
    1082 1007
                  Nothing -> return ()
    
    1083 1008
                  Just dc -> checkTypeDataConOcc "expression" dc
    
    1084 1009
     
    
    1085
    -        ; checkJoinOcc in_id nargs
    
    1086
    -        ; usage <- varCallSiteUsage in_id
    
    1010
    +        ; checkJoinOcc id nargs
    
    1011
    +        ; usage <- varCallSiteUsage id
    
    1087 1012
     
    
    1088
    -        ; return (out_ty, usage) }
    
    1013
    +        ; return (idType id, usage) }
    
    1089 1014
     
    
    1090 1015
     
    
    1091 1016
     
    
    1092 1017
     lintCoreFun :: CoreExpr
    
    1093 1018
                 -> Int                       -- Number of arguments (type or val) being passed
    
    1094
    -            -> LintM (OutType, UsageEnv) -- Returns type of the *function*
    
    1019
    +            -> LintM (Type, UsageEnv) -- Returns type of the *function*
    
    1095 1020
     lintCoreFun (Var var) nargs
    
    1096 1021
       = lintIdOcc var nargs
    
    1097 1022
     
    
    ... ... @@ -1109,10 +1034,10 @@ lintCoreFun expr nargs
    1109 1034
     lintLambda :: Var -> LintM (Type, UsageEnv) -> LintM (Type, UsageEnv)
    
    1110 1035
     lintLambda var lintBody =
    
    1111 1036
         addLoc (LambdaBodyOf var) $
    
    1112
    -    lintBinder LambdaBind var $ \ var' ->
    
    1037
    +    lintBinder LambdaBind var $
    
    1113 1038
         do { (body_ty, ue) <- lintBody
    
    1114
    -       ; ue' <- checkLinearity ue var'
    
    1115
    -       ; return (mkLamType var' body_ty, ue') }
    
    1039
    +       ; ue' <- checkLinearity ue var
    
    1040
    +       ; return (mkLamType var body_ty, ue') }
    
    1116 1041
     ------------------
    
    1117 1042
     checkDeadIdOcc :: Id -> LintM ()
    
    1118 1043
     -- Occurrences of an Id should never be dead....
    
    ... ... @@ -1126,8 +1051,8 @@ checkDeadIdOcc id
    1126 1051
       = return ()
    
    1127 1052
     
    
    1128 1053
     ------------------
    
    1129
    -lintJoinBndrType :: OutType -- Type of the body
    
    1130
    -                 -> OutId   -- Possibly a join Id
    
    1054
    +lintJoinBndrType :: Type -- Type of the body
    
    1055
    +                 -> Id   -- Possibly a join Id
    
    1131 1056
                      -> LintM ()
    
    1132 1057
     -- Checks that the return type of a join Id matches the body
    
    1133 1058
     -- E.g. join j x = rhs in body
    
    ... ... @@ -1458,23 +1383,24 @@ subtype of the required type, as one would expect.
    1458 1383
     -- Takes the functions type and arguments as argument.
    
    1459 1384
     -- Returns the *result* of applying the function to arguments.
    
    1460 1385
     -- e.g. f :: Int -> Bool -> Int would return `Int` as result type.
    
    1461
    -lintCoreArgs  :: (OutType, UsageEnv) -> [InExpr] -> LintM (OutType, UsageEnv)
    
    1386
    +lintCoreArgs  :: (Type, UsageEnv) -> [CoreExpr] -> LintM (Type, UsageEnv)
    
    1462 1387
     lintCoreArgs (fun_ty, fun_ue) args
    
    1463 1388
       = lintApp (text "expression")
    
    1464 1389
                   lintTyArg lintValArg fun_ty args fun_ue
    
    1465 1390
     
    
    1466
    -lintTyArg :: InExpr -> LintM OutType
    
    1391
    +lintTyArg :: CoreExpr -> LintM Type
    
    1467 1392
     
    
    1468 1393
     -- Type argument
    
    1469 1394
     lintTyArg (Type arg_ty)
    
    1470 1395
       = do { checkL (not (isCoercionTy arg_ty))
    
    1471 1396
                     (text "Unnecessary coercion-to-type injection:"
    
    1472 1397
                       <+> ppr arg_ty)
    
    1473
    -       ; lintTypeAndSubst arg_ty }
    
    1398
    +       ; lintType arg_ty
    
    1399
    +       ; return arg_ty }
    
    1474 1400
     lintTyArg arg
    
    1475 1401
       = failWithL (hang (text "Expected type argument but found") 2 (ppr arg))
    
    1476 1402
     
    
    1477
    -lintValArg  :: InExpr -> Mult -> UsageEnv -> LintM (OutType, UsageEnv)
    
    1403
    +lintValArg  :: CoreExpr -> Mult -> UsageEnv -> LintM (Type, UsageEnv)
    
    1478 1404
     lintValArg arg mult fun_ue
    
    1479 1405
       = do { (arg_ty, arg_ue) <- markAllJoinsBad $ lintCoreExpr arg
    
    1480 1406
                -- See Note [Representation polymorphism invariants] in GHC.Core
    
    ... ... @@ -1494,8 +1420,8 @@ lintValArg arg mult fun_ue
    1494 1420
     -----------------
    
    1495 1421
     lintAltBinders :: UsageEnv
    
    1496 1422
                    -> Var         -- Case binder
    
    1497
    -               -> OutType     -- Scrutinee type
    
    1498
    -               -> OutType     -- Constructor type
    
    1423
    +               -> Type     -- Scrutinee type
    
    1424
    +               -> Type     -- Constructor type
    
    1499 1425
                    -> [(Mult, OutVar)]    -- Binders
    
    1500 1426
                    -> LintM UsageEnv
    
    1501 1427
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1545,7 +1471,7 @@ checkCaseLinearity ue case_bndr var_w bndr = do
    1545 1471
     
    
    1546 1472
     
    
    1547 1473
     -----------------
    
    1548
    -lintTyApp :: OutType -> OutType -> LintM OutType
    
    1474
    +lintTyApp :: Type -> Type -> LintM Type
    
    1549 1475
     lintTyApp fun_ty arg_ty
    
    1550 1476
       | Just (tv,body_ty) <- splitForAllTyVar_maybe fun_ty
    
    1551 1477
       = do  { lintTyKind tv arg_ty
    
    ... ... @@ -1563,8 +1489,8 @@ lintTyApp fun_ty arg_ty
    1563 1489
     -- | @lintValApp arg fun_ty arg_ty@ lints an application of @fun arg@
    
    1564 1490
     -- where @fun :: fun_ty@ and @arg :: arg_ty@, returning the type of the
    
    1565 1491
     -- application.
    
    1566
    -lintValApp :: CoreExpr -> OutType -> OutType -> UsageEnv -> UsageEnv
    
    1567
    -           -> LintM (OutType, UsageEnv)
    
    1492
    +lintValApp :: CoreExpr -> Type -> Type -> UsageEnv -> UsageEnv
    
    1493
    +           -> LintM (Type, UsageEnv)
    
    1568 1494
     lintValApp arg fun_ty arg_ty fun_ue arg_ue
    
    1569 1495
       | Just (_, w, arg_ty', res_ty') <- splitFunTy_maybe fun_ty
    
    1570 1496
       = do { ensureEqTys arg_ty' arg_ty (mkAppMsg arg_ty' arg_ty arg)
    
    ... ... @@ -1575,7 +1501,7 @@ lintValApp arg fun_ty arg_ty fun_ue arg_ue
    1575 1501
       where
    
    1576 1502
         err2 = mkNonFunAppMsg fun_ty arg_ty arg
    
    1577 1503
     
    
    1578
    -lintTyKind :: OutTyVar -> OutType -> LintM ()
    
    1504
    +lintTyKind :: OutTyVar -> Type -> LintM ()
    
    1579 1505
     -- Both args have had substitution applied
    
    1580 1506
     
    
    1581 1507
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1595,36 +1521,36 @@ lintTyKind tyvar arg_ty
    1595 1521
     ************************************************************************
    
    1596 1522
     -}
    
    1597 1523
     
    
    1598
    -lintCaseExpr :: CoreExpr -> InId -> InType -> [CoreAlt] -> LintM (OutType, UsageEnv)
    
    1524
    +lintCaseExpr :: CoreExpr -> Id -> Type -> [CoreAlt] -> LintM (Type, UsageEnv)
    
    1599 1525
     lintCaseExpr scrut case_bndr alt_ty alts
    
    1600 1526
       = do { let e = Case scrut case_bndr alt_ty alts   -- Just for error messages
    
    1601 1527
     
    
    1602 1528
            -- Check the scrutinee
    
    1603
    -       ; (scrut_ty', scrut_ue) <- markAllJoinsBad $ lintCoreExpr scrut
    
    1529
    +       ; (scrut_ty, scrut_ue) <- markAllJoinsBad $ lintCoreExpr scrut
    
    1604 1530
                 -- See Note [Join points are less general than the paper]
    
    1605 1531
                 -- in GHC.Core
    
    1606 1532
     
    
    1607
    -       ; alt_ty' <- addLoc (CaseTy scrut) $ lintValueType alt_ty
    
    1533
    +       ; addLoc (CaseTy scrut) $ lintValueType alt_ty
    
    1608 1534
     
    
    1609
    -       ; checkCaseAlts e scrut scrut_ty' alts
    
    1535
    +       ; checkCaseAlts e scrut scrut_ty alts
    
    1610 1536
     
    
    1611 1537
            -- Lint the case-binder. Must do this after linting the scrutinee
    
    1612 1538
            -- because the case-binder isn't in scope in the scrutineex
    
    1613
    -       ; lintBinder CaseBind case_bndr $ \case_bndr' ->
    
    1539
    +       ; lintBinder CaseBind case_bndr $
    
    1614 1540
           -- Don't use lintIdBndr on case_bndr, because unboxed tuple is legitimate
    
    1615 1541
     
    
    1616
    -    do { let case_bndr_ty' = idType case_bndr'
    
    1617
    -             scrut_mult    = idMult case_bndr'
    
    1542
    +    do { let case_bndr_ty = idType case_bndr
    
    1543
    +             scrut_mult   = idMult case_bndr
    
    1618 1544
     
    
    1619
    -       ; ensureEqTys case_bndr_ty' scrut_ty' (mkScrutMsg case_bndr case_bndr_ty' scrut_ty')
    
    1545
    +       ; ensureEqTys case_bndr_ty scrut_ty (mkScrutMsg case_bndr case_bndr_ty scrut_ty)
    
    1620 1546
              -- See GHC.Core Note [Case expression invariants] item (7)
    
    1621 1547
     
    
    1622 1548
            ; -- Check the alternatives
    
    1623
    -       ; alt_ues <- mapM (lintCoreAlt case_bndr' scrut_ty' scrut_mult alt_ty') alts
    
    1549
    +       ; alt_ues <- mapM (lintCoreAlt case_bndr scrut_ty scrut_mult alt_ty) alts
    
    1624 1550
            ; let case_ue = (scaleUE scrut_mult scrut_ue) `addUE` supUEs alt_ues
    
    1625
    -       ; return (alt_ty', case_ue) } }
    
    1551
    +       ; return (alt_ty, case_ue) } }
    
    1626 1552
     
    
    1627
    -checkCaseAlts :: InExpr -> InExpr -> OutType -> [CoreAlt] -> LintM ()
    
    1553
    +checkCaseAlts :: CoreExpr -> CoreExpr -> Type -> [CoreAlt] -> LintM ()
    
    1628 1554
     -- a) Check that the alts are non-empty
    
    1629 1555
     -- b1) Check that the DEFAULT comes first, if it exists
    
    1630 1556
     -- b2) Check that the others are in increasing order
    
    ... ... @@ -1699,17 +1625,17 @@ checkCaseAlts e scrut scrut_ty alts
    1699 1625
         is_lit_alt (Alt (LitAlt _) _  _) = True
    
    1700 1626
         is_lit_alt _                     = False
    
    1701 1627
     
    
    1702
    -lintAltExpr :: CoreExpr -> OutType -> LintM UsageEnv
    
    1628
    +lintAltExpr :: CoreExpr -> Type -> LintM UsageEnv
    
    1703 1629
     lintAltExpr expr ann_ty
    
    1704 1630
       = do { (actual_ty, ue) <- lintCoreExpr expr
    
    1705 1631
            ; ensureEqTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty)
    
    1706 1632
            ; return ue }
    
    1707 1633
              -- See GHC.Core Note [Case expression invariants] item (6)
    
    1708 1634
     
    
    1709
    -lintCoreAlt :: OutId         -- Case binder
    
    1710
    -            -> OutType       -- Type of scrutinee
    
    1635
    +lintCoreAlt :: Id         -- Case binder
    
    1636
    +            -> Type       -- Type of scrutinee
    
    1711 1637
                 -> Mult          -- Multiplicity of scrutinee
    
    1712
    -            -> OutType       -- Type of the alternative
    
    1638
    +            -> Type       -- Type of the alternative
    
    1713 1639
                 -> CoreAlt
    
    1714 1640
                 -> LintM UsageEnv
    
    1715 1641
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1754,11 +1680,11 @@ lintCoreAlt case_bndr scrut_ty _scrut_mult alt_ty alt@(Alt (DataAlt con) args rh
    1754 1680
               ; multiplicities = map binderMult $ fst $ splitPiTys con_payload_ty }
    
    1755 1681
     
    
    1756 1682
             -- And now bring the new binders into scope
    
    1757
    -    ; lintBinders CasePatBind args $ \ args' -> do
    
    1683
    +    ; lintBinders CasePatBind args $ do
    
    1758 1684
           { rhs_ue <- lintAltExpr rhs alt_ty
    
    1759 1685
           ; rhs_ue' <- addLoc (CasePat alt) $
    
    1760 1686
                        lintAltBinders rhs_ue case_bndr scrut_ty con_payload_ty
    
    1761
    -                                  (zipEqual multiplicities  args')
    
    1687
    +                                  (zipEqual multiplicities  args)
    
    1762 1688
           ; return $ deleteUE rhs_ue' case_bndr
    
    1763 1689
           }
    
    1764 1690
        }
    
    ... ... @@ -1803,49 +1729,50 @@ lintLinearBinder doc actual_usage described_usage
    1803 1729
     --  1. Lint var types or kinds (possibly substituting)
    
    1804 1730
     --  2. Add the binder to the in scope set, and if its a coercion var,
    
    1805 1731
     --     we may extend the substitution to reflect its (possibly) new kind
    
    1806
    -lintBinders :: HasDebugCallStack => BindingSite -> [InVar] -> ([OutVar] -> LintM a) -> LintM a
    
    1807
    -lintBinders _    []         linterF = linterF []
    
    1808
    -lintBinders site (var:vars) linterF = lintBinder site var $ \var' ->
    
    1809
    -                                      lintBinders site vars $ \ vars' ->
    
    1810
    -                                      linterF (var':vars')
    
    1732
    +lintBinders :: HasDebugCallStack => BindingSite -> [Var] -> LintM a -> LintM a
    
    1733
    +lintBinders _    []         linterF = linterF
    
    1734
    +lintBinders site (var:vars) linterF = lintBinder site var $
    
    1735
    +                                      lintBinders site vars $
    
    1736
    +                                      linterF
    
    1811 1737
     
    
    1812 1738
     -- If you edit this function, you may need to update the GHC formalism
    
    1813 1739
     -- See Note [GHC Formalism]
    
    1814
    -lintBinder :: HasDebugCallStack => BindingSite -> InVar -> (OutVar -> LintM a) -> LintM a
    
    1740
    +lintBinder :: HasDebugCallStack => BindingSite -> Var -> LintM a -> LintM a
    
    1815 1741
     lintBinder site var linterF
    
    1816 1742
       | isTyCoVar var = lintTyCoBndr var linterF
    
    1817 1743
       | otherwise     = lintIdBndr NotTopLevel site var linterF
    
    1818 1744
     
    
    1819
    -lintTyCoBndr :: HasDebugCallStack => TyCoVar -> (OutTyCoVar -> LintM a) -> LintM a
    
    1745
    +lintTyCoBndr :: HasDebugCallStack => TyCoVar -> LintM a -> LintM a
    
    1820 1746
     lintTyCoBndr tcv thing_inside
    
    1821
    -  = do { tcv_type' <- lintTypeAndSubst (varType tcv)
    
    1822
    -       ; let tcv_kind' = typeKind tcv_type'
    
    1747
    +  = do { let tcv_type = varType tcv
    
    1748
    +             tcv_kind = typeKind tcv_type
    
    1823 1749
     
    
    1750
    +       ; lintType (varType tcv)
    
    1824 1751
              -- See (FORALL1) and (FORALL2) in GHC.Core.Type
    
    1825 1752
            ; if (isTyVar tcv)
    
    1826 1753
              then -- Check that in (forall (a:ki). blah) we have ki:Type
    
    1827
    -              lintL (isLiftedTypeKind tcv_kind') $
    
    1754
    +              lintL (isLiftedTypeKind tcv_kind) $
    
    1828 1755
                   hang (text "TyVar whose kind does not have kind Type:")
    
    1829
    -                 2 (ppr tcv <+> dcolon <+> ppr tcv_type' <+> dcolon <+> ppr tcv_kind')
    
    1756
    +                 2 (ppr tcv <+> dcolon <+> ppr tcv_type <+> dcolon <+> ppr tcv_kind)
    
    1830 1757
              else -- Check that in (forall (cv::ty). blah),
    
    1831 1758
                   -- then ty looks like (t1 ~# t2)
    
    1832
    -              lintL (isCoVarType tcv_type') $
    
    1759
    +              lintL (isCoVarType tcv_type) $
    
    1833 1760
                   text "CoVar with non-coercion type:" <+> pprTyVar tcv
    
    1834 1761
     
    
    1835
    -       ; addInScopeTyCoVar tcv tcv_type' thing_inside }
    
    1762
    +       ; addInScopeTyCoVar tcv thing_inside }
    
    1836 1763
     
    
    1837
    -lintIdBndrs :: forall a. TopLevelFlag -> [InId] -> ([OutId] -> LintM a) -> LintM a
    
    1764
    +lintIdBndrs :: forall a. TopLevelFlag -> [Id] -> LintM a -> LintM a
    
    1838 1765
     lintIdBndrs top_lvl ids thing_inside
    
    1839 1766
       = go ids thing_inside
    
    1840 1767
       where
    
    1841
    -    go :: [Id] -> ([Id] -> LintM a) -> LintM a
    
    1842
    -    go []       thing_inside = thing_inside []
    
    1843
    -    go (id:ids) thing_inside = lintIdBndr top_lvl LetBind id  $ \id' ->
    
    1844
    -                               go ids                         $ \ids' ->
    
    1845
    -                               thing_inside (id' : ids')
    
    1768
    +    go :: [Id] -> LintM a -> LintM a
    
    1769
    +    go []       thing_inside = thing_inside
    
    1770
    +    go (id:ids) thing_inside = lintIdBndr top_lvl LetBind id  $
    
    1771
    +                               go ids                         $
    
    1772
    +                               thing_inside
    
    1846 1773
     
    
    1847 1774
     lintIdBndr :: TopLevelFlag -> BindingSite
    
    1848
    -           -> InVar -> (OutVar -> LintM a) -> LintM a
    
    1775
    +           -> Var -> LintM a -> LintM a
    
    1849 1776
     -- Do substitution on the type of a binder and add the var with this
    
    1850 1777
     -- new type to the in-scope set of the second argument
    
    1851 1778
     -- ToDo: lint its rules
    
    ... ... @@ -1885,9 +1812,9 @@ lintIdBndr top_lvl bind_site id thing_inside
    1885 1812
            ; lintL (not (bind_site == LambdaBind && isEvaldUnfolding (idUnfolding id)))
    
    1886 1813
                     (text "Lambda binder with value or OtherCon unfolding.")
    
    1887 1814
     
    
    1888
    -       ; out_ty <- addLoc (IdTy id) (lintValueType id_ty)
    
    1815
    +       ; addLoc (IdTy id) (lintValueType id_ty)
    
    1889 1816
     
    
    1890
    -       ; addInScopeId id out_ty thing_inside }
    
    1817
    +       ; addInScopeId id thing_inside }
    
    1891 1818
       where
    
    1892 1819
         id_ty = idType id
    
    1893 1820
     
    
    ... ... @@ -1907,8 +1834,8 @@ lintIdBndr top_lvl bind_site id thing_inside
    1907 1834
     {- Note [Linting types and coercions]
    
    1908 1835
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1909 1836
     Notice that
    
    1910
    -   lintType     :: InType     -> LintM ()
    
    1911
    -   lintCoercion :: InCoercion -> LintM ()
    
    1837
    +   lintType     :: Type     -> LintM ()
    
    1838
    +   lintCoercion :: Coercion -> LintM ()
    
    1912 1839
     Neither returns anything.
    
    1913 1840
     
    
    1914 1841
     If you need the kind of the type, then do `typeKind` and then apply
    
    ... ... @@ -1921,48 +1848,41 @@ and then take the kind, becaues the kind is usually smaller.
    1921 1848
     
    
    1922 1849
     Note: you might wonder if we should apply the same logic to expressions.
    
    1923 1850
     Why do we have
    
    1924
    -  lintExpr :: InExpr -> LintM OutType
    
    1851
    +  lintExpr :: CoreExpr -> LintM Type
    
    1925 1852
     Partly inertia; but also taking the type of an expresison involve looking
    
    1926 1853
     down a deep chain of let's, whereas that is not true of taking the kind
    
    1927 1854
     of a type.  It'd be worth an experiment though.
    
    1928 1855
     
    
    1929 1856
     Historical note: in the olden days we had
    
    1930
    -   lintType :: InType -> LintM OutType
    
    1931
    -but that burned a huge amount of allocation building an OutType that was
    
    1857
    +   lintType :: Type -> LintM Type
    
    1858
    +but that burned a huge amount of allocation building an Type that was
    
    1932 1859
     often discarded, or used only to get its kind.
    
    1933 1860
     
    
    1934 1861
     I also experimented with
    
    1935
    -   lintType :: InType -> LintM OutKind
    
    1862
    +   lintType :: Type -> LintM Kind
    
    1936 1863
     but that too was slower.  It is also much simpler to return ()!  If we
    
    1937 1864
     return the kind we have to duplicate the logic in `typeKind`; and it is
    
    1938 1865
     much worse for coercions.
    
    1939 1866
     -}
    
    1940 1867
     
    
    1941
    -lintValueType :: Type -> LintM OutType
    
    1868
    +lintValueType :: Type -> LintM ()
    
    1942 1869
     -- Types only, not kinds
    
    1943 1870
     -- Check the type, and apply the substitution to it
    
    1944 1871
     -- See Note [Linting type lets]
    
    1945 1872
     lintValueType ty
    
    1946 1873
       = addLoc (InType ty) $
    
    1947
    -    do  { ty' <- lintTypeAndSubst ty
    
    1948
    -        ; let sk = typeKind ty'
    
    1874
    +    do  { lintType ty
    
    1875
    +        ; let sk = typeKind ty
    
    1949 1876
             ; lintL (isTYPEorCONSTRAINT sk) $
    
    1950 1877
               hang (text "Ill-kinded type:" <+> ppr ty)
    
    1951
    -             2 (text "has kind:" <+> ppr sk)
    
    1952
    -        ; return ty' }
    
    1878
    +             2 (text "has kind:" <+> ppr sk)}
    
    1953 1879
     
    
    1954 1880
     checkTyCon :: TyCon -> LintM ()
    
    1955 1881
     checkTyCon tc
    
    1956 1882
       = checkL (not (isTcTyCon tc)) (text "Found TcTyCon:" <+> ppr tc)
    
    1957 1883
     
    
    1958 1884
     -------------------
    
    1959
    -lintTypeAndSubst :: InType -> LintM OutType
    
    1960
    -lintTypeAndSubst ty = do { lintType ty; substTyM ty }
    
    1961
    -           -- In GHCi we may lint an expression with a free
    
    1962
    -           -- type variable.  Then it won't be in the
    
    1963
    -           -- substitution, but it should be in scope
    
    1964
    -
    
    1965
    -lintType :: InType -> LintM ()
    
    1885
    +lintType :: Type -> LintM ()
    
    1966 1886
     -- See Note [Linting types and coercions]
    
    1967 1887
     --
    
    1968 1888
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1972,8 +1892,7 @@ lintType (TyVarTy tv)
    1972 1892
       = failWithL (mkBadTyVarMsg tv)
    
    1973 1893
     
    
    1974 1894
       | otherwise
    
    1975
    -  = do { _ <- lintVarOcc tv
    
    1976
    -       ; return () }
    
    1895
    +  = lintVarOcc tv
    
    1977 1896
     
    
    1978 1897
     lintType ty@(AppTy t1 t2)
    
    1979 1898
       | TyConApp {} <- t1
    
    ... ... @@ -1981,7 +1900,7 @@ lintType ty@(AppTy t1 t2)
    1981 1900
       | otherwise
    
    1982 1901
       = do { let (fun_ty, arg_tys) = collect t1 [t2]
    
    1983 1902
            ; lintType fun_ty
    
    1984
    -       ; fun_kind <- substTyM (typeKind fun_ty)
    
    1903
    +       ; let fun_kind = typeKind fun_ty
    
    1985 1904
            ; lint_ty_app ty fun_kind arg_tys }
    
    1986 1905
       where
    
    1987 1906
         collect (AppTy f a) as = collect f (a:as)
    
    ... ... @@ -2013,21 +1932,21 @@ lintType ty@(FunTy af tw t1 t2)
    2013 1932
     lintType ty@(ForAllTy {})
    
    2014 1933
       = go [] ty
    
    2015 1934
       where
    
    2016
    -    go :: [OutTyCoVar] -> InType -> LintM ()
    
    1935
    +    go :: [OutTyCoVar] -> Type -> LintM ()
    
    2017 1936
         -- Loop, collecting the forall-binders
    
    2018 1937
         go tcvs ty@(ForAllTy (Bndr tcv _) body_ty)
    
    2019 1938
           | not (isTyCoVar tcv)
    
    2020 1939
           = failWithL (text "Non-TyVar or Non-CoVar bound in type:" <+> ppr ty)
    
    2021 1940
     
    
    2022 1941
           | otherwise
    
    2023
    -      = lintTyCoBndr tcv $ \tcv' ->
    
    1942
    +      = lintTyCoBndr tcv $
    
    2024 1943
             do { -- See GHC.Core.TyCo.Rep Note [Unused coercion variable in ForAllTy]
    
    2025 1944
                  -- Suspicious because it works on InTyCoVar; c.f. ForAllCo
    
    2026 1945
                  when (isCoVar tcv) $
    
    2027 1946
                  lintL (anyFreeVarsOfType (== tcv) body_ty) $
    
    2028 1947
                  text "Covar does not occur in the body:" <+> (ppr tcv $$ ppr body_ty)
    
    2029 1948
     
    
    2030
    -           ; go (tcv' : tcvs) body_ty }
    
    1949
    +           ; go (tcv : tcvs) body_ty }
    
    2031 1950
     
    
    2032 1951
         go tcvs body_ty
    
    2033 1952
           = do { lintType body_ty
    
    ... ... @@ -2035,7 +1954,7 @@ lintType ty@(ForAllTy {})
    2035 1954
     
    
    2036 1955
     lintType (CastTy ty co)
    
    2037 1956
       = do { lintType ty
    
    2038
    -       ; ty_kind <- substTyM (typeKind ty)
    
    1957
    +       ; let ty_kind = typeKind ty
    
    2039 1958
            ; co_lk <- lintStarCoercion co
    
    2040 1959
            ; ensureEqTys ty_kind co_lk (mkCastTyErr ty co ty_kind co_lk) }
    
    2041 1960
     
    
    ... ... @@ -2043,14 +1962,14 @@ lintType (LitTy l) = lintTyLit l
    2043 1962
     lintType (CoercionTy co) = lintCoercion co
    
    2044 1963
     
    
    2045 1964
     -----------------
    
    2046
    -lintForAllBody :: [OutTyCoVar] -> InType -> LintM ()
    
    1965
    +lintForAllBody :: [OutTyCoVar] -> Type -> LintM ()
    
    2047 1966
     -- Do the checks for the body of a forall-type
    
    2048 1967
     lintForAllBody tcvs body_ty
    
    2049 1968
       = do { -- For type variables, check for skolem escape
    
    2050 1969
              -- See Note [Phantom type variables in kinds] in GHC.Core.Type
    
    2051 1970
              -- The kind of (forall cv. th) is liftedTypeKind, so no
    
    2052 1971
              -- need to check for skolem-escape in the CoVar case
    
    2053
    -         body_kind <- substTyM (typeKind body_ty)
    
    1972
    +         let body_kind = typeKind body_ty
    
    2054 1973
            ; case occCheckExpand tcvs body_kind of
    
    2055 1974
                Just {} -> return ()
    
    2056 1975
                Nothing -> failWithL $
    
    ... ... @@ -2061,7 +1980,7 @@ lintForAllBody tcvs body_ty
    2061 1980
            ; checkValueType body_kind (text "the body of forall:" <+> ppr body_ty) }
    
    2062 1981
     
    
    2063 1982
     -----------------
    
    2064
    -lintTySynFamApp :: Bool -> InType -> TyCon -> [InType] -> LintM ()
    
    1983
    +lintTySynFamApp :: Bool -> Type -> TyCon -> [Type] -> LintM ()
    
    2065 1984
     -- The TyCon is a type synonym or a type family (not a data family)
    
    2066 1985
     -- See Note [Linting type synonym applications]
    
    2067 1986
     -- c.f. GHC.Tc.Validity.check_syn_tc_app
    
    ... ... @@ -2087,21 +2006,21 @@ lintTySynFamApp report_unsat ty tc tys
    2087 2006
     
    
    2088 2007
     -----------------
    
    2089 2008
     -- Confirms that a kind is really TYPE r or Constraint
    
    2090
    -checkValueType :: OutKind -> SDoc -> LintM ()
    
    2009
    +checkValueType :: Kind -> SDoc -> LintM ()
    
    2091 2010
     checkValueType kind doc
    
    2092 2011
       = lintL (isTYPEorCONSTRAINT kind)
    
    2093 2012
               (text "Non-Type-like kind when Type-like expected:" <+> ppr kind $$
    
    2094 2013
                text "when checking" <+> doc)
    
    2095 2014
     
    
    2096 2015
     -----------------
    
    2097
    -lintArrow :: SDoc -> FunTyFlag -> InType -> InType -> InType -> LintM ()
    
    2016
    +lintArrow :: SDoc -> FunTyFlag -> Type -> Type -> Type -> LintM ()
    
    2098 2017
     -- If you edit this function, you may need to update the GHC formalism
    
    2099 2018
     -- See Note [GHC Formalism]
    
    2100 2019
     lintArrow what af t1 t2 tw  -- Eg lintArrow "type or kind `blah'" k1 k2 kw
    
    2101 2020
                                 -- or lintArrow "coercion `blah'" k1 k2 kw
    
    2102
    -  = do { k1 <- substTyM (typeKind t1)
    
    2103
    -       ; k2 <- substTyM (typeKind t2)
    
    2104
    -       ; kw <- substTyM (typeKind tw)
    
    2021
    +  = do { let k1 = typeKind t1
    
    2022
    +             k2 = typeKind t2
    
    2023
    +             kw = typeKind tw
    
    2105 2024
            ; unless (isTYPEorCONSTRAINT k1) (report (text "argument")     t1 k1)
    
    2106 2025
            ; unless (isTYPEorCONSTRAINT k2) (report (text "result")       t2 k2)
    
    2107 2026
            ; unless (isMultiplicityTy kw)   (report (text "multiplicity") tw kw)
    
    ... ... @@ -2127,29 +2046,29 @@ lintTyLit (StrTyLit _) = return ()
    2127 2046
     lintTyLit (CharTyLit _) = return ()
    
    2128 2047
     
    
    2129 2048
     -----------------
    
    2130
    -lint_ty_app :: InType -> OutKind -> [InType] -> LintM ()
    
    2049
    +lint_ty_app :: Type -> Kind -> [Type] -> LintM ()
    
    2131 2050
     lint_ty_app ty = lint_tyco_app (text "type" <+> quotes (ppr ty))
    
    2132 2051
     
    
    2133
    -lint_co_app :: HasDebugCallStack => Coercion -> OutKind -> [InType] -> LintM ()
    
    2052
    +lint_co_app :: HasDebugCallStack => Coercion -> Kind -> [Type] -> LintM ()
    
    2134 2053
     lint_co_app co = lint_tyco_app (text "coercion" <+> quotes (ppr co))
    
    2135 2054
     
    
    2136
    -lint_tyco_app :: SDoc -> OutKind -> [InType] -> LintM ()
    
    2055
    +lint_tyco_app :: SDoc -> Kind -> [Type] -> LintM ()
    
    2137 2056
     lint_tyco_app msg fun_kind arg_tys
    
    2138 2057
         -- See Note [Avoiding compiler perf traps when constructing error messages.]
    
    2139
    -  = do { _ <- lintApp msg (\ty     -> do { lintType ty; substTyM ty })
    
    2140
    -                            (\ty _ _ -> do { lintType ty; ki <- substTyM (typeKind ty); return (ki,()) })
    
    2141
    -                            fun_kind arg_tys ()
    
    2058
    +  = do { _ <- lintApp msg (\ty     -> do { lintType ty; return ty })
    
    2059
    +                          (\ty _ _ -> do { lintType ty; return (typeKind ty,()) })
    
    2060
    +                          fun_kind arg_tys ()
    
    2142 2061
            ; return () }
    
    2143 2062
     
    
    2144 2063
     ----------------
    
    2145 2064
     lintApp :: forall in_a acc. Outputable in_a =>
    
    2146 2065
                  SDoc
    
    2147
    -          -> (in_a -> LintM OutType)                        -- Lint the thing and return its value
    
    2148
    -          -> (in_a -> Mult -> acc -> LintM (OutKind, acc))  -- Lint the thing and return its type
    
    2149
    -          -> OutType
    
    2066
    +          -> (in_a -> LintM Type)                        -- Lint the thing and return its value
    
    2067
    +          -> (in_a -> Mult -> acc -> LintM (Kind, acc))  -- Lint the thing and return its type
    
    2068
    +          -> Type
    
    2150 2069
               -> [in_a]                               -- The arguments, always "In" things
    
    2151 2070
               -> acc                                  -- Used (only) for UsageEnv in /term/ applications
    
    2152
    -          -> LintM (OutType,acc)
    
    2071
    +          -> LintM (Type,acc)
    
    2153 2072
     -- lintApp is a performance-critical function, which deals with multiple
    
    2154 2073
     -- applications such as  (/\a./\b./\c. expr) @ta @tb @tc
    
    2155 2074
     -- When returning the type of this expression we want to avoid substituting a:=ta,
    
    ... ... @@ -2174,7 +2093,7 @@ lintApp msg lint_forall_arg lint_arrow_arg !orig_fun_ty all_args acc
    2174 2093
     
    
    2175 2094
              ; let init_subst = mkEmptySubst in_scope
    
    2176 2095
     
    
    2177
    -               go :: Subst -> OutType -> acc -> [in_a] -> LintM (OutType, acc)
    
    2096
    +               go :: Subst -> Type -> acc -> [in_a] -> LintM (Type, acc)
    
    2178 2097
                          -- The Subst applies (only) to the fun_ty
    
    2179 2098
                          -- c.f. GHC.Core.Type.piResultTys, which has a similar loop
    
    2180 2099
     
    
    ... ... @@ -2218,7 +2137,7 @@ lintApp msg lint_forall_arg lint_arrow_arg !orig_fun_ty all_args acc
    2218 2137
     -- explicitly and don't capture them as free variables. Otherwise this binder might
    
    2219 2138
     -- become a thunk that get's allocated in the hot code path.
    
    2220 2139
     -- See Note [Avoiding compiler perf traps when constructing error messages.]
    
    2221
    -lint_app_fail_msg :: (Outputable a2) => SDoc -> OutType -> a2 -> SDoc -> SDoc
    
    2140
    +lint_app_fail_msg :: (Outputable a2) => SDoc -> Type -> a2 -> SDoc -> SDoc
    
    2222 2141
     lint_app_fail_msg msg kfn arg_tys extra
    
    2223 2142
       = vcat [ hang (text "Application error in") 2 msg
    
    2224 2143
              , nest 2 (text "Function type =" <+> ppr kfn)
    
    ... ... @@ -2231,7 +2150,7 @@ lint_app_fail_msg msg kfn arg_tys extra
    2231 2150
     *                                                                      *
    
    2232 2151
     ********************************************************************* -}
    
    2233 2152
     
    
    2234
    -lintCoreRule :: OutVar -> OutType -> CoreRule -> LintM ()
    
    2153
    +lintCoreRule :: OutVar -> Type -> CoreRule -> LintM ()
    
    2235 2154
     lintCoreRule _ _ (BuiltinRule {})
    
    2236 2155
       = return ()  -- Don't bother
    
    2237 2156
     
    
    ... ... @@ -2239,7 +2158,7 @@ lintCoreRule fun fun_ty rule@(Rule { ru_name = name, ru_bndrs = bndrs
    2239 2158
                                        , ru_args = args, ru_rhs = rhs })
    
    2240 2159
       = noMultiplicityChecks $ -- Skip linearity checking for rules
    
    2241 2160
                                -- See Note [Linting linearity]
    
    2242
    -    lintBinders LambdaBind bndrs $ \ _ ->
    
    2161
    +    lintBinders LambdaBind bndrs $
    
    2243 2162
         do { (lhs_ty, _) <- lintCoreArgs (fun_ty, zeroUE) args
    
    2244 2163
            ; (rhs_ty, _) <- case idJoinPointHood fun of
    
    2245 2164
                          JoinPoint join_arity
    
    ... ... @@ -2349,23 +2268,16 @@ which is what used to happen. But that proved tricky and error prone
    2349 2268
     -- lintStarCoercion lints a coercion, confirming that its lh kind and
    
    2350 2269
     -- its rh kind are both *; also ensures that the role is Nominal
    
    2351 2270
     -- Returns the lh kind
    
    2352
    -lintStarCoercion :: InCoercion -> LintM OutType
    
    2271
    +lintStarCoercion :: Coercion -> LintM Type
    
    2353 2272
     lintStarCoercion g
    
    2354 2273
       = do { lintCoercion g
    
    2355
    -       ; Pair t1 t2 <- substCoKindM g
    
    2274
    +       ; let Pair t1 t2 = coercionKind g
    
    2356 2275
            ; checkValueType (typeKind t1) (text "the kind of the left type in" <+> ppr g)
    
    2357 2276
            ; checkValueType (typeKind t2) (text "the kind of the right type in" <+> ppr g)
    
    2358 2277
            ; lintRole g Nominal (coercionRole g)
    
    2359 2278
            ; return t1 }
    
    2360 2279
     
    
    2361
    -substCoKindM :: InCoercion -> LintM (Pair OutType)
    
    2362
    -substCoKindM co
    
    2363
    -  = do { let !(Pair lk rk) = coercionKind co
    
    2364
    -       ; lk' <- substTyM lk
    
    2365
    -       ; rk' <- substTyM rk
    
    2366
    -       ; return (Pair lk' rk') }
    
    2367
    -
    
    2368
    -lintCoercion :: HasDebugCallStack => InCoercion -> LintM ()
    
    2280
    +lintCoercion :: HasDebugCallStack => Coercion -> LintM ()
    
    2369 2281
     -- See Note [Linting types and coercions]
    
    2370 2282
     --
    
    2371 2283
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -2377,7 +2289,7 @@ lintCoercion (CoVarCo cv)
    2377 2289
                       2 (text "With offending type:" <+> ppr (varType cv)))
    
    2378 2290
     
    
    2379 2291
       | otherwise  -- C.f. lintType (TyVarTy tv), which has better docs
    
    2380
    -  = do { _ <- lintVarOcc cv; return () }
    
    2292
    +  = lintVarOcc cv
    
    2381 2293
     
    
    2382 2294
     lintCoercion (Refl ty)          = lintType ty
    
    2383 2295
     lintCoercion (GRefl _r ty MRefl) = lintType ty
    
    ... ... @@ -2385,8 +2297,8 @@ lintCoercion (GRefl _r ty MRefl) = lintType ty
    2385 2297
     lintCoercion (GRefl _r ty (MCo co))
    
    2386 2298
       = do { lintType ty
    
    2387 2299
            ; lintCoercion co
    
    2388
    -       ; tk <- substTyM (typeKind ty)
    
    2389
    -       ; tl <- substTyM (coercionLKind co)
    
    2300
    +       ; let tk = typeKind ty
    
    2301
    +             tl = coercionLKind co
    
    2390 2302
            ; ensureEqTys tk tl $
    
    2391 2303
              hang (text "GRefl coercion kind mis-match:" <+> ppr co)
    
    2392 2304
                 2 (vcat [ppr ty, ppr tk, ppr tl])
    
    ... ... @@ -2419,8 +2331,8 @@ lintCoercion co@(AppCo co1 co2)
    2419 2331
       = do { lintCoercion co1
    
    2420 2332
            ; lintCoercion co2
    
    2421 2333
            ; let !(Pair lt1 rt1) = coercionKind co1
    
    2422
    -       ; lk1 <- substTyM (typeKind lt1)
    
    2423
    -       ; rk1 <- substTyM (typeKind rt1)
    
    2334
    +             lk1 = typeKind lt1
    
    2335
    +             rk1 = typeKind rt1
    
    2424 2336
            ; lint_co_app co lk1 [coercionLKind co2]
    
    2425 2337
            ; lint_co_app co rk1 [coercionRKind co2]
    
    2426 2338
     
    
    ... ... @@ -2437,7 +2349,7 @@ lintCoercion co@(ForAllCo {})
    2437 2349
       = do { _ <- go [] co; return () }
    
    2438 2350
       where
    
    2439 2351
         go :: [OutTyCoVar]   -- Binders in reverse order
    
    2440
    -       -> InCoercion -> LintM Role
    
    2352
    +       -> Coercion -> LintM Role
    
    2441 2353
         go tcvs co@(ForAllCo { fco_tcv = tcv, fco_visL = visL, fco_visR = visR
    
    2442 2354
                              , fco_kind = kind_mco, fco_body = body_co })
    
    2443 2355
           | not (isTyCoVar tcv)
    
    ... ... @@ -2447,15 +2359,15 @@ lintCoercion co@(ForAllCo {})
    2447 2359
           = do { mb_lk <- case kind_mco of
    
    2448 2360
                          MRefl -> return Nothing
    
    2449 2361
                          MCo kind_co -> Just <$> lintStarCoercion kind_co
    
    2450
    -           ; lintTyCoBndr tcv $ \tcv' ->
    
    2362
    +           ; lintTyCoBndr tcv $ 
    
    2451 2363
             do { case mb_lk of
    
    2452 2364
                     Nothing -> return ()
    
    2453
    -                Just lk -> ensureEqTys (varType tcv') lk $
    
    2365
    +                Just lk -> ensureEqTys (varType tcv) lk $
    
    2454 2366
                                text "Kind mis-match in ForallCo" <+> ppr co
    
    2455 2367
     
    
    2456 2368
                -- I'm not very sure about this part, because it traverses body_co
    
    2457 2369
                -- but at least it's on a cold path (a ForallCo for a CoVar)
    
    2458
    -           -- Also it works on InTyCoVar and InCoercion, which is suspect
    
    2370
    +           -- Also it works on InTyCoVar and Coercion, which is suspect
    
    2459 2371
                ; when (isCoVar tcv) $
    
    2460 2372
                  do { lintL (visL == coreTyLamForAllTyFlag && visR == coreTyLamForAllTyFlag) $
    
    2461 2373
                       text "Invalid visibility flags in CoVar ForAllCo" <+> ppr co
    
    ... ... @@ -2464,7 +2376,7 @@ lintCoercion co@(ForAllCo {})
    2464 2376
                       text "Covar can only appear in Refl and GRefl: " <+> ppr co }
    
    2465 2377
                       -- See (FC6) in Note [ForAllCo] in GHC.Core.TyCo.Rep
    
    2466 2378
     
    
    2467
    -           ; role <- go (tcv':tcvs) body_co
    
    2379
    +           ; role <- go (tcv:tcvs) body_co
    
    2468 2380
     
    
    2469 2381
                ; when (role == Nominal) $
    
    2470 2382
                  lintL (visL `eqForAllVis` visR) $
    
    ... ... @@ -2521,8 +2433,8 @@ lintCoercion co@(UnivCo { uco_role = r, uco_prov = prov
    2521 2433
            -- Check the to and from types
    
    2522 2434
            ; lintType ty1
    
    2523 2435
            ; lintType ty2
    
    2524
    -       ; tk1 <- substTyM (typeKind ty1)
    
    2525
    -       ; tk2 <- substTyM (typeKind ty2)
    
    2436
    +       ; let tk1 = typeKind ty1
    
    2437
    +             tk2 = typeKind ty2
    
    2526 2438
     
    
    2527 2439
            ; when (r /= Phantom && isTYPEorCONSTRAINT tk1 && isTYPEorCONSTRAINT tk2)
    
    2528 2440
                   (checkTypes ty1 ty2)
    
    ... ... @@ -2576,8 +2488,8 @@ lintCoercion (SymCo co) = lintCoercion co
    2576 2488
     lintCoercion co@(TransCo co1 co2)
    
    2577 2489
       = do { lintCoercion co1
    
    2578 2490
            ; lintCoercion co2
    
    2579
    -       ; rk1 <- substTyM (coercionRKind co1)
    
    2580
    -       ; lk2 <- substTyM (coercionLKind co2)
    
    2491
    +       ; let rk1 = coercionRKind co1
    
    2492
    +             lk2 = coercionLKind co2
    
    2581 2493
            ; ensureEqTys rk1 lk2
    
    2582 2494
                    (hang (text "Trans coercion mis-match:" <+> ppr co)
    
    2583 2495
                        2 (vcat [ppr (coercionKind co1), ppr (coercionKind co2)]))
    
    ... ... @@ -2585,7 +2497,7 @@ lintCoercion co@(TransCo co1 co2)
    2585 2497
     
    
    2586 2498
     lintCoercion the_co@(SelCo cs co)
    
    2587 2499
       = do { lintCoercion co
    
    2588
    -       ; Pair s t <- substCoKindM co
    
    2500
    +       ; let Pair s t = coercionKind co
    
    2589 2501
     
    
    2590 2502
            ; if -- forall (both TyVar and CoVar)
    
    2591 2503
                 | Just _ <- splitForAllTyCoVar_maybe s
    
    ... ... @@ -2620,7 +2532,7 @@ lintCoercion the_co@(SelCo cs co)
    2620 2532
     
    
    2621 2533
     lintCoercion the_co@(LRCo _lr co)
    
    2622 2534
       = do { lintCoercion co
    
    2623
    -       ; Pair s t <- substCoKindM co
    
    2535
    +       ; let Pair s t = coercionKind co
    
    2624 2536
            ; lintRole co Nominal (coercionRole co)
    
    2625 2537
            ; case (splitAppTy_maybe s, splitAppTy_maybe t) of
    
    2626 2538
                (Just {}, Just {}) -> return ()
    
    ... ... @@ -2634,14 +2546,12 @@ lintCoercion orig_co@(InstCo co arg)
    2634 2546
         go (InstCo co arg) args = do { lintCoercion arg; go co (arg:args) }
    
    2635 2547
         go co              args = do { lintCoercion co
    
    2636 2548
                                      ; let Pair lty rty = coercionKind co
    
    2637
    -                                 ; lty' <- substTyM lty
    
    2638
    -                                 ; rty' <- substTyM rty
    
    2639 2549
                                      ; in_scope <- getInScope
    
    2640 2550
                                      ; let subst = mkEmptySubst in_scope
    
    2641
    -                                 ; go_args (subst, lty') (subst,rty') args }
    
    2551
    +                                 ; go_args (subst, lty) (subst,rty) args }
    
    2642 2552
     
    
    2643 2553
         -------------
    
    2644
    -    go_args :: (Subst, OutType) -> (Subst,OutType) -> [InCoercion]
    
    2554
    +    go_args :: (Subst, Type) -> (Subst,Type) -> [Coercion]
    
    2645 2555
                -> LintM ()
    
    2646 2556
         go_args _ _ []
    
    2647 2557
           = return ()
    
    ... ... @@ -2650,11 +2560,11 @@ lintCoercion orig_co@(InstCo co arg)
    2650 2560
                ; go_args lty1 rty1 args }
    
    2651 2561
     
    
    2652 2562
         -------------
    
    2653
    -    go_arg :: (Subst, OutType) -> (Subst,OutType) -> InCoercion
    
    2654
    -           -> LintM ((Subst,OutType), (Subst,OutType))
    
    2563
    +    go_arg :: (Subst, Type) -> (Subst,Type) -> Coercion
    
    2564
    +           -> LintM ((Subst,Type), (Subst,Type))
    
    2655 2565
         go_arg (lsubst,lty) (rsubst,rty) arg
    
    2656 2566
           = do { lintRole arg Nominal (coercionRole arg)
    
    2657
    -           ; Pair arg_lty arg_rty <- substCoKindM arg
    
    2567
    +           ; let Pair arg_lty arg_rty = coercionKind arg
    
    2658 2568
     
    
    2659 2569
                ; case (splitForAllTyCoVar_maybe lty, splitForAllTyCoVar_maybe rty) of
    
    2660 2570
                   -- forall over tvar
    
    ... ... @@ -2678,11 +2588,11 @@ lintCoercion orig_co@(InstCo co arg)
    2678 2588
     lintCoercion this_co@(AxiomCo ax cos)
    
    2679 2589
       = do { mapM_ lintCoercion cos
    
    2680 2590
            ; lint_roles 0 (coAxiomRuleArgRoles ax) cos
    
    2681
    -       ; prs <- mapM substCoKindM cos
    
    2591
    +       ; let prs = map coercionKind cos
    
    2682 2592
            ; lint_ax ax prs }
    
    2683 2593
     
    
    2684 2594
       where
    
    2685
    -    lint_ax :: CoAxiomRule -> [Pair OutType] -> LintM ()
    
    2595
    +    lint_ax :: CoAxiomRule -> [Pair Type] -> LintM ()
    
    2686 2596
         lint_ax (BuiltInFamRew  bif) prs
    
    2687 2597
           = checkL (isJust (bifrw_proves bif prs))  bad_bif
    
    2688 2598
         lint_ax (BuiltInFamInj bif) prs
    
    ... ... @@ -2770,8 +2680,8 @@ lintBranch this_co fam_tc branch arg_kinds
    2770 2680
       = do { checkL (arg_kinds `equalLength` (ktvs ++ cvs)) $
    
    2771 2681
                     (bad_ax this_co (text "lengths"))
    
    2772 2682
     
    
    2773
    -       ; subst <- getSubst
    
    2774
    -       ; let empty_subst = zapSubst subst
    
    2683
    +       ; in_scope <- getInScope
    
    2684
    +       ; let empty_subst = mkEmptySubst in_scope
    
    2775 2685
            ; _ <- foldlM check_ki (empty_subst, empty_subst)
    
    2776 2686
                                   (zip (ktvs ++ cvs) arg_kinds)
    
    2777 2687
     
    
    ... ... @@ -2896,12 +2806,12 @@ lint_axiom ax@(CoAxiom { co_ax_tc = tc, co_ax_branches = branches
    2896 2806
     lint_branch :: TyCon -> CoAxBranch -> LintM ()
    
    2897 2807
     lint_branch ax_tc (CoAxBranch { cab_tvs = tvs, cab_cvs = cvs
    
    2898 2808
                                   , cab_lhs = lhs_args, cab_rhs = rhs })
    
    2899
    -  = lintBinders LambdaBind (tvs ++ cvs) $ \_ ->
    
    2809
    +  = lintBinders LambdaBind (tvs ++ cvs) $ 
    
    2900 2810
         do { let lhs = mkTyConApp ax_tc lhs_args
    
    2901 2811
            ; lintType lhs
    
    2902 2812
            ; lintType rhs
    
    2903
    -       ; lhs_kind <- substTyM (typeKind lhs)
    
    2904
    -       ; rhs_kind <- substTyM (typeKind rhs)
    
    2813
    +       ; let lhs_kind = typeKind lhs
    
    2814
    +             rhs_kind = typeKind rhs
    
    2905 2815
            ; lintL (not (lhs_kind `typesAreApart` rhs_kind)) $
    
    2906 2816
              hang (text "Inhomogeneous axiom")
    
    2907 2817
                 2 (text "lhs:" <+> ppr lhs <+> dcolon <+> ppr lhs_kind $$
    
    ... ... @@ -2985,35 +2895,26 @@ type LintLevel = Int
    2985 2895
     -- If you edit this type, you may need to update the GHC formalism
    
    2986 2896
     -- See Note [GHC Formalism]
    
    2987 2897
     data LintEnv
    
    2988
    -  = LE { le_flags :: LintFlags       -- Linting the result of this pass
    
    2989
    -       , le_loc   :: [LintLocInfo]   -- Locations
    
    2990
    -
    
    2991
    -       , le_subst :: Subst
    
    2992
    -                  -- Current substitution, for TyCoVars only.
    
    2993
    -                  -- Non-CoVar Ids don't appear in here, not even in the InScopeSet
    
    2994
    -                  -- Used for (a) cloning to avoid shadowing of TyCoVars,
    
    2995
    -                  --              so that eqType works ok
    
    2996
    -                  --          (b) substituting for let-bound tyvars, when we have
    
    2997
    -                  --              (let @a = Int -> Int in ...)
    
    2998
    -
    
    2999
    -       , le_level   :: LintLevel
    
    3000
    -       , le_in_vars :: VarEnv (InVar, OutType, LintLevel)
    
    3001
    -                    -- Maps an InVar (i.e. its unique) to its binding InVar
    
    3002
    -                    --    and to its OutType
    
    3003
    -                    -- /All/ in-scope variables are here (term variables,
    
    3004
    -                    --    type variables, and coercion variables)
    
    3005
    -                    -- Used at an occurrence of the InVar
    
    2898
    +  = LE { le_flags    :: LintFlags       -- Linting the result of this pass
    
    2899
    +       , le_loc      :: [LintLocInfo]   -- Locations
    
    2900
    +       , le_level    :: LintLevel
    
    2901
    +       , le_in_scope :: InScopeSet
    
    2902
    +
    
    2903
    +       , le_in_vars  :: VarEnv (Var, LintLevel)
    
    2904
    +                     -- Maps an Var (i.e. its unique) to its binding Var and level
    
    2905
    +                     -- /All/ in-scope variables are here (term variables,
    
    2906
    +                     --    type variables, and coercion variables)
    
    2907
    +                     -- Used at an occurrence of the Var
    
    3006 2908
     
    
    3007 2909
            , le_joins :: UniqMap Id JoinOcc
    
    3008 2910
                -- ^ Join points in scope that are valid
    
    3009
    -           -- A subset of the InScopeSet in le_subst
    
    3010 2911
                -- See Note [Join points]
    
    3011 2912
     
    
    3012 2913
            , le_ue_aliases :: NameEnv UsageEnv
    
    3013 2914
                  -- See Note [Linting linearity]
    
    3014 2915
                  -- Assigns usage environments to the alias-like binders,
    
    3015 2916
                  -- as found in non-recursive lets.
    
    3016
    -             -- Domain is OutIds
    
    2917
    +             -- Domain is Ids
    
    3017 2918
     
    
    3018 2919
            , le_platform   :: Platform         -- ^ Target platform
    
    3019 2920
            , le_diagOpts   :: DiagOpts         -- ^ Target platform
    
    ... ... @@ -3369,12 +3270,12 @@ initL cfg m
    3369 3270
       where
    
    3370 3271
         vars = l_vars cfg
    
    3371 3272
         init_level = 0
    
    3372
    -    env = LE { le_flags   = l_flags cfg
    
    3373
    -             , le_subst   = mkEmptySubst (mkInScopeSetList vars)
    
    3374
    -             , le_level   = init_level
    
    3375
    -             , le_in_vars = mkVarEnv [ (v,(v, varType v, init_level)) | v <- vars ]
    
    3376
    -             , le_joins   = emptyUniqMap
    
    3377
    -             , le_loc     = []
    
    3273
    +    env = LE { le_flags    = l_flags cfg
    
    3274
    +             , le_level    = init_level
    
    3275
    +             , le_in_vars  = mkVarEnv [ (v,(v, init_level)) | v <- vars ]
    
    3276
    +             , le_in_scope = mkInScopeSetList vars
    
    3277
    +             , le_joins    = emptyUniqMap
    
    3278
    +             , le_loc      = []
    
    3378 3279
                  , le_ue_aliases = emptyNameEnv
    
    3379 3280
                  , le_platform = l_platform cfg
    
    3380 3281
                  , le_diagOpts = l_diagOpts cfg
    
    ... ... @@ -3437,8 +3338,7 @@ addMsg show_context env msgs msg
    3437 3338
        loc_msgs :: [(SrcLoc, SDoc)]  -- Innermost first
    
    3438 3339
        loc_msgs = map dumpLoc (le_loc env)
    
    3439 3340
     
    
    3440
    -   cxt_doc = vcat [ vcat $ reverse $ map snd loc_msgs
    
    3441
    -                  , text "Substitution:" <+> ppr (le_subst env) ]
    
    3341
    +   cxt_doc = vcat $ reverse $ map snd loc_msgs
    
    3442 3342
     
    
    3443 3343
        context | show_context  = cxt_doc
    
    3444 3344
                | otherwise     = whenPprDebug cxt_doc
    
    ... ... @@ -3465,78 +3365,45 @@ inCasePat = LintM $ \ env errs -> fromBoxedLResult (Just (is_case_pat env), errs
    3465 3365
         is_case_pat (LE { le_loc = CasePat {} : _ }) = True
    
    3466 3366
         is_case_pat _other                           = False
    
    3467 3367
     
    
    3468
    -addInScopeId :: InId -> OutType -> (OutId -> LintM a) -> LintM a
    
    3368
    +addInScopeId :: Id -> LintM a -> LintM a
    
    3469 3369
     -- Unlike addInScopeTyCoVar, this function does no cloning; Ids never get cloned
    
    3470
    -addInScopeId in_id out_ty thing_inside
    
    3370
    +addInScopeId id thing_inside
    
    3471 3371
       = LintM $ \ env errs ->
    
    3472
    -    let !(out_id, env') = add env
    
    3473
    -    in unLintM (thing_inside out_id) env' errs
    
    3474
    -
    
    3372
    +    unLintM thing_inside (add env) errs
    
    3475 3373
       where
    
    3476 3374
         add env@(LE { le_level = level, le_in_vars = id_vars, le_joins = valid_joins
    
    3477
    -                , le_ue_aliases = aliases, le_subst = subst })
    
    3478
    -      = (out_id, env1)
    
    3375
    +                , le_ue_aliases = aliases, le_in_scope = in_scope })
    
    3376
    +      = env { le_level = level1, le_in_vars = in_vars'
    
    3377
    +            , le_in_scope = in_scope `extendInScopeSet` id
    
    3378
    +            , le_joins = valid_joins', le_ue_aliases = aliases' }
    
    3479 3379
           where
    
    3480 3380
             level1 = level + 1
    
    3481
    -        env1 = env { le_level = level1, le_in_vars = in_vars'
    
    3482
    -                   , le_joins = valid_joins', le_ue_aliases = aliases' }
    
    3483 3381
     
    
    3484
    -        in_vars' = extendVarEnv id_vars in_id (in_id, out_ty, level1)
    
    3485
    -        aliases' = delFromNameEnv aliases (idName in_id)
    
    3382
    +        in_vars' = extendVarEnv id_vars id (id, level1)
    
    3383
    +        aliases' = delFromNameEnv aliases (idName id)
    
    3486 3384
                -- aliases': when shadowing an alias, we need to make sure the
    
    3487 3385
                -- Id is no longer classified as such. E.g.
    
    3488 3386
                --   let x = <e1> in case x of x { _DEFAULT -> <e2> }
    
    3489 3387
                -- Occurrences of 'x' in e2 shouldn't count as occurrences of e1.
    
    3490 3388
     
    
    3491
    -        -- A very tiny optimisation, not sure if it's really worth it
    
    3492
    -        -- Short-cut when the substitution is a no-op
    
    3493
    -        out_id | isEmptyTCvSubst subst = in_id
    
    3494
    -               | otherwise             = setIdType in_id out_ty
    
    3495
    -
    
    3496 3389
             valid_joins'
    
    3497
    -          | isJoinId out_id = addToUniqMap   valid_joins in_id NormalJoinOcc -- Overwrite with new arity
    
    3498
    -          | otherwise       = delFromUniqMap valid_joins in_id -- Remove any existing binding
    
    3390
    +          | isJoinId id = addToUniqMap   valid_joins id NormalJoinOcc -- Overwrite with new arity
    
    3391
    +          | otherwise   = delFromUniqMap valid_joins id -- Remove any existing binding
    
    3499 3392
     
    
    3500
    -addInScopeTyCoVar :: InTyCoVar -> OutType -> (OutTyCoVar -> LintM a) -> LintM a
    
    3393
    +addInScopeTyCoVar :: TyCoVar -> LintM a -> LintM a
    
    3501 3394
     -- This function clones to avoid shadowing of TyCoVars
    
    3502
    -addInScopeTyCoVar tcv tcv_type thing_inside
    
    3503
    -  = LintM $ \ env@(LE { le_level = level, le_in_vars = in_vars, le_subst = subst }) errs ->
    
    3504
    -    let (tcv', subst') = subst_bndr subst
    
    3505
    -        level' = level + 1
    
    3395
    +addInScopeTyCoVar tcv thing_inside
    
    3396
    +  = LintM $ \ env@(LE { le_level = level, le_in_vars = in_vars
    
    3397
    +                      , le_in_scope = in_scope }) errs ->
    
    3398
    +    let level' = level + 1
    
    3506 3399
             env' = env { le_level = level'
    
    3507
    -                   , le_in_vars = extendVarEnv in_vars tcv (tcv, tcv_type, level')
    
    3508
    -                   , le_subst = subst' }
    
    3509
    -    in unLintM (thing_inside tcv') env' errs
    
    3510
    -  where
    
    3511
    -    subst_bndr subst
    
    3512
    -      = (tcv, subst `extendSubstInScope` tcv)
    
    3513
    -{-
    
    3514
    -      | isEmptyTCvSubst subst                -- No change in kind
    
    3515
    -      , not (tcv `elemInScopeSet` in_scope)  -- Not already in scope
    
    3516
    -      = -- Do not extend the substitution, just the in-scope set
    
    3517
    -        (if (varType tcv `eqType` tcv_type) then (\x->x) else
    
    3518
    -          pprTrace "addInScopeTyCoVar" (
    
    3519
    -            vcat [ text "tcv" <+> ppr tcv <+> dcolon <+> ppr (varType tcv)
    
    3520
    -                 , text "tcv_type" <+> ppr tcv_type ])) $
    
    3521
    -        (tcv, subst `extendSubstInScope` tcv)
    
    3522
    -
    
    3523
    -      -- Clone, and extend the substitution
    
    3524
    -      | let tcv' = uniqAway in_scope (setVarType tcv tcv_type)
    
    3525
    -      = (tcv', extendTCvSubstWithClone subst tcv tcv')
    
    3526
    -      where
    
    3527
    -        in_scope = substInScopeSet subst
    
    3528
    --}
    
    3400
    +                   , le_in_scope = in_scope `extendInScopeSet` tcv
    
    3401
    +                   , le_in_vars = extendVarEnv in_vars tcv (tcv, level') }
    
    3402
    +    in unLintM thing_inside env' errs
    
    3529 3403
     
    
    3530
    -getInVarEnv :: LintM (VarEnv (InId, OutType, LintLevel))
    
    3404
    +getInVarEnv :: LintM (VarEnv (Id, LintLevel))
    
    3531 3405
     getInVarEnv = LintM (\env errs -> fromBoxedLResult (Just (le_in_vars env), errs))
    
    3532 3406
     
    
    3533
    -{-
    
    3534
    -extendTvSubstL :: TyVar -> Type -> LintM a -> LintM a
    
    3535
    -extendTvSubstL tv ty m
    
    3536
    -  = LintM $ \ env errs ->
    
    3537
    -    unLintM m (env { le_subst = Type.extendTvSubst (le_subst env) tv ty }) errs
    
    3538
    --}
    
    3539
    -
    
    3540 3407
     markAllJoinsBad :: LintM a -> LintM a
    
    3541 3408
     markAllJoinsBad m
    
    3542 3409
       = LintM $ \ env errs -> unLintM m (env { le_joins = emptyUniqMap }) errs
    
    ... ... @@ -3570,54 +3437,42 @@ markAllJoinsBadIf False m = m
    3570 3437
     getValidJoins :: LintM (UniqMap Id JoinOcc)
    
    3571 3438
     getValidJoins = LintM (\ env errs -> fromBoxedLResult (Just (le_joins env), errs))
    
    3572 3439
     
    
    3573
    -getSubst :: LintM Subst
    
    3574
    -getSubst = LintM (\ env errs -> fromBoxedLResult (Just (le_subst env), errs))
    
    3575
    -
    
    3576
    -substTyM :: InType -> LintM OutType
    
    3577
    --- Apply the substitution to the type
    
    3578
    --- The substitution is often empty, in which case it is a no-op
    
    3579
    -substTyM ty
    
    3580
    -  = do { subst <- getSubst
    
    3581
    -       ; return (substTy subst ty) }
    
    3582
    -
    
    3583 3440
     getUEAliases :: LintM (NameEnv UsageEnv)
    
    3584 3441
     getUEAliases = LintM (\ env errs -> fromBoxedLResult (Just (le_ue_aliases env), errs))
    
    3585 3442
     
    
    3586 3443
     getInScope :: LintM InScopeSet
    
    3587
    -getInScope = LintM (\ env errs -> fromBoxedLResult (Just (substInScopeSet $ le_subst env), errs))
    
    3444
    +getInScope = LintM (\ env errs -> fromBoxedLResult (Just (le_in_scope env), errs))
    
    3588 3445
     
    
    3589
    -lintVarOcc :: InVar -> LintM OutType
    
    3446
    +lintVarOcc :: Var -> LintM ()
    
    3590 3447
     -- Used at an occurrence of a variable: term variables, type variables, and coercion variables
    
    3591 3448
     -- Checks
    
    3592 3449
     --   - that it is in scope
    
    3593 3450
     --   - that it is not a GlobalId bound by a LocalId
    
    3594
    ---   - that the InType at the ocurrence matches the InType at the binding site
    
    3451
    +--   - that the Type at the ocurrence matches the Type at the binding site
    
    3595 3452
     --   - that the variables free in its type are not shadowed at the occurrence site
    
    3596 3453
     lintVarOcc v_occ
    
    3597 3454
       | isGlobalId v_occ
    
    3598
    -  = return (idType v_occ)
    
    3455
    +  = return ()
    
    3599 3456
       | otherwise
    
    3600 3457
       = do { in_var_env <- getInVarEnv
    
    3601 3458
            ; case lookupVarEnv in_var_env v_occ of
    
    3602 3459
                Nothing -> failWithL (text pp_what <+> quotes (ppr v_occ)
    
    3603 3460
                                      <+> text "is out of scope")
    
    3604
    -           Just (v_bndr, out_ty, bind_level)
    
    3461
    +           Just (v_bndr, bind_level)
    
    3605 3462
                  -> do { let bndr_ty = idType v_bndr
    
    3606 3463
                        ; check_bad_global v_bndr
    
    3607 3464
                        ; check_occ_type_match bndr_ty
    
    3608
    -                   ; check_occ_type_scope in_var_env bndr_ty bind_level
    
    3609
    -                   ; return out_ty }
    
    3610
    -
    
    3465
    +                   ; check_occ_type_scope in_var_env bndr_ty bind_level }
    
    3611 3466
         }
    
    3612 3467
       where
    
    3613
    -    occ_ty :: InType
    
    3468
    +    occ_ty :: Type
    
    3614 3469
         occ_ty = idType v_occ
    
    3615 3470
     
    
    3616 3471
         pp_what | isTyVar v_occ = "The type variable"
    
    3617 3472
                 | isCoVar v_occ = "The coercion variable"
    
    3618 3473
                 | otherwise     = "The value variable"
    
    3619 3474
     
    
    3620
    -    check_bad_global :: InVar -> LintM ()
    
    3475
    +    check_bad_global :: Var -> LintM ()
    
    3621 3476
         -- 'check_bad_global' checks for the case where an /occurrence/ is
    
    3622 3477
         -- a GlobalId, but there is an enclosing binding for a LocalId.
    
    3623 3478
         -- NB: the in-scope variables are mostly LocalIds, checked by lintIdBndr,
    
    ... ... @@ -3637,26 +3492,26 @@ lintVarOcc v_occ
    3637 3492
           | otherwise
    
    3638 3493
           = return ()
    
    3639 3494
     
    
    3640
    -    check_occ_type_match :: InType -> LintM ()
    
    3495
    +    check_occ_type_match :: Type -> LintM ()
    
    3641 3496
         -- Check that the type in /binder/ and the type in the /occurrence/ are the same
    
    3642 3497
         check_occ_type_match bndr_ty
    
    3643
    -      = ensureEqTys bndr_ty occ_ty $  -- Compares InTypes
    
    3498
    +      = ensureEqTys bndr_ty occ_ty $  -- Compares Types
    
    3644 3499
             mkBndrOccTypeMismatchMsg v_occ bndr_ty occ_ty
    
    3645 3500
     
    
    3646
    -    check_occ_type_scope :: VarEnv (InVar,OutType,LintLevel) -> InType -> LintLevel -> LintM ()
    
    3501
    +    check_occ_type_scope :: VarEnv (Var,LintLevel) -> Type -> LintLevel -> LintM ()
    
    3647 3502
         -- Check that the free vars of the binder's type
    
    3648 3503
         -- are not shadowed at the occurrence site
    
    3649 3504
         check_occ_type_scope in_var_env bndr_ty bind_level
    
    3650 3505
           = checkL (null bad_fvs) $
    
    3651 3506
             mkBndrOccFreeVarMsg v_occ occ_ty bad_fvs
    
    3652 3507
           where
    
    3653
    -        bad_fvs :: [InVar]
    
    3508
    +        bad_fvs :: [Var]
    
    3654 3509
             bad_fvs = filter is_bad (tyCoVarsOfTypeList bndr_ty)
    
    3655 3510
     
    
    3656
    -        is_bad :: InVar -> Bool
    
    3511
    +        is_bad :: Var -> Bool
    
    3657 3512
             -- True of a variable bound inside bind_level
    
    3658 3513
             is_bad v = case lookupVarEnv in_var_env v of
    
    3659
    -                      Just (_, _, v_level) -> v_level > bind_level
    
    3514
    +                      Just (_, v_level) -> v_level > bind_level
    
    3660 3515
                           Nothing -> True
    
    3661 3516
     
    
    3662 3517
     lookupJoinId :: Id -> LintM (Maybe (JoinArity, JoinOcc))
    
    ... ... @@ -3668,21 +3523,21 @@ lookupJoinId id
    3668 3523
                 Just join_occ -> return $ Just (idJoinArity id, join_occ)
    
    3669 3524
                 Nothing       -> return Nothing }
    
    3670 3525
     
    
    3671
    -addAliasUE :: OutId -> UsageEnv -> LintM a -> LintM a
    
    3526
    +addAliasUE :: Id -> UsageEnv -> LintM a -> LintM a
    
    3672 3527
     addAliasUE id ue thing_inside = LintM $ \ env errs ->
    
    3673 3528
       let new_ue_aliases =
    
    3674 3529
             extendNameEnv (le_ue_aliases env) (getName id) ue
    
    3675 3530
       in
    
    3676 3531
         unLintM thing_inside (env { le_ue_aliases = new_ue_aliases }) errs
    
    3677 3532
     
    
    3678
    -varCallSiteUsage :: OutId -> LintM UsageEnv
    
    3533
    +varCallSiteUsage :: Id -> LintM UsageEnv
    
    3679 3534
     varCallSiteUsage id =
    
    3680 3535
       do m <- getUEAliases
    
    3681 3536
          return $ case lookupNameEnv m (getName id) of
    
    3682 3537
              Nothing    -> singleUsageUE id
    
    3683 3538
              Just id_ue -> id_ue
    
    3684 3539
     
    
    3685
    -ensureEqTys :: OutType -> OutType -> SDoc -> LintM ()
    
    3540
    +ensureEqTys :: Type -> Type -> SDoc -> LintM ()
    
    3686 3541
     -- check ty2 is subtype of ty1 (ie, has same structure but usage
    
    3687 3542
     -- annotations need only be consistent, not equal)
    
    3688 3543
     -- Assumes ty1,ty2 are have already had the substitution applied
    
    ... ... @@ -3906,7 +3761,7 @@ mkLetErr bndr rhs
    3906 3761
               hang (text "Rhs:")
    
    3907 3762
                      4 (ppr rhs)]
    
    3908 3763
     
    
    3909
    -mkTyAppMsg :: OutType -> Type -> SDoc
    
    3764
    +mkTyAppMsg :: Type -> Type -> SDoc
    
    3910 3765
     mkTyAppMsg ty arg_ty
    
    3911 3766
       = vcat [text "Illegal type application:",
    
    3912 3767
                   hang (text "Function type:")
    
    ... ... @@ -4027,13 +3882,13 @@ mkJoinBndrOccMismatchMsg bndr join_arity_bndr join_arity_occ
    4027 3882
              , text "Arity at binding site:" <+> ppr join_arity_bndr
    
    4028 3883
              , text "Arity at occurrence:  " <+> ppr join_arity_occ ]
    
    4029 3884
     
    
    4030
    -mkBndrOccTypeMismatchMsg :: InVar -> InType -> InType -> SDoc
    
    3885
    +mkBndrOccTypeMismatchMsg :: Var -> Type -> Type -> SDoc
    
    4031 3886
     mkBndrOccTypeMismatchMsg var bndr_ty occ_ty
    
    4032 3887
       = vcat [ text "Mismatch in type between binder and occurrence"
    
    4033 3888
              , text "Binder:    " <+> ppr var <+> dcolon <+> ppr bndr_ty
    
    4034 3889
              , text "Occurrence:" <+> ppr var <+> dcolon <+> ppr occ_ty ]
    
    4035 3890
     
    
    4036
    -mkBndrOccFreeVarMsg :: InVar -> InType -> [TyCoVar] -> SDoc
    
    3891
    +mkBndrOccFreeVarMsg :: Var -> Type -> [TyCoVar] -> SDoc
    
    4037 3892
     mkBndrOccFreeVarMsg var occ_ty bad_tvs
    
    4038 3893
       = vcat [ text "Free vars of type are shadowed:" <+> ppr bad_tvs
    
    4039 3894
              , text "Occurrence:"  <+> ppr var <+> dcolon <+> ppr occ_ty ]