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

Commits:

10 changed files:

Changes:

  • compiler/GHC/Hs/Instances.hs
    ... ... @@ -640,11 +640,12 @@ deriving instance Eq (IE GhcRn)
    640 640
     deriving instance Eq (IE GhcTc)
    
    641 641
     
    
    642 642
     -- ---------------------------------------------------------------------
    
    643
    -
    
    643
    +-- TODO: I think we still need instances for StmtCtxt, ExprCtxt and PatCtxt ctors of ErrCtxtMsg
    
    644 644
     instance Data ErrCtxtMsg where
    
    645 645
       gunfold _ _ _ = error "no gunfold for ErrCtxtMsg"
    
    646 646
       gfoldl _ _ _ = error "no goldl for ErrCtxtMsg"
    
    647
    -
    
    647
    +  toConstr = error "no toConstr for ErrCtxtMsg"
    
    648
    +  dataTypeOf = error "no dataTypeOf for ErrCtxtMsg"
    
    648 649
     
    
    649 650
     deriving instance Data XXExprGhcRn
    
    650 651
     
    

  • compiler/GHC/Tc/Gen/App.hs
    ... ... @@ -20,6 +20,7 @@ import GHC.Hs
    20 20
     
    
    21 21
     import GHC.Tc.Gen.Head
    
    22 22
     import GHC.Tc.Errors.Types
    
    23
    +import GHC.Tc.Errors.Ppr
    
    23 24
     import GHC.Tc.Utils.Monad
    
    24 25
     import GHC.Tc.Utils.Unify
    
    25 26
     import GHC.Tc.Utils.Instantiate
    
    ... ... @@ -275,7 +276,7 @@ tcApp works like this:
    275 276
     2. Use tcInferAppHead to infer the type of the function,
    
    276 277
          as an (uninstantiated) TcSigmaType
    
    277 278
        There are special cases for
    
    278
    -     HsVar, HsRecSel, and ExprWithTySig
    
    279
    +     HsVar, HsRecSel, and ExprWithTySig and XExpr
    
    279 280
        Otherwise, delegate back to tcExpr, which
    
    280 281
          infers an (instantiated) TcRhoType
    
    281 282
     
    
    ... ... @@ -2050,6 +2051,7 @@ mk_origin fun_lspan_arg rn_fun_arg rn_fun
    2050 2051
       = return $ exprCtOrigin rn_fun_arg
    
    2051 2052
       | otherwise
    
    2052 2053
       = do { code_orig <- getSrcCodeOrigin
    
    2054
    +       ; traceTc "mk_origin" (case (pprErrCtxtMsg <$> code_orig) of { Just e -> e; _ -> text "Nothing"})
    
    2053 2055
            ; return $ srcCodeOriginCtOrigin rn_fun code_orig
    
    2054 2056
            }
    
    2055 2057
     
    

  • compiler/GHC/Tc/Gen/Do.hs
    ... ... @@ -213,7 +213,7 @@ mk_fail_block doFlav pat stmt e (Just (SyntaxExprRn fail_op)) =
    213 213
     
    
    214 214
               fail_op_expr :: DynFlags -> LPat GhcRn -> HsExpr GhcRn -> LHsExpr GhcRn
    
    215 215
               fail_op_expr dflags pat@(L pat_lspan _) fail_op
    
    216
    -            = L pat_lspan $ mkExpandedPatRn doFlav (unLoc pat) stmt $ genHsApp fail_op (mk_fail_msg_expr dflags pat)
    
    216
    +            = L pat_lspan $ mkExpandedPatRn doFlav pat stmt $ genHsApp fail_op (mk_fail_msg_expr dflags pat)
    
    217 217
     
    
    218 218
               mk_fail_msg_expr :: DynFlags -> LPat GhcRn -> LHsExpr GhcRn
    
    219 219
               mk_fail_msg_expr dflags pat
    
    ... ... @@ -481,7 +481,7 @@ It stores the original statement (with location) and the expanded expression
    481 481
     -}
    
    482 482
     
    
    483 483
     
    
    484
    -mkExpandedPatRn :: HsDoFlavour -> Pat GhcRn -> ExprLStmt GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    484
    +mkExpandedPatRn :: HsDoFlavour -> LPat GhcRn -> ExprLStmt GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    485 485
     mkExpandedPatRn flav pat stmt e = XExpr $ ExpandedThingRn
    
    486 486
                                        { xrn_orig = StmtErrCtxtPat (HsDoStmt flav) stmt pat
    
    487 487
                                        , xrn_expanded = e}

  • compiler/GHC/Tc/Gen/Head.hs
    ... ... @@ -430,6 +430,7 @@ tcInferAppHead :: (HsExpr GhcRn, SrcSpan)
    430 430
     --   * A bare identifier (just look it up)
    
    431 431
     --     This case also covers a record selector HsRecSel
    
    432 432
     --   * An expression with a type signature (e :: ty)
    
    433
    +--   * An XExpr where 'f' is actually an expanded out expression
    
    433 434
     -- See Note [Application chains and heads] in GHC.Tc.Gen.App
    
    434 435
     --
    
    435 436
     -- Note that [] and (,,) are both HsVar:
    
    ... ... @@ -452,6 +453,8 @@ tcInferAppHead_maybe :: HsExpr GhcRn
    452 453
                          -> TcM (Maybe (HsExpr GhcTc, DeepSubsumptionFlag, TcSigmaType))
    
    453 454
     -- See Note [Application chains and heads] in GHC.Tc.Gen.App
    
    454 455
     -- Returns Nothing for a complicated head
    
    456
    +-- XExpr's although complicated needs to be looked through, useful for QL things when
    
    457
    +-- the argument is an XExpr
    
    455 458
     tcInferAppHead_maybe fun = case fun of
    
    456 459
           HsVar _ nm                  -> Just <$> with_get_ds (tcInferId nm)
    
    457 460
           ExprWithTySig _ e hs_ty     -> Just <$> with_get_ds (tcExprWithSig e hs_ty)
    

  • compiler/GHC/Tc/Types.hs
    1 1
     {-# LANGUAGE PatternSynonyms            #-}
    
    2
    -
    
    3 2
     {-
    
    4 3
     (c) The University of Glasgow 2006-2012
    
    5 4
     (c) The GRASP Project, Glasgow University, 1992-2002
    

  • compiler/GHC/Tc/Types/ErrCtxt.hs
    ... ... @@ -340,7 +340,7 @@ data ErrCtxtMsg
    340 340
       | DoStmtErrCtxt !HsStmtContextRn !(ExprLStmt GhcRn)
    
    341 341
     
    
    342 342
       -- | In patten of the do statement. (c.f. MonadFailErrors)
    
    343
    -  | StmtErrCtxtPat !HsStmtContextRn !(ExprLStmt GhcRn) (Pat GhcRn)
    
    343
    +  | StmtErrCtxtPat !HsStmtContextRn !(ExprLStmt GhcRn) (LPat GhcRn)
    
    344 344
     
    
    345 345
       -- | In an rebindable syntax expression.
    
    346 346
       | SyntaxNameCtxt !(HsExpr GhcRn) !CtOrigin !TcType !SrcSpan
    

  • compiler/GHC/Tc/Types/LclEnv.hs
    ... ... @@ -211,11 +211,12 @@ setLclEnvSrcCodeOrigin ec = modifyLclCtxt (setLclCtxtSrcCodeOrigin ec)
    211 211
     -- See Note [ErrCtxtStack Manipulation]
    
    212 212
     setLclCtxtSrcCodeOrigin :: ErrCtxt -> TcLclCtxt -> TcLclCtxt
    
    213 213
     setLclCtxtSrcCodeOrigin ec lclCtxt
    
    214
    -  | ecs@(MkErrCtxt ExpansionCodeCtxt _ : _) <- tcl_err_ctxt lclCtxt
    
    215
    -  , MkErrCtxt ExpansionCodeCtxt ExprCtxt{} <- ec
    
    216
    -  = lclCtxt { tcl_err_ctxt =  ec : ecs }
    
    217
    -  | MkErrCtxt ExpansionCodeCtxt _ : ecs <- tcl_err_ctxt lclCtxt
    
    218
    -  , MkErrCtxt ExpansionCodeCtxt _ <- ec
    
    214
    +  -- | ecs@(MkErrCtxt ExpansionCodeCtxt _ : _) <- tcl_err_ctxt lclCtxt
    
    215
    +  -- , MkErrCtxt ExpansionCodeCtxt ExprCtxt{} <- ec
    
    216
    +  -- = lclCtxt { tcl_err_ctxt =  ec : ecs }
    
    217
    +  -- never stack 2 statement error contexts on top of each other
    
    218
    +  | MkErrCtxt _ DoStmtErrCtxt{} : ecs <- tcl_err_ctxt lclCtxt
    
    219
    +  , MkErrCtxt _ DoStmtErrCtxt{} <- ec
    
    219 220
       = lclCtxt { tcl_err_ctxt =  ec : ecs }
    
    220 221
       | otherwise
    
    221 222
       = lclCtxt { tcl_err_ctxt = ec : tcl_err_ctxt lclCtxt }
    

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -455,7 +455,7 @@ data CtOrigin
    455 455
           -- `ty1` to `ty2`.
    
    456 456
     
    
    457 457
       | DefaultOrigin       -- Typechecking a default decl
    
    458
    -  | DoStmtOrigin            -- Arising from a do expression
    
    458
    +  | DoStmtOrigin            -- Arising from a do statement
    
    459 459
       | DoPatOrigin (LPat GhcRn) -- Arising from a failable pattern in
    
    460 460
                                  -- a do expression
    
    461 461
       | MCompOrigin         -- Arising from a monad comprehension
    
    ... ... @@ -690,12 +690,22 @@ exprCtOrigin e@(HsIf {}) = ExpansionOrigin (ExprCtxt e)
    690 690
     exprCtOrigin e@(HsProjection _ _) = ExpansionOrigin (ExprCtxt e)
    
    691 691
     exprCtOrigin e@(RecordUpd{})      = ExpansionOrigin (ExprCtxt e)
    
    692 692
     exprCtOrigin e@(HsGetField{})     = ExpansionOrigin (ExprCtxt e)
    
    693
    -exprCtOrigin (XExpr (ExpandedThingRn o _)) = ExpansionOrigin o
    
    693
    +exprCtOrigin (XExpr (ExpandedThingRn o _)) = errCtxtCtOrigin o
    
    694 694
     exprCtOrigin (XExpr (HsRecSelRn f))  = OccurrenceOfRecSel $ L (getLoc $ foLabel f) (foExt f)
    
    695 695
     
    
    696 696
     srcCodeOriginCtOrigin :: HsExpr GhcRn -> Maybe ErrCtxtMsg -> CtOrigin
    
    697 697
     srcCodeOriginCtOrigin e Nothing = exprCtOrigin e
    
    698
    -srcCodeOriginCtOrigin _ (Just o) = ExpansionOrigin o
    
    698
    +srcCodeOriginCtOrigin _ (Just o) = errCtxtCtOrigin o
    
    699
    +
    
    700
    +
    
    701
    +errCtxtCtOrigin :: ErrCtxtMsg -> CtOrigin
    
    702
    +errCtxtCtOrigin (ExprCtxt e) = exprCtOrigin e
    
    703
    +errCtxtCtOrigin (FunAppCtxt (FunAppCtxtExpr _ e) _) = exprCtOrigin e
    
    704
    +errCtxtCtOrigin (StmtErrCtxt{}) = DoStmtOrigin
    
    705
    +errCtxtCtOrigin (DoStmtErrCtxt{}) = DoStmtOrigin
    
    706
    +errCtxtCtOrigin (StmtErrCtxtPat _ _ p) = DoPatOrigin p
    
    707
    +errCtxtCtOrigin _ = Shouldn'tHappenOrigin "errCtxtCtOrigin"
    
    708
    +
    
    699 709
     
    
    700 710
     -- | Extract a suitable CtOrigin from a MatchGroup
    
    701 711
     matchesCtOrigin :: MatchGroup GhcRn (LHsExpr GhcRn) -> CtOrigin
    
    ... ... @@ -731,6 +741,8 @@ pprCtOrigin (ExpansionOrigin o)
    731 741
           what = case o of
    
    732 742
             StmtErrCtxt{} ->
    
    733 743
               text "a do statement"
    
    744
    +        DoStmtErrCtxt{} ->
    
    745
    +          text "a do statement"
    
    734 746
             StmtErrCtxtPat _ _ p ->
    
    735 747
               text "a do statement" $$
    
    736 748
                  text "with the failable pattern" <+> quotes (ppr p)
    
    ... ... @@ -744,6 +756,7 @@ pprCtOrigin (ExpansionOrigin o)
    744 756
             ExprCtxt (HsProjection _ p) -> text "the record selector" <+>
    
    745 757
                  quotes (ppr ((FieldLabelStrings $ fmap noLocA p)))
    
    746 758
             ExprCtxt e -> text "the expression" <+> (ppr e)
    
    759
    +        RecordUpdCtxt{} -> text "a record update"
    
    747 760
             _ -> text "shouldn't happen ExpansionOrigin pprCtOrigin"
    
    748 761
     
    
    749 762
     pprCtOrigin (GivenSCOrigin sk d blk)
    

  • compiler/GHC/Tc/Utils/Instantiate.hs
    ... ... @@ -52,7 +52,6 @@ import GHC.Core.FamInstEnv
    52 52
     import GHC.Core ( isOrphan ) -- For the Coercion constructor
    
    53 53
     import GHC.Core.Type
    
    54 54
     import GHC.Core.TyCo.Ppr ( debugPprType )
    
    55
    -import GHC.Core.TyCo.Tidy ( tidyType )
    
    56 55
     import GHC.Core.Class( Class )
    
    57 56
     import GHC.Core.Coercion.Axiom
    
    58 57
     
    

  • compiler/GHC/Tc/Utils/Monad.hs
    ... ... @@ -1330,27 +1330,28 @@ addLExprCtxt lspan e thing_inside
    1330 1330
       = setSrcSpan lspan $ add_expr_ctxt e thing_inside
    
    1331 1331
       | otherwise   -- no op in generated code
    
    1332 1332
       = thing_inside
    
    1333
    +    where
    
    1334
    +       add_expr_ctxt :: HsExpr GhcRn -> TcRn a -> TcRn a
    
    1335
    +       add_expr_ctxt e thing_inside
    
    1336
    +         = case e of
    
    1337
    +             -- The HsHole special case addresses situations like
    
    1338
    +             --    f x = _
    
    1339
    +             -- when we don't want to say "In the expression: _",
    
    1340
    +             -- because it is mentioned in the error message itself
    
    1341
    +             HsHole{} -> thing_inside
    
    1342
    +
    
    1343
    +             -- There is a special case for expressions with signatures to avoid having too verbose
    
    1344
    +             -- error context. So here we flip the ErrCtxt state to expanded if the expression is expanded.
    
    1345
    +             -- c.f. RecordDotSyntaxFail9
    
    1346
    +             ExprWithTySig _ (L _ e') _
    
    1347
    +               | XExpr (ExpandedThingRn o _) <- e' -> addExpansionErrCtxt o thing_inside
    
    1348
    +
    
    1349
    +             -- Flip error ctxt into expansion mode
    
    1350
    +             XExpr (ExpandedThingRn o _) -> addExpansionErrCtxt o thing_inside
    
    1351
    +
    
    1352
    +             _ -> addErrCtxt (ExprCtxt e) thing_inside
    
    1353
    +
    
    1333 1354
     
    
    1334
    --- | !Caution!: Users should not call add_expr_ctxt, they ought to use addLExprCtxt
    
    1335
    -add_expr_ctxt :: HsExpr GhcRn -> TcRn a -> TcRn a
    
    1336
    -add_expr_ctxt e thing_inside
    
    1337
    -  = case e of
    
    1338
    -      HsHole{} -> thing_inside
    
    1339
    -   -- The HsHole special case addresses situations like
    
    1340
    -   --    f x = _
    
    1341
    -   -- when we don't want to say "In the expression: _",
    
    1342
    -   -- because it is mentioned in the error message itself
    
    1343
    -
    
    1344
    -      ExprWithTySig _ (L _ e') _
    
    1345
    -        | XExpr (ExpandedThingRn o _) <- e' -> addExpansionErrCtxt o thing_inside
    
    1346
    -   -- There is a special case for expressions with signatures to avoid having too verbose
    
    1347
    -   -- error context. So here we flip the ErrCtxt state to expanded if the expression is expanded.
    
    1348
    -   -- c.f. RecordDotSyntaxFail9
    
    1349
    -
    
    1350
    -      XExpr (ExpandedThingRn o _) -> addExpansionErrCtxt o thing_inside
    
    1351
    -   -- Flip error ctxt into expansion mode
    
    1352
    -
    
    1353
    -      _ -> addErrCtxt (ExprCtxt e) thing_inside
    
    1354 1355
     
    
    1355 1356
     
    
    1356 1357
     getErrCtxt :: TcM [ErrCtxt]