Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

9 changed files:

Changes:

  • compiler/GHC/Core/Lint.hs
    ... ... @@ -18,7 +18,7 @@ module GHC.Core.Lint (
    18 18
         LintConfig (..),
    
    19 19
         WarnsAndErrs,
    
    20 20
     
    
    21
    -    lintCoreBindings', lintUnfolding,
    
    21
    +    lintCoreBindings, lintUnfolding,
    
    22 22
         lintPassResult, lintExpr,
    
    23 23
         lintAnnots, lintAxioms,
    
    24 24
     
    
    ... ... @@ -46,6 +46,7 @@ import GHC.Core.FVs
    46 46
     import GHC.Core.Utils
    
    47 47
     import GHC.Core.Stats ( coreBindsStats )
    
    48 48
     import GHC.Core.DataCon
    
    49
    +import GHC.Core.Lint.SubstTypeLets( substTypeLets )
    
    49 50
     import GHC.Core.Ppr
    
    50 51
     import GHC.Core.Coercion
    
    51 52
     import GHC.Core.Type as Type
    
    ... ... @@ -178,65 +179,7 @@ Note [Linting function types]
    178 179
     All saturated applications of funTyCon are represented with the FunTy constructor.
    
    179 180
     See Note [Function type constructors and FunTy] in GHC.Builtin.Types.Prim
    
    180 181
     
    
    181
    - We check this invariant in lintType.
    
    182
    -
    
    183
    -Note [Linting type lets]
    
    184
    -~~~~~~~~~~~~~~~~~~~~~~~~
    
    185
    -In the desugarer, it's very very convenient to be able to say (in effect)
    
    186
    -        let a = Type Bool in
    
    187
    -        let x::a = True in <body>
    
    188
    -That is, use a type let.  See Note [Core type and coercion invariant] in "GHC.Core".
    
    189
    -One place it is used is in mkWwBodies; see Note [Join points and beta-redexes]
    
    190
    -in GHC.Core.Opt.WorkWrap.Utils.  (Maybe there are other "clients" of this feature; I'm not sure).
    
    191
    -
    
    192
    -* Hence when linting <body> we need to remember that a=Int, else we
    
    193
    -  might reject a correct program.  So we carry a type substitution (in
    
    194
    -  this example [a -> Bool]) and apply this substitution before
    
    195
    -  comparing types. In effect, in Lint, type equality is always
    
    196
    -  equality-modulo-le-subst.  This is in the le_subst field of
    
    197
    -  LintEnv.  But nota bene:
    
    198
    -
    
    199
    -  (SI1) The le_subst substitution is applied to types and coercions only
    
    200
    -
    
    201
    -  (SI2) The result of that substitution is used only to check for type
    
    202
    -        equality, to check well-typed-ness, /but is then discarded/.
    
    203
    -        The result of substitution does not outlive the CoreLint pass.
    
    204
    -
    
    205
    -  (SI3) The InScopeSet of le_subst includes only TyVar and CoVar binders.
    
    206
    -
    
    207
    -* The function
    
    208
    -        lintInTy :: Type -> LintM (Type, Kind)
    
    209
    -  returns a substituted type.
    
    210
    -
    
    211
    -* When we encounter a binder (like x::a) we must apply the substitution
    
    212
    -  to the type of the binding variable.  lintBinders does this.
    
    213
    -
    
    214
    -* Clearly we need to clone tyvar binders as we go.
    
    215
    -
    
    216
    -* But take care (#17590)! We must also clone CoVar binders:
    
    217
    -    let a = TYPE (ty |> cv)
    
    218
    -    in \cv -> blah
    
    219
    -  blindly substituting for `a` might capture `cv`.
    
    220
    -
    
    221
    -* Alas, when cloning a coercion variable we might choose a unique
    
    222
    -  that happens to clash with an inner Id, thus
    
    223
    -      \cv_66 -> let wild_X7 = blah in blah
    
    224
    -  We decide to clone `cv_66` because it's already in scope.  Fine,
    
    225
    -  choose a new unique.  Aha, X7 looks good.  So we check the lambda
    
    226
    -  body with le_subst of [cv_66 :-> cv_X7]
    
    227
    -
    
    228
    -  This is all fine, even though we use the same unique as wild_X7.
    
    229
    -  As (SI2) says, we do /not/ return a new lambda
    
    230
    -     (\cv_X7 -> let wild_X7 = blah in ...)
    
    231
    -  We simply use the le_subst substitution in types/coercions only, when
    
    232
    -  checking for equality.
    
    233
    -
    
    234
    -* We still need to check that Id occurrences are bound by some
    
    235
    -  enclosing binding.  We do /not/ use the InScopeSet for the le_subst
    
    236
    -  for this purpose -- it contains only TyCoVars.  Instead we have a separate
    
    237
    -  le_ids for the in-scope Id binders.
    
    238
    -
    
    239
    -Sigh.  We might want to explore getting rid of type-let!
    
    182
    +We check this invariant in lintType.
    
    240 183
     
    
    241 184
     Note [Bad unsafe coercion]
    
    242 185
     ~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -311,6 +254,7 @@ path does not result in allocation in the hot path. This can be surprisingly
    311 254
     impactful. Changing `lint_app` reduced allocations for one test program I was
    
    312 255
     looking at by ~4%.
    
    313 256
     
    
    257
    +
    
    314 258
     ************************************************************************
    
    315 259
     *                                                                      *
    
    316 260
                      Beginning and ending passes
    
    ... ... @@ -407,26 +351,37 @@ data LintPassResultConfig = LintPassResultConfig
    407 351
       , lpr_platform         :: !Platform
    
    408 352
       , lpr_makeLintFlags    :: !LintFlags
    
    409 353
       , lpr_passPpr          :: !SDoc
    
    354
    +  , lpr_preSubst         :: !Bool  -- True <=> run substTypeLets before linting
    
    355
    +                                   -- See Note [Substituting type-lets]
    
    410 356
       , lpr_localsInScope    :: ![Var]
    
    411 357
       }
    
    412 358
     
    
    413 359
     lintPassResult :: Logger -> LintPassResultConfig
    
    414 360
                    -> CoreProgram -> IO ()
    
    415 361
     lintPassResult logger cfg binds
    
    416
    -  = do { let warns_and_errs = lintCoreBindings'
    
    417
    -               (LintConfig
    
    362
    +  = do { let lint_config = LintConfig
    
    418 363
                     { l_diagOpts = lpr_diagOpts cfg
    
    419 364
                     , l_platform = lpr_platform cfg
    
    420 365
                     , l_flags    = lpr_makeLintFlags cfg
    
    421 366
                     , l_vars     = lpr_localsInScope cfg
    
    422
    -                })
    
    423
    -               binds
    
    367
    +                }
    
    368
    +
    
    369
    +       -- Do the pre-substitution if necessary
    
    370
    +       -- See Note [Substituting type-lets] in GHC.Core.SubstTypeLets
    
    371
    +       -- especially wrinkle (STL2)
    
    372
    +       ; let binds1 | lpr_preSubst cfg = substTypeLets binds
    
    373
    +                    | otherwise        = binds
    
    374
    +
    
    375
    +       -- Do the main Lint pass itself
    
    376
    +       ; let warns_and_errs = lintCoreBindings lint_config binds1
    
    377
    +
    
    378
    +       -- Report the results
    
    424 379
            ; Err.showPass logger $
    
    425 380
                "Core Linted result of " ++
    
    426 381
                renderWithContext defaultSDocContext (lpr_passPpr cfg)
    
    427 382
            ; displayLintResults logger
    
    428 383
                                 (lpr_passPpr cfg)
    
    429
    -                            (pprCoreBindings binds) warns_and_errs
    
    384
    +                            (pprCoreBindings binds1) warns_and_errs
    
    430 385
            }
    
    431 386
     
    
    432 387
     displayLintResults :: Logger
    
    ... ... @@ -456,11 +411,11 @@ lint_banner string pass = text "*** Core Lint" <+> text string
    456 411
                               <+> text "***"
    
    457 412
     
    
    458 413
     -- | Type-check a 'CoreProgram'. See Note [Core Lint guarantee].
    
    459
    -lintCoreBindings' :: LintConfig -> CoreProgram -> WarnsAndErrs
    
    414
    +lintCoreBindings :: LintConfig -> CoreProgram -> WarnsAndErrs
    
    460 415
     --   Returns (warnings, errors)
    
    461 416
     -- If you edit this function, you may need to update the GHC formalism
    
    462 417
     -- See Note [GHC Formalism]
    
    463
    -lintCoreBindings' cfg binds
    
    418
    +lintCoreBindings cfg binds
    
    464 419
       = initL cfg $
    
    465 420
         addLoc TopLevelBindings           $
    
    466 421
         do { -- Check that all top-level binders are distinct
    
    ... ... @@ -472,8 +427,7 @@ lintCoreBindings' cfg binds
    472 427
            ; checkL (null ext_dups) (dupExtVars ext_dups)
    
    473 428
     
    
    474 429
              -- Typecheck the bindings
    
    475
    -       ; lintRecBindings TopLevel all_pairs $ \_ ->
    
    476
    -         return () }
    
    430
    +       ; lintRecBindings TopLevel all_pairs $ return () }
    
    477 431
       where
    
    478 432
         all_pairs = flattenBinds binds
    
    479 433
          -- Put all the top-level binders in scope at the start
    
    ... ... @@ -555,28 +509,28 @@ Check a core binding, returning the list of variables bound.
    555 509
     -- Let
    
    556 510
     
    
    557 511
     lintRecBindings :: TopLevelFlag -> [(Id, CoreExpr)]
    
    558
    -                -> ([OutId] -> LintM a) -> LintM (a, [UsageEnv])
    
    512
    +                -> LintM a -> LintM (a, [UsageEnv])
    
    559 513
     lintRecBindings top_lvl pairs thing_inside
    
    560
    -  = lintIdBndrs top_lvl bndrs $ \ bndrs' ->
    
    561
    -    do { ues <- zipWithM lint_pair bndrs' rhss
    
    562
    -       ; a <- thing_inside bndrs'
    
    514
    +  = lintIdBndrs top_lvl bndrs $
    
    515
    +    do { ues <- zipWithM lint_pair bndrs rhss
    
    516
    +       ; a <- thing_inside
    
    563 517
            ; return (a, ues) }
    
    564 518
       where
    
    565 519
         (bndrs, rhss) = unzip pairs
    
    566
    -    lint_pair bndr' rhs
    
    567
    -      = addLoc (RhsOf bndr') $
    
    568
    -        do { (rhs_ty, ue) <- lintRhs bndr' rhs         -- Check the rhs
    
    569
    -           ; lintLetBind top_lvl Recursive bndr' rhs rhs_ty
    
    520
    +    lint_pair bndr rhs
    
    521
    +      = addLoc (RhsOf bndr) $
    
    522
    +        do { (rhs_ty, ue) <- lintRhs bndr rhs         -- Check the rhs
    
    523
    +           ; lintLetBind top_lvl Recursive bndr rhs rhs_ty
    
    570 524
                ; return ue }
    
    571 525
     
    
    572
    -lintLetBody :: LintLocInfo -> [OutId] -> CoreExpr -> LintM (OutType, UsageEnv)
    
    526
    +lintLetBody :: LintLocInfo -> [Id] -> CoreExpr -> LintM (Type, UsageEnv)
    
    573 527
     lintLetBody loc bndrs body
    
    574 528
       = do { (body_ty, body_ue) <- addLoc loc (lintCoreExpr body)
    
    575 529
            ; mapM_ (lintJoinBndrType body_ty) bndrs
    
    576 530
            ; return (body_ty, body_ue) }
    
    577 531
     
    
    578
    -lintLetBind :: TopLevelFlag -> RecFlag -> OutId
    
    579
    -              -> CoreExpr -> OutType -> LintM ()
    
    532
    +lintLetBind :: TopLevelFlag -> RecFlag -> Id
    
    533
    +            -> CoreExpr -> Type -> LintM ()
    
    580 534
     -- Binder's type, and the RHS, have already been linted
    
    581 535
     -- This function checks other invariants
    
    582 536
     lintLetBind top_lvl rec_flag binder rhs rhs_ty
    
    ... ... @@ -651,14 +605,17 @@ lintLetBind top_lvl rec_flag binder rhs rhs_ty
    651 605
     
    
    652 606
                _ -> return ()
    
    653 607
     
    
    654
    -       ; addLoc (RuleOf binder) $ mapM_ (lintCoreRule binder binder_ty) (idCoreRules binder)
    
    608
    +       -- Lint any RULES
    
    609
    +       ; addLoc (RuleOf binder) $
    
    610
    +         mapM_ (lintCoreRule binder binder_ty) (idCoreRules binder)
    
    655 611
     
    
    612
    +       -- Lint the unfolding
    
    613
    +       -- Do this here, not in lintIdBinder, so that all the
    
    614
    +       -- binders of the letrec group are in scope
    
    656 615
            ; addLoc (UnfoldingOf binder) $
    
    657 616
              lintIdUnfolding binder binder_ty (idUnfolding binder)
    
    658
    -       ; return () }
    
    659 617
     
    
    660
    -        -- We should check the unfolding, if any, but this is tricky because
    
    661
    -        -- the unfolding is a SimplifiableCoreExpr. Give up for now.
    
    618
    +       ; return () }
    
    662 619
     
    
    663 620
     -- | Checks the RHS of bindings. It only differs from 'lintCoreExpr'
    
    664 621
     -- in that it doesn't reject occurrences of the function 'makeStatic' when they
    
    ... ... @@ -667,7 +624,7 @@ lintLetBind top_lvl rec_flag binder rhs rhs_ty
    667 624
     -- join point.
    
    668 625
     --
    
    669 626
     -- See Note [Checking StaticPtrs].
    
    670
    -lintRhs :: Id -> CoreExpr -> LintM (OutType, UsageEnv)
    
    627
    +lintRhs :: Id -> CoreExpr -> LintM (Type, UsageEnv)
    
    671 628
     -- NB: the Id can be Linted or not -- it's only used for
    
    672 629
     --     its OccInfo and join-pointer-hood
    
    673 630
     lintRhs bndr rhs
    
    ... ... @@ -682,7 +639,7 @@ lintRhs _bndr rhs = fmap lf_check_static_ptrs getLintFlags >>= go
    682 639
       where
    
    683 640
         -- Allow occurrences of 'makeStatic' at the top-level but produce errors
    
    684 641
         -- otherwise.
    
    685
    -    go :: StaticPtrCheck -> LintM (OutType, UsageEnv)
    
    642
    +    go :: StaticPtrCheck -> LintM (Type, UsageEnv)
    
    686 643
         go AllowAtTopLevel
    
    687 644
           | (binders0, rhs') <- collectTyBinders rhs
    
    688 645
           , Just (fun, t, info, e) <- collectMakeStaticArgs rhs'
    
    ... ... @@ -699,7 +656,7 @@ lintRhs _bndr rhs = fmap lf_check_static_ptrs getLintFlags >>= go
    699 656
     
    
    700 657
     -- | Lint the RHS of a join point with expected join arity of @n@ (see Note
    
    701 658
     -- [Join points] in "GHC.Core").
    
    702
    -lintJoinLams :: JoinArity -> Maybe Id -> CoreExpr -> LintM (OutType, UsageEnv)
    
    659
    +lintJoinLams :: JoinArity -> Maybe Id -> CoreExpr -> LintM (Type, UsageEnv)
    
    703 660
     lintJoinLams join_arity enforce rhs
    
    704 661
       = go join_arity rhs
    
    705 662
       where
    
    ... ... @@ -715,17 +672,22 @@ lintIdUnfolding :: Id -> Type -> Unfolding -> LintM ()
    715 672
     lintIdUnfolding bndr bndr_ty uf
    
    716 673
       | isStableUnfolding uf
    
    717 674
       , Just rhs <- maybeUnfoldingTemplate uf
    
    718
    -  = noMultiplicityChecks $ -- Skip linearity checking for unfoldings
    
    719
    -                           -- See Note [Linting linearity]
    
    720
    -  do { ty <- fst <$> (if isCompulsoryUnfolding uf
    
    721
    -                        then noFixedRuntimeRepChecks $ lintRhs bndr rhs
    
    722
    -            --               ^^^^^^^^^^^^^^^^^^^^^^^
    
    723
    -            -- See Note [Checking for representation polymorphism]
    
    724
    -                        else lintRhs bndr rhs)
    
    725
    -       ; ensureEqTys bndr_ty ty (mkRhsMsg bndr (text "unfolding") ty) }
    
    726
    -lintIdUnfolding  _ _ _
    
    727
    -  = return ()       -- Do not Lint unstable unfoldings, because that leads
    
    728
    -                    -- to exponential behaviour; c.f. GHC.Core.FVs.idUnfoldingVars
    
    675
    +   = suppress_rr_checks   $
    
    676
    +     noMultiplicityChecks $ -- Skip linearity checking for unfoldings
    
    677
    +                            -- See Note [Linting linearity]
    
    678
    +     do { (unf_ty, _unf_ue) <- lintRhs bndr rhs
    
    679
    +        ; ensureEqTys bndr_ty unf_ty (mkRhsMsg bndr (text "unfolding") unf_ty) }
    
    680
    +
    
    681
    +  | otherwise
    
    682
    +  = -- Do not Lint the body of an unstable unfolding, because that leads
    
    683
    +    -- to exponential behaviour; c.f. GHC.Core.FVs.idUnfoldingVars
    
    684
    +    return ()
    
    685
    +
    
    686
    +  where
    
    687
    +    -- See Note [Checking for representation polymorphism]
    
    688
    +    suppress_rr_checks thing_inside
    
    689
    +      | isCompulsoryUnfolding uf = noFixedRuntimeRepChecks thing_inside
    
    690
    +      | otherwise                = thing_inside
    
    729 691
     
    
    730 692
     {- Note [Checking for INLINE loop breakers]
    
    731 693
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -887,13 +849,8 @@ suspicious and worth investigating if you have a seg-fault or bizarre behaviour.
    887 849
     ************************************************************************
    
    888 850
     -}
    
    889 851
     
    
    890
    -lintCoreExpr :: InExpr -> LintM (OutType, UsageEnv)
    
    891
    --- The returned type has the substitution from the monad
    
    892
    --- already applied to it:
    
    893
    ---      lintCoreExpr e subst = exprType (subst e)
    
    894
    ---
    
    895
    --- The returned "type" can be a kind, if the expression is (Type ty)
    
    896
    -
    
    852
    +lintCoreExpr :: CoreExpr -> LintM (Type, UsageEnv)
    
    853
    +-- The returned type is the type of the expression
    
    897 854
     -- If you edit this function, you may need to update the GHC formalism
    
    898 855
     -- See Note [GHC Formalism]
    
    899 856
     
    
    ... ... @@ -920,7 +877,7 @@ lintCoreExpr (Cast expr co)
    920 877
     
    
    921 878
            ; lintCoercion co
    
    922 879
            ; lintRole co Representational (coercionRole co)
    
    923
    -       ; Pair from_ty to_ty <- substCoKindM co
    
    880
    +       ; let Pair from_ty to_ty = coercionKind co
    
    924 881
            ; checkValueType (typeKind to_ty) $
    
    925 882
              text "target of cast" <+> quotes (ppr co)
    
    926 883
            ; ensureEqTys from_ty expr_ty (mkCastErr expr co from_ty expr_ty)
    
    ... ... @@ -934,27 +891,22 @@ lintCoreExpr (Tick tickish expr)
    934 891
     
    
    935 892
     lintCoreExpr (Let (NonRec tv (Type ty)) body)
    
    936 893
       | isTyVar tv
    
    937
    -  =     -- See Note [Linting type lets]
    
    938
    -    do  { ty' <- lintTypeAndSubst ty
    
    939
    -        ; lintTyCoBndr tv              $ \ tv' ->
    
    940
    -    do  { addLoc (RhsOf tv) $ lintTyKind tv' ty'
    
    941
    -                -- Now extend the substitution so we
    
    942
    -                -- take advantage of it in the body
    
    943
    -        ; extendTvSubstL tv ty' $
    
    944
    -          addLoc (BodyOfLet tv) $
    
    945
    -          lintCoreExpr body } }
    
    894
    +  = do  { lintType ty
    
    895
    +        ; lintTyCoBndr tv              $
    
    896
    +    do  { addLoc (RhsOf tv)     $ lintTyKind tv ty
    
    897
    +        ; addLoc (BodyOfLet tv) $ lintCoreExpr body } }
    
    946 898
     
    
    947 899
     lintCoreExpr (Let (NonRec bndr rhs) body)
    
    948 900
       | isId bndr
    
    949 901
       = do { -- First Lint the RHS, before bringing the binder into scope
    
    950 902
              (rhs_ty, let_ue) <- lintRhs bndr rhs
    
    951 903
     
    
    952
    -          -- See Note [Multiplicity of let binders] in Var
    
    904
    +         -- See Note [Multiplicity of let binders] in Var
    
    953 905
              -- Now lint the binder
    
    954
    -       ; lintBinder LetBind bndr $ \bndr' ->
    
    955
    -    do { lintLetBind NotTopLevel NonRecursive bndr' rhs rhs_ty
    
    956
    -       ; addAliasUE bndr' let_ue $
    
    957
    -         lintLetBody (BodyOfLet bndr') [bndr'] body } }
    
    906
    +       ; lintBinder LetBind bndr $
    
    907
    +    do { lintLetBind NotTopLevel NonRecursive bndr rhs rhs_ty
    
    908
    +       ; addAliasUE bndr let_ue $
    
    909
    +         lintLetBody (BodyOfLet bndr) [bndr] body } }
    
    958 910
     
    
    959 911
       | otherwise
    
    960 912
       = failWithL (mkLetErr bndr rhs)       -- Not quite accurate
    
    ... ... @@ -973,8 +925,8 @@ lintCoreExpr e@(Let (Rec pairs) body)
    973 925
     
    
    974 926
               -- See Note [Multiplicity of let binders] in Var
    
    975 927
             ; ((body_type, body_ue), ues) <-
    
    976
    -            lintRecBindings NotTopLevel pairs $ \ bndrs' ->
    
    977
    -            lintLetBody (BodyOfLetRec bndrs') bndrs' body
    
    928
    +            lintRecBindings NotTopLevel pairs $
    
    929
    +            lintLetBody (BodyOfLetRec bndrs) bndrs body
    
    978 930
             ; return (body_type, body_ue  `addUE` scaleUE ManyTy (foldr1WithDefault zeroUE addUE ues)) }
    
    979 931
       where
    
    980 932
         bndrs = map fst pairs
    
    ... ... @@ -986,7 +938,7 @@ lintCoreExpr e@(App _ _)
    986 938
         -- N.B. we may have an over-saturated application of the form:
    
    987 939
         --   runRW (\s -> \x -> ...) y
    
    988 940
       , ty_arg1 : ty_arg2 : cont_arg : rest <- args
    
    989
    -  = do { let lint_rw_cont :: CoreArg -> Mult -> UsageEnv -> LintM (OutType, UsageEnv)
    
    941
    +  = do { let lint_rw_cont :: CoreArg -> Mult -> UsageEnv -> LintM (Type, UsageEnv)
    
    990 942
                  lint_rw_cont expr@(Lam _ _) mult fun_ue
    
    991 943
                     = do { (arg_ty, arg_ue) <- lintJoinLams 1 (Just fun) expr
    
    992 944
                          ; let app_ue = addUE fun_ue (scaleUE mult arg_ue)
    
    ... ... @@ -1036,74 +988,73 @@ lintCoreExpr (Type ty)
    1036 988
     lintCoreExpr (Coercion co)
    
    1037 989
       -- See Note [Coercions in terms]
    
    1038 990
       = do { addLoc (InCo co) $ lintCoercion co
    
    1039
    -       ; ty <- substTyM (coercionType co)
    
    991
    +       ; let ty = coercionType co
    
    1040 992
            ; return (ty, zeroUE) }
    
    1041 993
     
    
    1042 994
     ----------------------
    
    1043
    -lintIdOcc :: InId -> Int -- Number of arguments (type or value) being passed
    
    1044
    -          -> LintM (OutType, UsageEnv) -- returns type of the *variable*
    
    1045
    -lintIdOcc in_id nargs
    
    1046
    -  = addLoc (OccOf in_id) $
    
    1047
    -    do  { checkL (isNonCoVarId in_id)
    
    1048
    -                 (text "Non term variable" <+> ppr in_id)
    
    995
    +lintIdOcc :: Id -> Int -- Number of arguments (type or value) being passed
    
    996
    +          -> LintM (Type, UsageEnv) -- returns type of the *variable*
    
    997
    +lintIdOcc id nargs
    
    998
    +  = addLoc (OccOf id) $
    
    999
    +    do  { checkL (isNonCoVarId id)
    
    1000
    +                 (text "Non term variable" <+> ppr id)
    
    1049 1001
                      -- See GHC.Core Note [Variable occurrences in Core]
    
    1050 1002
     
    
    1051
    -        -- Check that the type of the occurrence is the same
    
    1052
    -        -- as the type of the binding site.  The inScopeIds are
    
    1053
    -        -- /un-substituted/, so this checks that the occurrence type
    
    1054
    -        -- is identical to the binder type.
    
    1055
    -        -- This makes things much easier for things like:
    
    1056
    -        --    /\a. \(x::Maybe a). /\a. ...(x::Maybe a)...
    
    1057
    -        -- The "::Maybe a" on the occurrence is referring to the /outer/ a.
    
    1058
    -        -- If we compared /substituted/ types we'd risk comparing
    
    1059
    -        -- (Maybe a) from the binding site with bogus (Maybe a1) from
    
    1060
    -        -- the occurrence site.  Comparing un-substituted types finesses
    
    1061
    -        -- this altogether
    
    1062
    -        ; out_ty <- lintVarOcc in_id
    
    1003
    +        ; lintVarOcc id
    
    1063 1004
     
    
    1064 1005
               -- Check for a nested occurrence of the StaticPtr constructor.
    
    1065 1006
               -- See Note [Checking StaticPtrs].
    
    1066 1007
             ; when (nargs /= 0) $
    
    1067
    -          checkL (idName in_id /= makeStaticName) $
    
    1008
    +          checkL (idName id /= makeStaticName) $
    
    1068 1009
               text "Found makeStatic nested in an expression"
    
    1069 1010
     
    
    1070
    -        ; checkDeadIdOcc in_id
    
    1011
    +        ; checkDeadIdOcc id
    
    1071 1012
     
    
    1072
    -        ; case isDataConId_maybe in_id of
    
    1013
    +        ; case isDataConId_maybe id of
    
    1073 1014
                  Nothing -> return ()
    
    1074 1015
                  Just dc -> checkTypeDataConOcc "expression" dc
    
    1075 1016
     
    
    1076
    -        ; checkJoinOcc in_id nargs
    
    1077
    -        ; usage <- varCallSiteUsage in_id
    
    1078
    -
    
    1079
    -        ; return (out_ty, usage) }
    
    1017
    +        ; checkJoinOcc id nargs
    
    1018
    +        ; usage <- varCallSiteUsage id
    
    1080 1019
     
    
    1020
    +        ; return (idType id, usage) }
    
    1081 1021
     
    
    1082 1022
     
    
    1023
    +------------------
    
    1083 1024
     lintCoreFun :: CoreExpr
    
    1084
    -            -> Int                          -- Number of arguments (type or val) being passed
    
    1085
    -            -> LintM (OutType, UsageEnv) -- Returns type of the *function*
    
    1025
    +            -> Int                    -- Number of arguments (type or val) being passed
    
    1026
    +            -> LintM (Type, UsageEnv) -- Returns type of the *function*
    
    1086 1027
     lintCoreFun (Var var) nargs
    
    1087 1028
       = lintIdOcc var nargs
    
    1088 1029
     
    
    1089 1030
     lintCoreFun (Lam var body) nargs
    
    1090
    -  -- Act like lintCoreExpr of Lam, but *don't* call markAllJoinsBad;
    
    1091
    -  -- See Note [Beta redexes]
    
    1031
    +  -- Act like lintCoreExpr of Lam, but *don't* necessarily call markAllJoinsBad;
    
    1032
    +  -- See Note [Join points and beta-redexes]
    
    1092 1033
       | nargs /= 0
    
    1093 1034
       = lintLambda var $ lintCoreFun body (nargs - 1)
    
    1094 1035
     
    
    1095 1036
     lintCoreFun expr nargs
    
    1096
    -  = markAllJoinsBadIf (nargs /= 0) $
    
    1097
    -      -- See Note [Join points are less general than the paper]
    
    1098
    -    lintCoreExpr expr
    
    1037
    +  = do { mark_bad_joins
    
    1038
    +           <- if nargs == 0
    
    1039
    +              then -- Saturated lambda
    
    1040
    +                   -- See Note [Join points and beta-redexes]
    
    1041
    +                   do { flags <- getLintFlags
    
    1042
    +                      ; return (not (lf_allow_beta_joins flags)) }
    
    1043
    +              else -- Something else
    
    1044
    +                   -- See Note [Join points are less general than the paper]
    
    1045
    +                   return True
    
    1046
    +
    
    1047
    +       ; markAllJoinsBadIf mark_bad_joins $
    
    1048
    +         lintCoreExpr expr }
    
    1049
    +
    
    1099 1050
     ------------------
    
    1100 1051
     lintLambda :: Var -> LintM (Type, UsageEnv) -> LintM (Type, UsageEnv)
    
    1101 1052
     lintLambda var lintBody =
    
    1102 1053
         addLoc (LambdaBodyOf var) $
    
    1103
    -    lintBinder LambdaBind var $ \ var' ->
    
    1054
    +    lintBinder LambdaBind var $
    
    1104 1055
         do { (body_ty, ue) <- lintBody
    
    1105
    -       ; ue' <- checkLinearity ue var'
    
    1106
    -       ; return (mkLamType var' body_ty, ue') }
    
    1056
    +       ; ue' <- checkLinearity ue var
    
    1057
    +       ; return (mkLamType var body_ty, ue') }
    
    1107 1058
     ------------------
    
    1108 1059
     checkDeadIdOcc :: Id -> LintM ()
    
    1109 1060
     -- Occurrences of an Id should never be dead....
    
    ... ... @@ -1117,8 +1068,8 @@ checkDeadIdOcc id
    1117 1068
       = return ()
    
    1118 1069
     
    
    1119 1070
     ------------------
    
    1120
    -lintJoinBndrType :: OutType -- Type of the body
    
    1121
    -                 -> OutId   -- Possibly a join Id
    
    1071
    +lintJoinBndrType :: Type -- Type of the body
    
    1072
    +                 -> Id   -- Possibly a join Id
    
    1122 1073
                      -> LintM ()
    
    1123 1074
     -- Checks that the return type of a join Id matches the body
    
    1124 1075
     -- E.g. join j x = rhs in body
    
    ... ... @@ -1337,8 +1288,55 @@ checkLinearity body_ue lam_var =
    1337 1288
           return body_ue'
    
    1338 1289
         Nothing    -> return body_ue -- A type variable
    
    1339 1290
     
    
    1340
    -{- Note [Linting join points with casts or ticks]
    
    1341
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1291
    +{- Note [Join points and beta-redexes]
    
    1292
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1293
    +In the worker/wrapper pass, the worker invokes the original function by calling
    
    1294
    +it with arguments, thus producing a beta-redex for the simplifier to munch away:
    
    1295
    +
    
    1296
    +  \x y z -> e   =>    (\x y z -> e) wx wy wz
    
    1297
    +
    
    1298
    +But we need to take care if `e` invokes a join point.  For example:
    
    1299
    +
    
    1300
    +  join j1 x = ...
    
    1301
    +  join j2 y = if y == 0 then 0 else j1 y
    
    1302
    +=>
    
    1303
    +  join j1 x = ...
    
    1304
    +  join $wj2 y# = (\y -> if y == 0 then 0 else jump j1 y) (I# y#)
    
    1305
    +  join j2 y = case y of I# y# -> jump $wj2 y#
    
    1306
    +
    
    1307
    +Now the jump to `j1` is inside a lambda and inside an application. That is ill-typed
    
    1308
    +from Lint's point of view.  And yet, after one round of simplification it'll all be
    
    1309
    +fine.
    
    1310
    +
    
    1311
    +You might wonder if we could use a `let` instead of a lambda for the worker:
    
    1312
    +
    
    1313
    +  join $wj2 y# = let y = I# y#
    
    1314
    +                 in  if y == 0 then 0 else jump j1 y
    
    1315
    +
    
    1316
    +That would solve the join-point problem, but it really doesn't work because
    
    1317
    + 1. The lets shadow each other
    
    1318
    + 2. In particular the invariant (NoTypeShadowing) is easily broken.
    
    1319
    +    (We might have type lambdas of course.)
    
    1320
    +
    
    1321
    +In short, te lambda arguments should not "see" any of the lambda-bound
    
    1322
    +variables.
    
    1323
    +
    
    1324
    +So our solution is this:
    
    1325
    +
    
    1326
    +* Use straightforward applicaion in the worker-wrapper pass, creating a beta-redex.
    
    1327
    +  See the call to `mkApps` in GHC.Core.Opt.WorkWrap.Utils.mkWwBodies.
    
    1328
    +
    
    1329
    +* Tell Lint not to complain about a join-point invocation hidden under a
    
    1330
    +  saturated beta-redex.  The code is rather simple: see `lintCoreFun`.
    
    1331
    +
    
    1332
    +  We guard this with a Lint flag `lf_allow_beta_joins`.
    
    1333
    +
    
    1334
    +* Teach occurrence analysis that `j1` is still a join point, despite its
    
    1335
    +  call being nested inside the beta-redex.  See Note [occAnal for applications]
    
    1336
    +  in GHC.Core.Opt.OccurAnal.
    
    1337
    +
    
    1338
    +Note [Linting join points with casts or ticks]
    
    1339
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1342 1340
     As per Note [Join points, casts, and ticks] in GHC.Core, we have to be careful
    
    1343 1341
     when a cast or tick occurs in between a join point binding and a corresponding
    
    1344 1342
     join point occurrence.
    
    ... ... @@ -1409,33 +1407,6 @@ lose track of why an expression is bottom, so we shouldn't make too
    1409 1407
     much fuss when that happens.
    
    1410 1408
     
    
    1411 1409
     
    
    1412
    -Note [Beta redexes]
    
    1413
    -~~~~~~~~~~~~~~~~~~~
    
    1414
    -Consider:
    
    1415
    -
    
    1416
    -  join j @x y z = ... in
    
    1417
    -  (\@x y z -> jump j @x y z) @t e1 e2
    
    1418
    -
    
    1419
    -This is clearly ill-typed, since the jump is inside both an application and a
    
    1420
    -lambda, either of which is enough to disqualify it as a tail call (see Note
    
    1421
    -[Invariants on join points] in GHC.Core). However, strictly from a
    
    1422
    -lambda-calculus perspective, the term doesn't go wrong---after the two beta
    
    1423
    -reductions, the jump *is* a tail call and everything is fine.
    
    1424
    -
    
    1425
    -Why would we want to allow this when we have let? One reason is that a compound
    
    1426
    -beta redex (that is, one with more than one argument) has different scoping
    
    1427
    -rules: naively reducing the above example using lets will capture any free
    
    1428
    -occurrence of y in e2. More fundamentally, type lets are tricky; many passes,
    
    1429
    -such as Float Out, tacitly assume that the incoming program's type lets have
    
    1430
    -all been dealt with by the simplifier. Thus we don't want to let-bind any types
    
    1431
    -in, say, GHC.Core.Subst.simpleOptPgm, which in some circumstances can run immediately
    
    1432
    -before Float Out.
    
    1433
    -
    
    1434
    -All that said, currently GHC.Core.Subst.simpleOptPgm is the only thing using this
    
    1435
    -loophole, doing so to avoid re-traversing large functions (beta-reducing a type
    
    1436
    -lambda without introducing a type let requires a substitution). TODO: Improve
    
    1437
    -simpleOptPgm so that we can forget all this ever happened.
    
    1438
    -
    
    1439 1410
     ************************************************************************
    
    1440 1411
     *                                                                      *
    
    1441 1412
     \subsection[lintCoreArgs]{lintCoreArgs}
    
    ... ... @@ -1449,23 +1420,23 @@ subtype of the required type, as one would expect.
    1449 1420
     -- Takes the functions type and arguments as argument.
    
    1450 1421
     -- Returns the *result* of applying the function to arguments.
    
    1451 1422
     -- e.g. f :: Int -> Bool -> Int would return `Int` as result type.
    
    1452
    -lintCoreArgs  :: (OutType, UsageEnv) -> [InExpr] -> LintM (OutType, UsageEnv)
    
    1423
    +lintCoreArgs  :: (Type, UsageEnv) -> [CoreExpr] -> LintM (Type, UsageEnv)
    
    1453 1424
     lintCoreArgs (fun_ty, fun_ue) args
    
    1454
    -  = lintApp (text "expression")
    
    1455
    -              lintTyArg lintValArg fun_ty args fun_ue
    
    1425
    +  = lintApp (text "expression") lintTyArg lintValArg fun_ty args fun_ue
    
    1456 1426
     
    
    1457
    -lintTyArg :: InExpr -> LintM OutType
    
    1427
    +lintTyArg :: CoreExpr -> LintM Type
    
    1458 1428
     
    
    1459 1429
     -- Type argument
    
    1460 1430
     lintTyArg (Type arg_ty)
    
    1461 1431
       = do { checkL (not (isCoercionTy arg_ty))
    
    1462 1432
                     (text "Unnecessary coercion-to-type injection:"
    
    1463 1433
                       <+> ppr arg_ty)
    
    1464
    -       ; lintTypeAndSubst arg_ty }
    
    1434
    +       ; lintType arg_ty
    
    1435
    +       ; return arg_ty }
    
    1465 1436
     lintTyArg arg
    
    1466 1437
       = failWithL (hang (text "Expected type argument but found") 2 (ppr arg))
    
    1467 1438
     
    
    1468
    -lintValArg  :: InExpr -> Mult -> UsageEnv -> LintM (OutType, UsageEnv)
    
    1439
    +lintValArg  :: CoreExpr -> Mult -> UsageEnv -> LintM (Type, UsageEnv)
    
    1469 1440
     lintValArg arg mult fun_ue
    
    1470 1441
       = do { (arg_ty, arg_ue) <- markAllJoinsBad $ lintCoreExpr arg
    
    1471 1442
                -- See Note [Representation polymorphism invariants] in GHC.Core
    
    ... ... @@ -1484,9 +1455,9 @@ lintValArg arg mult fun_ue
    1484 1455
     
    
    1485 1456
     -----------------
    
    1486 1457
     lintAltBinders :: UsageEnv
    
    1487
    -               -> Var         -- Case binder
    
    1488
    -               -> OutType     -- Scrutinee type
    
    1489
    -               -> OutType     -- Constructor type
    
    1458
    +               -> Var      -- Case binder
    
    1459
    +               -> Type     -- Scrutinee type
    
    1460
    +               -> Type     -- Constructor type
    
    1490 1461
                    -> [(Mult, OutVar)]    -- Binders
    
    1491 1462
                    -> LintM UsageEnv
    
    1492 1463
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1505,6 +1476,7 @@ lintAltBinders rhs_ue case_bndr scrut_ty con_ty ((var_w, bndr):bndrs)
    1505 1476
            ; rhs_ue' <- checkCaseLinearity rhs_ue case_bndr var_w bndr
    
    1506 1477
            ; lintAltBinders rhs_ue' case_bndr scrut_ty con_ty' bndrs }
    
    1507 1478
     
    
    1479
    +
    
    1508 1480
     -- | Implements the case rules for linearity
    
    1509 1481
     checkCaseLinearity :: UsageEnv -> Var -> Mult -> Var -> LintM UsageEnv
    
    1510 1482
     checkCaseLinearity ue case_bndr var_w bndr = do
    
    ... ... @@ -1529,7 +1501,7 @@ checkCaseLinearity ue case_bndr var_w bndr = do
    1529 1501
     
    
    1530 1502
     
    
    1531 1503
     -----------------
    
    1532
    -lintTyApp :: OutType -> OutType -> LintM OutType
    
    1504
    +lintTyApp :: Type -> Type -> LintM Type
    
    1533 1505
     lintTyApp fun_ty arg_ty
    
    1534 1506
       | Just (tv,body_ty) <- splitForAllTyVar_maybe fun_ty
    
    1535 1507
       = do  { lintTyKind tv arg_ty
    
    ... ... @@ -1547,8 +1519,8 @@ lintTyApp fun_ty arg_ty
    1547 1519
     -- | @lintValApp arg fun_ty arg_ty@ lints an application of @fun arg@
    
    1548 1520
     -- where @fun :: fun_ty@ and @arg :: arg_ty@, returning the type of the
    
    1549 1521
     -- application.
    
    1550
    -lintValApp :: CoreExpr -> OutType -> OutType -> UsageEnv -> UsageEnv
    
    1551
    -           -> LintM (OutType, UsageEnv)
    
    1522
    +lintValApp :: CoreExpr -> Type -> Type -> UsageEnv -> UsageEnv
    
    1523
    +           -> LintM (Type, UsageEnv)
    
    1552 1524
     lintValApp arg fun_ty arg_ty fun_ue arg_ue
    
    1553 1525
       | Just (_, w, arg_ty', res_ty') <- splitFunTy_maybe fun_ty
    
    1554 1526
       = do { ensureEqTys arg_ty' arg_ty (mkAppMsg arg_ty' arg_ty arg)
    
    ... ... @@ -1559,9 +1531,7 @@ lintValApp arg fun_ty arg_ty fun_ue arg_ue
    1559 1531
       where
    
    1560 1532
         err2 = mkNonFunAppMsg fun_ty arg_ty arg
    
    1561 1533
     
    
    1562
    -lintTyKind :: OutTyVar -> OutType -> LintM ()
    
    1563
    --- Both args have had substitution applied
    
    1564
    -
    
    1534
    +lintTyKind :: OutTyVar -> Type -> LintM ()
    
    1565 1535
     -- If you edit this function, you may need to update the GHC formalism
    
    1566 1536
     -- See Note [GHC Formalism]
    
    1567 1537
     lintTyKind tyvar arg_ty
    
    ... ... @@ -1579,36 +1549,36 @@ lintTyKind tyvar arg_ty
    1579 1549
     ************************************************************************
    
    1580 1550
     -}
    
    1581 1551
     
    
    1582
    -lintCaseExpr :: CoreExpr -> InId -> InType -> [CoreAlt] -> LintM (OutType, UsageEnv)
    
    1552
    +lintCaseExpr :: CoreExpr -> Id -> Type -> [CoreAlt] -> LintM (Type, UsageEnv)
    
    1583 1553
     lintCaseExpr scrut case_bndr alt_ty alts
    
    1584 1554
       = do { let e = Case scrut case_bndr alt_ty alts   -- Just for error messages
    
    1585 1555
     
    
    1586 1556
            -- Check the scrutinee
    
    1587
    -       ; (scrut_ty', scrut_ue) <- markAllJoinsBad $ lintCoreExpr scrut
    
    1557
    +       ; (scrut_ty, scrut_ue) <- markAllJoinsBad $ lintCoreExpr scrut
    
    1588 1558
                 -- See Note [Join points are less general than the paper]
    
    1589 1559
                 -- in GHC.Core
    
    1590 1560
     
    
    1591
    -       ; alt_ty' <- addLoc (CaseTy scrut) $ lintValueType alt_ty
    
    1561
    +       ; addLoc (CaseTy scrut) $ lintValueType alt_ty
    
    1592 1562
     
    
    1593
    -       ; checkCaseAlts e scrut scrut_ty' alts
    
    1563
    +       ; checkCaseAlts e scrut scrut_ty alts
    
    1594 1564
     
    
    1595 1565
            -- Lint the case-binder. Must do this after linting the scrutinee
    
    1596 1566
            -- because the case-binder isn't in scope in the scrutineex
    
    1597
    -       ; lintBinder CaseBind case_bndr $ \case_bndr' ->
    
    1567
    +       ; lintBinder CaseBind case_bndr $
    
    1598 1568
           -- Don't use lintIdBndr on case_bndr, because unboxed tuple is legitimate
    
    1599 1569
     
    
    1600
    -    do { let case_bndr_ty' = idType case_bndr'
    
    1601
    -             scrut_mult    = idMult case_bndr'
    
    1570
    +    do { let case_bndr_ty = idType case_bndr
    
    1571
    +             scrut_mult   = idMult case_bndr
    
    1602 1572
     
    
    1603
    -       ; ensureEqTys case_bndr_ty' scrut_ty' (mkScrutMsg case_bndr case_bndr_ty' scrut_ty')
    
    1573
    +       ; ensureEqTys case_bndr_ty scrut_ty (mkScrutMsg case_bndr case_bndr_ty scrut_ty)
    
    1604 1574
              -- See GHC.Core Note [Case expression invariants] item (7)
    
    1605 1575
     
    
    1606 1576
            ; -- Check the alternatives
    
    1607
    -       ; alt_ues <- mapM (lintCoreAlt case_bndr' scrut_ty' scrut_mult alt_ty') alts
    
    1577
    +       ; alt_ues <- mapM (lintCoreAlt case_bndr scrut_ty scrut_mult alt_ty) alts
    
    1608 1578
            ; let case_ue = (scaleUE scrut_mult scrut_ue) `addUE` supUEs alt_ues
    
    1609
    -       ; return (alt_ty', case_ue) } }
    
    1579
    +       ; return (alt_ty, case_ue) } }
    
    1610 1580
     
    
    1611
    -checkCaseAlts :: InExpr -> InExpr -> OutType -> [CoreAlt] -> LintM ()
    
    1581
    +checkCaseAlts :: CoreExpr -> CoreExpr -> Type -> [CoreAlt] -> LintM ()
    
    1612 1582
     -- a) Check that the alts are non-empty
    
    1613 1583
     -- b1) Check that the DEFAULT comes first, if it exists
    
    1614 1584
     -- b2) Check that the others are in increasing order
    
    ... ... @@ -1683,17 +1653,17 @@ checkCaseAlts e scrut scrut_ty alts
    1683 1653
         is_lit_alt (Alt (LitAlt _) _  _) = True
    
    1684 1654
         is_lit_alt _                     = False
    
    1685 1655
     
    
    1686
    -lintAltExpr :: CoreExpr -> OutType -> LintM UsageEnv
    
    1656
    +lintAltExpr :: CoreExpr -> Type -> LintM UsageEnv
    
    1687 1657
     lintAltExpr expr ann_ty
    
    1688 1658
       = do { (actual_ty, ue) <- lintCoreExpr expr
    
    1689 1659
            ; ensureEqTys actual_ty ann_ty (mkCaseAltMsg expr actual_ty ann_ty)
    
    1690 1660
            ; return ue }
    
    1691 1661
              -- See GHC.Core Note [Case expression invariants] item (6)
    
    1692 1662
     
    
    1693
    -lintCoreAlt :: OutId         -- Case binder
    
    1694
    -            -> OutType       -- Type of scrutinee
    
    1663
    +lintCoreAlt :: Id         -- Case binder
    
    1664
    +            -> Type       -- Type of scrutinee
    
    1695 1665
                 -> Mult          -- Multiplicity of scrutinee
    
    1696
    -            -> OutType       -- Type of the alternative
    
    1666
    +            -> Type       -- Type of the alternative
    
    1697 1667
                 -> CoreAlt
    
    1698 1668
                 -> LintM UsageEnv
    
    1699 1669
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1738,11 +1708,11 @@ lintCoreAlt case_bndr scrut_ty _scrut_mult alt_ty alt@(Alt (DataAlt con) args rh
    1738 1708
               ; multiplicities = map binderMult $ fst $ splitPiTys con_payload_ty }
    
    1739 1709
     
    
    1740 1710
             -- And now bring the new binders into scope
    
    1741
    -    ; lintBinders CasePatBind args $ \ args' -> do
    
    1711
    +    ; lintBinders CasePatBind args $ do
    
    1742 1712
           { rhs_ue <- lintAltExpr rhs alt_ty
    
    1743 1713
           ; rhs_ue' <- addLoc (CasePat alt) $
    
    1744 1714
                        lintAltBinders rhs_ue case_bndr scrut_ty con_payload_ty
    
    1745
    -                                  (zipEqual multiplicities  args')
    
    1715
    +                                  (zipEqual multiplicities  args)
    
    1746 1716
           ; return $ deleteUE rhs_ue' case_bndr
    
    1747 1717
           }
    
    1748 1718
        }
    
    ... ... @@ -1784,54 +1754,52 @@ lintLinearBinder doc actual_usage described_usage
    1784 1754
     -}
    
    1785 1755
     
    
    1786 1756
     -- When we lint binders, we (one at a time and in order):
    
    1787
    ---  1. Lint var types or kinds (possibly substituting)
    
    1788
    ---  2. Add the binder to the in scope set, and if its a coercion var,
    
    1789
    ---     we may extend the substitution to reflect its (possibly) new kind
    
    1790
    -lintBinders :: HasDebugCallStack => BindingSite -> [InVar] -> ([OutVar] -> LintM a) -> LintM a
    
    1791
    -lintBinders _    []         linterF = linterF []
    
    1792
    -lintBinders site (var:vars) linterF = lintBinder site var $ \var' ->
    
    1793
    -                                      lintBinders site vars $ \ vars' ->
    
    1794
    -                                      linterF (var':vars')
    
    1757
    +--  1. Lint var types or kinds
    
    1758
    +--  2. Add the binder to the in scope set
    
    1759
    +lintBinders :: HasDebugCallStack => BindingSite -> [Var] -> LintM a -> LintM a
    
    1760
    +lintBinders _    []         linterF = linterF
    
    1761
    +lintBinders site (var:vars) linterF = lintBinder site var $
    
    1762
    +                                      lintBinders site vars $
    
    1763
    +                                      linterF
    
    1795 1764
     
    
    1796 1765
     -- If you edit this function, you may need to update the GHC formalism
    
    1797 1766
     -- See Note [GHC Formalism]
    
    1798
    -lintBinder :: HasDebugCallStack => BindingSite -> InVar -> (OutVar -> LintM a) -> LintM a
    
    1767
    +lintBinder :: HasDebugCallStack => BindingSite -> Var -> LintM a -> LintM a
    
    1799 1768
     lintBinder site var linterF
    
    1800 1769
       | isTyCoVar var = lintTyCoBndr var linterF
    
    1801 1770
       | otherwise     = lintIdBndr NotTopLevel site var linterF
    
    1802 1771
     
    
    1803
    -lintTyCoBndr :: HasDebugCallStack => TyCoVar -> (OutTyCoVar -> LintM a) -> LintM a
    
    1772
    +lintTyCoBndr :: HasDebugCallStack => TyCoVar -> LintM a -> LintM a
    
    1804 1773
     lintTyCoBndr tcv thing_inside
    
    1805
    -  = do { tcv_type' <- lintTypeAndSubst (varType tcv)
    
    1806
    -       ; let tcv_kind' = typeKind tcv_type'
    
    1774
    +  = do { let tcv_type = varType tcv
    
    1775
    +             tcv_kind = typeKind tcv_type
    
    1807 1776
     
    
    1777
    +       ; lintType (varType tcv)
    
    1808 1778
              -- See (FORALL1) and (FORALL2) in GHC.Core.Type
    
    1809 1779
            ; if (isTyVar tcv)
    
    1810 1780
              then -- Check that in (forall (a:ki). blah) we have ki:Type
    
    1811
    -              lintL (isLiftedTypeKind tcv_kind') $
    
    1781
    +              lintL (isLiftedTypeKind tcv_kind) $
    
    1812 1782
                   hang (text "TyVar whose kind does not have kind Type:")
    
    1813
    -                 2 (ppr tcv <+> dcolon <+> ppr tcv_type' <+> dcolon <+> ppr tcv_kind')
    
    1783
    +                 2 (ppr tcv <+> dcolon <+> ppr tcv_type <+> dcolon <+> ppr tcv_kind)
    
    1814 1784
              else -- Check that in (forall (cv::ty). blah),
    
    1815 1785
                   -- then ty looks like (t1 ~# t2)
    
    1816
    -              lintL (isCoVarType tcv_type') $
    
    1786
    +              lintL (isCoVarType tcv_type) $
    
    1817 1787
                   text "CoVar with non-coercion type:" <+> pprTyVar tcv
    
    1818 1788
     
    
    1819
    -       ; addInScopeTyCoVar tcv tcv_type' thing_inside }
    
    1789
    +       ; addInScopeTyCoVar tcv thing_inside }
    
    1820 1790
     
    
    1821
    -lintIdBndrs :: forall a. TopLevelFlag -> [InId] -> ([OutId] -> LintM a) -> LintM a
    
    1791
    +lintIdBndrs :: forall a. TopLevelFlag -> [Id] -> LintM a -> LintM a
    
    1822 1792
     lintIdBndrs top_lvl ids thing_inside
    
    1823 1793
       = go ids thing_inside
    
    1824 1794
       where
    
    1825
    -    go :: [Id] -> ([Id] -> LintM a) -> LintM a
    
    1826
    -    go []       thing_inside = thing_inside []
    
    1827
    -    go (id:ids) thing_inside = lintIdBndr top_lvl LetBind id  $ \id' ->
    
    1828
    -                               go ids                         $ \ids' ->
    
    1829
    -                               thing_inside (id' : ids')
    
    1795
    +    go :: [Id] -> LintM a -> LintM a
    
    1796
    +    go []       thing_inside = thing_inside
    
    1797
    +    go (id:ids) thing_inside = lintIdBndr top_lvl LetBind id  $
    
    1798
    +                               go ids                         $
    
    1799
    +                               thing_inside
    
    1830 1800
     
    
    1831 1801
     lintIdBndr :: TopLevelFlag -> BindingSite
    
    1832
    -           -> InVar -> (OutVar -> LintM a) -> LintM a
    
    1833
    --- Do substitution on the type of a binder and add the var with this
    
    1834
    --- new type to the in-scope set of the second argument
    
    1802
    +           -> Var -> LintM a -> LintM a
    
    1835 1803
     -- ToDo: lint its rules
    
    1836 1804
     lintIdBndr top_lvl bind_site id thing_inside
    
    1837 1805
       = assertPpr (isId id) (ppr id) $
    
    ... ... @@ -1864,14 +1832,16 @@ lintIdBndr top_lvl bind_site id thing_inside
    1864 1832
            ; lintL (not (isCoVarType id_ty))
    
    1865 1833
                    (text "Non-CoVar has coercion type" <+> ppr id <+> dcolon <+> ppr id_ty)
    
    1866 1834
     
    
    1867
    -       -- Check that the lambda binder has no value or OtherCon unfolding.
    
    1835
    +       -- Check that lambda-bound Ids have no unfolding; not even OtherCon
    
    1868 1836
            -- See #21496
    
    1869
    -       ; lintL (not (bind_site == LambdaBind && isEvaldUnfolding (idUnfolding id)))
    
    1870
    -                (text "Lambda binder with value or OtherCon unfolding.")
    
    1837
    +       ; let unf = idUnfolding id
    
    1838
    +       ; checkL (not (bind_site == LambdaBind && hasSomeUnfolding unf)) $
    
    1839
    +         hang (text "Lambda binder" <+> quotes (ppr id) <+> text "has an unfolding")
    
    1840
    +            2 (ppr unf)
    
    1871 1841
     
    
    1872
    -       ; out_ty <- addLoc (IdTy id) (lintValueType id_ty)
    
    1842
    +       ; addLoc (IdTy id) (lintValueType id_ty)
    
    1873 1843
     
    
    1874
    -       ; addInScopeId id out_ty thing_inside }
    
    1844
    +       ; addInScopeId id thing_inside }
    
    1875 1845
       where
    
    1876 1846
         id_ty = idType id
    
    1877 1847
     
    
    ... ... @@ -1891,62 +1861,44 @@ lintIdBndr top_lvl bind_site id thing_inside
    1891 1861
     {- Note [Linting types and coercions]
    
    1892 1862
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1893 1863
     Notice that
    
    1894
    -   lintType     :: InType     -> LintM ()
    
    1895
    -   lintCoercion :: InCoercion -> LintM ()
    
    1864
    +   lintType     :: Type     -> LintM ()
    
    1865
    +   lintCoercion :: Coercion -> LintM ()
    
    1896 1866
     Neither returns anything.
    
    1897 1867
     
    
    1898
    -If you need the kind of the type, then do `typeKind` and then apply
    
    1899
    -the ambient substitution using `substTyM`.  Note that the substitution
    
    1900
    -empty unless there is shadowing or type-lets; and if the substitution is
    
    1901
    -empty, the `substTyM` is a no-op.
    
    1902
    -
    
    1903
    -It is better to take the kind and then substitute, rather than substitute
    
    1904
    -and then take the kind, becaues the kind is usually smaller.
    
    1905
    -
    
    1906
    -Note: you might wonder if we should apply the same logic to expressions.
    
    1907
    -Why do we have
    
    1908
    -  lintExpr :: InExpr -> LintM OutType
    
    1909
    -Partly inertia; but also taking the type of an expresison involve looking
    
    1910
    -down a deep chain of let's, whereas that is not true of taking the kind
    
    1911
    -of a type.  It'd be worth an experiment though.
    
    1912
    -
    
    1913
    -Historical note: in the olden days we had
    
    1914
    -   lintType :: InType -> LintM OutType
    
    1915
    -but that burned a huge amount of allocation building an OutType that was
    
    1916
    -often discarded, or used only to get its kind.
    
    1917
    -
    
    1918
    -I also experimented with
    
    1919
    -   lintType :: InType -> LintM OutKind
    
    1920
    -but that too was slower.  It is also much simpler to return ()!  If we
    
    1921
    -return the kind we have to duplicate the logic in `typeKind`; and it is
    
    1922
    -much worse for coercions.
    
    1868
    +Note: you might wonder why we have
    
    1869
    +  lintExpr :: CoreExpr -> LintM Type
    
    1870
    +  lintType :: Type     -> LintM ()
    
    1871
    +
    
    1872
    +That is, linting an expression yields its type, but linting a type does not
    
    1873
    +yield its kind. Partly inertia; but:
    
    1874
    +
    
    1875
    +* Taking the type of an expresison involves looking down a deep chain of let's,
    
    1876
    +  whereas that is not true of taking the kind of a type.  It'd be worth an
    
    1877
    +  experiment though.
    
    1878
    +
    
    1879
    +* I did experiment with
    
    1880
    +   lintType :: Type -> LintM Kind
    
    1881
    +  but that too was slower.  It is also much simpler to return ()!  If we return
    
    1882
    +  the kind we have to duplicate the logic in `typeKind`; and it is much worse
    
    1883
    +  for coercions.
    
    1923 1884
     -}
    
    1924 1885
     
    
    1925
    -lintValueType :: Type -> LintM OutType
    
    1886
    +lintValueType :: Type -> LintM ()
    
    1926 1887
     -- Types only, not kinds
    
    1927
    --- Check the type, and apply the substitution to it
    
    1928
    --- See Note [Linting type lets]
    
    1929 1888
     lintValueType ty
    
    1930 1889
       = addLoc (InType ty) $
    
    1931
    -    do  { ty' <- lintTypeAndSubst ty
    
    1932
    -        ; let sk = typeKind ty'
    
    1890
    +    do  { lintType ty
    
    1891
    +        ; let sk = typeKind ty
    
    1933 1892
             ; lintL (isTYPEorCONSTRAINT sk) $
    
    1934 1893
               hang (text "Ill-kinded type:" <+> ppr ty)
    
    1935
    -             2 (text "has kind:" <+> ppr sk)
    
    1936
    -        ; return ty' }
    
    1894
    +             2 (text "has kind:" <+> ppr sk)}
    
    1937 1895
     
    
    1938 1896
     checkTyCon :: TyCon -> LintM ()
    
    1939 1897
     checkTyCon tc
    
    1940 1898
       = checkL (not (isTcTyCon tc)) (text "Found TcTyCon:" <+> ppr tc)
    
    1941 1899
     
    
    1942 1900
     -------------------
    
    1943
    -lintTypeAndSubst :: InType -> LintM OutType
    
    1944
    -lintTypeAndSubst ty = do { lintType ty; substTyM ty }
    
    1945
    -           -- In GHCi we may lint an expression with a free
    
    1946
    -           -- type variable.  Then it won't be in the
    
    1947
    -           -- substitution, but it should be in scope
    
    1948
    -
    
    1949
    -lintType :: InType -> LintM ()
    
    1901
    +lintType :: Type -> LintM ()
    
    1950 1902
     -- See Note [Linting types and coercions]
    
    1951 1903
     --
    
    1952 1904
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -1956,8 +1908,7 @@ lintType (TyVarTy tv)
    1956 1908
       = failWithL (mkBadTyVarMsg tv)
    
    1957 1909
     
    
    1958 1910
       | otherwise
    
    1959
    -  = do { _ <- lintVarOcc tv
    
    1960
    -       ; return () }
    
    1911
    +  = lintVarOcc tv
    
    1961 1912
     
    
    1962 1913
     lintType ty@(AppTy t1 t2)
    
    1963 1914
       | TyConApp {} <- t1
    
    ... ... @@ -1965,7 +1916,7 @@ lintType ty@(AppTy t1 t2)
    1965 1916
       | otherwise
    
    1966 1917
       = do { let (fun_ty, arg_tys) = collect t1 [t2]
    
    1967 1918
            ; lintType fun_ty
    
    1968
    -       ; fun_kind <- substTyM (typeKind fun_ty)
    
    1919
    +       ; let fun_kind = typeKind fun_ty
    
    1969 1920
            ; lint_ty_app ty fun_kind arg_tys }
    
    1970 1921
       where
    
    1971 1922
         collect (AppTy f a) as = collect f (a:as)
    
    ... ... @@ -1997,21 +1948,21 @@ lintType ty@(FunTy af tw t1 t2)
    1997 1948
     lintType ty@(ForAllTy {})
    
    1998 1949
       = go [] ty
    
    1999 1950
       where
    
    2000
    -    go :: [OutTyCoVar] -> InType -> LintM ()
    
    1951
    +    go :: [OutTyCoVar] -> Type -> LintM ()
    
    2001 1952
         -- Loop, collecting the forall-binders
    
    2002 1953
         go tcvs ty@(ForAllTy (Bndr tcv _) body_ty)
    
    2003 1954
           | not (isTyCoVar tcv)
    
    2004 1955
           = failWithL (text "Non-TyVar or Non-CoVar bound in type:" <+> ppr ty)
    
    2005 1956
     
    
    2006 1957
           | otherwise
    
    2007
    -      = lintTyCoBndr tcv $ \tcv' ->
    
    1958
    +      = lintTyCoBndr tcv $
    
    2008 1959
             do { -- See GHC.Core.TyCo.Rep Note [Unused coercion variable in ForAllTy]
    
    2009 1960
                  -- Suspicious because it works on InTyCoVar; c.f. ForAllCo
    
    2010 1961
                  when (isCoVar tcv) $
    
    2011 1962
                  lintL (anyFreeVarsOfType (== tcv) body_ty) $
    
    2012 1963
                  text "Covar does not occur in the body:" <+> (ppr tcv $$ ppr body_ty)
    
    2013 1964
     
    
    2014
    -           ; go (tcv' : tcvs) body_ty }
    
    1965
    +           ; go (tcv : tcvs) body_ty }
    
    2015 1966
     
    
    2016 1967
         go tcvs body_ty
    
    2017 1968
           = do { lintType body_ty
    
    ... ... @@ -2019,7 +1970,7 @@ lintType ty@(ForAllTy {})
    2019 1970
     
    
    2020 1971
     lintType (CastTy ty co)
    
    2021 1972
       = do { lintType ty
    
    2022
    -       ; ty_kind <- substTyM (typeKind ty)
    
    1973
    +       ; let ty_kind = typeKind ty
    
    2023 1974
            ; co_lk <- lintStarCoercion co
    
    2024 1975
            ; ensureEqTys ty_kind co_lk (mkCastTyErr ty co ty_kind co_lk) }
    
    2025 1976
     
    
    ... ... @@ -2027,14 +1978,14 @@ lintType (LitTy l) = lintTyLit l
    2027 1978
     lintType (CoercionTy co) = lintCoercion co
    
    2028 1979
     
    
    2029 1980
     -----------------
    
    2030
    -lintForAllBody :: [OutTyCoVar] -> InType -> LintM ()
    
    1981
    +lintForAllBody :: [OutTyCoVar] -> Type -> LintM ()
    
    2031 1982
     -- Do the checks for the body of a forall-type
    
    2032 1983
     lintForAllBody tcvs body_ty
    
    2033 1984
       = do { -- For type variables, check for skolem escape
    
    2034 1985
              -- See Note [Phantom type variables in kinds] in GHC.Core.Type
    
    2035 1986
              -- The kind of (forall cv. th) is liftedTypeKind, so no
    
    2036 1987
              -- need to check for skolem-escape in the CoVar case
    
    2037
    -         body_kind <- substTyM (typeKind body_ty)
    
    1988
    +         let body_kind = typeKind body_ty
    
    2038 1989
            ; case occCheckExpand tcvs body_kind of
    
    2039 1990
                Just {} -> return ()
    
    2040 1991
                Nothing -> failWithL $
    
    ... ... @@ -2045,7 +1996,7 @@ lintForAllBody tcvs body_ty
    2045 1996
            ; checkValueType body_kind (text "the body of forall:" <+> ppr body_ty) }
    
    2046 1997
     
    
    2047 1998
     -----------------
    
    2048
    -lintTySynFamApp :: Bool -> InType -> TyCon -> [InType] -> LintM ()
    
    1999
    +lintTySynFamApp :: Bool -> Type -> TyCon -> [Type] -> LintM ()
    
    2049 2000
     -- The TyCon is a type synonym or a type family (not a data family)
    
    2050 2001
     -- See Note [Linting type synonym applications]
    
    2051 2002
     -- c.f. GHC.Tc.Validity.check_syn_tc_app
    
    ... ... @@ -2071,21 +2022,21 @@ lintTySynFamApp report_unsat ty tc tys
    2071 2022
     
    
    2072 2023
     -----------------
    
    2073 2024
     -- Confirms that a kind is really TYPE r or Constraint
    
    2074
    -checkValueType :: OutKind -> SDoc -> LintM ()
    
    2025
    +checkValueType :: Kind -> SDoc -> LintM ()
    
    2075 2026
     checkValueType kind doc
    
    2076 2027
       = lintL (isTYPEorCONSTRAINT kind)
    
    2077 2028
               (text "Non-Type-like kind when Type-like expected:" <+> ppr kind $$
    
    2078 2029
                text "when checking" <+> doc)
    
    2079 2030
     
    
    2080 2031
     -----------------
    
    2081
    -lintArrow :: SDoc -> FunTyFlag -> InType -> InType -> InType -> LintM ()
    
    2032
    +lintArrow :: SDoc -> FunTyFlag -> Type -> Type -> Type -> LintM ()
    
    2082 2033
     -- If you edit this function, you may need to update the GHC formalism
    
    2083 2034
     -- See Note [GHC Formalism]
    
    2084 2035
     lintArrow what af t1 t2 tw  -- Eg lintArrow "type or kind `blah'" k1 k2 kw
    
    2085 2036
                                 -- or lintArrow "coercion `blah'" k1 k2 kw
    
    2086
    -  = do { k1 <- substTyM (typeKind t1)
    
    2087
    -       ; k2 <- substTyM (typeKind t2)
    
    2088
    -       ; kw <- substTyM (typeKind tw)
    
    2037
    +  = do { let k1 = typeKind t1
    
    2038
    +             k2 = typeKind t2
    
    2039
    +             kw = typeKind tw
    
    2089 2040
            ; unless (isTYPEorCONSTRAINT k1) (report (text "argument")     t1 k1)
    
    2090 2041
            ; unless (isTYPEorCONSTRAINT k2) (report (text "result")       t2 k2)
    
    2091 2042
            ; unless (isMultiplicityTy kw)   (report (text "multiplicity") tw kw)
    
    ... ... @@ -2111,34 +2062,34 @@ lintTyLit (StrTyLit _) = return ()
    2111 2062
     lintTyLit (CharTyLit _) = return ()
    
    2112 2063
     
    
    2113 2064
     -----------------
    
    2114
    -lint_ty_app :: InType -> OutKind -> [InType] -> LintM ()
    
    2065
    +lint_ty_app :: Type -> Kind -> [Type] -> LintM ()
    
    2115 2066
     lint_ty_app ty = lint_tyco_app (text "type" <+> quotes (ppr ty))
    
    2116 2067
     
    
    2117
    -lint_co_app :: HasDebugCallStack => Coercion -> OutKind -> [InType] -> LintM ()
    
    2068
    +lint_co_app :: HasDebugCallStack => Coercion -> Kind -> [Type] -> LintM ()
    
    2118 2069
     lint_co_app co = lint_tyco_app (text "coercion" <+> quotes (ppr co))
    
    2119 2070
     
    
    2120
    -lint_tyco_app :: SDoc -> OutKind -> [InType] -> LintM ()
    
    2071
    +lint_tyco_app :: SDoc -> Kind -> [Type] -> LintM ()
    
    2121 2072
     lint_tyco_app msg fun_kind arg_tys
    
    2122 2073
         -- See Note [Avoiding compiler perf traps when constructing error messages.]
    
    2123
    -  = do { _ <- lintApp msg (\ty     -> do { lintType ty; substTyM ty })
    
    2124
    -                            (\ty _ _ -> do { lintType ty; ki <- substTyM (typeKind ty); return (ki,()) })
    
    2125
    -                            fun_kind arg_tys ()
    
    2074
    +  = do { _ <- lintApp msg (\ty     -> do { lintType ty; return ty })
    
    2075
    +                          (\ty _ _ -> do { lintType ty; return (typeKind ty,()) })
    
    2076
    +                          fun_kind arg_tys ()
    
    2126 2077
            ; return () }
    
    2127 2078
     
    
    2128 2079
     ----------------
    
    2129
    -lintApp :: forall in_a acc. Outputable in_a =>
    
    2080
    +lintApp :: forall a acc. Outputable a =>
    
    2130 2081
                  SDoc
    
    2131
    -          -> (in_a -> LintM OutType)                        -- Lint the thing and return its value
    
    2132
    -          -> (in_a -> Mult -> acc -> LintM (OutKind, acc))  -- Lint the thing and return its type
    
    2133
    -          -> OutType
    
    2134
    -          -> [in_a]                               -- The arguments, always "In" things
    
    2135
    -          -> acc                                  -- Used (only) for UsageEnv in /term/ applications
    
    2136
    -          -> LintM (OutType,acc)
    
    2082
    +          -> (a -> LintM Type)                        -- Lint the thing and return its value
    
    2083
    +          -> (a -> Mult -> acc -> LintM (Kind, acc))  -- Lint the thing and return its type
    
    2084
    +          -> Type
    
    2085
    +          -> [a]                          -- The arguments
    
    2086
    +          -> acc                          -- Used (only) for UsageEnv in /term/ applications
    
    2087
    +          -> LintM (Type,acc)
    
    2137 2088
     -- lintApp is a performance-critical function, which deals with multiple
    
    2138 2089
     -- applications such as  (/\a./\b./\c. expr) @ta @tb @tc
    
    2139 2090
     -- When returning the type of this expression we want to avoid substituting a:=ta,
    
    2140 2091
     -- and /then/ substituting b:=tb, etc.  That's quadratic, and can be a huge
    
    2141
    --- perf hole.  So we gather all the arguments [in_a], and then gather the
    
    2092
    +-- perf hole.  So we gather all the arguments [a], and then gather the
    
    2142 2093
     -- substitution incrementally in the `go` loop.
    
    2143 2094
     --
    
    2144 2095
     -- lintApp is used:
    
    ... ... @@ -2158,7 +2109,7 @@ lintApp msg lint_forall_arg lint_arrow_arg !orig_fun_ty all_args acc
    2158 2109
     
    
    2159 2110
              ; let init_subst = mkEmptySubst in_scope
    
    2160 2111
     
    
    2161
    -               go :: Subst -> OutType -> acc -> [in_a] -> LintM (OutType, acc)
    
    2112
    +               go :: Subst -> Type -> acc -> [a] -> LintM (Type, acc)
    
    2162 2113
                          -- The Subst applies (only) to the fun_ty
    
    2163 2114
                          -- c.f. GHC.Core.Type.piResultTys, which has a similar loop
    
    2164 2115
     
    
    ... ... @@ -2202,7 +2153,7 @@ lintApp msg lint_forall_arg lint_arrow_arg !orig_fun_ty all_args acc
    2202 2153
     -- explicitly and don't capture them as free variables. Otherwise this binder might
    
    2203 2154
     -- become a thunk that get's allocated in the hot code path.
    
    2204 2155
     -- See Note [Avoiding compiler perf traps when constructing error messages.]
    
    2205
    -lint_app_fail_msg :: (Outputable a2) => SDoc -> OutType -> a2 -> SDoc -> SDoc
    
    2156
    +lint_app_fail_msg :: (Outputable a2) => SDoc -> Type -> a2 -> SDoc -> SDoc
    
    2206 2157
     lint_app_fail_msg msg kfn arg_tys extra
    
    2207 2158
       = vcat [ hang (text "Application error in") 2 msg
    
    2208 2159
              , nest 2 (text "Function type =" <+> ppr kfn)
    
    ... ... @@ -2215,7 +2166,7 @@ lint_app_fail_msg msg kfn arg_tys extra
    2215 2166
     *                                                                      *
    
    2216 2167
     ********************************************************************* -}
    
    2217 2168
     
    
    2218
    -lintCoreRule :: OutVar -> OutType -> CoreRule -> LintM ()
    
    2169
    +lintCoreRule :: OutVar -> Type -> CoreRule -> LintM ()
    
    2219 2170
     lintCoreRule _ _ (BuiltinRule {})
    
    2220 2171
       = return ()  -- Don't bother
    
    2221 2172
     
    
    ... ... @@ -2223,7 +2174,7 @@ lintCoreRule fun fun_ty rule@(Rule { ru_name = name, ru_bndrs = bndrs
    2223 2174
                                        , ru_args = args, ru_rhs = rhs })
    
    2224 2175
       = noMultiplicityChecks $ -- Skip linearity checking for rules
    
    2225 2176
                                -- See Note [Linting linearity]
    
    2226
    -    lintBinders LambdaBind bndrs $ \ _ ->
    
    2177
    +    lintBinders LambdaBind bndrs $
    
    2227 2178
         do { (lhs_ty, _) <- lintCoreArgs (fun_ty, zeroUE) args
    
    2228 2179
            ; (rhs_ty, _) <- case idJoinPointHood fun of
    
    2229 2180
                          JoinPoint join_arity
    
    ... ... @@ -2311,10 +2262,10 @@ Note [Join points and unfoldings/rules] in "GHC.Core.Opt.OccurAnal" for further
    2311 2262
     
    
    2312 2263
     {- Note [Asymptotic efficiency]
    
    2313 2264
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2314
    -When linting coercions (and types actually) we return a linted
    
    2315
    -(substituted) coercion.  Then we often have to take the coercionKind of
    
    2316
    -that returned coercion. If we get long chains, that can be asymptotically
    
    2317
    -inefficient, notably in
    
    2265
    +When linting coercions we traverse the coercion. Then we often have to take the
    
    2266
    +coercionKind of that returned coercion. If we get long chains, that can be
    
    2267
    +asymptotically inefficient, notably in
    
    2268
    +
    
    2318 2269
     * TransCo
    
    2319 2270
     * InstCo
    
    2320 2271
     * SelCo (cf #9233)
    
    ... ... @@ -2326,30 +2277,23 @@ the bad perf bites us in practice.
    2326 2277
     A solution would be to return the kind and role of the coercion,
    
    2327 2278
     as well as the linted coercion.  Or perhaps even *only* the kind and role,
    
    2328 2279
     which is what used to happen.   But that proved tricky and error prone
    
    2329
    -(#17923), so now we return the coercion.
    
    2280
    +(#17923).
    
    2330 2281
     -}
    
    2331 2282
     
    
    2332 2283
     
    
    2333 2284
     -- lintStarCoercion lints a coercion, confirming that its lh kind and
    
    2334 2285
     -- its rh kind are both *; also ensures that the role is Nominal
    
    2335 2286
     -- Returns the lh kind
    
    2336
    -lintStarCoercion :: InCoercion -> LintM OutType
    
    2287
    +lintStarCoercion :: Coercion -> LintM Type
    
    2337 2288
     lintStarCoercion g
    
    2338 2289
       = do { lintCoercion g
    
    2339
    -       ; Pair t1 t2 <- substCoKindM g
    
    2290
    +       ; let Pair t1 t2 = coercionKind g
    
    2340 2291
            ; checkValueType (typeKind t1) (text "the kind of the left type in" <+> ppr g)
    
    2341 2292
            ; checkValueType (typeKind t2) (text "the kind of the right type in" <+> ppr g)
    
    2342 2293
            ; lintRole g Nominal (coercionRole g)
    
    2343 2294
            ; return t1 }
    
    2344 2295
     
    
    2345
    -substCoKindM :: InCoercion -> LintM (Pair OutType)
    
    2346
    -substCoKindM co
    
    2347
    -  = do { let !(Pair lk rk) = coercionKind co
    
    2348
    -       ; lk' <- substTyM lk
    
    2349
    -       ; rk' <- substTyM rk
    
    2350
    -       ; return (Pair lk' rk') }
    
    2351
    -
    
    2352
    -lintCoercion :: HasDebugCallStack => InCoercion -> LintM ()
    
    2296
    +lintCoercion :: HasDebugCallStack => Coercion -> LintM ()
    
    2353 2297
     -- See Note [Linting types and coercions]
    
    2354 2298
     --
    
    2355 2299
     -- If you edit this function, you may need to update the GHC formalism
    
    ... ... @@ -2361,7 +2305,7 @@ lintCoercion (CoVarCo cv)
    2361 2305
                       2 (text "With offending type:" <+> ppr (varType cv)))
    
    2362 2306
     
    
    2363 2307
       | otherwise  -- C.f. lintType (TyVarTy tv), which has better docs
    
    2364
    -  = do { _ <- lintVarOcc cv; return () }
    
    2308
    +  = lintVarOcc cv
    
    2365 2309
     
    
    2366 2310
     lintCoercion (Refl ty)          = lintType ty
    
    2367 2311
     lintCoercion (GRefl _r ty MRefl) = lintType ty
    
    ... ... @@ -2369,8 +2313,8 @@ lintCoercion (GRefl _r ty MRefl) = lintType ty
    2369 2313
     lintCoercion (GRefl _r ty (MCo co))
    
    2370 2314
       = do { lintType ty
    
    2371 2315
            ; lintCoercion co
    
    2372
    -       ; tk <- substTyM (typeKind ty)
    
    2373
    -       ; tl <- substTyM (coercionLKind co)
    
    2316
    +       ; let tk = typeKind ty
    
    2317
    +             tl = coercionLKind co
    
    2374 2318
            ; ensureEqTys tk tl $
    
    2375 2319
              hang (text "GRefl coercion kind mis-match:" <+> ppr co)
    
    2376 2320
                 2 (vcat [ppr ty, ppr tk, ppr tl])
    
    ... ... @@ -2403,8 +2347,8 @@ lintCoercion co@(AppCo co1 co2)
    2403 2347
       = do { lintCoercion co1
    
    2404 2348
            ; lintCoercion co2
    
    2405 2349
            ; let !(Pair lt1 rt1) = coercionKind co1
    
    2406
    -       ; lk1 <- substTyM (typeKind lt1)
    
    2407
    -       ; rk1 <- substTyM (typeKind rt1)
    
    2350
    +             lk1 = typeKind lt1
    
    2351
    +             rk1 = typeKind rt1
    
    2408 2352
            ; lint_co_app co lk1 [coercionLKind co2]
    
    2409 2353
            ; lint_co_app co rk1 [coercionRKind co2]
    
    2410 2354
     
    
    ... ... @@ -2421,7 +2365,7 @@ lintCoercion co@(ForAllCo {})
    2421 2365
       = do { _ <- go [] co; return () }
    
    2422 2366
       where
    
    2423 2367
         go :: [OutTyCoVar]   -- Binders in reverse order
    
    2424
    -       -> InCoercion -> LintM Role
    
    2368
    +       -> Coercion -> LintM Role
    
    2425 2369
         go tcvs co@(ForAllCo { fco_tcv = tcv, fco_visL = visL, fco_visR = visR
    
    2426 2370
                              , fco_kind = kind_mco, fco_body = body_co })
    
    2427 2371
           | not (isTyCoVar tcv)
    
    ... ... @@ -2431,15 +2375,15 @@ lintCoercion co@(ForAllCo {})
    2431 2375
           = do { mb_lk <- case kind_mco of
    
    2432 2376
                          MRefl -> return Nothing
    
    2433 2377
                          MCo kind_co -> Just <$> lintStarCoercion kind_co
    
    2434
    -           ; lintTyCoBndr tcv $ \tcv' ->
    
    2378
    +           ; lintTyCoBndr tcv $
    
    2435 2379
             do { case mb_lk of
    
    2436 2380
                     Nothing -> return ()
    
    2437
    -                Just lk -> ensureEqTys (varType tcv') lk $
    
    2381
    +                Just lk -> ensureEqTys (varType tcv) lk $
    
    2438 2382
                                text "Kind mis-match in ForallCo" <+> ppr co
    
    2439 2383
     
    
    2440 2384
                -- I'm not very sure about this part, because it traverses body_co
    
    2441 2385
                -- but at least it's on a cold path (a ForallCo for a CoVar)
    
    2442
    -           -- Also it works on InTyCoVar and InCoercion, which is suspect
    
    2386
    +           -- Also it works on InTyCoVar and Coercion, which is suspect
    
    2443 2387
                ; when (isCoVar tcv) $
    
    2444 2388
                  do { lintL (visL == coreTyLamForAllTyFlag && visR == coreTyLamForAllTyFlag) $
    
    2445 2389
                       text "Invalid visibility flags in CoVar ForAllCo" <+> ppr co
    
    ... ... @@ -2448,7 +2392,7 @@ lintCoercion co@(ForAllCo {})
    2448 2392
                       text "Covar can only appear in Refl and GRefl: " <+> ppr co }
    
    2449 2393
                       -- See (FC6) in Note [ForAllCo] in GHC.Core.TyCo.Rep
    
    2450 2394
     
    
    2451
    -           ; role <- go (tcv':tcvs) body_co
    
    2395
    +           ; role <- go (tcv:tcvs) body_co
    
    2452 2396
     
    
    2453 2397
                ; when (role == Nominal) $
    
    2454 2398
                  lintL (visL `eqForAllVis` visR) $
    
    ... ... @@ -2505,8 +2449,8 @@ lintCoercion co@(UnivCo { uco_role = r, uco_prov = prov
    2505 2449
            -- Check the to and from types
    
    2506 2450
            ; lintType ty1
    
    2507 2451
            ; lintType ty2
    
    2508
    -       ; tk1 <- substTyM (typeKind ty1)
    
    2509
    -       ; tk2 <- substTyM (typeKind ty2)
    
    2452
    +       ; let tk1 = typeKind ty1
    
    2453
    +             tk2 = typeKind ty2
    
    2510 2454
     
    
    2511 2455
            ; when (r /= Phantom && isTYPEorCONSTRAINT tk1 && isTYPEorCONSTRAINT tk2)
    
    2512 2456
                   (checkTypes ty1 ty2)
    
    ... ... @@ -2560,8 +2504,8 @@ lintCoercion (SymCo co) = lintCoercion co
    2560 2504
     lintCoercion co@(TransCo co1 co2)
    
    2561 2505
       = do { lintCoercion co1
    
    2562 2506
            ; lintCoercion co2
    
    2563
    -       ; rk1 <- substTyM (coercionRKind co1)
    
    2564
    -       ; lk2 <- substTyM (coercionLKind co2)
    
    2507
    +       ; let rk1 = coercionRKind co1
    
    2508
    +             lk2 = coercionLKind co2
    
    2565 2509
            ; ensureEqTys rk1 lk2
    
    2566 2510
                    (hang (text "Trans coercion mis-match:" <+> ppr co)
    
    2567 2511
                        2 (vcat [ppr (coercionKind co1), ppr (coercionKind co2)]))
    
    ... ... @@ -2569,7 +2513,7 @@ lintCoercion co@(TransCo co1 co2)
    2569 2513
     
    
    2570 2514
     lintCoercion the_co@(SelCo cs co)
    
    2571 2515
       = do { lintCoercion co
    
    2572
    -       ; Pair s t <- substCoKindM co
    
    2516
    +       ; let Pair s t = coercionKind co
    
    2573 2517
     
    
    2574 2518
            ; if -- forall (both TyVar and CoVar)
    
    2575 2519
                 | Just _ <- splitForAllTyCoVar_maybe s
    
    ... ... @@ -2604,7 +2548,7 @@ lintCoercion the_co@(SelCo cs co)
    2604 2548
     
    
    2605 2549
     lintCoercion the_co@(LRCo _lr co)
    
    2606 2550
       = do { lintCoercion co
    
    2607
    -       ; Pair s t <- substCoKindM co
    
    2551
    +       ; let Pair s t = coercionKind co
    
    2608 2552
            ; lintRole co Nominal (coercionRole co)
    
    2609 2553
            ; case (splitAppTy_maybe s, splitAppTy_maybe t) of
    
    2610 2554
                (Just {}, Just {}) -> return ()
    
    ... ... @@ -2618,14 +2562,12 @@ lintCoercion orig_co@(InstCo co arg)
    2618 2562
         go (InstCo co arg) args = do { lintCoercion arg; go co (arg:args) }
    
    2619 2563
         go co              args = do { lintCoercion co
    
    2620 2564
                                      ; let Pair lty rty = coercionKind co
    
    2621
    -                                 ; lty' <- substTyM lty
    
    2622
    -                                 ; rty' <- substTyM rty
    
    2623 2565
                                      ; in_scope <- getInScope
    
    2624 2566
                                      ; let subst = mkEmptySubst in_scope
    
    2625
    -                                 ; go_args (subst, lty') (subst,rty') args }
    
    2567
    +                                 ; go_args (subst, lty) (subst,rty) args }
    
    2626 2568
     
    
    2627 2569
         -------------
    
    2628
    -    go_args :: (Subst, OutType) -> (Subst,OutType) -> [InCoercion]
    
    2570
    +    go_args :: (Subst, Type) -> (Subst,Type) -> [Coercion]
    
    2629 2571
                -> LintM ()
    
    2630 2572
         go_args _ _ []
    
    2631 2573
           = return ()
    
    ... ... @@ -2634,11 +2576,11 @@ lintCoercion orig_co@(InstCo co arg)
    2634 2576
                ; go_args lty1 rty1 args }
    
    2635 2577
     
    
    2636 2578
         -------------
    
    2637
    -    go_arg :: (Subst, OutType) -> (Subst,OutType) -> InCoercion
    
    2638
    -           -> LintM ((Subst,OutType), (Subst,OutType))
    
    2579
    +    go_arg :: (Subst, Type) -> (Subst,Type) -> Coercion
    
    2580
    +           -> LintM ((Subst,Type), (Subst,Type))
    
    2639 2581
         go_arg (lsubst,lty) (rsubst,rty) arg
    
    2640 2582
           = do { lintRole arg Nominal (coercionRole arg)
    
    2641
    -           ; Pair arg_lty arg_rty <- substCoKindM arg
    
    2583
    +           ; let Pair arg_lty arg_rty = coercionKind arg
    
    2642 2584
     
    
    2643 2585
                ; case (splitForAllTyCoVar_maybe lty, splitForAllTyCoVar_maybe rty) of
    
    2644 2586
                   -- forall over tvar
    
    ... ... @@ -2662,11 +2604,11 @@ lintCoercion orig_co@(InstCo co arg)
    2662 2604
     lintCoercion this_co@(AxiomCo ax cos)
    
    2663 2605
       = do { mapM_ lintCoercion cos
    
    2664 2606
            ; lint_roles 0 (coAxiomRuleArgRoles ax) cos
    
    2665
    -       ; prs <- mapM substCoKindM cos
    
    2607
    +       ; let prs = map coercionKind cos
    
    2666 2608
            ; lint_ax ax prs }
    
    2667 2609
     
    
    2668 2610
       where
    
    2669
    -    lint_ax :: CoAxiomRule -> [Pair OutType] -> LintM ()
    
    2611
    +    lint_ax :: CoAxiomRule -> [Pair Type] -> LintM ()
    
    2670 2612
         lint_ax (BuiltInFamRew  bif) prs
    
    2671 2613
           = checkL (isJust (bifrw_proves bif prs))  bad_bif
    
    2672 2614
         lint_ax (BuiltInFamInj bif) prs
    
    ... ... @@ -2754,8 +2696,8 @@ lintBranch this_co fam_tc branch arg_kinds
    2754 2696
       = do { checkL (arg_kinds `equalLength` (ktvs ++ cvs)) $
    
    2755 2697
                     (bad_ax this_co (text "lengths"))
    
    2756 2698
     
    
    2757
    -       ; subst <- getSubst
    
    2758
    -       ; let empty_subst = zapSubst subst
    
    2699
    +       ; in_scope <- getInScope
    
    2700
    +       ; let empty_subst = mkEmptySubst in_scope
    
    2759 2701
            ; _ <- foldlM check_ki (empty_subst, empty_subst)
    
    2760 2702
                                   (zip (ktvs ++ cvs) arg_kinds)
    
    2761 2703
     
    
    ... ... @@ -2880,12 +2822,12 @@ lint_axiom ax@(CoAxiom { co_ax_tc = tc, co_ax_branches = branches
    2880 2822
     lint_branch :: TyCon -> CoAxBranch -> LintM ()
    
    2881 2823
     lint_branch ax_tc (CoAxBranch { cab_tvs = tvs, cab_cvs = cvs
    
    2882 2824
                                   , cab_lhs = lhs_args, cab_rhs = rhs })
    
    2883
    -  = lintBinders LambdaBind (tvs ++ cvs) $ \_ ->
    
    2825
    +  = lintBinders LambdaBind (tvs ++ cvs) $
    
    2884 2826
         do { let lhs = mkTyConApp ax_tc lhs_args
    
    2885 2827
            ; lintType lhs
    
    2886 2828
            ; lintType rhs
    
    2887
    -       ; lhs_kind <- substTyM (typeKind lhs)
    
    2888
    -       ; rhs_kind <- substTyM (typeKind rhs)
    
    2829
    +       ; let lhs_kind = typeKind lhs
    
    2830
    +             rhs_kind = typeKind rhs
    
    2889 2831
            ; lintL (not (lhs_kind `typesAreApart` rhs_kind)) $
    
    2890 2832
              hang (text "Inhomogeneous axiom")
    
    2891 2833
                 2 (text "lhs:" <+> ppr lhs <+> dcolon <+> ppr lhs_kind $$
    
    ... ... @@ -2969,35 +2911,26 @@ type LintLevel = Int
    2969 2911
     -- If you edit this type, you may need to update the GHC formalism
    
    2970 2912
     -- See Note [GHC Formalism]
    
    2971 2913
     data LintEnv
    
    2972
    -  = LE { le_flags :: LintFlags       -- Linting the result of this pass
    
    2973
    -       , le_loc   :: [LintLocInfo]   -- Locations
    
    2974
    -
    
    2975
    -       , le_subst :: Subst
    
    2976
    -                  -- Current substitution, for TyCoVars only.
    
    2977
    -                  -- Non-CoVar Ids don't appear in here, not even in the InScopeSet
    
    2978
    -                  -- Used for (a) cloning to avoid shadowing of TyCoVars,
    
    2979
    -                  --              so that eqType works ok
    
    2980
    -                  --          (b) substituting for let-bound tyvars, when we have
    
    2981
    -                  --              (let @a = Int -> Int in ...)
    
    2982
    -
    
    2983
    -       , le_level   :: LintLevel
    
    2984
    -       , le_in_vars :: VarEnv (InVar, OutType, LintLevel)
    
    2985
    -                    -- Maps an InVar (i.e. its unique) to its binding InVar
    
    2986
    -                    --    and to its OutType
    
    2987
    -                    -- /All/ in-scope variables are here (term variables,
    
    2988
    -                    --    type variables, and coercion variables)
    
    2989
    -                    -- Used at an occurrence of the InVar
    
    2914
    +  = LE { le_flags    :: LintFlags       -- Linting the result of this pass
    
    2915
    +       , le_loc      :: [LintLocInfo]   -- Locations
    
    2916
    +       , le_level    :: LintLevel
    
    2917
    +       , le_in_scope :: InScopeSet
    
    2918
    +
    
    2919
    +       , le_vars     :: VarEnv (Var, LintLevel)
    
    2920
    +                     -- Maps a Var (i.e. its unique) to its binding Var and level
    
    2921
    +                     -- /All/ in-scope variables are here (term variables,
    
    2922
    +                     --    type variables, and coercion variables)
    
    2923
    +                     -- So the domain is the same as the le_in_scope in-scope set
    
    2924
    +                     -- Used at an occurrence of the Var
    
    2990 2925
     
    
    2991 2926
            , le_joins :: UniqMap Id JoinOcc
    
    2992 2927
                -- ^ Join points in scope that are valid
    
    2993
    -           -- A subset of the InScopeSet in le_subst
    
    2994 2928
                -- See Note [Join points]
    
    2995 2929
     
    
    2996 2930
            , le_ue_aliases :: NameEnv UsageEnv
    
    2997 2931
                  -- See Note [Linting linearity]
    
    2998 2932
                  -- Assigns usage environments to the alias-like binders,
    
    2999 2933
                  -- as found in non-recursive lets.
    
    3000
    -             -- Domain is OutIds
    
    3001 2934
     
    
    3002 2935
            , le_platform   :: Platform         -- ^ Target platform
    
    3003 2936
            , le_diagOpts   :: DiagOpts         -- ^ Target platform
    
    ... ... @@ -3011,7 +2944,8 @@ data LintFlags
    3011 2944
            , lf_check_linearity :: Bool    -- ^ See Note [Linting linearity]
    
    3012 2945
            , lf_check_fixed_rep :: Bool    -- ^ See Note [Checking for representation polymorphism]
    
    3013 2946
            , lf_check_rubbish_lits :: Bool -- ^ See Note [Checking for rubbish literals]
    
    3014
    -       , lf_allow_weak_joins :: Bool -- ^ See Note [Linting join points with casts or ticks]
    
    2947
    +       , lf_allow_weak_joins :: Bool   -- ^ See Note [Linting join points with casts or ticks]
    
    2948
    +       , lf_allow_beta_joins :: Bool   -- ^ See Note [Join points and beta-redexes]
    
    3015 2949
         }
    
    3016 2950
     
    
    3017 2951
     -- See Note [Checking StaticPtrs]
    
    ... ... @@ -3078,20 +3012,6 @@ top-level bindings. See SimplCore Note [Grand plan for static forms].
    3078 3012
     
    
    3079 3013
     The linter checks that no occurrence or `makeStatic` occurs nested.
    
    3080 3014
     
    
    3081
    -Note [Type substitution]
    
    3082
    -~~~~~~~~~~~~~~~~~~~~~~~~
    
    3083
    -Why do we need a type substitution?  Consider
    
    3084
    -        /\(a:*). \(x:a). /\(a:*). id a x
    
    3085
    -This is ill typed, because (renaming variables) it is really
    
    3086
    -        /\(a:*). \(x:a). /\(b:*). id b x
    
    3087
    -Hence, when checking an application, we can't naively compare x's type
    
    3088
    -(at its binding site) with its expected type (at a use site).  So we
    
    3089
    -rename type binders as we go, maintaining a substitution.
    
    3090
    -
    
    3091
    -The same substitution also supports let-type, current expressed as
    
    3092
    -        (/\(a:*). body) ty
    
    3093
    -Here we substitute 'ty' for 'a' in 'body', on the fly.
    
    3094
    -
    
    3095 3015
     Note [Linting type synonym applications]
    
    3096 3016
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    3097 3017
     When linting a type-synonym, or type-family, application
    
    ... ... @@ -3353,12 +3273,12 @@ initL cfg m
    3353 3273
       where
    
    3354 3274
         vars = l_vars cfg
    
    3355 3275
         init_level = 0
    
    3356
    -    env = LE { le_flags   = l_flags cfg
    
    3357
    -             , le_subst   = mkEmptySubst (mkInScopeSetList vars)
    
    3358
    -             , le_level   = init_level
    
    3359
    -             , le_in_vars = mkVarEnv [ (v,(v, varType v, init_level)) | v <- vars ]
    
    3360
    -             , le_joins   = emptyUniqMap
    
    3361
    -             , le_loc     = []
    
    3276
    +    env = LE { le_flags    = l_flags cfg
    
    3277
    +             , le_level    = init_level
    
    3278
    +             , le_vars     = mkVarEnv [ (v,(v, init_level)) | v <- vars ]
    
    3279
    +             , le_in_scope = mkInScopeSetList vars
    
    3280
    +             , le_joins    = emptyUniqMap
    
    3281
    +             , le_loc      = []
    
    3362 3282
                  , le_ue_aliases = emptyNameEnv
    
    3363 3283
                  , le_platform = l_platform cfg
    
    3364 3284
                  , le_diagOpts = l_diagOpts cfg
    
    ... ... @@ -3421,8 +3341,7 @@ addMsg show_context env msgs msg
    3421 3341
        loc_msgs :: [(SrcLoc, SDoc)]  -- Innermost first
    
    3422 3342
        loc_msgs = map dumpLoc (le_loc env)
    
    3423 3343
     
    
    3424
    -   cxt_doc = vcat [ vcat $ reverse $ map snd loc_msgs
    
    3425
    -                  , text "Substitution:" <+> ppr (le_subst env) ]
    
    3344
    +   cxt_doc = vcat $ reverse $ map snd loc_msgs
    
    3426 3345
     
    
    3427 3346
        context | show_context  = cxt_doc
    
    3428 3347
                | otherwise     = whenPprDebug cxt_doc
    
    ... ... @@ -3449,72 +3368,44 @@ inCasePat = LintM $ \ env errs -> fromBoxedLResult (Just (is_case_pat env), errs
    3449 3368
         is_case_pat (LE { le_loc = CasePat {} : _ }) = True
    
    3450 3369
         is_case_pat _other                           = False
    
    3451 3370
     
    
    3452
    -addInScopeId :: InId -> OutType -> (OutId -> LintM a) -> LintM a
    
    3371
    +addInScopeId :: Id -> LintM a -> LintM a
    
    3453 3372
     -- Unlike addInScopeTyCoVar, this function does no cloning; Ids never get cloned
    
    3454
    -addInScopeId in_id out_ty thing_inside
    
    3373
    +addInScopeId id thing_inside
    
    3455 3374
       = LintM $ \ env errs ->
    
    3456
    -    let !(out_id, env') = add env
    
    3457
    -    in unLintM (thing_inside out_id) env' errs
    
    3458
    -
    
    3375
    +    unLintM thing_inside (add env) errs
    
    3459 3376
       where
    
    3460
    -    add env@(LE { le_level = level, le_in_vars = id_vars, le_joins = valid_joins
    
    3461
    -                , le_ue_aliases = aliases, le_subst = subst })
    
    3462
    -      = (out_id, env1)
    
    3377
    +    add env@(LE { le_level = level, le_vars = id_vars, le_joins = valid_joins
    
    3378
    +                , le_ue_aliases = aliases, le_in_scope = in_scope })
    
    3379
    +      = env { le_level = level1, le_vars = in_vars'
    
    3380
    +            , le_in_scope = in_scope `extendInScopeSet` id
    
    3381
    +            , le_joins = valid_joins', le_ue_aliases = aliases' }
    
    3463 3382
           where
    
    3464 3383
             level1 = level + 1
    
    3465
    -        env1 = env { le_level = level1, le_in_vars = in_vars'
    
    3466
    -                   , le_joins = valid_joins', le_ue_aliases = aliases' }
    
    3467 3384
     
    
    3468
    -        in_vars' = extendVarEnv id_vars in_id (in_id, out_ty, level1)
    
    3469
    -        aliases' = delFromNameEnv aliases (idName in_id)
    
    3385
    +        in_vars' = extendVarEnv id_vars id (id, level1)
    
    3386
    +        aliases' = delFromNameEnv aliases (idName id)
    
    3470 3387
                -- aliases': when shadowing an alias, we need to make sure the
    
    3471 3388
                -- Id is no longer classified as such. E.g.
    
    3472 3389
                --   let x = <e1> in case x of x { _DEFAULT -> <e2> }
    
    3473 3390
                -- Occurrences of 'x' in e2 shouldn't count as occurrences of e1.
    
    3474 3391
     
    
    3475
    -        -- A very tiny optimisation, not sure if it's really worth it
    
    3476
    -        -- Short-cut when the substitution is a no-op
    
    3477
    -        out_id | isEmptyTCvSubst subst = in_id
    
    3478
    -               | otherwise             = setIdType in_id out_ty
    
    3479
    -
    
    3480 3392
             valid_joins'
    
    3481
    -          | isJoinId out_id = addToUniqMap   valid_joins in_id NormalJoinOcc -- Overwrite with new arity
    
    3482
    -          | otherwise       = delFromUniqMap valid_joins in_id -- Remove any existing binding
    
    3393
    +          | isJoinId id = addToUniqMap   valid_joins id NormalJoinOcc -- Overwrite with new arity
    
    3394
    +          | otherwise   = delFromUniqMap valid_joins id -- Remove any existing binding
    
    3483 3395
     
    
    3484
    -addInScopeTyCoVar :: InTyCoVar -> OutType -> (OutTyCoVar -> LintM a) -> LintM a
    
    3396
    +addInScopeTyCoVar :: TyCoVar -> LintM a -> LintM a
    
    3485 3397
     -- This function clones to avoid shadowing of TyCoVars
    
    3486
    -addInScopeTyCoVar tcv tcv_type thing_inside
    
    3487
    -  = LintM $ \ env@(LE { le_level = level, le_in_vars = in_vars, le_subst = subst }) errs ->
    
    3488
    -    let (tcv', subst') = subst_bndr subst
    
    3489
    -        level' = level + 1
    
    3398
    +addInScopeTyCoVar tcv thing_inside
    
    3399
    +  = LintM $ \ env@(LE { le_level = level, le_vars = in_vars
    
    3400
    +                      , le_in_scope = in_scope }) errs ->
    
    3401
    +    let level' = level + 1
    
    3490 3402
             env' = env { le_level = level'
    
    3491
    -                   , le_in_vars = extendVarEnv in_vars tcv (tcv, tcv_type, level')
    
    3492
    -                   , le_subst = subst' }
    
    3493
    -    in unLintM (thing_inside tcv') env' errs
    
    3494
    -  where
    
    3495
    -    subst_bndr subst
    
    3496
    -      | isEmptyTCvSubst subst                -- No change in kind
    
    3497
    -      , not (tcv `elemInScopeSet` in_scope)  -- Not already in scope
    
    3498
    -      = -- Do not extend the substitution, just the in-scope set
    
    3499
    -        (if (varType tcv `eqType` tcv_type) then (\x->x) else
    
    3500
    -          pprTrace "addInScopeTyCoVar" (
    
    3501
    -            vcat [ text "tcv" <+> ppr tcv <+> dcolon <+> ppr (varType tcv)
    
    3502
    -                 , text "tcv_type" <+> ppr tcv_type ])) $
    
    3503
    -        (tcv, subst `extendSubstInScope` tcv)
    
    3504
    -
    
    3505
    -      -- Clone, and extend the substitution
    
    3506
    -      | let tcv' = uniqAway in_scope (setVarType tcv tcv_type)
    
    3507
    -      = (tcv', extendTCvSubstWithClone subst tcv tcv')
    
    3508
    -      where
    
    3509
    -        in_scope = substInScopeSet subst
    
    3510
    -
    
    3511
    -getInVarEnv :: LintM (VarEnv (InId, OutType, LintLevel))
    
    3512
    -getInVarEnv = LintM (\env errs -> fromBoxedLResult (Just (le_in_vars env), errs))
    
    3403
    +                   , le_in_scope = in_scope `extendInScopeSet` tcv
    
    3404
    +                   , le_vars = extendVarEnv in_vars tcv (tcv, level') }
    
    3405
    +    in unLintM thing_inside env' errs
    
    3513 3406
     
    
    3514
    -extendTvSubstL :: TyVar -> Type -> LintM a -> LintM a
    
    3515
    -extendTvSubstL tv ty m
    
    3516
    -  = LintM $ \ env errs ->
    
    3517
    -    unLintM m (env { le_subst = Type.extendTvSubst (le_subst env) tv ty }) errs
    
    3407
    +getInVarEnv :: LintM (VarEnv (Id, LintLevel))
    
    3408
    +getInVarEnv = LintM (\env errs -> fromBoxedLResult (Just (le_vars env), errs))
    
    3518 3409
     
    
    3519 3410
     markAllJoinsBad :: LintM a -> LintM a
    
    3520 3411
     markAllJoinsBad m
    
    ... ... @@ -3549,54 +3440,42 @@ markAllJoinsBadIf False m = m
    3549 3440
     getValidJoins :: LintM (UniqMap Id JoinOcc)
    
    3550 3441
     getValidJoins = LintM (\ env errs -> fromBoxedLResult (Just (le_joins env), errs))
    
    3551 3442
     
    
    3552
    -getSubst :: LintM Subst
    
    3553
    -getSubst = LintM (\ env errs -> fromBoxedLResult (Just (le_subst env), errs))
    
    3554
    -
    
    3555
    -substTyM :: InType -> LintM OutType
    
    3556
    --- Apply the substitution to the type
    
    3557
    --- The substitution is often empty, in which case it is a no-op
    
    3558
    -substTyM ty
    
    3559
    -  = do { subst <- getSubst
    
    3560
    -       ; return (substTy subst ty) }
    
    3561
    -
    
    3562 3443
     getUEAliases :: LintM (NameEnv UsageEnv)
    
    3563 3444
     getUEAliases = LintM (\ env errs -> fromBoxedLResult (Just (le_ue_aliases env), errs))
    
    3564 3445
     
    
    3565 3446
     getInScope :: LintM InScopeSet
    
    3566
    -getInScope = LintM (\ env errs -> fromBoxedLResult (Just (substInScopeSet $ le_subst env), errs))
    
    3447
    +getInScope = LintM (\ env errs -> fromBoxedLResult (Just (le_in_scope env), errs))
    
    3567 3448
     
    
    3568
    -lintVarOcc :: InVar -> LintM OutType
    
    3449
    +lintVarOcc :: Var -> LintM ()
    
    3569 3450
     -- Used at an occurrence of a variable: term variables, type variables, and coercion variables
    
    3570 3451
     -- Checks
    
    3571 3452
     --   - that it is in scope
    
    3572 3453
     --   - that it is not a GlobalId bound by a LocalId
    
    3573
    ---   - that the InType at the ocurrence matches the InType at the binding site
    
    3454
    +--   - that the Type at the ocurrence matches the Type at the binding site
    
    3574 3455
     --   - that the variables free in its type are not shadowed at the occurrence site
    
    3575 3456
     lintVarOcc v_occ
    
    3576 3457
       | isGlobalId v_occ
    
    3577
    -  = return (idType v_occ)
    
    3458
    +  = return ()
    
    3578 3459
       | otherwise
    
    3579 3460
       = do { in_var_env <- getInVarEnv
    
    3580 3461
            ; case lookupVarEnv in_var_env v_occ of
    
    3581 3462
                Nothing -> failWithL (text pp_what <+> quotes (ppr v_occ)
    
    3582 3463
                                      <+> text "is out of scope")
    
    3583
    -           Just (v_bndr, out_ty, bind_level)
    
    3464
    +           Just (v_bndr, bind_level)
    
    3584 3465
                  -> do { let bndr_ty = idType v_bndr
    
    3585 3466
                        ; check_bad_global v_bndr
    
    3586 3467
                        ; check_occ_type_match bndr_ty
    
    3587
    -                   ; check_occ_type_scope in_var_env bndr_ty bind_level
    
    3588
    -                   ; return out_ty }
    
    3589
    -
    
    3468
    +                   ; check_occ_type_scope in_var_env bndr_ty bind_level }
    
    3590 3469
         }
    
    3591 3470
       where
    
    3592
    -    occ_ty :: InType
    
    3471
    +    occ_ty :: Type
    
    3593 3472
         occ_ty = idType v_occ
    
    3594 3473
     
    
    3595 3474
         pp_what | isTyVar v_occ = "The type variable"
    
    3596 3475
                 | isCoVar v_occ = "The coercion variable"
    
    3597 3476
                 | otherwise     = "The value variable"
    
    3598 3477
     
    
    3599
    -    check_bad_global :: InVar -> LintM ()
    
    3478
    +    check_bad_global :: Var -> LintM ()
    
    3600 3479
         -- 'check_bad_global' checks for the case where an /occurrence/ is
    
    3601 3480
         -- a GlobalId, but there is an enclosing binding for a LocalId.
    
    3602 3481
         -- NB: the in-scope variables are mostly LocalIds, checked by lintIdBndr,
    
    ... ... @@ -3616,26 +3495,26 @@ lintVarOcc v_occ
    3616 3495
           | otherwise
    
    3617 3496
           = return ()
    
    3618 3497
     
    
    3619
    -    check_occ_type_match :: InType -> LintM ()
    
    3498
    +    check_occ_type_match :: Type -> LintM ()
    
    3620 3499
         -- Check that the type in /binder/ and the type in the /occurrence/ are the same
    
    3621 3500
         check_occ_type_match bndr_ty
    
    3622
    -      = ensureEqTys bndr_ty occ_ty $  -- Compares InTypes
    
    3501
    +      = ensureEqTys bndr_ty occ_ty $  -- Compares Types
    
    3623 3502
             mkBndrOccTypeMismatchMsg v_occ bndr_ty occ_ty
    
    3624 3503
     
    
    3625
    -    check_occ_type_scope :: VarEnv (InVar,OutType,LintLevel) -> InType -> LintLevel -> LintM ()
    
    3504
    +    check_occ_type_scope :: VarEnv (Var,LintLevel) -> Type -> LintLevel -> LintM ()
    
    3626 3505
         -- Check that the free vars of the binder's type
    
    3627 3506
         -- are not shadowed at the occurrence site
    
    3628 3507
         check_occ_type_scope in_var_env bndr_ty bind_level
    
    3629 3508
           = checkL (null bad_fvs) $
    
    3630 3509
             mkBndrOccFreeVarMsg v_occ occ_ty bad_fvs
    
    3631 3510
           where
    
    3632
    -        bad_fvs :: [InVar]
    
    3511
    +        bad_fvs :: [Var]
    
    3633 3512
             bad_fvs = filter is_bad (tyCoVarsOfTypeList bndr_ty)
    
    3634 3513
     
    
    3635
    -        is_bad :: InVar -> Bool
    
    3514
    +        is_bad :: Var -> Bool
    
    3636 3515
             -- True of a variable bound inside bind_level
    
    3637 3516
             is_bad v = case lookupVarEnv in_var_env v of
    
    3638
    -                      Just (_, _, v_level) -> v_level > bind_level
    
    3517
    +                      Just (_, v_level) -> v_level > bind_level
    
    3639 3518
                           Nothing -> True
    
    3640 3519
     
    
    3641 3520
     lookupJoinId :: Id -> LintM (Maybe (JoinArity, JoinOcc))
    
    ... ... @@ -3647,21 +3526,21 @@ lookupJoinId id
    3647 3526
                 Just join_occ -> return $ Just (idJoinArity id, join_occ)
    
    3648 3527
                 Nothing       -> return Nothing }
    
    3649 3528
     
    
    3650
    -addAliasUE :: OutId -> UsageEnv -> LintM a -> LintM a
    
    3529
    +addAliasUE :: Id -> UsageEnv -> LintM a -> LintM a
    
    3651 3530
     addAliasUE id ue thing_inside = LintM $ \ env errs ->
    
    3652 3531
       let new_ue_aliases =
    
    3653 3532
             extendNameEnv (le_ue_aliases env) (getName id) ue
    
    3654 3533
       in
    
    3655 3534
         unLintM thing_inside (env { le_ue_aliases = new_ue_aliases }) errs
    
    3656 3535
     
    
    3657
    -varCallSiteUsage :: OutId -> LintM UsageEnv
    
    3536
    +varCallSiteUsage :: Id -> LintM UsageEnv
    
    3658 3537
     varCallSiteUsage id =
    
    3659 3538
       do m <- getUEAliases
    
    3660 3539
          return $ case lookupNameEnv m (getName id) of
    
    3661 3540
              Nothing    -> singleUsageUE id
    
    3662 3541
              Just id_ue -> id_ue
    
    3663 3542
     
    
    3664
    -ensureEqTys :: OutType -> OutType -> SDoc -> LintM ()
    
    3543
    +ensureEqTys :: Type -> Type -> SDoc -> LintM ()
    
    3665 3544
     -- check ty2 is subtype of ty1 (ie, has same structure but usage
    
    3666 3545
     -- annotations need only be consistent, not equal)
    
    3667 3546
     -- Assumes ty1,ty2 are have already had the substitution applied
    
    ... ... @@ -3885,7 +3764,7 @@ mkLetErr bndr rhs
    3885 3764
               hang (text "Rhs:")
    
    3886 3765
                      4 (ppr rhs)]
    
    3887 3766
     
    
    3888
    -mkTyAppMsg :: OutType -> Type -> SDoc
    
    3767
    +mkTyAppMsg :: Type -> Type -> SDoc
    
    3889 3768
     mkTyAppMsg ty arg_ty
    
    3890 3769
       = vcat [text "Illegal type application:",
    
    3891 3770
                   hang (text "Function type:")
    
    ... ... @@ -4006,13 +3885,13 @@ mkJoinBndrOccMismatchMsg bndr join_arity_bndr join_arity_occ
    4006 3885
              , text "Arity at binding site:" <+> ppr join_arity_bndr
    
    4007 3886
              , text "Arity at occurrence:  " <+> ppr join_arity_occ ]
    
    4008 3887
     
    
    4009
    -mkBndrOccTypeMismatchMsg :: InVar -> InType -> InType -> SDoc
    
    3888
    +mkBndrOccTypeMismatchMsg :: Var -> Type -> Type -> SDoc
    
    4010 3889
     mkBndrOccTypeMismatchMsg var bndr_ty occ_ty
    
    4011 3890
       = vcat [ text "Mismatch in type between binder and occurrence"
    
    4012 3891
              , text "Binder:    " <+> ppr var <+> dcolon <+> ppr bndr_ty
    
    4013 3892
              , text "Occurrence:" <+> ppr var <+> dcolon <+> ppr occ_ty ]
    
    4014 3893
     
    
    4015
    -mkBndrOccFreeVarMsg :: InVar -> InType -> [TyCoVar] -> SDoc
    
    3894
    +mkBndrOccFreeVarMsg :: Var -> Type -> [TyCoVar] -> SDoc
    
    4016 3895
     mkBndrOccFreeVarMsg var occ_ty bad_tvs
    
    4017 3896
       = vcat [ text "Free vars of type are shadowed:" <+> ppr bad_tvs
    
    4018 3897
              , text "Occurrence:"  <+> ppr var <+> dcolon <+> ppr occ_ty ]
    

  • compiler/GHC/Core/Lint/SubstTypeLets.hs
    1
    +{-
    
    2
    +(c) The University of Glasgow 2006
    
    3
    +(c) The GRASP/AQUA Project, Glasgow University, 1993-1998
    
    4
    +-}
    
    5
    +
    
    6
    +module GHC.Core.Lint.SubstTypeLets(
    
    7
    +         substTypeLets
    
    8
    +     ) where
    
    9
    +
    
    10
    +import GHC.Prelude
    
    11
    +
    
    12
    +import GHC.Core
    
    13
    +import GHC.Core.Subst
    
    14
    +import GHC.Core.Utils( mkInScopeSetBndrs )
    
    15
    +
    
    16
    +import GHC.Types.Var
    
    17
    +
    
    18
    +import GHC.Utils.Misc( mapSnd )
    
    19
    +import GHC.Utils.Outputable
    
    20
    +import GHC.Utils.Panic
    
    21
    +
    
    22
    +{- Note [Substituting type-lets]
    
    23
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    24
    +When desugaring pattern matching we really, really need non-Lint-acceptable type-lets.
    
    25
    +Suppose we have
    
    26
    +   f (MkT a (Just a (x::a)) (y::a)) = rhs1
    
    27
    +   f (MkT b (Nothing b) (z::b)) = rhs2
    
    28
    +where
    
    29
    +   MkT :: ∀ a. Maybe a -> a -> T
    
    30
    +
    
    31
    +We desugar this to
    
    32
    +  f x = case x of
    
    33
    +          MkT w (v :: Maybe w) (p:w)
    
    34
    +              ->  let { a=w, b=w }
    
    35
    +                  in let { y:a=p, z:b=p }
    
    36
    +                  in case v of
    
    37
    +                      Just a (x:a) -> rhs1 [y::a]
    
    38
    +                      Nothing b    -> rhs2 [z::b]
    
    39
    +
    
    40
    +Look at those type-lets { a=w, b=w }.  They make the type variables in the
    
    41
    +/two/ separately-typechecked clauses for `f` line up with the /single/ pattern
    
    42
    +match on `x`, which binds the type variable `w`.
    
    43
    +
    
    44
    +Key point: the body of the let is only type-correct /after/ substituting
    
    45
    +a:=w, b:=w.  Even the next let, { y:a=p } isn't type-correct without that
    
    46
    +substitution, because (p:w).
    
    47
    +
    
    48
    +So the `substTypeLets` pass does this:
    
    49
    +  - It runs as part of Lint, as a pre-pass before the main Lint
    
    50
    +  - It runs only when we are Linting the output of the desugarer
    
    51
    +  - The result of substTypeLets is discarded after linting
    
    52
    +
    
    53
    +When it finds a nested type-let
    
    54
    +    let @a = ty in body
    
    55
    +it substitutes a:=ty in `body`
    
    56
    +
    
    57
    +Wrinkles
    
    58
    +
    
    59
    +(STL1) It only substitutes /nested/ type-lets, not top level.
    
    60
    +
    
    61
    +(STL2) You might think that we'd run it unconditionally, after desugaring.  But actually,
    
    62
    +  the Simplifier (or SimpleOpt) will deal with these type-lets, so it is just Lint
    
    63
    +  that we must placate.  We don't want to incur the cost of this pass except when
    
    64
    +  we are Linting.
    
    65
    +
    
    66
    +  TL;DR: we do substTypeLets as a pre-pass to the Lint pass that immediately follows
    
    67
    +  desugaring. See `GHC.Core.lintPassResult`, and the `lpr_preSubst` field in
    
    68
    +  `LintPassResultConfig`.
    
    69
    +
    
    70
    +(STL3) Should `substTypeLets` process (stable) unfoldings? It does not need to
    
    71
    +  because all unfoldings have `simpleOptExpr` applied to them, so the tricky
    
    72
    +  type-lets will already be substituted.
    
    73
    +
    
    74
    +  Of course we stil need to apply the current substitution, but that is done
    
    75
    +  automatically by `substBndr`.
    
    76
    +-}
    
    77
    +
    
    78
    +substTypeLets :: CoreProgram -> CoreProgram
    
    79
    +substTypeLets binds = map stl_top binds
    
    80
    +  where
    
    81
    +     stl_top (NonRec b r) = NonRec b (stlExpr empty_subst r)
    
    82
    +     stl_top (Rec prs)    = Rec (mapSnd (stlExpr empty_subst) prs)
    
    83
    +
    
    84
    +     empty_subst = mkEmptySubst $
    
    85
    +                   mkInScopeSetBndrs binds
    
    86
    +
    
    87
    +----------------------
    
    88
    +stlBind :: Subst -> CoreBind -> (Subst, CoreBind)
    
    89
    +stlBind subst (Rec prs)
    
    90
    +  = assertPpr (not (any isTyVar bndrs)) (ppr prs) $
    
    91
    +    (subst', Rec prs')
    
    92
    +  where
    
    93
    +    (bndrs,rhss) = unzip prs
    
    94
    +    (subst', bndrs') = substRecBndrs subst bndrs
    
    95
    +       -- substRecBndrs: see (STL3) in Note [Substituting type-lets]
    
    96
    +    rhss' = map (stlExpr subst') rhss
    
    97
    +    prs'  = bndrs' `zip` rhss'
    
    98
    +
    
    99
    +stlBind subst (NonRec bndr rhs)
    
    100
    +  = (subst', NonRec bndr' (stlExpr subst rhs))
    
    101
    +  where
    
    102
    +    (subst', bndr')  = substBndr subst bndr
    
    103
    +      -- substBndr: see (STL3) in Note [Substituting type-lets]
    
    104
    +
    
    105
    +----------------------
    
    106
    +stlExpr :: Subst -> CoreExpr -> CoreExpr
    
    107
    +
    
    108
    +stlExpr subst (Let (NonRec tv (Type ty)) body)
    
    109
    +  = -- This equation is the main payload of the entire pass!
    
    110
    +    stlExpr (extendTvSubst subst tv (substTy subst ty)) body
    
    111
    +
    
    112
    +stlExpr subst (Let bind body)
    
    113
    +  = Let bind' (stlExpr subst' body)
    
    114
    +  where
    
    115
    +    (subst', bind') = stlBind subst bind
    
    116
    +
    
    117
    +stlExpr subst (Lam bndr body)
    
    118
    +  = Lam bndr' (stlExpr subst' body)
    
    119
    +  where
    
    120
    +    (subst', bndr') = substBndr subst bndr
    
    121
    +
    
    122
    +stlExpr subst (Case scrut bndr ty alts)
    
    123
    +  = Case (stlExpr subst scrut) bndr' (substTy subst ty)
    
    124
    +         (map stl_alt alts)
    
    125
    +  where
    
    126
    +    (subst', bndr') = substBndr subst bndr
    
    127
    +
    
    128
    +    stl_alt (Alt con bndrs rhs)
    
    129
    +       = Alt con bndrs' (stlExpr subst'' rhs)
    
    130
    +       where
    
    131
    +         (subst'', bndrs') = substBndrs subst' bndrs
    
    132
    +
    
    133
    +-- Simple cases
    
    134
    +stlExpr _     (Lit l)       = Lit l
    
    135
    +stlExpr subst (Var v)       = lookupIdSubst subst v
    
    136
    +stlExpr subst (App e1 e2)   = App (stlExpr subst e1) (stlExpr subst e2)
    
    137
    +stlExpr subst (Type ty)     = Type (substTy subst ty)
    
    138
    +stlExpr subst (Tick t e)    = Tick (substTickish subst t) (stlExpr subst e)
    
    139
    +stlExpr subst (Cast e co)   = Cast (stlExpr subst e) (substCo subst co)
    
    140
    +stlExpr subst (Coercion co) = Coercion (substCo subst co)

  • compiler/GHC/Core/Opt/OccurAnal.hs
    ... ... @@ -2676,6 +2676,8 @@ occAnalArgs :: OccEnv -> CoreExpr -> [CoreExpr]
    2676 2676
                 -> WithUsageDetails CoreExpr
    
    2677 2677
     -- The `fun` argument is just an accumulating parameter,
    
    2678 2678
     -- the base for building the application we return
    
    2679
    +--
    
    2680
    +-- We have applied markAllNonTail to the returned usage-details
    
    2679 2681
     occAnalArgs env fun args one_shots
    
    2680 2682
       = go emptyDetails fun args one_shots
    
    2681 2683
       where
    
    ... ... @@ -2686,7 +2688,9 @@ occAnalArgs env fun args one_shots
    2686 2688
         encl | Var f <- fun, isDeadEndSig (idDmdSig f) = OccScrut
    
    2687 2689
              | otherwise                               = OccVanilla
    
    2688 2690
     
    
    2689
    -    go uds fun [] _ = WUD uds fun
    
    2691
    +    go uds fun [] _ = WUD (markAllNonTail uds) fun
    
    2692
    +       -- markAllNonTail: calls in arguments are not tail calls!
    
    2693
    +
    
    2690 2694
         go uds fun (arg:args) one_shots
    
    2691 2695
           = go (uds `andUDs` arg_uds) (fun `App` arg') args one_shots'
    
    2692 2696
           where
    
    ... ... @@ -2778,8 +2782,7 @@ occAnalApp env (Var fun_id, args, ticks)
    2778 2782
     
    
    2779 2783
         all_uds = fun_uds `andUDs` final_args_uds
    
    2780 2784
     
    
    2781
    -    !final_args_uds = markAllNonTail                              $
    
    2782
    -                      markAllInsideLamIf (isRhsEnv env && is_exp) $
    
    2785
    +    !final_args_uds = markAllInsideLamIf (isRhsEnv env && is_exp) $
    
    2783 2786
                             -- isRhsEnv: see Note [OccEncl]
    
    2784 2787
                           args_uds
    
    2785 2788
            -- We mark the free vars of the argument of a constructor or PAP
    
    ... ... @@ -2809,20 +2812,27 @@ occAnalApp env (Var fun_id, args, ticks)
    2809 2812
             -- See Note [Sources of one-shot information], bullet point A']
    
    2810 2813
     
    
    2811 2814
     occAnalApp env (fun, args, ticks)
    
    2812
    -  = let app_out = mkTicks ticks app'
    
    2813
    -    in WUD (markAllNonTail (fun_uds `andUDs` args_uds)) app_out
    
    2814
    -
    
    2815
    +  = WUD (fun_uds `andUDs` args_uds) (mkTicks ticks app')
    
    2815 2816
       where
    
    2816 2817
         !(WUD args_uds app') = occAnalArgs env fun' args []
    
    2817
    -    !(WUD fun_uds fun')  = occAnal (addAppCtxt env args) fun
    
    2818
    -        -- The addAppCtxt is a bit cunning.  One iteration of the simplifier
    
    2819
    -        -- often leaves behind beta redexes like
    
    2820
    -        --      (\x y -> e) a1 a2
    
    2821
    -        -- Here we would like to mark x,y as one-shot, and treat the whole
    
    2822
    -        -- thing much like a let.  We do this by pushing some OneShotLam items
    
    2823
    -        -- onto the context stack.
    
    2818
    +    !(WUD fun_uds fun')  = go_fun env fun args
    
    2819
    +
    
    2820
    +    -- See (A2) in Note [occAnal for applications]
    
    2821
    +    go_fun env (Lam bndr body) (_ : args)
    
    2822
    +      = addInScopeOne env bndr $ \ env' ->
    
    2823
    +        let !(WUD body_uds body') = go_fun env' body args
    
    2824
    +            !bndr' = tagLamBinder body_uds bndr
    
    2825
    +        in WUD body_uds (Lam bndr' body')
    
    2826
    +    go_fun env fun args
    
    2827
    +      | null args
    
    2828
    +      = occAnal env fun
    
    2829
    +      | otherwise
    
    2830
    +      = let !env' = addAppCtxt env args
    
    2831
    +            !(WUD fun_uds fun') = occAnal env' fun
    
    2832
    +        in WUD (markAllNonTail fun_uds) fun'
    
    2824 2833
     
    
    2825 2834
     addAppCtxt :: OccEnv -> [Arg CoreBndr] -> OccEnv
    
    2835
    +-- See (A3) in Note [occAnal for applications]
    
    2826 2836
     addAppCtxt env@(OccEnv { occ_one_shots = ctxt }) args
    
    2827 2837
       | n_val_args > 0
    
    2828 2838
       = env { occ_one_shots = replicate n_val_args OneShotLam ++ ctxt
    
    ... ... @@ -2834,8 +2844,40 @@ addAppCtxt env@(OccEnv { occ_one_shots = ctxt }) args
    2834 2844
       where
    
    2835 2845
         n_val_args = valArgCount args
    
    2836 2846
     
    
    2847
    +{- Note [occAnal for applications]
    
    2848
    +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2849
    +One iteration of the simplifier sometimes leaves behind beta redexes like
    
    2850
    +     (\x y -> e) a1 a2
    
    2851
    +This happens particularly in worker/wrapper; see Note [Join points and beta-redexes]
    
    2852
    +in GHC.Core.Lint.  In these cases there are three things we want to take care of
    
    2853
    +in the occurrence analyser:
    
    2854
    +
    
    2855
    +(A1) We don't want to mark variables inside `e` as `InsideLam`; that would just
    
    2856
    +  delay inlining them for another iteration of the Simplifier.
    
    2857
    +
    
    2858
    +(A2) If there is a join-point invocation inside `e`, we don't want to complain about
    
    2859
    +  lost join points.  See Note [Join points and beta-redexes] in GHC.Core.Lint for
    
    2860
    +  more detail.
    
    2861
    +
    
    2862
    +(A3) Suppose we have something like
    
    2863
    +     (case e of (a,b) -> (\x.blah) |> co) arg
    
    2864
    +  which can happen during 'gentle' simplification when we don't do case-of-case,
    
    2865
    +  not push arguments into cases.  Then we'd still like to mark that lambda
    
    2866
    +  as one-shot, so that things can get inlined inside it.  We can to this
    
    2867
    +  by pushing OneShotLam items onto the context stack.
    
    2868
    +
    
    2869
    +  Live example: `read_tup4` in test CoOpt_Read.
    
    2870
    +
    
    2871
    +How we address these:
    
    2872
    +
    
    2873
    +* (A2): we focus narrowly on visible beta-redexes ((\x.e) arg), since that
    
    2874
    +  is what is needed for Note [Join points and beta-redexes].  We do this
    
    2875
    +  via the `go_fun` loop in `occAnalApp`.
    
    2876
    +
    
    2877
    +* (A1) and (A3): for visible beta-redexes, the `go_fun` loop does the job.
    
    2878
    +  But for less-visible ones, like in (A3) we push `OneShotLam` items onto
    
    2879
    +  the context stack, in `addAppCtxt`.
    
    2837 2880
     
    
    2838
    -{-
    
    2839 2881
     Note [Sources of one-shot information]
    
    2840 2882
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    2841 2883
     The occurrence analyser obtains one-shot-lambda information from two sources:
    

  • compiler/GHC/Core/Opt/WorkWrap/Utils.hs
    ... ... @@ -241,10 +241,10 @@ mkWwBodies opts fun_id ww_arity arg_vars res_ty demands res_cpr
    241 241
                     = (work_args, work_args, work_marks)
    
    242 242
     
    
    243 243
                   call_work work_fn  = mkVarApps (Var work_fn) work_call_args
    
    244
    -              call_rhs fn_rhs = mkAppsBeta fn_rhs fn_args
    
    245
    -                                  -- See Note [Join points and beta-redexes]
    
    244
    +              call_rhs fn_rhs = mkApps fn_rhs fn_args
    
    245
    +                   -- See Note [Join points and beta-redexes] in GHC.Core.Lint
    
    246 246
                   wrapper_body = mkLams cloned_arg_vars . wrap_fn_cpr . wrap_fn_str . call_work
    
    247
    -                                  -- See Note [Call-by-value for worker args]
    
    247
    +                   -- See Note [Call-by-value for worker args]
    
    248 248
                   work_seq_str_flds = mkStrictFieldSeqs (zip work_lam_args work_call_str)
    
    249 249
                   worker_body = mkLams work_lam_args . work_seq_str_flds . work_fn_cpr . call_rhs
    
    250 250
                   worker_args_dmds= [ idDemandInfo v | v <- work_call_args, isId v]
    
    ... ... @@ -280,14 +280,6 @@ mkWwBodies opts fun_id ww_arity arg_vars res_ty demands res_cpr
    280 280
         arity_ok | isJoinId fun_id = ww_arity <= n_dmds
    
    281 281
                  | otherwise       = ww_arity == n_dmds
    
    282 282
     
    
    283
    --- | Version of 'GHC.Core.mkApps' that does beta reduction on-the-fly.
    
    284
    --- PRECONDITION: The arg expressions are not free in any of the lambdas binders.
    
    285
    -mkAppsBeta :: CoreExpr -> [CoreArg] -> CoreExpr
    
    286
    --- The precondition holds for our call site in mkWwBodies, because all the FVs
    
    287
    --- of as are either cloned_arg_vars (and thus fresh) or fresh worker args.
    
    288
    -mkAppsBeta (Lam b body) (a:as) = bindNonRec b a $! mkAppsBeta body as
    
    289
    -mkAppsBeta f            as     = mkApps f as
    
    290
    -
    
    291 283
     -- See Note [Limit w/w arity]
    
    292 284
     isWorkerSmallEnough :: Int -> Int -> [Var] -> Bool
    
    293 285
     isWorkerSmallEnough max_worker_args old_n_args vars
    
    ... ... @@ -525,36 +517,6 @@ Solution is simple: put the void argument /last/:
    525 517
     
    
    526 518
     c.f Note [SpecConstr void argument insertion] in GHC.Core.Opt.SpecConstr
    
    527 519
     
    
    528
    -Note [Join points and beta-redexes]
    
    529
    -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    530
    -Originally, the worker would invoke the original function by calling it with
    
    531
    -arguments, thus producing a beta-redex for the simplifier to munch away:
    
    532
    -
    
    533
    -  \x y z -> e => (\x y z -> e) wx wy wz
    
    534
    -
    
    535
    -Now that we have special rules about join points, however, this is Not Good if
    
    536
    -the original function is itself a join point, as then it may contain invocations
    
    537
    -of other join points:
    
    538
    -
    
    539
    -  join j1 x = ...
    
    540
    -  join j2 y = if y == 0 then 0 else j1 y
    
    541
    -
    
    542
    -  =>
    
    543
    -
    
    544
    -  join j1 x = ...
    
    545
    -  join $wj2 y# = let wy = I# y# in (\y -> if y == 0 then 0 else jump j1 y) wy
    
    546
    -  join j2 y = case y of I# y# -> jump $wj2 y#
    
    547
    -
    
    548
    -There can't be an intervening lambda between a join point's declaration and its
    
    549
    -occurrences, so $wj2 here is wrong. But of course, this is easy enough to fix:
    
    550
    -
    
    551
    -  ...
    
    552
    -  let join $wj2 y# = let wy = I# y# in let y = wy in if y == 0 then 0 else j1 y
    
    553
    -  ...
    
    554
    -
    
    555
    -Hence we simply do the beta-reduction here. (This would be harder if we had to
    
    556
    -worry about hygiene, but luckily wy is freshly generated.)
    
    557
    -
    
    558 520
     Note [Freshen WW arguments]
    
    559 521
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    560 522
     When we do a worker/wrapper split, we must freshen the arg vars of the original
    

  • compiler/GHC/Core/Subst.hs
    ... ... @@ -13,7 +13,8 @@ module GHC.Core.Subst (
    13 13
     
    
    14 14
             -- ** Substituting into expressions and related types
    
    15 15
             deShadowBinds, substRuleInfo, substRulesForImportedIds,
    
    16
    -        substTyUnchecked, substCo, substExpr, substExprSC, substBind, substBindSC,
    
    16
    +        substTy, substTyUnchecked, substCo,
    
    17
    +        substExpr, substExprSC, substBind, substBindSC,
    
    17 18
             substUnfolding, substUnfoldingSC,
    
    18 19
             lookupIdSubst, lookupIdSubst_maybe, substIdType, substIdOcc,
    
    19 20
             substTickish, substDVarSet, substIdInfo,
    
    ... ... @@ -42,8 +43,7 @@ import GHC.Core.FVs
    42 43
     import GHC.Core.Seq
    
    43 44
     import GHC.Core.Utils
    
    44 45
     
    
    45
    -        -- We are defining local versions
    
    46
    -import GHC.Core.Type hiding ( substTy )
    
    46
    +import GHC.Core.Type
    
    47 47
     import GHC.Core.Coercion( mkCoVarCo, substCoVarBndr )
    
    48 48
     import GHC.Core.TyCo.FVs
    
    49 49
     
    

  • compiler/GHC/Driver/Config/Core/Lint.hs
    1 1
     module GHC.Driver.Config.Core.Lint
    
    2 2
       ( endPass
    
    3 3
       , endPassHscEnvIO
    
    4
    -  , lintCoreBindings
    
    5 4
       , initEndPassConfig
    
    6 5
       , initLintPassResultConfig
    
    7 6
       , initLintConfig
    
    ... ... @@ -50,16 +49,6 @@ endPassHscEnvIO hsc_env name_ppr_ctx pass binds rules
    50 49
                binds rules
    
    51 50
            }
    
    52 51
     
    
    53
    --- | Type-check a 'CoreProgram'. See Note [Core Lint guarantee].
    
    54
    -lintCoreBindings :: DynFlags -> CoreToDo -> [Var] -> CoreProgram -> WarnsAndErrs
    
    55
    -lintCoreBindings dflags coreToDo vars -- binds
    
    56
    -  = lintCoreBindings' $ LintConfig
    
    57
    -      { l_diagOpts = initDiagOpts dflags
    
    58
    -      , l_platform = targetPlatform dflags
    
    59
    -      , l_flags    = perPassFlags dflags coreToDo
    
    60
    -      , l_vars     = vars
    
    61
    -      }
    
    62
    -
    
    63 52
     initEndPassConfig :: DynFlags -> [Var] -> NamePprCtx -> CoreToDo -> EndPassConfig
    
    64 53
     initEndPassConfig dflags extra_vars name_ppr_ctx pass = EndPassConfig
    
    65 54
       { ep_dumpCoreSizes = not (gopt Opt_SuppressCoreSizes dflags)
    
    ... ... @@ -104,10 +93,17 @@ initLintPassResultConfig dflags extra_vars pass = LintPassResultConfig
    104 93
       { lpr_diagOpts      = initDiagOpts dflags
    
    105 94
       , lpr_platform      = targetPlatform dflags
    
    106 95
       , lpr_makeLintFlags = perPassFlags dflags pass
    
    107
    -  , lpr_passPpr = ppr pass
    
    96
    +  , lpr_passPpr       = ppr pass
    
    97
    +  , lpr_preSubst      = doPreSubst pass
    
    108 98
       , lpr_localsInScope = extra_vars
    
    109 99
       }
    
    110 100
     
    
    101
    +doPreSubst :: CoreToDo -> Bool
    
    102
    +doPreSubst CoreDesugar = True   -- Output of desugarer, /before/ running any optimisation,
    
    103
    +                                -- not even simpleOpt. See Note Note [Substituting type-lets]
    
    104
    +                                -- in GHC.Core.SubstTypeLets
    
    105
    +doPreSubst _           = False
    
    106
    +
    
    111 107
     perPassFlags :: DynFlags -> CoreToDo -> LintFlags
    
    112 108
     perPassFlags dflags pass
    
    113 109
       = (defaultLintFlags dflags)
    
    ... ... @@ -116,7 +112,8 @@ perPassFlags dflags pass
    116 112
                    , lf_check_static_ptrs          = check_static_ptrs
    
    117 113
                    , lf_check_linearity            = check_linearity
    
    118 114
                    , lf_check_rubbish_lits         = check_rubbish
    
    119
    -               , lf_allow_weak_joins           = allow_weak_joins }
    
    115
    +               , lf_allow_weak_joins           = allow_weak_joins
    
    116
    +               , lf_allow_beta_joins           = allow_beta_joins }
    
    120 117
       where
    
    121 118
         -- See Note [Checking for global Ids]
    
    122 119
         check_globals = case pass of
    
    ... ... @@ -158,6 +155,11 @@ perPassFlags dflags pass
    158 155
                           CorePrep -> True
    
    159 156
                           _        -> False
    
    160 157
     
    
    158
    +    -- See Note [Join points and beta-redexes] in GHC.Core.Lint
    
    159
    +    allow_beta_joins = case pass of
    
    160
    +                          CoreDoWorkerWrapper -> True
    
    161
    +                          _                   -> False
    
    162
    +
    
    161 163
     initLintConfig :: DynFlags -> [Var] -> LintConfig
    
    162 164
     initLintConfig dflags vars =LintConfig
    
    163 165
       { l_diagOpts = initDiagOpts dflags
    
    ... ... @@ -175,4 +177,5 @@ defaultLintFlags dflags = LF { lf_check_global_ids = False
    175 177
                                  , lf_check_fixed_rep = True
    
    176 178
                                  , lf_check_rubbish_lits = True
    
    177 179
                                  , lf_allow_weak_joins = False
    
    180
    +                             , lf_allow_beta_joins = False
    
    178 181
                                  }

  • compiler/ghc.cabal.in
    ... ... @@ -361,6 +361,7 @@ Library
    361 361
             GHC.Core.InstEnv
    
    362 362
             GHC.Core.Lint
    
    363 363
             GHC.Core.Lint.Interactive
    
    364
    +        GHC.Core.Lint.SubstTypeLets
    
    364 365
             GHC.Core.LateCC
    
    365 366
             GHC.Core.LateCC.Types
    
    366 367
             GHC.Core.LateCC.TopLevelBinds
    

  • testsuite/tests/corelint/LintEtaExpand.stderr
    1 1
     <no location info>: warning:
    
    2 2
         • The first argument of ‘coerce’ does not have a fixed runtime representation:
    
    3 3
             a :: TYPE k
    
    4
    -    Substitution: <InScope = {a q}
    
    5
    -                   IdSubst   = []
    
    6
    -                   TvSubst   = []
    
    7
    -                   CvSubst   = []>
    
    8 4
     in coerce BAD 1
    
    9 5
     <no location info>: warning:
    
    10 6
         • The first argument of ‘coerce’ does not have a fixed runtime representation:
    
    11 7
             ‘q’ is not concrete.
    
    12
    -    Substitution: <InScope = {a q}
    
    13
    -                   IdSubst   = []
    
    14
    -                   TvSubst   = []
    
    15
    -                   CvSubst   = []>
    
    16 8
     in coerce BAD 2
    
    17 9
     <no location info>: warning:
    
    18 10
         • The result of the first argument of the primop ‘catch#’ does not have a fixed runtime representation:
    
    19 11
             a :: TYPE q
    
    20
    -    Substitution: <InScope = {a q}
    
    21
    -                   IdSubst   = []
    
    22
    -                   TvSubst   = []
    
    23
    -                   CvSubst   = []>
    
    24 12
     in catch# BAD 1
    
    25 13
     <no location info>: warning:
    
    26 14
         • The result of the first argument of the primop ‘catch#’ does not have a fixed runtime representation:
    
    27 15
             ‘q’ is not concrete.
    
    28
    -    Substitution: <InScope = {a q}
    
    29
    -                   IdSubst   = []
    
    30
    -                   TvSubst   = []
    
    31
    -                   CvSubst   = []>
    
    32 16
     in catch# BAD 2

  • testsuite/tests/corelint/T21115b.stderr
    ... ... @@ -6,10 +6,6 @@ T21115b.hs:9:1: warning:
    6 6
         In the body of lambda with binder ds :: Double#
    
    7 7
         In the body of a let with binder fail :: (# #) -> Int#
    
    8 8
         In the body of a let with binder fail :: (# #) -> Int#
    
    9
    -    Substitution: <InScope = {}
    
    10
    -                   IdSubst   = []
    
    11
    -                   TvSubst   = []
    
    12
    -                   CvSubst   = []>
    
    13 9
     *** Offending Program ***
    
    14 10
     Rec {
    
    15 11
     $trModule = Module (TrNameS "main"#) (TrNameS "T21115b"#)