... |
... |
@@ -21,6 +21,7 @@ module GHC.Tc.Types.Origin ( |
21
|
21
|
CtOrigin(..), exprCtOrigin, lexprCtOrigin, matchesCtOrigin, grhssCtOrigin,
|
22
|
22
|
srcCodeOriginCtOrigin,
|
23
|
23
|
isVisibleOrigin, toInvisibleOrigin,
|
|
24
|
+ updatePositionCtOrigin,
|
24
|
25
|
pprCtOrigin, isGivenOrigin, isWantedWantedFunDepOrigin,
|
25
|
26
|
isWantedSuperclassOrigin,
|
26
|
27
|
ClsInstOrQC(..), NakedScFlag(..), NonLinearPatternReason(..),
|
... |
... |
@@ -45,7 +46,7 @@ module GHC.Tc.Types.Origin ( |
45
|
46
|
FRRArrowContext(..), pprFRRArrowContext,
|
46
|
47
|
|
47
|
48
|
-- ** ExpectedFunTy FixedRuntimeRepOrigin
|
48
|
|
- ExpectedFunTyOrigin(..), pprExpectedFunTyOrigin, pprExpectedFunTyHerald,
|
|
49
|
+ pprExpectedFunTyHerald,
|
49
|
50
|
|
50
|
51
|
-- * InstanceWhat
|
51
|
52
|
InstanceWhat(..), SafeOverlapping
|
... |
... |
@@ -653,8 +654,67 @@ data CtOrigin |
653
|
654
|
Type -- the instantiated type of the method
|
654
|
655
|
| AmbiguityCheckOrigin UserTypeCtxt
|
655
|
656
|
| ImplicitLiftOrigin HsImplicitLiftSplice
|
|
657
|
+
|
656
|
658
|
| ExpansionOrigin SrcCodeOrigin -- This is due to an expansion of the original thing given by SrcCodeOrigin
|
657
|
659
|
|
|
660
|
+ -- | A rebindable syntax operator is expected to have a function type.
|
|
661
|
+ --
|
|
662
|
+ -- Test cases for representation-polymorphism checks:
|
|
663
|
+ -- RepPolyDoBind, RepPolyDoBody{1,2}, RepPolyMc{Bind,Body,Guard}, RepPolyNPlusK
|
|
664
|
+ | forall (p :: Pass)
|
|
665
|
+ . (OutputableBndrId p)
|
|
666
|
+ => ExpectedFunTySyntaxOp Int
|
|
667
|
+ !CtOrigin !(HsExpr (GhcPass p))
|
|
668
|
+ -- ^ rebindable syntax operator
|
|
669
|
+
|
|
670
|
+ -- | A view pattern must have a function type.
|
|
671
|
+ --
|
|
672
|
+ -- Test cases for representation-polymorphism checks:
|
|
673
|
+ -- RepPolyBinder
|
|
674
|
+ | ExpectedFunTyViewPat Int
|
|
675
|
+ !(HsExpr GhcRn)
|
|
676
|
+ -- ^ function used in the view pattern
|
|
677
|
+
|
|
678
|
+ -- | Need to be able to extract an argument type from a function type.
|
|
679
|
+ --
|
|
680
|
+ -- Test cases for representation-polymorphism checks:
|
|
681
|
+ -- RepPolyApp
|
|
682
|
+ | forall (p :: Pass)
|
|
683
|
+ . Outputable (HsExpr (GhcPass p)) => ExpectedFunTyArg
|
|
684
|
+ Int
|
|
685
|
+ -- ^ Argument number
|
|
686
|
+ !TypedThing
|
|
687
|
+ -- ^ function
|
|
688
|
+ !(HsExpr (GhcPass p))
|
|
689
|
+ -- ^ argument
|
|
690
|
+
|
|
691
|
+ -- | Ensure that a function defined by equations indeed has a function type
|
|
692
|
+ -- with the appropriate number of arguments.
|
|
693
|
+ --
|
|
694
|
+ -- Test cases for representation-polymorphism checks:
|
|
695
|
+ -- RepPolyBinder, RepPolyRecordPattern, RepPolyWildcardPattern
|
|
696
|
+ | ExpectedFunTyMatches Int
|
|
697
|
+ !TypedThing
|
|
698
|
+ -- ^ name of the function
|
|
699
|
+ !(MatchGroup GhcRn (LHsExpr GhcRn))
|
|
700
|
+ -- ^ equations
|
|
701
|
+
|
|
702
|
+ -- | Ensure that a lambda abstraction has a function type.
|
|
703
|
+ --
|
|
704
|
+ -- Test cases for representation-polymorphism checks:
|
|
705
|
+ -- RepPolyLambda, RepPolyMatch
|
|
706
|
+ | ExpectedFunTyLam HsLamVariant
|
|
707
|
+ !(HsExpr GhcRn)
|
|
708
|
+ -- ^ the entire lambda-case expression
|
|
709
|
+
|
|
710
|
+updatePositionCtOrigin :: Int -> CtOrigin -> CtOrigin
|
|
711
|
+updatePositionCtOrigin i (ExpectedFunTySyntaxOp _ c e) = ExpectedFunTySyntaxOp i c e
|
|
712
|
+updatePositionCtOrigin i (ExpectedFunTyViewPat _ e) = ExpectedFunTyViewPat i e
|
|
713
|
+updatePositionCtOrigin i (ExpectedFunTyArg _ t e) = ExpectedFunTyArg i t e
|
|
714
|
+updatePositionCtOrigin i (ExpectedFunTyMatches _ t e) = ExpectedFunTyMatches i t e
|
|
715
|
+updatePositionCtOrigin _ c = c
|
|
716
|
+
|
|
717
|
+
|
658
|
718
|
data NonLinearPatternReason
|
659
|
719
|
= LazyPatternReason
|
660
|
720
|
| GeneralisedPatternReason
|
... |
... |
@@ -727,7 +787,8 @@ lexprCtOrigin (L _ e) = exprCtOrigin e |
727
|
787
|
|
728
|
788
|
exprCtOrigin :: HsExpr GhcRn -> CtOrigin
|
729
|
789
|
exprCtOrigin (HsVar _ (L _ (WithUserRdr _ name))) = OccurrenceOf name
|
730
|
|
-exprCtOrigin (HsGetField _ _ (L _ f)) = GetFieldOrigin (field_label $ unLoc $ dfoLabel f)
|
|
790
|
+exprCtOrigin e@(HsGetField _ _ (L _ _)) = ExpansionOrigin (OrigExpr e)
|
|
791
|
+ -- GetFieldOrigin (field_label $ unLoc $ dfoLabel f)
|
731
|
792
|
exprCtOrigin (HsOverLabel _ l) = OverLabelOrigin l
|
732
|
793
|
exprCtOrigin (ExplicitList {}) = ListOrigin
|
733
|
794
|
exprCtOrigin (HsIPVar _ ip) = IPOccOrigin ip
|
... |
... |
@@ -739,7 +800,7 @@ exprCtOrigin (HsAppType _ e1 _) = lexprCtOrigin e1 |
739
|
800
|
exprCtOrigin (OpApp _ _ op _) = lexprCtOrigin op
|
740
|
801
|
exprCtOrigin (NegApp _ e _) = lexprCtOrigin e
|
741
|
802
|
exprCtOrigin (HsPar _ e) = lexprCtOrigin e
|
742
|
|
-exprCtOrigin (HsProjection _ p) = GetFieldOrigin ((field_label . unLoc . dfoLabel . NE.head . NE.reverse) p)
|
|
803
|
+exprCtOrigin e@(HsProjection _ _) = ExpansionOrigin (OrigExpr e)
|
743
|
804
|
exprCtOrigin (SectionL _ _ _) = SectionOrigin
|
744
|
805
|
exprCtOrigin (SectionR _ _ _) = SectionOrigin
|
745
|
806
|
exprCtOrigin (ExplicitTuple {}) = Shouldn'tHappenOrigin "explicit tuple"
|
... |
... |
@@ -802,7 +863,7 @@ pprCtOrigin (ExpansionOrigin o) |
802
|
863
|
where what :: SDoc
|
803
|
864
|
what = case o of
|
804
|
865
|
OrigStmt{} -> text "a do statement"
|
805
|
|
- OrigExpr e -> text "an expression" <+> ppr e
|
|
866
|
+ OrigExpr e -> pprCtO (exprCtOrigin e)
|
806
|
867
|
OrigPat p -> text "a pattern" <+> ppr p
|
807
|
868
|
|
808
|
869
|
pprCtOrigin (GivenSCOrigin sk d blk)
|
... |
... |
@@ -917,9 +978,38 @@ pprCtOrigin (NonLinearPatternOrigin reason pat) |
917
|
978
|
= hang (ctoHerald <+> text "a non-linear pattern" <+> quotes (ppr pat))
|
918
|
979
|
2 (pprNonLinearPatternReason reason)
|
919
|
980
|
|
|
981
|
+pprCtOrigin (ExpectedFunTySyntaxOp i orig op) =
|
|
982
|
+ vcat [ sep [ the_arg_of i
|
|
983
|
+ , text "the rebindable syntax operator"
|
|
984
|
+ , quotes (ppr op) ]
|
|
985
|
+ , nest 2 (ppr orig) ]
|
|
986
|
+pprCtOrigin (ExpectedFunTyViewPat i expr) =
|
|
987
|
+ vcat [ the_arg_of i <+> text "the view pattern"
|
|
988
|
+ , nest 2 (ppr expr) ]
|
|
989
|
+pprCtOrigin (ExpectedFunTyArg i fun arg) =
|
|
990
|
+ sep [ text "The" <+> speakNth i <+> text "argument"
|
|
991
|
+ , quotes (ppr arg)
|
|
992
|
+ , text "of"
|
|
993
|
+ , quotes (ppr fun) ]
|
|
994
|
+pprCtOrigin (ExpectedFunTyMatches i fun (MG { mg_alts = L _ alts }))
|
|
995
|
+ | null alts
|
|
996
|
+ = the_arg_of i <+> quotes (ppr fun)
|
|
997
|
+ | otherwise
|
|
998
|
+ = text "The" <+> speakNth i <+> text "pattern in the equation" <> plural alts
|
|
999
|
+ <+> text "for" <+> quotes (ppr fun)
|
|
1000
|
+pprCtOrigin (ExpectedFunTyLam lam_variant _) = binder_of $ lamCaseKeyword lam_variant
|
|
1001
|
+
|
920
|
1002
|
pprCtOrigin simple_origin
|
921
|
1003
|
= ctoHerald <+> pprCtO simple_origin
|
922
|
1004
|
|
|
1005
|
+
|
|
1006
|
+the_arg_of :: Int -> SDoc
|
|
1007
|
+the_arg_of i = text "The" <+> speakNth i <+> text "argument of"
|
|
1008
|
+
|
|
1009
|
+binder_of :: SDoc -> SDoc
|
|
1010
|
+binder_of what = text "The binder of the" <+> what <+> text "expression"
|
|
1011
|
+
|
|
1012
|
+
|
923
|
1013
|
-- | Short one-liners
|
924
|
1014
|
pprCtO :: HasDebugCallStack => CtOrigin -> SDoc
|
925
|
1015
|
pprCtO (OccurrenceOf name) = hsep [text "a use of", quotes (ppr name)]
|
... |
... |
@@ -945,7 +1035,7 @@ pprCtO (ScOrigin (IsQC {}) _) = text "the head of a quantified constraint" |
945
|
1035
|
pprCtO DerivClauseOrigin = text "the 'deriving' clause of a data type declaration"
|
946
|
1036
|
pprCtO StandAloneDerivOrigin = text "a 'deriving' declaration"
|
947
|
1037
|
pprCtO DefaultOrigin = text "a 'default' declaration"
|
948
|
|
-pprCtO DoStmtOrigin = text "a do statement"
|
|
1038
|
+pprCtO DoStmtOrigin = text "a do statement"
|
949
|
1039
|
pprCtO MCompOrigin = text "a statement in a monad comprehension"
|
950
|
1040
|
pprCtO ProcOrigin = text "a proc expression"
|
951
|
1041
|
pprCtO ArrowCmdOrigin = text "an arrow command"
|
... |
... |
@@ -990,7 +1080,13 @@ pprCtO (ImpedanceMatching {}) = text "combining required constraints" |
990
|
1080
|
pprCtO (NonLinearPatternOrigin _ pat) = hsep [text "a non-linear pattern" <+> quotes (ppr pat)]
|
991
|
1081
|
pprCtO (ExpansionOrigin (OrigPat p)) = hsep [text "a pattern" <+> quotes (ppr p)]
|
992
|
1082
|
pprCtO (ExpansionOrigin (OrigStmt{})) = text "a do statement"
|
993
|
|
-pprCtO (ExpansionOrigin (OrigExpr{})) = text "an expression"
|
|
1083
|
+pprCtO (ExpansionOrigin (OrigExpr (HsGetField _ _ (L _ f)))) = hsep [text "selecting the field", quotes (ppr f)]
|
|
1084
|
+pprCtO (ExpansionOrigin (OrigExpr e)) = text "an expression" <+> ppr e
|
|
1085
|
+pprCtO (ExpectedFunTySyntaxOp{}) = text "a rebindable syntax operator"
|
|
1086
|
+pprCtO (ExpectedFunTyViewPat{}) = text "a view pattern"
|
|
1087
|
+pprCtO (ExpectedFunTyArg{}) = text "a funtion head"
|
|
1088
|
+pprCtO (ExpectedFunTyMatches{}) = text "a match statement"
|
|
1089
|
+pprCtO (ExpectedFunTyLam{}) = text "a lambda expression"
|
994
|
1090
|
|
995
|
1091
|
|
996
|
1092
|
pprNonLinearPatternReason :: HasDebugCallStack => NonLinearPatternReason -> SDoc
|
... |
... |
@@ -1204,7 +1300,7 @@ data FixedRuntimeRepContext |
1204
|
1300
|
--
|
1205
|
1301
|
-- See 'ExpectedFunTyOrigin' for more details.
|
1206
|
1302
|
| FRRExpectedFunTy
|
1207
|
|
- !ExpectedFunTyOrigin
|
|
1303
|
+ !CtOrigin -- !ExpectedFunTyOrigin
|
1208
|
1304
|
!Int
|
1209
|
1305
|
-- ^ argument position (1-indexed)
|
1210
|
1306
|
|
... |
... |
@@ -1285,8 +1381,8 @@ pprFixedRuntimeRepContext FRRBindStmtGuard |
1285
|
1381
|
= sep [ text "The body of the bind statement" ]
|
1286
|
1382
|
pprFixedRuntimeRepContext (FRRArrow arrowContext)
|
1287
|
1383
|
= pprFRRArrowContext arrowContext
|
1288
|
|
-pprFixedRuntimeRepContext (FRRExpectedFunTy funTyOrig arg_pos)
|
1289
|
|
- = pprExpectedFunTyOrigin funTyOrig arg_pos
|
|
1384
|
+pprFixedRuntimeRepContext (FRRExpectedFunTy funTyOrig _)
|
|
1385
|
+ = pprCtOrigin funTyOrig
|
1290
|
1386
|
|
1291
|
1387
|
instance Outputable FixedRuntimeRepContext where
|
1292
|
1388
|
ppr = pprFixedRuntimeRepContext
|
... |
... |
@@ -1439,102 +1535,56 @@ instance Outputable FRRArrowContext where |
1439
|
1535
|
-- Uses 'pprExpectedFunTyOrigin'.
|
1440
|
1536
|
-- See 'FixedRuntimeRepContext' for the situations in which
|
1441
|
1537
|
-- representation-polymorphism checks are performed.
|
1442
|
|
-data ExpectedFunTyOrigin
|
1443
|
|
-
|
1444
|
|
- -- | A rebindable syntax operator is expected to have a function type.
|
1445
|
|
- --
|
1446
|
|
- -- Test cases for representation-polymorphism checks:
|
1447
|
|
- -- RepPolyDoBind, RepPolyDoBody{1,2}, RepPolyMc{Bind,Body,Guard}, RepPolyNPlusK
|
1448
|
|
- = forall (p :: Pass)
|
1449
|
|
- . (OutputableBndrId p)
|
1450
|
|
- => ExpectedFunTySyntaxOp !CtOrigin !(HsExpr (GhcPass p))
|
1451
|
|
- -- ^ rebindable syntax operator
|
1452
|
|
-
|
1453
|
|
- -- | A view pattern must have a function type.
|
1454
|
|
- --
|
1455
|
|
- -- Test cases for representation-polymorphism checks:
|
1456
|
|
- -- RepPolyBinder
|
1457
|
|
- | ExpectedFunTyViewPat
|
1458
|
|
- !(HsExpr GhcRn)
|
1459
|
|
- -- ^ function used in the view pattern
|
1460
|
|
-
|
1461
|
|
- -- | Need to be able to extract an argument type from a function type.
|
1462
|
|
- --
|
1463
|
|
- -- Test cases for representation-polymorphism checks:
|
1464
|
|
- -- RepPolyApp
|
1465
|
|
- | forall (p :: Pass)
|
1466
|
|
- . Outputable (HsExpr (GhcPass p)) => ExpectedFunTyArg
|
1467
|
|
- !TypedThing
|
1468
|
|
- -- ^ function
|
1469
|
|
- !(HsExpr (GhcPass p))
|
1470
|
|
- -- ^ argument
|
1471
|
|
-
|
1472
|
|
- -- | Ensure that a function defined by equations indeed has a function type
|
1473
|
|
- -- with the appropriate number of arguments.
|
1474
|
|
- --
|
1475
|
|
- -- Test cases for representation-polymorphism checks:
|
1476
|
|
- -- RepPolyBinder, RepPolyRecordPattern, RepPolyWildcardPattern
|
1477
|
|
- | ExpectedFunTyMatches
|
1478
|
|
- !TypedThing
|
1479
|
|
- -- ^ name of the function
|
1480
|
|
- !(MatchGroup GhcRn (LHsExpr GhcRn))
|
1481
|
|
- -- ^ equations
|
1482
|
|
-
|
1483
|
|
- -- | Ensure that a lambda abstraction has a function type.
|
1484
|
|
- --
|
1485
|
|
- -- Test cases for representation-polymorphism checks:
|
1486
|
|
- -- RepPolyLambda, RepPolyMatch
|
1487
|
|
- | ExpectedFunTyLam HsLamVariant
|
1488
|
|
- !(HsExpr GhcRn)
|
1489
|
|
- -- ^ the entire lambda-case expression
|
1490
|
|
-
|
1491
|
|
-pprExpectedFunTyOrigin :: ExpectedFunTyOrigin
|
1492
|
|
- -> Int -- ^ argument position (starting at 1)
|
1493
|
|
- -> SDoc
|
1494
|
|
-pprExpectedFunTyOrigin funTy_origin i =
|
1495
|
|
- case funTy_origin of
|
1496
|
|
- ExpectedFunTySyntaxOp orig op ->
|
1497
|
|
- vcat [ sep [ the_arg_of
|
1498
|
|
- , text "the rebindable syntax operator"
|
1499
|
|
- , quotes (ppr op) ]
|
1500
|
|
- , nest 2 (ppr orig) ]
|
1501
|
|
- ExpectedFunTyViewPat expr ->
|
1502
|
|
- vcat [ the_arg_of <+> text "the view pattern"
|
1503
|
|
- , nest 2 (ppr expr) ]
|
1504
|
|
- ExpectedFunTyArg fun arg ->
|
1505
|
|
- sep [ text "The argument"
|
1506
|
|
- , quotes (ppr arg)
|
1507
|
|
- , text "of"
|
1508
|
|
- , quotes (ppr fun) ]
|
1509
|
|
- ExpectedFunTyMatches fun (MG { mg_alts = L _ alts })
|
1510
|
|
- | null alts
|
1511
|
|
- -> the_arg_of <+> quotes (ppr fun)
|
1512
|
|
- | otherwise
|
1513
|
|
- -> text "The" <+> speakNth i <+> text "pattern in the equation" <> plural alts
|
1514
|
|
- <+> text "for" <+> quotes (ppr fun)
|
1515
|
|
- ExpectedFunTyLam lam_variant _ -> binder_of $ lamCaseKeyword lam_variant
|
1516
|
|
- where
|
1517
|
|
- the_arg_of :: SDoc
|
1518
|
|
- the_arg_of = text "The" <+> speakNth i <+> text "argument of"
|
1519
|
1538
|
|
1520
|
|
- binder_of :: SDoc -> SDoc
|
1521
|
|
- binder_of what = text "The binder of the" <+> what <+> text "expression"
|
1522
|
1539
|
|
1523
|
|
-pprExpectedFunTyHerald :: ExpectedFunTyOrigin -> SDoc
|
|
1540
|
+-- pprExpectedFunTyOrigin :: -- ExpectedFunTyOrigin
|
|
1541
|
+-- -- -> Int -- ^ argument position (starting at 1)
|
|
1542
|
+-- -> SDoc
|
|
1543
|
+-- pprExpectedFunTyOrigin funTy_origin =
|
|
1544
|
+-- case funTy_origin of
|
|
1545
|
+-- ExpectedFunTySyntaxOp i orig op ->
|
|
1546
|
+-- vcat [ sep [ the_arg_of
|
|
1547
|
+-- , text "the rebindable syntax operator"
|
|
1548
|
+-- , quotes (ppr op) ]
|
|
1549
|
+-- , nest 2 (ppr orig) ]
|
|
1550
|
+-- ExpectedFunTyViewPat i expr ->
|
|
1551
|
+-- vcat [ the_arg_of <+> text "the view pattern"
|
|
1552
|
+-- , nest 2 (ppr expr) ]
|
|
1553
|
+-- ExpectedFunTyArg fun arg ->
|
|
1554
|
+-- sep [ text "The argument"
|
|
1555
|
+-- , quotes (ppr arg)
|
|
1556
|
+-- , text "of"
|
|
1557
|
+-- , quotes (ppr fun) ]
|
|
1558
|
+-- ExpectedFunTyMatches i fun (MG { mg_alts = L _ alts })
|
|
1559
|
+-- | null alts
|
|
1560
|
+-- -> the_arg_of <+> quotes (ppr fun)
|
|
1561
|
+-- | otherwise
|
|
1562
|
+-- -> text "The" <+> speakNth i <+> text "pattern in the equation" <> plural alts
|
|
1563
|
+-- <+> text "for" <+> quotes (ppr fun)
|
|
1564
|
+-- ExpectedFunTyLam lam_variant _ -> binder_of $ lamCaseKeyword lam_variant
|
|
1565
|
+-- where
|
|
1566
|
+-- the_arg_of :: Int -> SDoc
|
|
1567
|
+-- the_arg_of i = text "The" <+> speakNth i <+> text "argument of"
|
|
1568
|
+
|
|
1569
|
+-- binder_of :: SDoc -> SDoc
|
|
1570
|
+-- binder_of what = text "The binder of the" <+> what <+> text "expression"
|
|
1571
|
+
|
|
1572
|
+pprExpectedFunTyHerald :: CtOrigin -> SDoc
|
1524
|
1573
|
pprExpectedFunTyHerald (ExpectedFunTySyntaxOp {})
|
1525
|
1574
|
= text "This rebindable syntax expects a function with"
|
1526
|
1575
|
pprExpectedFunTyHerald (ExpectedFunTyViewPat {})
|
1527
|
1576
|
= text "A view pattern expression expects"
|
1528
|
|
-pprExpectedFunTyHerald (ExpectedFunTyArg fun _)
|
|
1577
|
+pprExpectedFunTyHerald (ExpectedFunTyArg _ fun _)
|
1529
|
1578
|
= sep [ text "The function" <+> quotes (ppr fun)
|
1530
|
1579
|
, text "is applied to" ]
|
1531
|
|
-pprExpectedFunTyHerald (ExpectedFunTyMatches fun (MG { mg_alts = L _ alts }))
|
|
1580
|
+pprExpectedFunTyHerald (ExpectedFunTyMatches _ fun (MG { mg_alts = L _ alts }))
|
1532
|
1581
|
= text "The equation" <> plural alts <+> text "for" <+> quotes (ppr fun) <+> hasOrHave alts
|
1533
|
1582
|
pprExpectedFunTyHerald (ExpectedFunTyLam lam_variant expr)
|
1534
|
1583
|
= sep [ text "The" <+> lamCaseKeyword lam_variant <+> text "expression"
|
1535
|
1584
|
<+> quotes (pprSetDepth (PartWay 1) (ppr expr))
|
1536
|
1585
|
-- The pprSetDepth makes the lambda abstraction print briefly
|
1537
|
1586
|
, text "has" ]
|
|
1587
|
+pprExpectedFunTyHerald orig = ppr (Shouldn'tHappenOrigin "pprExpectedFunTyHerald") <+> ppr orig
|
1538
|
1588
|
|
1539
|
1589
|
{- *******************************************************************
|
1540
|
1590
|
* *
|