| ... |
... |
@@ -798,8 +798,8 @@ Example: Typechecking the do expression. The typechecker looks (somewhat) like t |
|
798
|
798
|
|
|
799
|
799
|
The `expandDoStmts` replaces the HsDo { x <- e1; return x }
|
|
800
|
800
|
with something like
|
|
801
|
|
- HSE { hs_ctxt = e
|
|
802
|
|
- , expanded_expr = e1 >>= \ x -> x }
|
|
|
801
|
+ HSE { hse_ctxt = ExprCtxt e
|
|
|
802
|
+ , hse_exp = e1 >>= \ x -> x }
|
|
803
|
803
|
and we then typecheck the expression `e1 >>= \ x -> x`
|
|
804
|
804
|
|
|
805
|
805
|
See also Note [Handling overloaded and rebindable constructs]
|
| ... |
... |
@@ -812,8 +812,8 @@ The rest of this Note explains how that is done. |
|
812
|
812
|
* The expansion process typically takes a user written thing
|
|
813
|
813
|
L lspan ue
|
|
814
|
814
|
and returns
|
|
815
|
|
- L lspan (XExpr (ExpandedThingRn (HSE { hs_ctxt = ue
|
|
816
|
|
- , expanded_expr = ee } ))
|
|
|
815
|
+ L lspan (XExpr (ExpandedThingRn (HSE { hse_ctxt = ue
|
|
|
816
|
+ , hse_exp = ee } ))
|
|
817
|
817
|
where `ee` is the expansion of the user written thing `ue`
|
|
818
|
818
|
|
|
819
|
819
|
* The type checker context has 3 key fields that describe the context:
|
| ... |
... |
@@ -830,36 +830,32 @@ The rest of this Note explains how that is done. |
|
830
|
830
|
The `tcl_in_gen_code` is a boolean that keeps track of whether
|
|
831
|
831
|
the current expression being typechecked is compiler generated
|
|
832
|
832
|
or user generated.
|
|
833
|
|
- INVARIANT: `tcl_in_gen_code` is modified only in `setSrcSpan`.
|
|
834
|
833
|
|
|
|
834
|
+ INVARIANT: `tcl_loc` and `tcl_in_gen_code` are modified only in `setSrcSpan`.
|
|
835
|
835
|
|
|
836
|
836
|
* Now, when
|
|
837
|
837
|
tcMonoLExpr :: LHsExpr GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
838
|
838
|
gets a located expression, it does 3 things:
|
|
839
|
839
|
(a) Calls `setSrcSpanA` to set the ambient source-code location
|
|
840
|
|
- (b) Calls `addExprCtxt` to add a suitable `HsCtxt` on top of the `tcl_err_ctxt`.
|
|
|
840
|
+ (b) Calls `addExprCtxt` to push a suitable `HsCtxt` on top of the `tcl_err_ctxt`.
|
|
841
|
841
|
(c) Calls `tcExpr` to typecheck the expression.
|
|
842
|
842
|
|
|
843
|
843
|
* In these calls, if the `span` is generated (see `isGeneratedSrcSpan`), then
|
|
844
|
|
- - `setSrcSpanA` sets `tcl_in_gen_code` to `True`
|
|
845
|
|
- - `addErrCtxt` is a no-op if `tcl_in_gen_code` is True
|
|
846
|
|
- This is how we avoid populating the TcLclCtxt with generated code.
|
|
847
|
|
-
|
|
848
|
|
-* The type checker error-stack element `GHC.Tc.Types.ErrCtxt.HsCtxt`
|
|
849
|
|
- just stores an error message
|
|
850
|
|
-
|
|
851
|
|
- type ErrCtxtStack = [HsCtxt]
|
|
852
|
|
-
|
|
853
|
|
- When called on an `XExpr`, `addLExprCtxt`, adds the user written thing
|
|
854
|
|
- `ue`, and the error message provided by the caller on the `ErrCtxtStack` See
|
|
855
|
|
- Note [ErrCtxtStack Manipulation] for more details.
|
|
856
|
|
-
|
|
|
844
|
+ - `setSrcSpanA` sets `tcl_in_gen_code` to `True`, and leaves `tcl_loc` unchanged
|
|
|
845
|
+ - `addExprCtxt` is a no-op if `tcl_in_gen_code` is True
|
|
|
846
|
+ The result is that `tcl_loc` has the span from the innermost /user/ tree node;
|
|
|
847
|
+ and the ErrCtxtStack in `tcl_err_ctxt` only has contexts arisign from user code.
|
|
|
848
|
+
|
|
|
849
|
+* Note that inside an expansion we have sub-expressions from the original program.
|
|
|
850
|
+ As soon as we enter one of those, identified by a /user/ span, `setSrcSpanA` will
|
|
|
851
|
+ sets the `tcl_loc` to reflect that span, and switch off `tcl_in_gen_code`. Nice!
|
|
857
|
852
|
-}
|
|
858
|
853
|
|
|
859
|
854
|
tcHsExpansion :: HsExpansion GhcRn -> ExpRhoType -> TcM (HsExpr GhcTc)
|
|
860
|
|
-tcHsExpansion (HSE o e) res_ty
|
|
861
|
|
- = do e' <- tcMonoLExpr e res_ty
|
|
862
|
|
- return $ XExpr (ExpandedThingTc (HSE o e'))
|
|
|
855
|
+tcHsExpansion (HSE { hse_ctxt = o, hse_exp = e }) res_ty
|
|
|
856
|
+ = do { e' <- tcMonoLExpr e res_ty
|
|
|
857
|
+ ; return $ XExpr $ ExpandedThingTc $
|
|
|
858
|
+ HSE { hse_ctxt = o, hse_exp = e' } }
|
|
863
|
859
|
|
|
864
|
860
|
|
|
865
|
861
|
{-
|