Import Upstream version 20180207
[hcoop/debian/mlton.git] / doc / guide / src / WarnUnusedAnomalies.adoc
1 WarnUnusedAnomalies
2 ===================
3
4 The `warnUnused` <:MLBasisAnnotations:MLBasis annotation> can be used
5 to report unused identifiers. This can be useful for catching bugs
6 and for code maintenance (e.g., eliminating dead code). However, the
7 `warnUnused` annotation can sometimes behave in counter-intuitive
8 ways. This page gives some of the anomalies that have been reported.
9
10 * Functions whose only uses are recursive uses within their bodies are
11 not warned as unused:
12 +
13 [source,sml]
14 ----
15 local
16 fun foo () = foo () : unit
17 val bar = let fun baz () = baz () : unit in baz end
18 in
19 end
20 ----
21 +
22 ----
23 Warning: z.sml 3.5.
24 Unused variable: bar.
25 ----
26
27 * Components of actual functor argument that are necessary to match
28 the functor argument signature but are unused in the body of the
29 functor are warned as unused:
30 +
31 [source,sml]
32 ----
33 functor Warning (type t val x : t) = struct
34 val y = x
35 end
36 structure X = Warning (type t = int val x = 1)
37 ----
38 +
39 ----
40 Warning: z.sml 4.29.
41 Unused type: t.
42 ----
43
44
45 * No component of a functor result is warned as unused. In the
46 following, the only uses of `f2` are to match the functor argument
47 signatures of `functor G` and `functor H` and there are no uses of
48 `z`:
49 +
50 [source,sml]
51 ----
52 functor F(structure X : sig type t end) = struct
53 type t = X.t
54 fun f1 (_ : X.t) = ()
55 fun f2 (_ : X.t) = ()
56 val z = ()
57 end
58 functor G(structure Y : sig
59 type t
60 val f1 : t -> unit
61 val f2 : t -> unit
62 val z : unit
63 end) = struct
64 fun g (x : Y.t) = Y.f1 x
65 end
66 functor H(structure Y : sig
67 type t
68 val f1 : t -> unit
69 val f2 : t -> unit
70 val z : unit
71 end) = struct
72 fun h (x : Y.t) = Y.f1 x
73 end
74 functor Z() = struct
75 structure S = F(structure X = struct type t = unit end)
76 structure SG = G(structure Y = S)
77 structure SH = H(structure Y = S)
78 end
79 structure U = Z()
80 val _ = U.SG.g ()
81 val _ = U.SH.h ()
82 ----
83 +
84 ----
85 ----