Skip to content

Commit 065cbeb

Browse files
committed
[docs][methods][11/n] add coercion example
This shows that coercing an object implies use by requirements.
1 parent 64a4324 commit 065cbeb

File tree

15 files changed

+158
-12
lines changed

15 files changed

+158
-12
lines changed

check/classic/classic.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ Nothing else to report in this section
151151
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
152152
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
153153

154+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
155+
154156
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
155157
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
156158

check/classic/classic.ref

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ Nothing else to report in this section
156156
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
157157
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
158158

159+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
160+
159161
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
160162
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
161163

@@ -638,7 +640,7 @@ Nothing else to report in this section
638640
--------------------------------------------------------------------------------
639641

640642

641-
Total: 546
642-
Success: 540
643+
Total: 547
644+
Success: 541
643645
Failed: 6
644-
Ratio: 98.9010989011%
646+
Ratio: 98.9031078611%

check/internal/internal.exp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ Nothing else to report in this section
106106
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
107107
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
108108

109+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
110+
109111
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
110112
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
111113

check/internal/internal.ref

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Nothing else to report in this section
111111
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
112112
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
113113

114+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
115+
114116
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
115117
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
116118

@@ -593,7 +595,7 @@ Nothing else to report in this section
593595
--------------------------------------------------------------------------------
594596

595597

596-
Total: 504
597-
Success: 498
598+
Total: 505
599+
Success: 499
598600
Failed: 6
599-
Ratio: 98.8095238095%
601+
Ratio: 98.8118811881%

check/threshold-1/threshold-1.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139

140140
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times
141141

142+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
143+
142144
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:4: push_n_times
143145

144146
./examples/docs/methods/code_constructs/immediate_object/immediate_object_bin.ml:4: push_n_times
@@ -355,6 +357,8 @@ Nothing else to report in this section
355357
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
356358
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
357359

360+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
361+
358362
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
359363
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
360364

@@ -429,6 +433,8 @@ Nothing else to report in this section
429433
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop
430434
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push
431435

436+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
437+
432438
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#peek
433439
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#pop
434440
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#push

check/threshold-1/threshold-1.ref

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144

145145
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times
146146

147+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
148+
147149
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:4: push_n_times
148150

149151
./examples/docs/methods/code_constructs/immediate_object/immediate_object_bin.ml:4: push_n_times
@@ -360,6 +362,8 @@ Nothing else to report in this section
360362
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
361363
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
362364

365+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
366+
363367
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
364368
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
365369

@@ -434,6 +438,8 @@ Nothing else to report in this section
434438
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop
435439
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push
436440

441+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
442+
437443
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#peek
438444
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#pop
439445
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#push
@@ -999,7 +1005,7 @@ Nothing else to report in this section
9991005
--------------------------------------------------------------------------------
10001006

10011007

1002-
Total: 854
1003-
Success: 847
1008+
Total: 857
1009+
Success: 850
10041010
Failed: 7
1005-
Ratio: 99.1803278689%
1011+
Ratio: 99.1831971995%

check/threshold-3-0.5/threshold-3-0.5.exp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@
139139

140140
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times
141141

142+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
143+
142144
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:4: push_n_times
143145

144146
./examples/docs/methods/code_constructs/immediate_object/immediate_object_bin.ml:4: push_n_times
@@ -491,6 +493,8 @@ Nothing else to report in this section
491493
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
492494
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
493495

496+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
497+
494498
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
495499
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
496500

@@ -565,6 +569,8 @@ Nothing else to report in this section
565569
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop
566570
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push
567571

572+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
573+
568574
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#peek
569575
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#pop
570576
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#push

check/threshold-3-0.5/threshold-3-0.5.ref

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@
144144

145145
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:4: push_n_times
146146

147+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
148+
147149
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:4: push_n_times
148150

149151
./examples/docs/methods/code_constructs/immediate_object/immediate_object_bin.ml:4: push_n_times
@@ -496,6 +498,8 @@ Nothing else to report in this section
496498
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
497499
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#reset
498500

