Understanding how warnings on unused functions work

Adam Gundry adam at well-typed.com
Mon Mar 27 19:18:53 UTC 2023


Hi,

I think the function you are looking for is reportUnusedNames in 
GHC.Rename.Names [1], which calls findUses [2]. You may find the Note 
[Tracking unused binding and imports] in GHC.Tc.Types [3] helpful.

Roughly, GHC's renamer traverses the syntax tree to resolve names and 
along the way collects sets of defined/used names in tcg_dus. There are 
also a set of names used by the typechecker in tcg_keep. After 
typechecking, findUses takes the transitive closure of the set of uses 
and reportUnusedNames then issues unused-top-binds warnings for all 
names defined in the current module that are not in the set.

Hope this helps,

Adam


[1] 
https://gitlab.haskell.org/ghc/ghc/-/blob/e1fb56b24e2fe45a6f628f651bfc12b2b9743378/compiler/GHC/Rename/Names.hs#L1579

[2] 
https://gitlab.haskell.org/ghc/ghc/-/blob/e1fb56b24e2fe45a6f628f651bfc12b2b9743378/compiler/GHC/Types/Name/Set.hs#L203

[3] 
https://gitlab.haskell.org/ghc/ghc/-/blob/e1fb56b24e2fe45a6f628f651bfc12b2b9743378/compiler/GHC/Tc/Types.hs#L710


On 14/03/2023 17:13, Razetime wrote:
> This message is related to a feature in the futhark compiler 
> <https://github.com/diku-dk/futhark/issues/550>, which is a language 
> with a similar module system.
> 
> I am trying to create a tool to detect unused top-level functions in 
> Futhark, and I have been looking in the ghc codebase to find out how it 
> does so. So far I have found these areas:
> 
>  1. warning flag
>     <https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Driver/Flags.hs#L556-557>
>  2. WarnUnusedTopBinds
>     <https://gitlab.haskell.org/ghc/ghc/-/blob/master/compiler/GHC/Rename/Utils.hs#L355>
> 
> The project is a bit too big for my machine to take, so HLS is not 
> helping me with finding where the computation of unused bindings 
> (specifically functions) is done.
> I would like some help with finding where ghc does this computation, and 
> maybe some help with the naming conventions of the code.
> 
> Thanks in advance.

-- 
Adam Gundry, Haskell Consultant
Well-Typed LLP, https://www.well-typed.com/

Registered in England & Wales, OC335890
27 Old Gloucester Street, London WC1N 3AX, England



More information about the ghc-devs mailing list