Changes between Version 62 and Version 63 of Records/OverloadedRecordFields/Implementation


Ignore:
Timestamp:
Sep 4, 2013 9:14:10 AM (2 years ago)
Author:
adamgundry
Comment:

unused bindings

Legend:

Unmodified
Added
Removed
Modified
  • Records/OverloadedRecordFields/Implementation

    v62 v63  
    132132Since the instances are not in scope in the usual way, `matchClassInst` and `tcLookupFamInst` look for the relevant constraints or type families and find the instances directly, rather than consulting `tcg_inst_env` or `tcg_fam_inst_env`. They first perform a lookup to check that the field name is in scope. A new field `tcg_fld_inst_env` in `TcGblEnv` maps a selector name in the current module to its `DFunId`s and `FamInst`s; this is needed for solving constraints that arise while checking the automatically generated instances themselves.
    133133
    134 '''AMG''' The instance lookup is currently implemented as a separate check, which means virtual fields should be forbidden. Perhaps it could be integrated into the existing overlapping instances mechanism, but type families need some care to avoid soundness bugs.
    135 
    136134
    137135== Unused imports ==
     
    151149
    152150  foo :: T -> Int
    153   foo r = r.x + 2
    154 }}}
    155 
    156 Now, do we expect to report the 'x' in S(x) import as unused?  Actually the entire 'import B' is unused.  Only the typechecker will eventually know that.
    157 
    158 To record this, I've added a new field `tcg_used_selectors :: TcRef NameSet` to the `TcGblEnv`, which records the selector names for fields that are encountered during typechecking (when looking up a `Has` instance etc.). This set is used to calculate the import usage and unused top-level bindings. Thus a field will be counted as used if it is needed by the typechecker, regardless of whether any definitions it appears in are themselves used.
    159 
    160 '''AMG''' One problem remains: typechecking the instances generated for a field counts as a use of that field! So all locally defined overloaded fields will be counted as used.
     151  foo r = x r + 2
     152}}}
     153
     154Now, do we expect to report the `import B( S(x) )` as unused? Only the typechecker will eventually know that. To record this, I've added a new field `tcg_used_selectors :: TcRef NameSet` to the `TcGblEnv`, which records the selector names for fields that are encountered during typechecking (when looking up a `Has` instance etc.). This set is used to calculate the import usage and unused top-level bindings. Thus a field will be counted as used if it is needed by the typechecker, regardless of whether any definitions it appears in are themselves used.
     155
     156Unused local bindings are trickier, as the following example illustrates:
     157
     158{{{
     159module M (f)
     160  data S = MkS { foo :: Int }
     161  data T = MkT { foo :: Int }
     162
     163  f = foo (MkS 3)
     164  g x = foo x
     165}}}
     166
     167The renamer calculates the free variables of each definition, to produce a list of `DefUses`. In both `f` and `g` we get potential uses of `S(foo)` and `T(foo)`, but the typechecker will discover that `f` uses only `S(foo)` while `g` uses neither. (But `g` requires `foo` to be in scope somehow!) The simplest thing is to make an occurrence of an overloaded field in an expression return as free variables all the selectors it might refer to. This will sometimes fail to report unused local bindings: in the example, it will not spot that `T(foo)` is unused.
    161168
    162169
     
    240247== To do ==
    241248
     249* Add `HsVarOut RdrName id` instead of `HsSingleRecFld` (or perhaps rename `HsVar` to `HsVarIn`)? This would also be useful to recall how the user referred to something.
     250
     251* Haddock omits fields from HTML index and prints selector names in LaTeX exports list.
     252
    242253* When there is only one thing in scope, what should we do? See [wiki:Records/OverloadedRecordFields/Plan#Scopeissuesorwhywemissdot discussion here].
    243 * Add `HsVarOut RdrName id` instead of `HsSingleRecFld` (or perhaps rename `HsVar` to `HsVarIn`)? This would also be useful to recall how the user referred to something.
    244 
    245 * Fix reporting of unused local field definitions.
    246 * Only do magic instance lookup when the extension is enabled?
    247 * Haddock omits fields from HTML index and prints selector names in LaTeX exports list.
     254* Is the story about `-fwarn-unused-binds` okay?
     255* Is `TcInstDcls.tcFldInsts` correct in its use of `simplifyTop` and assuming there will be no `ev_binds`?
    248256
    249257* Consider syntactic sugar for `Upd` constraints.