Skip to content

Fields and constructors uses are not tracked though module aliases #81

@fantazio

Description

@fantazio

Context

This is a pattern observed in Opam 2.5.1. In a compilation unit, a module is defined but not exported (OpamSys), it defines a variant or record type (os), the types' constructors/fields are used (here), the module is aliased (Sys), the alias is exported (Sys).

This leads the analyzer to report the constructors and fields in the module alias as unused although they are in the original alias.
Note that it is not necessary that the uses are within the original module or that they refer to the original module. Any internal use via the original module or the alias is not bound to the exported alias.

Example and reproduction

(* /tmp/modalias/modalias.mli *)
module Alias : sig
  type sum =
    | UsedByOriginal | UsedByAlias | Unused

  type product = {
    used_by_original : unit;
    used_by_alias : unit;
    unused:unit
  }
end
(* /tmp/modalias/modalias.ml *)
module Original = struct
  type sum =
    | UsedByOriginal | UsedByAlias | Unused

  type product = {
    used_by_original : unit;
    used_by_alias : unit;
    unused:unit
  }
end

let _ =  Original.UsedByOriginal
let _ = fun p -> p.Original.used_by_original

module Alias = Original

let _ =  Alias.UsedByAlias
let _ = fun p -> p.Alias.used_by_alias
$ ocamlopt -bin-annot modalias.mli modalias.ml
$ dead_code_analyzer --nothing -T all .
Scanning files...
 [DONE]

.> UNUSED CONSTRUCTORS/RECORD FIELDS:
====================================
/tmp/modalias/modalias.mli:4: Alias.sum.UsedByOriginal
/tmp/modalias/modalias.mli:4: Alias.sum.UsedByAlias
/tmp/modalias/modalias.mli:4: Alias.sum.Unused
/tmp/modalias/modalias.mli:7: Alias.product.used_by_original
/tmp/modalias/modalias.mli:8: Alias.product.used_by_alias
/tmp/modalias/modalias.mli:9: Alias.product.unused

Nothing else to report in this section
--------------------------------------------------------------------------------

All the constructors and fields exported by Alias are reported as unused although only Alias.sum.Unused and Alias.product.unused are actually unused.

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions