Apoorv Ingle pushed to branch wip/ani/kill-popErrCtxt at Glasgow Haskell Compiler / GHC

Commits:

11 changed files:

Changes:

  • compiler/GHC/Hs/Expr.hs
    ... ... @@ -676,22 +676,21 @@ data SrcCodeOrigin
    676 676
       = OrigExpr (HsExpr GhcRn)                -- ^ The source, user written, expression
    
    677 677
       | OrigStmt (ExprLStmt GhcRn) HsDoFlavour -- ^ which kind of do-block did this statement come from
    
    678 678
       | OrigPat  (Pat GhcRn)                   -- ^ Used for failable patterns that trigger MonadFail constraints
    
    679
    +  | PopErrCtxt -- A hint for typechecker to pop
    
    680
    +               -- the top of the error context stack
    
    681
    +               -- Does not presist post renaming phase
    
    682
    +               -- See Part 3. of Note [Expanding HsDo with XXExprGhcRn]
    
    683
    +               -- in `GHC.Tc.Gen.Do`
    
    679 684
     
    
    680 685
     data XXExprGhcRn
    
    681 686
       = ExpandedThingRn { xrn_orig     :: SrcCodeOrigin   -- The original source thing to be used for error messages
    
    682 687
                         , xrn_expanded :: HsExpr GhcRn    -- The compiler generated, expanded thing
    
    683 688
                         }
    
    684 689
     
    
    685
    -  | PopErrCtxt                                     -- A hint for typechecker to pop
    
    686
    -    {-# UNPACK #-} !(HsExpr GhcRn)                 -- the top of the error context stack
    
    687
    -                                                   -- Does not presist post renaming phase
    
    688
    -                                                   -- See Part 3. of Note [Expanding HsDo with XXExprGhcRn]
    
    689
    -                                                   -- in `GHC.Tc.Gen.Do`
    
    690 690
       | HsRecSelRn  (FieldOcc GhcRn)   -- ^ Variable pointing to record selector
    
    691 691
                                -- See Note [Non-overloaded record field selectors] and
    
    692 692
                                -- Note [Record selectors in the AST]
    
    693 693
     
    
    694
    -
    
    695 694
     -- | Build an expression using the extension constructor `XExpr`,
    
    696 695
     --   and the two components of the expansion: original expression and
    
    697 696
     --   expanded expressions.
    
    ... ... @@ -713,6 +712,12 @@ mkExpandedStmt
    713 712
     mkExpandedStmt oStmt flav eExpr = XExpr (ExpandedThingRn { xrn_orig = OrigStmt oStmt flav
    
    714 713
                                                              , xrn_expanded = eExpr })
    
    715 714
     
    
    715
    +mkExpandedLastStmt
    
    716
    +  :: HsExpr GhcRn         -- ^ expanded expression
    
    717
    +  -> HsExpr GhcRn         -- ^ suitably wrapped 'XXExprGhcRn'
    
    718
    +mkExpandedLastStmt eExpr = XExpr (ExpandedThingRn { xrn_orig = PopErrCtxt
    
    719
    +                                                  , xrn_expanded = eExpr })
    
    720
    +
    
    716 721
     data XXExprGhcTc
    
    717 722
       = WrapExpr        -- Type and evidence application and abstractions
    
    718 723
           HsWrapper (HsExpr GhcTc)
    
    ... ... @@ -1083,11 +1088,11 @@ instance Outputable SrcCodeOrigin where
    1083 1088
             OrigExpr x    -> ppr_builder "<OrigExpr>:" x
    
    1084 1089
             OrigStmt x _  -> ppr_builder "<OrigStmt>:" x
    
    1085 1090
             OrigPat  x    -> ppr_builder "<OrigPat>:" x
    
    1091
    +        PopErrCtxt    -> text "<PopErrCtxt>"
    
    1086 1092
         where ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens (ppr x))) (ppr x)
    
    1087 1093
     
    
    1088 1094
     instance Outputable XXExprGhcRn where
    
    1089 1095
       ppr (ExpandedThingRn o e) = ifPprDebug (braces $ vcat [ppr o, text ";;" , ppr e]) (ppr o)
    
    1090
    -  ppr (PopErrCtxt e)        = ifPprDebug (braces (text "<PopErrCtxt>" <+> ppr e)) (ppr e)
    
    1091 1096
       ppr (HsRecSelRn f)        = pprPrefixOcc f
    
    1092 1097
     
    
    1093 1098
     instance Outputable XXExprGhcTc where
    
    ... ... @@ -1133,7 +1138,6 @@ ppr_infix_expr _ = Nothing
    1133 1138
     
    
    1134 1139
     ppr_infix_expr_rn :: XXExprGhcRn -> Maybe SDoc
    
    1135 1140
     ppr_infix_expr_rn (ExpandedThingRn thing _) = ppr_infix_hs_expansion thing
    
    1136
    -ppr_infix_expr_rn (PopErrCtxt a)            = ppr_infix_expr a
    
    1137 1141
     ppr_infix_expr_rn (HsRecSelRn f)            = Just (pprInfixOcc f)
    
    1138 1142
     
    
    1139 1143
     ppr_infix_expr_tc :: XXExprGhcTc -> Maybe SDoc
    
    ... ... @@ -1233,7 +1237,6 @@ hsExprNeedsParens prec = go
    1233 1237
     
    
    1234 1238
         go_x_rn :: XXExprGhcRn -> Bool
    
    1235 1239
         go_x_rn (ExpandedThingRn thing _ )   = hsExpandedNeedsParens thing
    
    1236
    -    go_x_rn (PopErrCtxt a)               = hsExprNeedsParens prec a
    
    1237 1240
         go_x_rn (HsRecSelRn{})               = False
    
    1238 1241
     
    
    1239 1242
         hsExpandedNeedsParens :: SrcCodeOrigin -> Bool
    
    ... ... @@ -1286,7 +1289,6 @@ isAtomicHsExpr (XExpr x)
    1286 1289
     
    
    1287 1290
         go_x_rn :: XXExprGhcRn -> Bool
    
    1288 1291
         go_x_rn (ExpandedThingRn thing _)   = isAtomicExpandedThingRn thing
    
    1289
    -    go_x_rn (PopErrCtxt a)              = isAtomicHsExpr a
    
    1290 1292
         go_x_rn (HsRecSelRn{})              = True
    
    1291 1293
     
    
    1292 1294
         isAtomicExpandedThingRn :: SrcCodeOrigin -> Bool
    

  • compiler/GHC/HsToCore/Quote.hs
    ... ... @@ -1747,7 +1747,6 @@ repE e@(XExpr (ExpandedThingRn o x))
    1747 1747
              else repE e }
    
    1748 1748
       | otherwise
    
    1749 1749
       = notHandled (ThExpressionForm e)
    
    1750
    -repE (XExpr (PopErrCtxt e)) = repE e
    
    1751 1750
     repE (XExpr (HsRecSelRn (FieldOcc _ (L _ x)))) = repE (mkHsVar (noLocA x))
    
    1752 1751
     repE e@(HsPragE _ (HsPragSCC {}) _) = notHandled (ThCostCentres e)
    
    1753 1752
     repE e@(HsTypedBracket{})   = notHandled (ThExpressionForm e)
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -951,10 +951,10 @@ addArgCtxt arg_no fun (L arg_loc arg) thing_inside
    951 951
                                         , text "arg: " <+> ppr (arg, arg_no)
    
    952 952
                                         , text "arg_loc:" <+> ppr arg_loc
    
    953 953
                                         , text "fun:" <+> ppr fun
    
    954
    -                                    , text "err_ctx" <+> vcat (fmap (\ (x, y) -> case x of
    
    955
    -                                                                        UserCodeCtxt{} -> text "<USER>" <+> pprErrCtxtMsg y
    
    956
    -                                                                        ExpansionCodeCtxt{} -> text "<EXPN>" <+> pprErrCtxtMsg y)
    
    957
    -                                                               (take 4 (zip err_ctx err_ctx_msg)))
    
    954
    +                                    -- , text "err_ctx" <+> vcat (fmap (\ (x, y) -> case x of
    
    955
    +                                    --                                     UserCodeCtxt{} -> text "<USER>" <+> pprErrCtxtMsg y
    
    956
    +                                    --                                     ExpansionCodeCtxt{} -> text "<EXPN>" <+> pprErrCtxtMsg y)
    
    957
    +                                    --                            (take 4 (zip err_ctx err_ctx_msg)))
    
    958 958
                                         ])
    
    959 959
            ; if in_generated_code
    
    960 960
              then updCtxtForArg (locA arg_loc) arg $
    
    ... ... @@ -968,10 +968,10 @@ addArgCtxt arg_no fun (L arg_loc arg) thing_inside
    968 968
           do setSrcSpan l $
    
    969 969
                addExprCtxt e $
    
    970 970
                thing_inside
    
    971
    -    updCtxtForArg (UnhelpfulSpan UnhelpfulGenerated) _ thing_inside = -- See 2.i above
    
    972
    -      thing_inside
    
    971
    +    -- updCtxtForArg (UnhelpfulSpan UnhelpfulGenerated) _ thing_inside = -- See 2.i above
    
    972
    +    --   thing_inside
    
    973 973
         updCtxtForArg (UnhelpfulSpan {}) _ thing_inside = -- See 2.ii above
    
    974
    -      do setInUserCode $
    
    974
    +      do -- setInUserCode $
    
    975 975
                thing_inside
    
    976 976
     
    
    977 977
     
    

  • compiler/GHC/Tc/Gen/Do.hs
    ... ... @@ -81,7 +81,7 @@ expand_do_stmts flav [stmt@(L sloc (LastStmt _ body@(L body_loc _) _ ret_expr))]
    81 81
     -- See `checkLastStmt` and `Syntax.Expr.StmtLR.LastStmt`
    
    82 82
        | NoSyntaxExprRn <- ret_expr
    
    83 83
        -- Last statement is just body if we are not in ListComp context. See Syntax.Expr.LastStmt
    
    84
    -   = return $ L sloc (mkExpandedStmt stmt flav (HsPar noExtField body))
    
    84
    +   = return $ L sloc (mkExpandedLastStmt (HsPar noExtField body))
    
    85 85
     
    
    86 86
        | SyntaxExprRn ret <- ret_expr  -- We have unfortunately lost the location on the return function :(
    
    87 87
        --
    
    ... ... @@ -89,7 +89,7 @@ expand_do_stmts flav [stmt@(L sloc (LastStmt _ body@(L body_loc _) _ ret_expr))]
    89 89
        --               return e  ~~> return e
    
    90 90
        -- to make T18324 work
    
    91 91
        = do let expansion = L body_loc (genHsApp ret body)
    
    92
    -        return $ L sloc (mkExpandedStmt stmt flav (HsPar noExtField expansion))
    
    92
    +        return $ L sloc (mkExpandedLastStmt (HsPar noExtField expansion))
    
    93 93
     
    
    94 94
     expand_do_stmts doFlavour (stmt@(L loc (LetStmt _ bs)) : lstmts) =
    
    95 95
     -- See  Note [Expanding HsDo with XXExprGhcRn] Equation (3) below
    
    ... ... @@ -126,7 +126,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BodyStmt _ e (SyntaxExprRn then_op) _))
    126 126
     --      e ; stmts ~~> (>>) e stmts'
    
    127 127
       do expand_stmts_expr <- expand_do_stmts doFlavour lstmts
    
    128 128
          let expansion = genHsExpApps then_op  -- (>>)
    
    129
    -                     [ wrapNoSpan $ unLoc e -- Span is set because of statement loc
    
    129
    +                     [ e -- Span is set because of statement loc
    
    130 130
                          , expand_stmts_expr ]
    
    131 131
          return $ L loc (mkExpandedStmt stmt doFlavour expansion)
    
    132 132
     
    

  • compiler/GHC/Tc/Gen/Expr.hs
    ... ... @@ -54,6 +54,7 @@ import GHC.Tc.Utils.Concrete ( hasFixedRuntimeRep_syntactic, hasFixedRuntimeRep
    54 54
     import GHC.Tc.Utils.Instantiate
    
    55 55
     import GHC.Tc.Utils.Env
    
    56 56
     import GHC.Tc.Types.Origin
    
    57
    +import GHC.Tc.Types.ErrCtxt ( srcCodeOriginErrCtxMsg )
    
    57 58
     import GHC.Tc.Types.Evidence
    
    58 59
     import GHC.Tc.Errors.Types hiding (HoleError)
    
    59 60
     
    
    ... ... @@ -665,9 +666,7 @@ tcExpr expr@(RecordUpd { rupd_expr = record_expr
    665 666
     
    
    666 667
             ; (ds_expr, ds_res_ty, err_msg)
    
    667 668
                 <- expandRecordUpd record_expr possible_parents rbnds res_ty
    
    668
    -        ; addErrCtxt err_msg $
    
    669
    -          updLclCtxt setLclCtxtInGenCode $
    
    670
    -          -- setInGeneratedCode (OrigExpr expr) $
    
    669
    +        ; addExpansionErrCtxt (OrigExpr expr) err_msg $
    
    671 670
               do { -- Typecheck the expanded expression.
    
    672 671
                    expr' <- tcExpr ds_expr (Check ds_res_ty)
    
    673 672
                    -- NB: it's important to use ds_res_ty and not res_ty here.
    
    ... ... @@ -722,7 +721,7 @@ tcExpr (HsProjection _ _) _ = panic "GHC.Tc.Gen.Expr: tcExpr: HsProjection: Not
    722 721
     -- Here we get rid of it and add the finalizers to the global environment.
    
    723 722
     -- See Note [Delaying modFinalizers in untyped splices] in GHC.Rename.Splice.
    
    724 723
     tcExpr (HsTypedSplice ext splice)   res_ty = tcTypedSplice ext splice res_ty
    
    725
    -tcExpr e@(HsTypedBracket _ext body)    res_ty = tcTypedBracket e body res_ty
    
    724
    +tcExpr e@(HsTypedBracket _ext body) res_ty = tcTypedBracket e body res_ty
    
    726 725
     
    
    727 726
     tcExpr e@(HsUntypedBracket ps body) res_ty = tcUntypedBracket e body ps res_ty
    
    728 727
     tcExpr (HsUntypedSplice splice _)   res_ty
    
    ... ... @@ -757,14 +756,8 @@ tcExpr (SectionR {}) ty = pprPanic "tcExpr:SectionR" (ppr ty)
    757 756
     -}
    
    758 757
     
    
    759 758
     tcXExpr :: XXExprGhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
    
    760
    -
    
    761
    -tcXExpr (PopErrCtxt e) res_ty
    
    762
    -  = do popErrCtxt $ -- See Part 3 of Note [Expanding HsDo with XXExprGhcRn] in `GHC.Tc.Gen.Do`
    
    763
    -         addExprCtxt e $
    
    764
    -         tcExpr e res_ty
    
    765
    -
    
    766 759
     tcXExpr (ExpandedThingRn o e) res_ty
    
    767
    -   = setInGeneratedCode o $
    
    760
    +   = addExpansionErrCtxt o (srcCodeOriginErrCtxMsg o) $
    
    768 761
          -- e is the expanded expression of o, so we need to set the error ctxt to generated
    
    769 762
          -- see Note [Error Context Stack] in `GHC.Tc.Type.LclEnv`
    
    770 763
             mkExpandedTc o <$> -- necessary for hpc ticks
    

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -49,6 +49,7 @@ import GHC.Tc.Solver ( InferMode(..), simplifyInfer )
    49 49
     import GHC.Tc.Utils.Env
    
    50 50
     import GHC.Tc.Utils.TcMType
    
    51 51
     import GHC.Tc.Types.Origin
    
    52
    +import GHC.Tc.Types.ErrCtxt ( srcCodeOriginErrCtxMsg )
    
    52 53
     import GHC.Tc.Types.Constraint( WantedConstraints )
    
    53 54
     import GHC.Tc.Utils.TcType as TcType
    
    54 55
     import GHC.Tc.Types.Evidence
    
    ... ... @@ -269,7 +270,6 @@ splitHsApps e = go e noSrcSpan []
    269 270
                         -- and its hard to say exactly what that is
    
    270 271
                    : EWrap (EExpand e)
    
    271 272
                    : args )
    
    272
    -    go (XExpr (PopErrCtxt fun)) lspan args = go fun lspan args
    
    273 273
           -- look through PopErrCtxt (cf. T17594f) we do not want to lose the opportunity of calling tcEValArgQL
    
    274 274
           -- unlike HsPar, it is okay to forget about the PopErrCtxts as it does not persist over in GhcTc land
    
    275 275
     
    
    ... ... @@ -471,9 +471,8 @@ tcInferAppHead_maybe fun =
    471 471
         case fun of
    
    472 472
           HsVar _ nm                  -> Just <$> tcInferId nm
    
    473 473
           XExpr (HsRecSelRn f)        -> Just <$> tcInferRecSelId f
    
    474
    -      XExpr (ExpandedThingRn o e) -> Just <$> (setInGeneratedCode o $ -- We do not want to instantiate c.f. T19167
    
    475
    -                                                tcExprSigma False e)
    
    476
    -      XExpr (PopErrCtxt e)        -> tcInferAppHead_maybe e
    
    474
    +      XExpr (ExpandedThingRn o e) -> Just <$> (addExpansionErrCtxt o (srcCodeOriginErrCtxMsg o) $ -- We do not want to instantiate c.f. T19167
    
    475
    +                                                    tcExprSigma False e)
    
    477 476
           ExprWithTySig _ e hs_ty     -> Just <$> tcExprWithSig e hs_ty
    
    478 477
           HsOverLit _ lit             -> Just <$> tcInferOverLit lit
    
    479 478
           _                           -> return Nothing
    

  • compiler/GHC/Tc/Gen/Match.hs
    ... ... @@ -57,6 +57,7 @@ import GHC.Tc.Gen.Bind
    57 57
     import GHC.Tc.Utils.Concrete ( hasFixedRuntimeRep_syntactic )
    
    58 58
     import GHC.Tc.Utils.Unify
    
    59 59
     import GHC.Tc.Types.Origin
    
    60
    +import GHC.Tc.Types.ErrCtxt ( srcCodeOriginErrCtxMsg )
    
    60 61
     import GHC.Tc.Types.Evidence
    
    61 62
     import GHC.Rename.Env ( irrefutableConLikeTc )
    
    62 63
     
    
    ... ... @@ -404,9 +405,9 @@ tcDoStmts doExpr@(DoExpr _) ss@(L l stmts) res_ty
    404 405
                       ; return (HsDo res_ty doExpr (L l stmts')) }
    
    405 406
               else do { expanded_expr <- expandDoStmts doExpr stmts -- Do expansion on the fly
    
    406 407
                       ; let orig = HsDo noExtField doExpr ss
    
    407
    -                  ; setInGeneratedCode (OrigExpr orig) $ do
    
    408
    -                      { e' <- tcMonoLExpr expanded_expr res_ty
    
    409
    -                      ; return (mkExpandedExprTc orig (unLoc e'))}
    
    408
    +                  ; addExpansionErrCtxt (OrigExpr orig) (srcCodeOriginErrCtxMsg (OrigExpr orig)) $
    
    409
    +                    do { e' <- tcMonoLExpr expanded_expr res_ty
    
    410
    +                       ; return (mkExpandedExprTc orig (unLoc e'))}
    
    410 411
                       }
    
    411 412
             }
    
    412 413
     
    

  • compiler/GHC/Tc/Types/ErrCtxt.hs
    ... ... @@ -4,7 +4,7 @@
    4 4
     {-# LANGUAGE UndecidableInstances #-}
    
    5 5
     
    
    6 6
     module GHC.Tc.Types.ErrCtxt
    
    7
    -  ( ErrCtxt (..), ErrCtxtMsg(..), srcCodeOriginErrCtxMsg
    
    7
    +  ( ErrCtxt (..), ErrCtxtMsg(..), ErrCtxtMsgM,  CodeSrcFlag (..), srcCodeOriginErrCtxMsg
    
    8 8
       , UserSigType(..), FunAppCtxtFunArg(..)
    
    9 9
       , TyConInstFlavour(..)
    
    10 10
       )
    
    ... ... @@ -48,9 +48,11 @@ import qualified Data.List.NonEmpty as NE
    48 48
     
    
    49 49
     --------------------------------------------------------------------------------
    
    50 50
     
    
    51
    +type ErrCtxtMsgM = TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)
    
    52
    +
    
    51 53
     -- | Additional context to include in an error message, e.g.
    
    52 54
     -- "In the type signature ...", "In the ambiguity check for ...", etc.
    
    53
    -data ErrCtxt = UserCodeCtxt (Bool, TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg))
    
    55
    +data ErrCtxt = MkErrCtxt CodeSrcFlag ErrCtxtMsgM
    
    54 56
                  -- Monadic so that we have a chance
    
    55 57
                  -- to deal with bound type variables just before error
    
    56 58
                  -- message construction
    
    ... ... @@ -58,11 +60,9 @@ data ErrCtxt = UserCodeCtxt (Bool, TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg))
    58 60
                  -- Bool:  True <=> this is a landmark context; do not
    
    59 61
                  --                 discard it when trimming for display
    
    60 62
     
    
    61
    -             | ExpansionCodeCtxt SrcCodeOrigin
    
    62
    -             -- The payload is a SrcCodeOrigin because it is used to generate
    
    63
    -             -- 1. The CtOrigin for CtLoc, and
    
    64
    -             -- 2. ErrCtxtMsg in error messages
    
    65
    -
    
    63
    +data CodeSrcFlag = VanillaUserSrcCode
    
    64
    +                 | LandmarkUserSrcCode
    
    65
    +                 | ExpansionCodeCtxt SrcCodeOrigin
    
    66 66
     
    
    67 67
     --------------------------------------------------------------------------------
    
    68 68
     -- Error message contexts
    
    ... ... @@ -233,3 +233,4 @@ srcCodeOriginErrCtxMsg :: SrcCodeOrigin -> ErrCtxtMsg
    233 233
     srcCodeOriginErrCtxMsg (OrigExpr e) = ExprCtxt e
    
    234 234
     srcCodeOriginErrCtxMsg (OrigStmt s f) = StmtErrCtxt (HsDoStmt f) (unLoc s)
    
    235 235
     srcCodeOriginErrCtxMsg (OrigPat  p) = PatCtxt p
    
    236
    +srcCodeOriginErrCtxMsg (PopErrCtxt) = error "Shouldn't happen srcCodeOriginErr"

  • compiler/GHC/Tc/Types/LclEnv.hs
    ... ... @@ -25,8 +25,6 @@ module GHC.Tc.Types.LclEnv (
    25 25
       , setLclEnvSrcCodeOrigin
    
    26 26
       , setLclCtxtSrcCodeOrigin
    
    27 27
       , lclEnvInGeneratedCode
    
    28
    -  , setLclCtxtInGenCode
    
    29
    -  , setLclCtxtInUserCode
    
    30 28
     
    
    31 29
       , addLclEnvErrCtxt
    
    32 30
     
    
    ... ... @@ -38,7 +36,7 @@ module GHC.Tc.Types.LclEnv (
    38 36
     
    
    39 37
     import GHC.Prelude
    
    40 38
     
    
    41
    -import GHC.Hs ( SrcCodeOrigin )
    
    39
    +import GHC.Hs ( SrcCodeOrigin (..) )
    
    42 40
     import GHC.Tc.Utils.TcType ( TcLevel )
    
    43 41
     import GHC.Tc.Errors.Types ( TcRnMessage )
    
    44 42
     
    
    ... ... @@ -119,7 +117,7 @@ type ErrCtxtStack = [ErrCtxt]
    119 117
     
    
    120 118
     -- | Get the original source code
    
    121 119
     get_src_code_origin :: ErrCtxtStack -> Maybe SrcCodeOrigin
    
    122
    -get_src_code_origin (ExpansionCodeCtxt origSrcCode : _) = Just origSrcCode
    
    120
    +get_src_code_origin (MkErrCtxt (ExpansionCodeCtxt origSrcCode) _ : _) = Just origSrcCode
    
    123 121
                        -- we are in generated code, due to the expansion of the original syntax origSrcCode
    
    124 122
     get_src_code_origin _ = Nothing
    
    125 123
                        -- we are in user code, so blame the expression in hand
    
    ... ... @@ -127,7 +125,6 @@ get_src_code_origin _ = Nothing
    127 125
     data TcLclCtxt
    
    128 126
       = TcLclCtxt {
    
    129 127
             tcl_loc         :: RealSrcSpan,     -- Source span
    
    130
    -        tcl_in_gen_code :: Bool,
    
    131 128
             tcl_err_ctxt    :: ErrCtxtStack,    -- See Note [Error Context Stack]
    
    132 129
             tcl_tclvl       :: TcLevel,
    
    133 130
             tcl_bndrs       :: TcBinderStack,   -- Used for reporting relevant bindings,
    
    ... ... @@ -199,33 +196,34 @@ setLclEnvErrCtxt :: ErrCtxtStack -> TcLclEnv -> TcLclEnv
    199 196
     setLclEnvErrCtxt ctxt = modifyLclCtxt (\env -> env { tcl_err_ctxt = ctxt })
    
    200 197
     
    
    201 198
     addLclEnvErrCtxt :: ErrCtxt -> TcLclEnv -> TcLclEnv
    
    202
    -addLclEnvErrCtxt (ExpansionCodeCtxt co) = setLclEnvSrcCodeOrigin co
    
    203
    -addLclEnvErrCtxt ec = modifyLclCtxt (\env -> if (tcl_in_gen_code env)
    
    199
    +addLclEnvErrCtxt ec@(MkErrCtxt (ExpansionCodeCtxt _) _) = setLclEnvSrcCodeOrigin ec
    
    200
    +addLclEnvErrCtxt ec = modifyLclCtxt (\env -> if lclCtxtInGeneratedCode env
    
    204 201
                                                  then env -- no op if we are in generated code
    
    205 202
                                                  else env { tcl_err_ctxt =  ec : (tcl_err_ctxt env) })
    
    206 203
     
    
    207 204
     getLclEnvSrcCodeOrigin :: TcLclEnv -> Maybe SrcCodeOrigin
    
    208 205
     getLclEnvSrcCodeOrigin = get_src_code_origin . tcl_err_ctxt . tcl_lcl_ctxt
    
    209 206
     
    
    210
    -setLclEnvSrcCodeOrigin :: SrcCodeOrigin -> TcLclEnv -> TcLclEnv
    
    211
    -setLclEnvSrcCodeOrigin o = modifyLclCtxt (setLclCtxtSrcCodeOrigin o)
    
    212
    -
    
    213
    -setLclCtxtInGenCode :: TcLclCtxt -> TcLclCtxt
    
    214
    -setLclCtxtInGenCode lclCtxt = lclCtxt { tcl_in_gen_code = True }
    
    215
    -
    
    216
    -setLclCtxtInUserCode :: TcLclCtxt -> TcLclCtxt
    
    217
    -setLclCtxtInUserCode lclCtxt = lclCtxt { tcl_in_gen_code = False }
    
    207
    +setLclEnvSrcCodeOrigin :: ErrCtxt -> TcLclEnv -> TcLclEnv
    
    208
    +setLclEnvSrcCodeOrigin ec = modifyLclCtxt (setLclCtxtSrcCodeOrigin ec)
    
    218 209
     
    
    219 210
     -- See Note [ErrCtxt Stack Manipulation]
    
    220
    -setLclCtxtSrcCodeOrigin :: SrcCodeOrigin -> TcLclCtxt -> TcLclCtxt
    
    221
    -setLclCtxtSrcCodeOrigin o lclCtxt
    
    222
    -  | (ExpansionCodeCtxt _ : ec) <- tcl_err_ctxt lclCtxt
    
    223
    -  = lclCtxt { tcl_err_ctxt = ExpansionCodeCtxt o : ec }
    
    211
    +setLclCtxtSrcCodeOrigin :: ErrCtxt -> TcLclCtxt -> TcLclCtxt
    
    212
    +setLclCtxtSrcCodeOrigin ec lclCtxt
    
    213
    +  | MkErrCtxt (ExpansionCodeCtxt PopErrCtxt) _ <- ec
    
    214
    +  = lclCtxt { tcl_err_ctxt = tail (tcl_err_ctxt lclCtxt) }
    
    215
    +  | MkErrCtxt (ExpansionCodeCtxt _) _ : ecs <- tcl_err_ctxt lclCtxt
    
    216
    +  , MkErrCtxt (ExpansionCodeCtxt _) _ <- ec
    
    217
    +  = lclCtxt { tcl_err_ctxt =  ec : ecs }
    
    224 218
       | otherwise
    
    225
    -  = lclCtxt { tcl_err_ctxt = ExpansionCodeCtxt o : tcl_err_ctxt lclCtxt }
    
    219
    +  = lclCtxt { tcl_err_ctxt = ec : tcl_err_ctxt lclCtxt }
    
    226 220
     
    
    227 221
     lclCtxtInGeneratedCode :: TcLclCtxt -> Bool
    
    228
    -lclCtxtInGeneratedCode = tcl_in_gen_code
    
    222
    +lclCtxtInGeneratedCode lclCtxt
    
    223
    +  | (MkErrCtxt (ExpansionCodeCtxt _) _ : _) <- tcl_err_ctxt lclCtxt
    
    224
    +  = True
    
    225
    +  | otherwise
    
    226
    +  = False
    
    229 227
     
    
    230 228
     lclEnvInGeneratedCode :: TcLclEnv -> Bool
    
    231 229
     lclEnvInGeneratedCode =  lclCtxtInGeneratedCode . tcl_lcl_ctxt
    

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -829,7 +829,6 @@ exprCtOrigin e@(HsProjection _ _) = ExpansionOrigin (OrigExpr e)
    829 829
     exprCtOrigin e@(RecordUpd{})      = ExpansionOrigin (OrigExpr e)
    
    830 830
     exprCtOrigin e@(HsGetField{})     = ExpansionOrigin (OrigExpr e)
    
    831 831
     exprCtOrigin (XExpr (ExpandedThingRn o _)) = ExpansionOrigin o
    
    832
    -exprCtOrigin (XExpr (PopErrCtxt e)) = exprCtOrigin e
    
    833 832
     exprCtOrigin (XExpr (HsRecSelRn f))  = OccurrenceOfRecSel (foExt f)
    
    834 833
     
    
    835 834
     
    
    ... ... @@ -882,6 +881,7 @@ pprCtOrigin (ExpansionOrigin o)
    882 881
             OrigExpr (ExplicitList{}) -> text "an overloaded list"
    
    883 882
             OrigExpr (HsIf{}) -> text "an if-then-else expression"
    
    884 883
             OrigExpr e -> text "the expression" <+> (ppr e)
    
    884
    +        PopErrCtxt -> text "Shouldn't Happen PopErrCtxt"
    
    885 885
     
    
    886 886
     pprCtOrigin (GivenSCOrigin sk d blk)
    
    887 887
       = vcat [ ctoHerald <+> pprSkolInfo sk
    
    ... ... @@ -1113,6 +1113,7 @@ ppr_br (ExpansionOrigin (OrigExpr (HsIf{}))) = text "an if-then-else expression"
    1113 1113
     ppr_br (ExpansionOrigin (OrigExpr e)) = text "an expression" <+> ppr e
    
    1114 1114
     ppr_br (ExpansionOrigin (OrigStmt{})) = text "a do statement"
    
    1115 1115
     ppr_br (ExpansionOrigin (OrigPat{})) = text "a do statement"
    
    1116
    +ppr_br (ExpansionOrigin PopErrCtxt) = text "SHOULDN'T HAPPEN POPERRORCTXT"
    
    1116 1117
     ppr_br (ExpectedTySyntax o _) = ppr_br o
    
    1117 1118
     ppr_br (ExpectedFunTySyntaxOp{}) = text "a rebindable syntax operator"
    
    1118 1119
     ppr_br (ExpectedFunTyViewPat{}) = text "a view pattern"
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -63,7 +63,7 @@ module GHC.Tc.Utils.Monad(
    63 63
       -- * Error management
    
    64 64
       getSrcCodeOrigin,
    
    65 65
       getSrcSpanM, setSrcSpan, setSrcSpanA, addLocM,
    
    66
    -  inGeneratedCode, setInGeneratedCode, setInUserCode, setLclCtxtInGenCode,
    
    66
    +  inGeneratedCode, -- setInGeneratedCode,
    
    67 67
       wrapLocM, wrapLocFstM, wrapLocFstMA, wrapLocSndM, wrapLocSndMA, wrapLocM_,
    
    68 68
       wrapLocMA_,wrapLocMA,
    
    69 69
       getErrsVar, setErrsVar,
    
    ... ... @@ -88,6 +88,7 @@ module GHC.Tc.Utils.Monad(
    88 88
     
    
    89 89
       -- * Context management for the type checker
    
    90 90
       getErrCtxt, setErrCtxt, addErrCtxt, addErrCtxtM, addLandmarkErrCtxt,
    
    91
    +  addExpansionErrCtxt, addExpansionErrCtxtM,
    
    91 92
       addLandmarkErrCtxtM, popErrCtxt, getCtLocM, setCtLocM, mkCtLocEnv,
    
    92 93
     
    
    93 94
       -- * Diagnostic message generation (type checker)
    
    ... ... @@ -418,7 +419,6 @@ initTcWithGbl hsc_env gbl_env loc do_this
    418 419
                     tcl_lcl_ctxt   = TcLclCtxt {
    
    419 420
                     tcl_loc        = loc,
    
    420 421
                     -- tcl_loc should be over-ridden very soon!
    
    421
    -                tcl_in_gen_code = False,
    
    422 422
                     tcl_err_ctxt   = [],
    
    423 423
                     tcl_rdr        = emptyLocalRdrEnv,
    
    424 424
                     tcl_th_ctxt    = topLevel,
    
    ... ... @@ -1078,10 +1078,10 @@ inGeneratedCode = lclEnvInGeneratedCode <$> getLclEnv
    1078 1078
     setSrcSpan :: SrcSpan -> TcRn a -> TcRn a
    
    1079 1079
     -- See Note [Error contexts in generated code]
    
    1080 1080
     setSrcSpan (RealSrcSpan loc _) thing_inside
    
    1081
    -  = updLclCtxt (\env -> env { tcl_loc = loc, tcl_in_gen_code = False }) thing_inside
    
    1081
    +  = updLclCtxt (\env -> env { tcl_loc = loc }) thing_inside
    
    1082 1082
     
    
    1083 1083
     setSrcSpan (UnhelpfulSpan _) thing_inside
    
    1084
    -  = updLclCtxt setLclCtxtInGenCode thing_inside
    
    1084
    +  = thing_inside
    
    1085 1085
     
    
    1086 1086
     getSrcCodeOrigin :: TcRn (Maybe SrcCodeOrigin)
    
    1087 1087
     getSrcCodeOrigin =
    
    ... ... @@ -1095,13 +1095,10 @@ getSrcCodeOrigin =
    1095 1095
     --
    
    1096 1096
     -- See Note [Error contexts in generated code]
    
    1097 1097
     -- See Note [Error Context Stack]
    
    1098
    -setInGeneratedCode :: SrcCodeOrigin -> TcRn a -> TcRn a
    
    1099
    -setInGeneratedCode sco thing_inside =
    
    1100
    -  updLclCtxt setLclCtxtInGenCode $
    
    1101
    -  updLclCtxt (setLclCtxtSrcCodeOrigin sco) thing_inside
    
    1102
    -
    
    1103
    -setInUserCode :: TcRn a -> TcRn a
    
    1104
    -setInUserCode = updLclCtxt setLclCtxtInUserCode
    
    1098
    +-- setInGeneratedCode :: SrcCodeOrigin -> TcRn a -> TcRn a
    
    1099
    +-- setInGeneratedCode sco thing_inside =
    
    1100
    +--   -- updLclCtxt setLclCtxtInGenCode $
    
    1101
    +--   updLclCtxt (setLclCtxtSrcCodeOrigin sco) thing_inside
    
    1105 1102
     
    
    1106 1103
     setSrcSpanA :: EpAnn ann -> TcRn a -> TcRn a
    
    1107 1104
     setSrcSpanA l = setSrcSpan (locA l)
    
    ... ... @@ -1349,12 +1346,20 @@ addErrCtxt :: ErrCtxtMsg -> TcM a -> TcM a
    1349 1346
     {-# INLINE addErrCtxt #-}   -- Note [Inlining addErrCtxt]
    
    1350 1347
     addErrCtxt msg = addErrCtxtM (\env -> return (env, msg))
    
    1351 1348
     
    
    1349
    +addExpansionErrCtxt :: SrcCodeOrigin -> ErrCtxtMsg -> TcM a -> TcM a
    
    1350
    +{-# INLINE addExpansionErrCtxt #-}   -- Note [Inlining addErrCtxt]
    
    1351
    +addExpansionErrCtxt o msg = addExpansionErrCtxtM o (\env -> return (env, msg))
    
    1352
    +
    
    1352 1353
     -- | Add a message to the error context. This message may do tidying.
    
    1353 1354
     --   NB. No op in generated code
    
    1354 1355
     --   See Note [Rebindable syntax and XXExprGhcRn] in GHC.Hs.Expr
    
    1355 1356
     addErrCtxtM :: (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a
    
    1356 1357
     {-# INLINE addErrCtxtM #-}  -- Note [Inlining addErrCtxt]
    
    1357
    -addErrCtxtM ctxt = pushCtxt (UserCodeCtxt (False, ctxt))
    
    1358
    +addErrCtxtM ctxt = pushCtxt (MkErrCtxt VanillaUserSrcCode ctxt)
    
    1359
    +
    
    1360
    +addExpansionErrCtxtM :: SrcCodeOrigin -> (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a
    
    1361
    +{-# INLINE addExpansionErrCtxtM #-}  -- Note [Inlining addErrCtxt]
    
    1362
    +addExpansionErrCtxtM o ctxt = pushCtxt (MkErrCtxt (ExpansionCodeCtxt o) ctxt)
    
    1358 1363
     
    
    1359 1364
     -- | Add a fixed landmark message to the error context. A landmark
    
    1360 1365
     -- message is always sure to be reported, even if there is a lot of
    
    ... ... @@ -1368,7 +1373,7 @@ addLandmarkErrCtxt msg = addLandmarkErrCtxtM (\env -> return (env, msg))
    1368 1373
     -- and tidying.
    
    1369 1374
     addLandmarkErrCtxtM :: (TidyEnv -> ZonkM (TidyEnv, ErrCtxtMsg)) -> TcM a -> TcM a
    
    1370 1375
     {-# INLINE addLandmarkErrCtxtM #-}  -- Note [Inlining addErrCtxt]
    
    1371
    -addLandmarkErrCtxtM ctxt = pushCtxt (UserCodeCtxt (True, ctxt))
    
    1376
    +addLandmarkErrCtxtM ctxt = pushCtxt (MkErrCtxt LandmarkUserSrcCode ctxt)
    
    1372 1377
     
    
    1373 1378
     -- | NB. no op in generated code
    
    1374 1379
     -- See Note [Rebindable syntax and XXExprGhcRn] in GHC.Hs.Expr
    
    ... ... @@ -1840,18 +1845,17 @@ mkErrCtxt env ctxts
    1840 1845
      where
    
    1841 1846
        go :: Bool -> Int -> TidyEnv -> [ErrCtxt] -> TcM [ErrCtxtMsg]
    
    1842 1847
        go _ _ _   [] = return []
    
    1843
    -   go dbg n env (UserCodeCtxt (is_landmark, ctxt) : ctxts)
    
    1844
    -     | is_landmark || n < mAX_CONTEXTS -- Too verbose || dbg
    
    1848
    +   go dbg n env (MkErrCtxt LandmarkUserSrcCode ctxt : ctxts)
    
    1849
    +     | n < mAX_CONTEXTS -- Too verbose || dbg
    
    1845 1850
          = do { (env', msg) <- liftZonkM $ ctxt env
    
    1846
    -          ; let n' = if is_landmark then n else n+1
    
    1847
    -          ; rest <- go dbg n' env' ctxts
    
    1851
    +          ; rest <- go dbg n env' ctxts
    
    1848 1852
               ; return (msg : rest) }
    
    1849 1853
          | otherwise
    
    1850 1854
          = go dbg n env ctxts
    
    1851
    -   go dbg n env (ExpansionCodeCtxt co : ctxts)
    
    1855
    +   go dbg n env (MkErrCtxt _ ctxt : ctxts)
    
    1852 1856
          | n < mAX_CONTEXTS -- Too verbose || dbg
    
    1853
    -     = do { let msg = srcCodeOriginErrCtxMsg co
    
    1854
    -          ; rest <- go dbg (n+1) env ctxts
    
    1857
    +     = do { (env', msg) <- liftZonkM $ ctxt env
    
    1858
    +          ; rest <- go dbg (n+1) env' ctxts
    
    1855 1859
               ; return (msg : rest) }
    
    1856 1860
          | otherwise
    
    1857 1861
          = go dbg n env ctxts