Apoorv Ingle pushed to branch wip/spj-apporv-Oct24 at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Hs/Expr.hs
    ... ... @@ -675,7 +675,7 @@ type instance XXExpr GhcTc = XXExprGhcTc
    675 675
     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
    -  | OrigPat  (Pat GhcRn)                   -- ^ Used for failable patterns that trigger MonadFail constraints
    
    678
    +  | OrigPat  (ExprLStmt GhcRn) (Pat GhcRn) -- ^ Used for failable patterns that trigger MonadFail constraints
    
    679 679
     
    
    680 680
     data XXExprGhcRn
    
    681 681
       = ExpandedThingRn { xrn_orig     :: SrcCodeOrigin   -- The original source thing to be used for error messages
    
    ... ... @@ -1076,7 +1076,7 @@ instance Outputable SrcCodeOrigin where
    1076 1076
         = case thing of
    
    1077 1077
             OrigExpr x    -> ppr_builder "<OrigExpr>:" x
    
    1078 1078
             OrigStmt x _  -> ppr_builder "<OrigStmt>:" x
    
    1079
    -        OrigPat  x    -> ppr_builder "<OrigPat>:" x
    
    1079
    +        OrigPat  _ x  -> ppr_builder "<OrigPat>:" x
    
    1080 1080
         where ppr_builder prefix x = ifPprDebug (braces (text prefix <+> parens (ppr x))) (ppr x)
    
    1081 1081
     
    
    1082 1082
     instance Outputable XXExprGhcRn where
    

  • compiler/GHC/Tc/Gen/Do.hs
    ... ... @@ -110,7 +110,7 @@ expand_do_stmts doFlavour (stmt@(L loc (BindStmt xbsrn pat e)): lstmts)
    110 110
     --    -------------------------------------------------------
    
    111 111
     --       pat <- e ; stmts   ~~> (>>=) e f
    
    112 112
       = do expand_stmts_expr <- expand_do_stmts doFlavour lstmts
    
    113
    -       failable_expr <- mk_failable_expr doFlavour pat expand_stmts_expr fail_op
    
    113
    +       failable_expr <- mk_failable_expr doFlavour pat stmt expand_stmts_expr fail_op
    
    114 114
            let expansion = genHsExpApps bind_op  -- (>>=)
    
    115 115
                            [ e
    
    116 116
                            , failable_expr ]
    
    ... ... @@ -181,8 +181,9 @@ expand_do_stmts doFlavour
    181 181
     expand_do_stmts _ stmts = pprPanic "expand_do_stmts: impossible happened" $ (ppr stmts)
    
    182 182
     
    
    183 183
     -- checks the pattern `pat` for irrefutability which decides if we need to wrap it with a fail block
    
    184
    -mk_failable_expr :: HsDoFlavour -> LPat GhcRn -> LHsExpr GhcRn -> FailOperator GhcRn -> TcM (LHsExpr GhcRn)
    
    185
    -mk_failable_expr doFlav lpat expr@(L _exprloc _) fail_op =
    
    184
    +mk_failable_expr :: HsDoFlavour -> LPat GhcRn -> ExprLStmt GhcRn -> LHsExpr GhcRn
    
    185
    +                 -> FailOperator GhcRn -> TcM (LHsExpr GhcRn)
    
    186
    +mk_failable_expr doFlav lpat stmt expr fail_op =
    
    186 187
       do { is_strict <- xoptM LangExt.Strict
    
    187 188
          ; hscEnv <- getTopEnv
    
    188 189
          ; rdrEnv <- getGlobalRdrEnv
    
    ... ... @@ -194,15 +195,16 @@ mk_failable_expr doFlav lpat expr@(L _exprloc _) fail_op =
    194 195
          ; if irrf_pat -- don't wrap with fail block if
    
    195 196
                        -- the pattern is irrefutable
    
    196 197
            then return $ genHsLamDoExp doFlav [lpat] expr
    
    197
    -       else wrapGenSpan <$> mk_fail_block doFlav lpat expr fail_op
    
    198
    +       else wrapGenSpan <$> mk_fail_block doFlav lpat stmt expr fail_op
    
    198 199
          }
    
    199 200
     
    
    200 201
     -- | Makes the fail block with a given fail_op
    
    201 202
     -- mk_fail_block pat rhs fail builds
    
    202 203
     -- \x. case x of {pat -> rhs; _ -> fail "Pattern match failure..."}
    
    203 204
     mk_fail_block :: HsDoFlavour
    
    204
    -              -> LPat GhcRn -> LHsExpr GhcRn -> FailOperator GhcRn -> TcM (HsExpr GhcRn)
    
    205
    -mk_fail_block doFlav pat e (Just (SyntaxExprRn fail_op)) =
    
    205
    +              -> LPat GhcRn -> ExprLStmt GhcRn
    
    206
    +              -> LHsExpr GhcRn -> FailOperator GhcRn -> TcM (HsExpr GhcRn)
    
    207
    +mk_fail_block doFlav pat stmt e (Just (SyntaxExprRn fail_op)) =
    
    206 208
       do  dflags <- getDynFlags
    
    207 209
           return $ HsLam noAnn LamCases $ mkMatchGroup (doExpansionOrigin doFlav) -- \
    
    208 210
                     (wrapGenSpan [ genHsCaseAltDoExp doFlav pat e                 --  pat -> expr
    
    ... ... @@ -210,12 +212,12 @@ mk_fail_block doFlav pat e (Just (SyntaxExprRn fail_op)) =
    210 212
                                  ])
    
    211 213
             where
    
    212 214
               fail_alt_case :: DynFlags -> LPat GhcRn -> HsExpr GhcRn -> LMatch GhcRn (LHsExpr GhcRn)
    
    213
    -          fail_alt_case dflags pat fail_op = genHsCaseAltDoExp doFlav genWildPat $
    
    214
    -                                             wrapGenSpan (fail_op_expr dflags pat fail_op)
    
    215
    +          fail_alt_case dflags pat@(L pat_span _) fail_op = genHsCaseAltDoExp doFlav genWildPat $
    
    216
    +                                                               fail_op_expr dflags pat fail_op
    
    215 217
     
    
    216
    -          fail_op_expr :: DynFlags -> LPat GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    217
    -          fail_op_expr dflags pat fail_op
    
    218
    -            = mkExpandedPatRn (unLoc pat) $ genHsApp fail_op (mk_fail_msg_expr dflags pat)
    
    218
    +          fail_op_expr :: DynFlags -> LPat GhcRn -> HsExpr GhcRn -> LHsExpr GhcRn
    
    219
    +          fail_op_expr dflags pat@(L pat_lspan _) fail_op
    
    220
    +            = L pat_lspan $ mkExpandedPatRn (unLoc pat) stmt $ genHsApp fail_op (mk_fail_msg_expr dflags pat)
    
    219 221
     
    
    220 222
               mk_fail_msg_expr :: DynFlags -> LPat GhcRn -> LHsExpr GhcRn
    
    221 223
               mk_fail_msg_expr dflags pat
    
    ... ... @@ -223,8 +225,7 @@ mk_fail_block doFlav pat e (Just (SyntaxExprRn fail_op)) =
    223 225
                   text "Pattern match failure in" <+> pprHsDoFlavour (DoExpr Nothing)
    
    224 226
                        <+> text "at" <+> ppr (getLocA pat)
    
    225 227
     
    
    226
    -
    
    227
    -mk_fail_block _ _ _ _ = pprPanic "mk_fail_block: impossible happened" empty
    
    228
    +mk_fail_block _ _ _ _ _ = pprPanic "mk_fail_block: impossible happened" empty
    
    228 229
     
    
    229 230
     
    
    230 231
     {- Note [Expanding HsDo with XXExprGhcRn]
    
    ... ... @@ -484,5 +485,5 @@ It stores the original statement (with location) and the expanded expression
    484 485
     -}
    
    485 486
     
    
    486 487
     
    
    487
    -mkExpandedPatRn :: Pat GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    488
    -mkExpandedPatRn pat e = XExpr (ExpandedThingRn (OrigPat pat) e)
    488
    +mkExpandedPatRn :: Pat GhcRn -> ExprLStmt GhcRn -> HsExpr GhcRn -> HsExpr GhcRn
    
    489
    +mkExpandedPatRn pat stmt e = XExpr (ExpandedThingRn (OrigPat stmt pat) e)

  • compiler/GHC/Tc/Types/ErrCtxt.hs
    ... ... @@ -233,4 +233,4 @@ data ErrCtxtMsg
    233 233
     srcCodeOriginErrCtxMsg :: SrcCodeOrigin -> ErrCtxtMsg
    
    234 234
     srcCodeOriginErrCtxMsg (OrigExpr e) = ExprCtxt e
    
    235 235
     srcCodeOriginErrCtxMsg (OrigStmt s f) = StmtErrCtxt (HsDoStmt f) (unLoc s)
    
    236
    -srcCodeOriginErrCtxMsg (OrigPat  p) = PatCtxt p
    236
    +srcCodeOriginErrCtxMsg (OrigPat s _) = StmtErrCtxt (HsDoStmt (DoExpr Nothing)) (unLoc s)

  • compiler/GHC/Tc/Types/Origin.hs
    ... ... @@ -879,7 +879,7 @@ pprCtOrigin (ExpansionOrigin o)
    879 879
           what = case o of
    
    880 880
             OrigStmt{} ->
    
    881 881
               text "a do statement"
    
    882
    -        OrigPat p ->
    
    882
    +        OrigPat _ p ->
    
    883 883
               text "a do statement" $$
    
    884 884
                  text "with the failable pattern" <+> quotes (ppr p)
    
    885 885
             OrigExpr (HsGetField _ _ (L _ f)) ->