Skip to content

Commit 10bf4d7

Browse files
committed
[docs][methods][11/n] add coercion example
This shows that coercing an object implies use by requirements.
1 parent b016092 commit 10bf4d7

File tree

15 files changed

+156
-12
lines changed

15 files changed

+156
-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:8: 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:10: 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:8: 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:10: 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:8: 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:10: 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:8: 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:10: 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
@@ -141,6 +141,8 @@
141141
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
142142
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
143143

144+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
145+
144146
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:6: push_n_times
145147
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:12: clear_stack
146148

@@ -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:8: 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:10: 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:8: int_stack#pop
435439
./examples/docs/methods/code_constructs/class/class_lib.mli:8: 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:10: int_stack#peek
438444
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#pop
439445
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#push

check/threshold-1/threshold-1.ref

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
147147
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
148148

149+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
150+
149151
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:6: push_n_times
150152
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:12: clear_stack
151153

@@ -365,6 +367,8 @@ Nothing else to report in this section
365367
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
366368
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#reset
367369

370+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
371+
368372
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
369373
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#reset
370374

@@ -439,6 +443,8 @@ Nothing else to report in this section
439443
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#pop
440444
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#push
441445

446+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
447+
442448
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#peek
443449
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#pop
444450
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#push
@@ -1004,7 +1010,7 @@ Nothing else to report in this section
10041010
--------------------------------------------------------------------------------
10051011

10061012

1007-
Total: 859
1008-
Success: 852
1013+
Total: 862
1014+
Success: 855
10091015
Failed: 7
1010-
Ratio: 99.1850989523%
1016+
Ratio: 99.1879350348%

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,8 @@
141141
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
142142
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
143143

144+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
145+
144146
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:6: push_n_times
145147
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:12: clear_stack
146148

@@ -500,6 +502,8 @@ Nothing else to report in this section
500502
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
501503
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#reset
502504

505+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
506+
503507
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
504508
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#reset
505509

@@ -574,6 +578,8 @@ Nothing else to report in this section
574578
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#pop
575579
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#push
576580

581+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
582+
577583
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#peek
578584
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#pop
579585
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: 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
@@ -146,6 +146,8 @@
146146
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:6: push_n_times
147147
./examples/docs/methods/code_constructs/class_type/class_type_bin.ml:12: clear_stack
148148

149+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj
150+
149151
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:6: push_n_times
150152
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:12: clear_stack
151153

@@ -505,6 +507,8 @@ Nothing else to report in this section
505507
./examples/docs/methods/code_constructs/class/class_bin.ml:2: unused_class#unused_method
506508
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#reset
507509

510+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
511+
508512
./examples/docs/methods/code_constructs/constructor/constructor_bin.ml:2: unused_class#unused_method
509513
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#reset
510514

@@ -579,6 +583,8 @@ Nothing else to report in this section
579583
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#pop
580584
./examples/docs/methods/code_constructs/class/class_lib.mli:8: int_stack#push
581585

586+
./examples/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#used_by_requirement
587+
582588
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#peek
583589
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#pop
584590
./examples/docs/methods/code_constructs/constructor/constructor_lib.mli:10: int_stack#push
@@ -1417,7 +1423,7 @@ Nothing else to report in this section
14171423
--------------------------------------------------------------------------------
14181424

14191425

1420-
Total: 1186
1421-
Success: 1179
1426+
Total: 1189
1427+
Success: 1182
14221428
Failed: 7
1423-
Ratio: 99.4097807757%
1429+
Ratio: 99.4112699748%

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_OBJ.md)
148148
- [Object type](./code_constructs/OBJECT_TYPE.md)
149+
- [Coercion](./code_constructs/COERCION.md)
149150

150151
# Limitations
151152

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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 = object
37+
method used_by_requirement = ()
38+
method unused = ()
39+
end
40+
```
41+
```OCaml
42+
(* coercion_bin.ml *)
43+
open Coercion_lib
44+
45+
let () =
46+
let _coerce = (obj :> < used_by_requirement : unit >) in
47+
()
48+
```
49+
50+
Before looking at the analysis results, let's look at the code.
51+
52+
There is 1 object : `Coercion_lib.obj`, which defined 2 zmthods :
53+
`used_by_requirement`, and `unused`. Neither of them is explicitly referenced,
54+
and the object is only manipulated through a coercion. The coercion produces an
55+
alias to `obj` named `_coerce`, which only exposes the method
56+
`used_by_requirement`. Because the method is required to exists in `obj` for the
57+
coercion, then the analyzer effectively considers it as used by requirement.
58+
59+
Compile and analyze:
60+
```
61+
$ make -C coercion
62+
make: Entering directory '/tmp/docs/methods/code_constructs/coercion'
63+
ocamlopt -bin-annot coercion_lib.mli coercion_lib.ml coercion_bin.ml
64+
dead_code_analyzer --nothing -M all .
65+
Scanning files...
66+
[DONE]
67+
68+
.> UNUSED METHODS:
69+
=================
70+
/tmp/docs/methods/code_constructs/coercion/coercion_lib.mli:2: obj#unused
71+
72+
Nothing else to report in this section
73+
--------------------------------------------------------------------------------
74+
75+
76+
make: Leaving directory '/tmp/docs/methods/code_constructs/coercion'
77+
```
78+
79+
As expected, the anlyzer reports `obj#unsed` as unused and not
80+
`obj#used_by_requirement`. The unused method can be removed from the `.mli` and
81+
the `.ml`. Our work here is done.

0 commit comments

Comments
 (0)