Skip to content

Commit 05e9ffd

Browse files
simonmarfacebook-github-bot
authored andcommitted
tag refs with import/export/code (#518)
Summary: It's useful to be able to know whether a ref is in an import or export, e.g. for dead code detection, so let's tag refs with an enum to say what kind of ref it is. I changed field names so that the schema change shouldn't trigger a validation failure (I hope). Note: stacked on #511 Pull Request resolved: #518 Test Plan: Imported from GitHub, without a `Test Plan:` line. Rollback Plan: Reviewed By: donsbot Differential Revision: D75066884 Pulled By: bochko fbshipit-source-id: b58f19b3bc9d4261b38640e30833916b654adc14
1 parent 7aab4ae commit 05e9ffd

File tree

5 files changed

+220
-157
lines changed

5 files changed

+220
-157
lines changed

glean/lang/haskell/HieIndexer/Index.hs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import qualified GHC
2727
import GHC.Iface.Ext.Utils (generateReferencesMap)
2828
import GHC.Iface.Ext.Types (
2929
getAsts, ContextInfo(..), IdentifierDetails(..), RecFieldContext(..),
30-
BindType(..), DeclType(..))
30+
BindType(..), DeclType(..), IEType(..))
3131
import qualified GHC.Types.Name.Occurrence as GHC
3232
import qualified GHC.Types.Name as GHC (isSystemName)
3333
import GHC.Unit.Types (unitFS)
@@ -45,6 +45,7 @@ import Glean.Util.Range
4545
{- TODO
4646
4747
- issues with record fields
48+
- DuplicateRecordFields generates names like $sel:field:Rec
4849
- weird references to the record constructor from field decls
4950
- why do we get a ref for the field decl?
5051
- span of record field in pattern match is wrong
@@ -196,16 +197,16 @@ indexHieFile writer hie = do
196197
Glean.makeFact_ @Hs.ModuleDeclarations $ Hs.ModuleDeclarations_key
197198
modfact (map snd names)
198199

199-
let refs = Map.fromListWith (++)
200-
[ (n, [span])
200+
let refs = Map.fromListWith (Map.unionWith (++))
201+
[ (n, Map.singleton kind [span])
201202
| (n, (span, dets)) <- allIds,
202-
any isRef (identInfo dets),
203+
Just kind <- map isRef (Set.toList (identInfo dets)),
203204
not (GHC.isSystemName n),
204205
-- TODO: we should exclude generated names in a cleaner way
205206
not (GHC.isDerivedOccName (nameOccName n))
206207
]
207208

208-
refs <- fmap catMaybes $ forM (Map.toList refs) $ \(name, spans) -> do
209+
refs <- fmap catMaybes $ forM (Map.toList refs) $ \(name, kindspans) -> do
209210
maybe_namefact <-
210211
case Map.lookup name nameMap of
211212
Just fact -> return $ Just fact
@@ -218,9 +219,11 @@ indexHieFile writer hie = do
218219
namemod <- mkModule mod
219220
Just <$> mkName name namemod (Hs.NameSort_external def)
220221
forM maybe_namefact $ \namefact -> do
221-
let gleanspans = map (toByteSpan . srcSpanToSrcRange filefact) spans
222+
refspans <- forM (Map.toList kindspans) $ \(kind, spans) -> do
223+
let gleanspans = map (toByteSpan . srcSpanToSrcRange filefact) spans
224+
return $ map (Hs.RefSpan kind) gleanspans
222225
Glean.makeFact @Hs.Reference $
223-
Hs.Reference_key namefact gleanspans
226+
Hs.Reference_key namefact (concat refspans)
224227

225228
Glean.makeFact_ @Hs.FileXRefs $ Hs.FileXRefs_key filefact refs
226229

@@ -236,12 +239,14 @@ indexHieFile writer hie = do
236239
| otherwise -> False
237240

238241
-- returns True if this ContextInfo is a reference
239-
isRef Use = True
240-
isRef (RecField r _) = isRecFieldRef r
241-
isRef (ValBind InstanceBind _ _) = True -- treat these as refs, not binds
242-
isRef TyDecl{} = True
243-
isRef IEThing{} = True
244-
isRef _ = False
242+
isRef Use = Just Hs.RefKind_coderef
243+
isRef (RecField r _) | isRecFieldRef r = Just Hs.RefKind_coderef
244+
isRef (ValBind InstanceBind _ _) = Just Hs.RefKind_coderef
245+
-- treat these as refs, not binds
246+
isRef TyDecl{} = Just Hs.RefKind_coderef
247+
isRef (IEThing Export) = Just Hs.RefKind_exportref
248+
isRef (IEThing _) = Just Hs.RefKind_importref
249+
isRef _ = Nothing
245250

246251
isRecFieldRef RecFieldAssign = True
247252
isRecFieldRef RecFieldMatch = True

0 commit comments

Comments
 (0)