501+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
502+
499503
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
500504
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#reset
501505

@@ -570,6 +574,8 @@ Nothing else to report in this section
570574
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#pop
571575
./examples/docs/methods/code_constructs/class/class_lib.mli:2: int_stack#push
572576

577+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
578+
573579
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#peek
574580
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#pop
575581
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:2: int_stack#push
@@ -1396,7 +1402,7 @@ Nothing else to report in this section
13961402
--------------------------------------------------------------------------------
13971403

13981404

1399-
Total: 1170
1400-
Success: 1163
1405+
Total: 1173
1406+
Success: 1166
14011407
Failed: 7
1402-
Ratio: 99.4017094017%
1408+
Ratio: 99.4032395567%

docs/methods/METHODS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ from the `.mli` if there is one and the `.ml`.
146146
- [Inheritance](./code_constructs/INHERITANCE.md)
147147
- [Immediate object](./code_constructs/IMMEDIATE_OBJECT.md)
148148
- [Object type](./code_constructs/OBJECT_TYPE.md)
149+
- [Coercion](./code_constructs/COERCION.md)
149150

150151
# Limitations
151152

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
The reference files for this example are in the
2+
[coercion](../../../examples/docs/methods/code_constructs/coercion) directory.
3+
4+
The reference takes place in `/tmp/docs/methods/code_constructs`, which
5+
is a copy of the [code\_constructs](../../../examples/docs/methods/code_constructs)
6+
directory. Reported locations may differ depending on the location of the source
7+
files.
8+
9+
The compilation command is :
10+
```
11+
make -C coercion build
12+
```
13+
14+
The analysis command is :
15+
```
16+
make -C coercion analyze
17+
```
18+
19+
The compile + analyze command is :
20+
```
21+
make -C coercion
22+
```
23+
24+
## First run
25+
26+
Code:
27+
```OCaml
28+
(* coercion_lib.mli *)
29+
val obj :
30+
< used_by_requirement : unit
31+
; unused : unit
32+
>
33+
```
34+
```OCaml
35+
(* coercion_lib.ml *)
36+
let obj =
37+
object
38+
method used_by_requirement = ()
39+
method unused = ()
40+
end
41+
```
42+
```OCaml
43+
(* coercion_bin.ml *)
44+
open Coercion_lib
45+
46+
let () =
47+
let _coerce = (obj :> < used_by_requirement : unit >) in
48+
()
49+
```
50+
51+
Before looking at the analysis results, let's look at the code.
52+
53+
There is 1 object : `Coercion_lib.obj`, which defined 2 zmthods :
54+
`used_by_requirement`, and `unused`. Neither of them is explicitly referenced,
55+
and the object is only manipulated through a coercion. The coercion produces an
56+
alias to `obj` named `_coerce`, which only exposes the method
57+
`used_by_requirement`. Because the method is required to exists in `obj` for the
58+
coercion, then the analyzer effectively considers it as used by requirement.
59+
60+
Compile and analyze:
61+
```
62+
$ make -C coercion
63+
make: Entering directory '/tmp/docs/methods/code_constructs/coercion'
64+
ocamlopt -bin-annot coercion_lib.mli coercion_lib.ml coercion_bin.ml
65+
dead_code_analyzer --nothing -M all .
66+
Scanning files...
67+
[DONE]
68+
69+
.> UNUSED METHODS:
70+
=================
71+
/tmp/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
72+
73+
Nothing else to report in this section
74+
--------------------------------------------------------------------------------
75+
76+
77+
make: Leaving directory '/tmp/docs/methods/code_constructs/coercion'
78+
```
79+
80+
As expected, the anlyzer reports `obj#unsed` as unused and not
81+
`obj#used_by_requirement`. The unused method can be removed from the `.mli` and
82+
the `.ml`. Our work here is done.

0 commit comments

Comments
 (0)