
#10089: feature: warn about unused data definitions (with typeclass instances) -------------------------------------+------------------------------------- Reporter: slyfox | Owner: Type: feature request | Status: new Priority: normal | Milestone: Component: Compiler | Version: 7.8.4 Resolution: | Keywords: Operating System: Unknown/Multiple | Architecture: Type of failure: None/Unknown | Unknown/Multiple Blocked By: | Test Case: Related Tickets: | Blocking: | Differential Revisions: -------------------------------------+------------------------------------- Comment (by simonpj): Good idea. It would take a little work to implement, though. A bit of background. Unused defintions are reported by `RnNames.reportUnusedNames`, which in turn consults the accumulated defs (definitions) and uses in the global `tcg_dus`. This in turn is set mainly by `RnSource.rnSrcDecls`. The defs/uses field is of type `NameSet.DefUses`, a type synonym for `[DefUse]`. However, ''the list is supposed to be in dependency order'', as you can see from `NameSet.findUses`. Instance declarations are currently treated as defining nothing, but having a bunch of uses. This is a `(Nothing, uses)` pair in the `DefUse` terminology. But that means that anything mentioned in an instance is unconditionally treated as "used", with the effect you observe. To do it right we'd really want to give the instance decl some "defs" too. Consider {{{ instance Show T where show = g }}} It could get a `DefUse` like this: `(Just {Show,T}, {g})`. This expresses the idea that if `Show` or `T` were referenced somewhere else (or exported) then the instance declaration (and all the things it refers to, namely `g`) would be treated as used. Actually you'd really want "and" rather than "or": if `Show` ''and'' `T` are both referenced somewhere else then the instance declarationis used. The trouble is that we can't then give the `[DefUse]` in dependency order: an instance declaration might mention a function, which refers to another type, which is in the head of another instance declaration. The obvious thing to do would be to give up on the dependency-order claim of `[DefUse]` and just take its transitive closure as a relation. Not too hard. So: * Treat `DefUses` as an un-ordered relation, and use transitive closure to find uses. * Give an instance decl `I` a `DefUse` like `(used in head of I, used elsewhere in I)`. * Provide "and" semantics as well as "or" semantics for `DefUse`. Not a big project, and worthwhile. Any volunteers? Simon -- Ticket URL: http://ghc.haskell.org/trac/ghc/ticket/10089#comment:1 GHC http://www.haskell.org/ghc/ The Glasgow Haskell Compiler