Hannes Siebenhandl pushed to branch wip/fendor/has-field-hie at Glasgow Haskell Compiler / GHC
Commits:
8 changed files:
- compiler/GHC/Iface/Ext/Ast.hs
- compiler/GHC/Iface/Ext/Types.hs
- compiler/GHC/Iface/Ext/Utils.hs
- testsuite/tests/hiefile/should_run/HasFieldQueries.stdout
- + testsuite/tests/hiefile/should_run/HieTypeable.hs
- + testsuite/tests/hiefile/should_run/HieTypeable.stdout
- testsuite/tests/hiefile/should_run/NoFieldSelectorQueries.stdout
- testsuite/tests/hiefile/should_run/all.T
Changes:
| ... | ... | @@ -32,7 +32,8 @@ import GHC.Types.Basic |
| 32 | 32 | import GHC.Types.UnresolvedImport ( isGeneratedImport )
|
| 33 | 33 | import GHC.Types.FieldLabel
|
| 34 | 34 | import GHC.Types.Avail ( Avails )
|
| 35 | -import GHC.Types.Id ( isDataConId_maybe, isId )
|
|
| 35 | +import GHC.Types.Id ( isDataConId_maybe, isId, idDetails )
|
|
| 36 | +import GHC.Types.Id.Info ( IdDetails(..), recSelParentName )
|
|
| 36 | 37 | import GHC.Types.Name ( Name, nameSrcSpan, nameUnique, wiredInNameTyThing_maybe, getName )
|
| 37 | 38 | import GHC.Types.Name.Env ( NameEnv, emptyNameEnv, extendNameEnv, lookupNameEnv )
|
| 38 | 39 | import GHC.Types.Name.Reader ( RecFieldInfo(..), WithUserRdr(..) )
|
| ... | ... | @@ -689,7 +690,7 @@ instance ToHie (EvBindContext (LocatedA TcEvBinds)) where |
| 689 | 690 | where
|
| 690 | 691 | go evbind = do
|
| 691 | 692 | let evDeps = hieEvIdsOfTerm $ eb_rhs evbind
|
| 692 | - depNames = EvBindDeps $ map varName evDeps
|
|
| 693 | + depNames = EvBindDeps $ map classifyEvBindDep evDeps
|
|
| 693 | 694 | concatM $
|
| 694 | 695 | [ toHie (C (EvidenceVarBind (EvLetBind depNames) (combineScopes sc (mkScope span)) sp)
|
| 695 | 696 | (L span $ eb_lhs evbind))
|
| ... | ... | @@ -697,6 +698,12 @@ instance ToHie (EvBindContext (LocatedA TcEvBinds)) where |
| 697 | 698 | ]
|
| 698 | 699 | toHie _ = pure []
|
| 699 | 700 | |
| 701 | +classifyEvBindDep :: EvId -> EvBindDep
|
|
| 702 | +classifyEvBindDep evId =
|
|
| 703 | + case idDetails evId of
|
|
| 704 | + RecSelId{sel_tycon} -> RecordField (varName evId) (recSelParentName sel_tycon)
|
|
| 705 | + _ -> EvidenceVar (varName evId)
|
|
| 706 | + |
|
| 700 | 707 | instance ToHie (LocatedA HsWrapper) where
|
| 701 | 708 | toHie (L osp wrap)
|
| 702 | 709 | = case wrap of
|
| ... | ... | @@ -598,17 +598,79 @@ instance Outputable EvVarSource where |
| 598 | 598 | ppr (EvInstBind True cls) = text "bound due to a superclass of " <+> ppr cls
|
| 599 | 599 | ppr (EvLetBind deps) = text "bound by a let, depending on:" <+> ppr deps
|
| 600 | 600 | |
| 601 | +-- | Evidence binding dependency with additional details.
|
|
| 602 | +data EvBindDep
|
|
| 603 | + = EvidenceVar Name
|
|
| 604 | + -- ^ Evidence variable such as a type class constructor.
|
|
| 605 | + --
|
|
| 606 | + -- For example, @C:HasField@, @$dShow@ (dictionaries) or @$fCList@.
|
|
| 607 | + --
|
|
| 608 | + -- This is the default representation of a 'EvBindDep'.
|
|
| 609 | + | RecordField
|
|
| 610 | + -- ^ Evidence variable of a record selector.
|
|
| 611 | + --
|
|
| 612 | + -- This is for example generated with @-XOverloadedRecordDot@, where
|
|
| 613 | + -- the evidence includes a reference to which record selector is used
|
|
| 614 | + -- to implement @getField@.
|
|
| 615 | + Name
|
|
| 616 | + -- ^ 'Name' of the record selector
|
|
| 617 | + Name
|
|
| 618 | + -- ^ 'Name' of the parent type.
|
|
| 619 | + | TypeableEvidence
|
|
| 620 | + -- ^ Evidence of a @Typeable@ constraint.
|
|
| 621 | + Name
|
|
| 622 | + -- ^ Name of the type the evidence was generated for.
|
|
| 623 | + deriving (Eq, Ord)
|
|
| 624 | + |
|
| 625 | +instance Outputable EvBindDep where
|
|
| 626 | + ppr = \ case
|
|
| 627 | + EvidenceVar n -> ppr n
|
|
| 628 | + RecordField sel parent -> ppr sel <+> text "of Record" <+> ppr parent
|
|
| 629 | + TypeableEvidence n -> ppr n <+> text "bound by Typeable"
|
|
| 630 | + |
|
| 631 | +evBindDepName :: EvBindDep -> Name
|
|
| 632 | +evBindDepName = \ case
|
|
| 633 | + EvidenceVar n -> n
|
|
| 634 | + RecordField selector _record -> selector
|
|
| 635 | + TypeableEvidence n -> n
|
|
| 636 | + |
|
| 637 | +evBindDepHieName :: EvBindDep -> HieName
|
|
| 638 | +evBindDepHieName = toHieName . evBindDepName
|
|
| 639 | + |
|
| 640 | +instance Binary EvBindDep where
|
|
| 641 | + put_ bh = \ case
|
|
| 642 | + EvidenceVar n -> do
|
|
| 643 | + putByte bh 0
|
|
| 644 | + put_ bh n
|
|
| 645 | + RecordField n sel -> do
|
|
| 646 | + putByte bh 1
|
|
| 647 | + put_ bh n
|
|
| 648 | + put_ bh sel
|
|
| 649 | + TypeableEvidence n -> do
|
|
| 650 | + putByte bh 2
|
|
| 651 | + put_ bh n
|
|
| 652 | + |
|
| 653 | + get bh =
|
|
| 654 | + getByte bh >>= \ case
|
|
| 655 | + 0 -> EvidenceVar <$> get bh
|
|
| 656 | + 1 -> RecordField <$> get bh <*> get bh
|
|
| 657 | + 2 -> TypeableEvidence <$> get bh
|
|
| 658 | + t -> fail $ "EvBindDep: Unknown tag: " ++ show t
|
|
| 659 | + |
|
| 601 | 660 | -- | Eq/Ord instances compare on the converted HieName,
|
| 602 | 661 | -- as non-exported names may have different uniques after
|
| 603 | 662 | -- a roundtrip
|
| 604 | -newtype EvBindDeps = EvBindDeps { getEvBindDeps :: [Name] }
|
|
| 663 | +newtype EvBindDeps = EvBindDeps { getEvBindDeps :: [EvBindDep] }
|
|
| 605 | 664 | deriving Outputable
|
| 606 | 665 | |
| 666 | +evBindDepsNames :: EvBindDeps -> [Name]
|
|
| 667 | +evBindDepsNames = map evBindDepName . getEvBindDeps
|
|
| 668 | + |
|
| 607 | 669 | instance Eq EvBindDeps where
|
| 608 | - (==) = coerce ((==) `on` map toHieName)
|
|
| 670 | + (==) = coerce ((==) `on` map evBindDepHieName)
|
|
| 609 | 671 | |
| 610 | 672 | instance Ord EvBindDeps where
|
| 611 | - compare = coerce (compare `on` map toHieName)
|
|
| 673 | + compare = coerce (compare `on` map evBindDepHieName)
|
|
| 612 | 674 | |
| 613 | 675 | instance Binary EvBindDeps where
|
| 614 | 676 | put_ bh (EvBindDeps xs) = put_ bh xs
|
| ... | ... | @@ -145,7 +145,7 @@ getEvidenceTree refmap var = go emptyNameSet var |
| 145 | 145 | det <- S.toList $ identInfo dets
|
| 146 | 146 | case det of
|
| 147 | 147 | EvidenceVarBind src@(EvLetBind (getEvBindDeps -> xs)) scp spn ->
|
| 148 | - pure $ Just ((src,scp,spn),mapMaybe (go $ extendNameSet seen var) xs)
|
|
| 148 | + pure $ Just ((src,scp,spn),mapMaybe (go (extendNameSet seen var) . evBindDepName) xs)
|
|
| 149 | 149 | EvidenceVarBind src scp spn -> pure $ Just ((src,scp,spn),[])
|
| 150 | 150 | _ -> pure Nothing
|
| 151 | 151 | pure $ Tree.Node (EvidenceInfo var sp typ (Just evdet)) children
|
| ... | ... | @@ -390,7 +390,7 @@ definedInAsts asts n = case nameSrcSpan n of |
| 390 | 390 | |
| 391 | 391 | getEvidenceBindDeps :: ContextInfo -> [Name]
|
| 392 | 392 | getEvidenceBindDeps (EvidenceVarBind (EvLetBind xs) _ _) =
|
| 393 | - getEvBindDeps xs
|
|
| 393 | + evBindDepsNames xs
|
|
| 394 | 394 | getEvidenceBindDeps _ = []
|
| 395 | 395 | |
| 396 | 396 | isEvidenceBind :: ContextInfo -> Bool
|
| ... | ... | @@ -4,7 +4,7 @@ At point (13,17), we found: |
| 4 | 4 | ┌
|
| 5 | 5 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "field1" Thing Char
|
| 6 | 6 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 7 | -│ field1]
|
|
| 7 | +│ field1 of Record Thing]
|
|
| 8 | 8 | │ with scope: ModuleScope
|
| 9 | 9 | │
|
| 10 | 10 | │ Defined at <no location info>
|
| ... | ... | @@ -31,7 +31,7 @@ At point (16,27), we found: |
| 31 | 31 | ┌
|
| 32 | 32 | │ $dHasField at HasFieldQueries.hs:16:1-32, of type: HasField "field1" Thing Char
|
| 33 | 33 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 34 | -│ field1]
|
|
| 34 | +│ field1 of Record Thing]
|
|
| 35 | 35 | │ with scope: LocalScope HasFieldQueries.hs:16:1-32
|
| 36 | 36 | │ bound at: HasFieldQueries.hs:16:1-32
|
| 37 | 37 | │ Defined at <no location info>
|
| ... | ... | @@ -55,7 +55,7 @@ At point (23,17), we found: |
| 55 | 55 | ┌
|
| 56 | 56 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "nested1" NestedThing Thing
|
| 57 | 57 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 58 | -│ nested1]
|
|
| 58 | +│ nested1 of Record NestedThing]
|
|
| 59 | 59 | │ with scope: ModuleScope
|
| 60 | 60 | │
|
| 61 | 61 | │ Defined at <no location info>
|
| ... | ... | @@ -87,7 +87,7 @@ At point (23,25), we found: |
| 87 | 87 | `- ┌
|
| 88 | 88 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "field1" Thing Char
|
| 89 | 89 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 90 | - │ field1]
|
|
| 90 | + │ field1 of Record Thing]
|
|
| 91 | 91 | │ with scope: ModuleScope
|
| 92 | 92 | │
|
| 93 | 93 | │ Defined at <no location info>
|
| ... | ... | @@ -176,7 +176,7 @@ At point (37,14), we found: |
| 176 | 176 | `- ┌
|
| 177 | 177 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "field1" Thing Char
|
| 178 | 178 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 179 | - │ field1]
|
|
| 179 | + │ field1 of Record Thing]
|
|
| 180 | 180 | │ with scope: ModuleScope
|
| 181 | 181 | │
|
| 182 | 182 | │ Defined at <no location info>
|
| ... | ... | @@ -200,7 +200,7 @@ At point (41,21), we found: |
| 200 | 200 | ┌
|
| 201 | 201 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "nested1" NestedThing Thing
|
| 202 | 202 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 203 | -│ nested1]
|
|
| 203 | +│ nested1 of Record NestedThing]
|
|
| 204 | 204 | │ with scope: ModuleScope
|
| 205 | 205 | │
|
| 206 | 206 | │ Defined at <no location info>
|
| ... | ... | @@ -224,7 +224,7 @@ At point (41,30), we found: |
| 224 | 224 | ┌
|
| 225 | 225 | │ $dHasField at HasFieldQueries.hs:1:1, of type: HasField "field1" Thing Char
|
| 226 | 226 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 227 | -│ field1]
|
|
| 227 | +│ field1 of Record Thing]
|
|
| 228 | 228 | │ with scope: ModuleScope
|
| 229 | 229 | │
|
| 230 | 230 | │ Defined at <no location info>
|
| 1 | +module Main where
|
|
| 2 | + |
|
| 3 | +import Data.Dynamic
|
|
| 4 | +import Data.Typeable
|
|
| 5 | +import TestUtils
|
|
| 6 | + |
|
| 7 | +data Thing = Thing {field1 :: Char, field2 :: Bool}
|
|
| 8 | + deriving (Show, Eq)
|
|
| 9 | + |
|
| 10 | +castFromDynamic :: Dynamic -> Maybe Thing
|
|
| 11 | +castFromDynamic d = fromDynamic d
|
|
| 12 | + -- ^ this is the point
|
|
| 13 | + |
|
| 14 | +rep :: Thing -> TypeRep
|
|
| 15 | +rep d = typeOf d
|
|
| 16 | + -- ^ this is the point
|
|
| 17 | + |
|
| 18 | +points =
|
|
| 19 | + [ (11,21)
|
|
| 20 | + , (15,10)
|
|
| 21 | + ]
|
|
| 22 | + |
|
| 23 | +main = do
|
|
| 24 | + (df, hf) <- readTestHie "HieTypeable.hie"
|
|
| 25 | + let refmap = generateReferencesMap $ getAsts $ hie_asts hf
|
|
| 26 | + |
|
| 27 | + traverse (explainEv df hf refmap) points
|
|
| 28 | + return () |
| 1 | +==========================
|
|
| 2 | +At point (11,21), we found:
|
|
| 3 | +==========================
|
|
| 4 | +┌
|
|
| 5 | +│ $dTypeable at HieTypeable.hs:1:1, of type: Typeable Thing
|
|
| 6 | +│ is an evidence variable bound by a let, depending on: [$dTypeable]
|
|
| 7 | +│ with scope: ModuleScope
|
|
| 8 | +│
|
|
| 9 | +│ Defined at <no location info>
|
|
| 10 | +└
|
|
| 11 | +|
|
|
| 12 | +`- ┌
|
|
| 13 | + │ $dTypeable at HieTypeable.hs:1:1, of type: Typeable Thing
|
|
| 14 | + │ is an evidence variable bound by a let, depending on: []
|
|
| 15 | + │ with scope: ModuleScope
|
|
| 16 | + │
|
|
| 17 | + │ Defined at <no location info>
|
|
| 18 | + └
|
|
| 19 | + |
|
| 20 | +==========================
|
|
| 21 | +At point (15,10), we found:
|
|
| 22 | +==========================
|
|
| 23 | +┌
|
|
| 24 | +│ $dTypeable at HieTypeable.hs:1:1, of type: Typeable Thing
|
|
| 25 | +│ is an evidence variable bound by a let, depending on: []
|
|
| 26 | +│ with scope: ModuleScope
|
|
| 27 | +│
|
|
| 28 | +│ Defined at <no location info>
|
|
| 29 | +└
|
|
| 30 | + |
| ... | ... | @@ -4,7 +4,7 @@ At point (23,16), we found: |
| 4 | 4 | ┌
|
| 5 | 5 | │ $dHasField at NoFieldSelectorQueries.hs:1:1, of type: HasField "a" A Int
|
| 6 | 6 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 7 | -│ a]
|
|
| 7 | +│ a of Record A]
|
|
| 8 | 8 | │ with scope: ModuleScope
|
| 9 | 9 | │
|
| 10 | 10 | │ Defined at <no location info>
|
| ... | ... | @@ -47,7 +47,7 @@ At point (30,15), we found: |
| 47 | 47 | ┌
|
| 48 | 48 | │ $dHasField at NoFieldSelectorQueries.hs:30:1-17, of type: HasField "n" B A
|
| 49 | 49 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 50 | -│ n]
|
|
| 50 | +│ n of Record B]
|
|
| 51 | 51 | │ with scope: LocalScope NoFieldSelectorQueries.hs:30:1-17
|
| 52 | 52 | │ bound at: NoFieldSelectorQueries.hs:30:1-17
|
| 53 | 53 | │ Defined at <no location info>
|
| ... | ... | @@ -71,7 +71,7 @@ At point (30,17), we found: |
| 71 | 71 | ┌
|
| 72 | 72 | │ $dHasField at NoFieldSelectorQueries.hs:30:1-17, of type: HasField "b" A Char
|
| 73 | 73 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 74 | -│ b]
|
|
| 74 | +│ b of Record A]
|
|
| 75 | 75 | │ with scope: LocalScope NoFieldSelectorQueries.hs:30:1-17
|
| 76 | 76 | │ bound at: NoFieldSelectorQueries.hs:30:1-17
|
| 77 | 77 | │ Defined at <no location info>
|
| ... | ... | @@ -95,7 +95,7 @@ At point (33,16), we found: |
| 95 | 95 | ┌
|
| 96 | 96 | │ $dHasField at NoFieldSelectorQueries.hs:33:1-16, of type: HasField "c" C Float
|
| 97 | 97 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 98 | -│ c]
|
|
| 98 | +│ c of Record C]
|
|
| 99 | 99 | │ with scope: LocalScope NoFieldSelectorQueries.hs:33:1-16
|
| 100 | 100 | │ bound at: NoFieldSelectorQueries.hs:33:1-16
|
| 101 | 101 | │ Defined at <no location info>
|
| ... | ... | @@ -119,7 +119,7 @@ At point (36,16), we found: |
| 119 | 119 | ┌
|
| 120 | 120 | │ $dHasField at NoFieldSelectorQueries.hs:36:1-16, of type: HasField "c" C Float
|
| 121 | 121 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 122 | -│ c]
|
|
| 122 | +│ c of Record C]
|
|
| 123 | 123 | │ with scope: LocalScope NoFieldSelectorQueries.hs:36:1-16
|
| 124 | 124 | │ bound at: NoFieldSelectorQueries.hs:36:1-16
|
| 125 | 125 | │ Defined at <no location info>
|
| ... | ... | @@ -143,7 +143,7 @@ At point (40,23), we found: |
| 143 | 143 | ┌
|
| 144 | 144 | │ $dHasField at NoFieldSelectorQueries.hs:1:1, of type: HasField "c" C Float
|
| 145 | 145 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 146 | -│ c]
|
|
| 146 | +│ c of Record C]
|
|
| 147 | 147 | │ with scope: ModuleScope
|
| 148 | 148 | │
|
| 149 | 149 | │ Defined at <no location info>
|
| ... | ... | @@ -167,7 +167,7 @@ At point (43,15), we found: |
| 167 | 167 | ┌
|
| 168 | 168 | │ $dHasField at NoFieldSelectorQueries.hs:43:1-15, of type: HasField "e" C Int
|
| 169 | 169 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 170 | -│ e]
|
|
| 170 | +│ e of Record C]
|
|
| 171 | 171 | │ with scope: LocalScope NoFieldSelectorQueries.hs:43:1-15
|
| 172 | 172 | │ bound at: NoFieldSelectorQueries.hs:43:1-15
|
| 173 | 173 | │ Defined at <no location info>
|
| ... | ... | @@ -191,7 +191,7 @@ At point (46,15), we found: |
| 191 | 191 | ┌
|
| 192 | 192 | │ $dHasField at NoFieldSelectorQueries.hs:46:1-15, of type: HasField "e" C Int
|
| 193 | 193 | │ is an evidence variable bound by a let, depending on: [C:HasField,
|
| 194 | -│ e]
|
|
| 194 | +│ e of Record C]
|
|
| 195 | 195 | │ with scope: LocalScope NoFieldSelectorQueries.hs:46:1-15
|
| 196 | 196 | │ bound at: NoFieldSelectorQueries.hs:46:1-15
|
| 197 | 197 | │ Defined at <no location info>
|
| ... | ... | @@ -12,3 +12,4 @@ test('HieGadtConSigs', [extra_run_opts('"' + config.libdir + '"'), extra_files([ |
| 12 | 12 | test('T25709', [extra_run_opts('"' + config.libdir + '"'), extra_files(['TestUtils.hs'])], compile_and_run, ['-package ghc -fwrite-ide-info'])
|
| 13 | 13 | test('HasFieldQueries', [extra_run_opts('"' + config.libdir + '"'), extra_files(['TestUtils.hs'])], compile_and_run, ['-package ghc -fwrite-ide-info'])
|
| 14 | 14 | test('NoFieldSelectorQueries', [extra_run_opts('"' + config.libdir + '"'), extra_files(['TestUtils.hs'])], compile_and_run, ['-package ghc -fwrite-ide-info'])
|
| 15 | +test('HieTypeable', [extra_run_opts('"' + config.libdir + '"'), extra_files(['TestUtils.hs'])], compile_and_run, ['-package ghc -fwrite-ide-info']) |