Source file typing_lint.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
open Ast
open Infer
open Typing_env
module Error = struct
open Wax_utils
let text = Message.text
let ( ++ ) = Message.( ++ )
let ( ^^ ) = Message.( ^^ )
let warn ?warning ?universal ?hint ?edit ?related context ~location message =
if not (Wax_utils.Diagnostic.in_recovery context) then
Diagnostic.report context ~location ~severity:Warning ?warning ?universal
?hint ?edit ?related ~message ()
let redundant_operation context ~location message =
warn ~warning:Wax_utils.Warning.Redundant_operation ~universal:true context
~location message
let shift_overflow context ~location ~width count =
warn ~warning:Wax_utils.Warning.Shift_overflow ~universal:true context
~location
~hint:
((text "Wasm masks the count modulo" ++ Message.int width)
^^ text "," ++ text "shifting by"
++ Message.uint64 (Int64.unsigned_rem count (Int64.of_int width))
++ text "instead.")
(text "The shift count" ++ Message.uint64 count
++ text "is at least the operand width ("
^^ Message.int width ^^ text " bits).")
let division_by_zero context ~location =
warn ~warning:Wax_utils.Warning.Constant_trap ~universal:true context
~location
(text "This integer division or remainder by zero always traps.")
let tautological_comparison context ~location ~value =
warn ~warning:Wax_utils.Warning.Tautological_comparison ~universal:true
context ~location
((text "This comparison is always" ++ Message.bool value) ^^ text ".")
let constant_condition context ~location ~value =
warn ~warning:Wax_utils.Warning.Constant_condition ~universal:true context
~location
((text "This condition is always" ++ Message.bool value) ^^ text ".")
let unused_result context ~location =
warn ~warning:Wax_utils.Warning.Unused_result ~universal:true context
~location
(text
"The result of this expression is discarded, and computing it has no \
effect.")
let conversion_out_of_range context ~location =
warn ~warning:Wax_utils.Warning.Constant_trap ~universal:true context
~location
(text
"This conversion always traps: the constant is out of the target \
type's range.")
let eager_select context ~location ~select =
warn ~warning:Wax_utils.Warning.Eager_select ~universal:true context
~location
~related:
[
{
Wax_utils.Diagnostic.location = select;
message =
text
"This '?:' evaluates both branches (it compiles to a 'select').";
};
]
~hint:(text "Use an 'if' expression to evaluate only the chosen branch.")
(text
"This operation is evaluated even when the condition selects the \
other branch.")
let precedence ?edit context ~location ~inner ~outer_kind ~inner_kind =
warn ?edit ~warning:Wax_utils.Warning.Precedence ~universal:true context
~location
~related:
[
{
Wax_utils.Diagnostic.location = inner;
message =
text "This" ++ text inner_kind
++ text "operator binds tighter than the"
++ text outer_kind ++ text "operator.";
};
]
~hint:(text "Add parentheses to make the grouping explicit.")
(text "Operator precedence here is easy to misread.")
end
let is_pure_unary_method = function
| "clz" | "ctz" | "popcnt" | "extend8_s" | "extend16_s" | "abs" | "ceil"
| "floor" | "trunc" | "nearest" | "sqrt" | "to_bits" | "from_bits" ->
true
| _ -> false
let is_pure_binary_method = function
| "rotl" | "rotr" | "min" | "max" | "copysign" -> true
| _ -> false
let cast_is_total = function
| Ast.Signedtype { typ; strict; _ } -> (
match typ with `F32 | `F64 -> true | `I32 | `I64 -> not strict)
| Valtype (I32 | I64 | F32 | F64) -> true
| Valtype (V128 | Ref _) | Functype _ -> false
| Ascribed _ -> true
let rec is_effectless (e : _ Ast.instr) =
let field (_, v) = match v with Some e -> is_effectless e | None -> true in
match e.desc with
| Get _ | Int _ | Float _ | Char _ | String _ | Null | StructDefault _ -> true
| UnOp (_, a) -> is_effectless a
| Call ({ desc = StructGet (recv, m); _ }, [])
when is_pure_unary_method m.desc ->
is_effectless recv
| Call ({ desc = StructGet (recv, m); _ }, [ a ])
when is_pure_binary_method m.desc ->
is_effectless recv && is_effectless a
| Call ({ desc = StructGet (recv, m); _ }, args)
when Wax_wasm.Simd.classify m.desc <> None ->
is_effectless recv && List.for_all is_effectless args
| Call ({ desc = Path ({ desc = "v128"; _ }, _); _ }, args) ->
List.for_all is_effectless args
| Call
({ desc = StructGet ({ desc = Get _; _ }, { desc = "size"; _ }); _ }, [])
->
true
| Cast ({ desc = Null; _ }, Valtype (Ref { nullable = true; _ })) -> true
| Cast (a, ct) when cast_is_total ct -> is_effectless a
| BinOp ({ desc = Div _ | Rem _; _ }, _, _) -> false
| BinOp (_, a, b) -> is_effectless a && is_effectless b
| Select (a, b, c) -> is_effectless a && is_effectless b && is_effectless c
| Test (a, _) -> is_effectless a
| Struct (_, fields) -> List.for_all field fields
| StructDesc (d, fields) -> is_effectless d && List.for_all field fields
| StructDefaultDesc d -> is_effectless d
| Array (_, elt, len) -> is_effectless elt && is_effectless len
| ArrayDefault (_, len) -> is_effectless len
| ArrayFixed (_, elts) -> List.for_all is_effectless elts
| _ -> false
let rec collect_assigned_locals acc i =
match i.desc with
| Set (id, _, e) | Tee (id, e) ->
collect_assigned_locals (StringSet.add id.desc acc) e
| Block { block; _ } | Loop { block; _ } | TryTable { block; _ } ->
collect_assigned_locals_list acc block.desc
| While { cond; step; block; _ } ->
let acc = collect_assigned_locals acc cond in
let acc =
Option.fold ~none:acc ~some:(collect_assigned_locals acc) step
in
collect_assigned_locals_list acc block.desc
| If { cond; if_block; else_block; _ } ->
let acc =
collect_assigned_locals_list
(collect_assigned_locals acc cond)
if_block.desc
in
Option.fold ~none:acc
~some:(fun b -> collect_assigned_locals_list acc b.Annot.desc)
else_block
| Try { block; catches; catch_all; _ } ->
let acc = collect_assigned_locals_list acc block.desc in
let acc =
List.fold_left
(fun acc (_, b) -> collect_assigned_locals_list acc b.Annot.desc)
acc catches
in
Option.fold ~none:acc
~some:(fun b -> collect_assigned_locals_list acc b.Annot.desc)
catch_all
| TryCatch { block; arms; _ } ->
let acc = collect_assigned_locals_list acc block.desc in
List.fold_left
(fun acc a -> collect_assigned_locals_list acc a.arm_body.desc)
acc arms
| Call (t, args) | TailCall (t, args) ->
collect_assigned_locals_list (collect_assigned_locals acc t) args
| Cast (e, _)
| Test (e, _)
| NonNull e
| StructGet (e, _)
| GetDescriptor e
| StructDefaultDesc e
| UnOp (_, e)
| Br_if (_, e)
| On (e, _)
| Labelled (_, e)
| Br_table (_, e)
| Br_on_null (_, e)
| Br_on_non_null (_, e)
| Br_on_cast (_, _, e)
| Br_on_cast_fail (_, _, e)
| ThrowRef e
| ArrayDefault (_, e)
| ContNew (_, e) ->
collect_assigned_locals acc e
| Struct (_, fields) ->
List.fold_left
(fun acc (_, e) ->
Option.fold ~none:acc ~some:(collect_assigned_locals acc) e)
acc fields
| StructDesc (d, fields) ->
List.fold_left
(fun acc (_, e) ->
Option.fold ~none:acc ~some:(collect_assigned_locals acc) e)
(collect_assigned_locals acc d)
fields
| CastDesc (e1, _, e2)
| Br_on_cast_desc_eq (_, _, e1, e2)
| Br_on_cast_desc_eq_fail (_, _, e1, e2)
| StructSet (e1, _, e2)
| Array (_, e1, e2)
| ArraySegment (_, _, e1, e2)
| ArrayGet (e1, e2)
| BinOp (_, e1, e2) ->
collect_assigned_locals (collect_assigned_locals acc e1) e2
| ArraySet (e1, e2, e3) | Select (e1, e2, e3) ->
collect_assigned_locals
(collect_assigned_locals (collect_assigned_locals acc e1) e2)
e3
| ArrayFixed (_, l)
| ContBind (_, _, l)
| Suspend (_, l)
| Resume (_, _, l)
| ResumeThrow (_, _, _, l)
| ResumeThrowRef (_, _, l)
| Switch (_, _, l)
| Throw (_, l)
| Sequence l ->
collect_assigned_locals_list acc l
| Dispatch { index; arms; _ } ->
List.fold_left
(fun acc (_, (b : (_ instr list, _) Ast.annotated)) ->
collect_assigned_locals_list acc b.desc)
(collect_assigned_locals acc index)
arms
| Match { scrutinee; arms; default } ->
let acc = collect_assigned_locals acc scrutinee in
let acc =
List.fold_left
(fun acc (_, b) -> collect_assigned_locals_list acc b.Annot.desc)
acc arms
in
collect_assigned_locals_list acc default.desc
| Let (_, body) -> collect_assigned_locals_opt acc body
| Br (_, o) | Return o -> collect_assigned_locals_opt acc o
| If_annotation { then_body; else_body; _ } ->
let acc = collect_assigned_locals_list acc then_body.desc in
Option.fold ~none:acc
~some:(fun b -> collect_assigned_locals_list acc b.Annot.desc)
else_body
| Get _ | Path _ | Unreachable | Nop | Hole | Null | Char _ | String _ | Int _
| Float _ | StructDefault _ ->
acc
and collect_assigned_locals_list acc l =
List.fold_left collect_assigned_locals acc l
and collect_assigned_locals_opt acc o =
match o with Some i -> collect_assigned_locals acc i | None -> acc
let rec collect_labels acc (i : _ Ast.instr) =
let add acc label = match label with Some l -> l :: acc | None -> acc in
match i.desc with
| Block { label; block; _ }
| Loop { label; block; _ }
| TryTable { label; block; _ } ->
collect_labels_list (add acc label) block.desc
| While { label; cond; step; block; _ } ->
let acc = collect_labels (add acc label) cond in
let acc = Option.fold ~none:acc ~some:(collect_labels acc) step in
collect_labels_list acc block.desc
| If { label; cond; if_block; else_block; _ } ->
let acc =
collect_labels_list (collect_labels (add acc label) cond) if_block.desc
in
Option.fold ~none:acc
~some:(fun b -> collect_labels_list acc b.Annot.desc)
else_block
| Try { label; block; catches; catch_all; _ } ->
let acc = collect_labels_list (add acc label) block.desc in
let acc =
List.fold_left
(fun acc (_, b) -> collect_labels_list acc b.Annot.desc)
acc catches
in
Option.fold ~none:acc
~some:(fun b -> collect_labels_list acc b.Annot.desc)
catch_all
| TryCatch { label; block; arms; _ } ->
let acc = collect_labels_list (add acc label) block.desc in
List.fold_left
(fun acc a -> collect_labels_list acc a.arm_body.desc)
acc arms
| Call (t, args) | TailCall (t, args) ->
collect_labels_list (collect_labels acc t) args
| Set (_, _, e)
| Tee (_, e)
| Labelled (_, e)
| Cast (e, _)
| Test (e, _)
| NonNull e
| StructGet (e, _)
| GetDescriptor e
| StructDefaultDesc e
| UnOp (_, e)
| Br_if (_, e)
| On (e, _)
| Br_table (_, e)
| Br_on_null (_, e)
| Br_on_non_null (_, e)
| Br_on_cast (_, _, e)
| Br_on_cast_fail (_, _, e)
| ThrowRef e
| ArrayDefault (_, e)
| ContNew (_, e) ->
collect_labels acc e
| Struct (_, fields) ->
List.fold_left (fun acc (_, e) -> collect_labels_opt acc e) acc fields
| StructDesc (d, fields) ->
List.fold_left
(fun acc (_, e) -> collect_labels_opt acc e)
(collect_labels acc d) fields
| CastDesc (e1, _, e2)
| Br_on_cast_desc_eq (_, _, e1, e2)
| Br_on_cast_desc_eq_fail (_, _, e1, e2)
| StructSet (e1, _, e2)
| Array (_, e1, e2)
| ArraySegment (_, _, e1, e2)
| ArrayGet (e1, e2)
| BinOp (_, e1, e2) ->
collect_labels (collect_labels acc e1) e2
| ArraySet (e1, e2, e3) | Select (e1, e2, e3) ->
collect_labels (collect_labels (collect_labels acc e1) e2) e3
| ArrayFixed (_, l)
| ContBind (_, _, l)
| Suspend (_, l)
| Resume (_, _, l)
| ResumeThrow (_, _, _, l)
| ResumeThrowRef (_, _, l)
| Switch (_, _, l)
| Throw (_, l)
| Sequence l ->
collect_labels_list acc l
| Dispatch { index; arms; _ } ->
List.fold_left
(fun acc (_, b) -> collect_labels_list acc b.Annot.desc)
(collect_labels acc index) arms
| Match { scrutinee; arms; default } ->
let acc = collect_labels acc scrutinee in
let acc =
List.fold_left
(fun acc (_, b) -> collect_labels_list acc b.Annot.desc)
acc arms
in
collect_labels_list acc default.desc
| Let (_, body) -> collect_labels_opt acc body
| Br (_, o) | Return o -> collect_labels_opt acc o
| If_annotation { then_body; else_body; _ } ->
let acc = collect_labels_list acc then_body.desc in
Option.fold ~none:acc
~some:(fun b -> collect_labels_list acc b.Annot.desc)
else_body
| Get _ | Path _ | Unreachable | Nop | Hole | Null | Char _ | String _ | Int _
| Float _ | StructDefault _ ->
acc
and collect_labels_list acc l = List.fold_left collect_labels acc l
and collect_labels_opt acc o =
match o with Some i -> collect_labels acc i | None -> acc
let rec find_eager_hazard (e : _ Ast.instr) =
let ( <|> ) o f = match o with Some _ -> o | None -> f () in
let descend l =
List.fold_left (fun acc e -> acc <|> fun () -> find_eager_hazard e) None l
in
match e.desc with
| ArrayGet _ | ArraySet _ | StructGet _ | StructSet _ | GetDescriptor _
| NonNull _ | CastDesc _ | ArraySegment _ | Unreachable | Call _ | TailCall _
| Set _ | Tee _ | Throw _ | ThrowRef _ | ContNew _ | ContBind _ | Suspend _
| Resume _ | ResumeThrow _ | ResumeThrowRef _ | Switch _ ->
Some e.info
| BinOp ({ desc = Div (Some _) | Rem _; _ }, _, _) -> Some e.info
| BinOp (_, a, b) -> descend [ a; b ]
| UnOp (_, a)
| Cast (a, _)
| Test (a, _)
| Labelled (_, a)
| ArrayDefault (_, a)
| StructDefaultDesc a
| On (a, _) ->
find_eager_hazard a
| Array (_, a, b) -> descend [ a; b ]
| ArrayFixed (_, l) | Sequence l -> descend l
| Struct (_, fields) -> descend (List.filter_map (fun (_, v) -> v) fields)
| StructDesc (d, fields) ->
find_eager_hazard d <|> fun () ->
descend (List.filter_map (fun (_, v) -> v) fields)
| Let (_, init) -> (
match init with Some e -> find_eager_hazard e | None -> None)
| Get _ | Path _ | Int _ | Float _ | Char _ | String _ | Null | Nop | Hole
| StructDefault _ | Block _ | Loop _ | While _ | If _ | TryTable _ | Try _
| TryCatch _ | Br _ | Br_if _ | Br_table _ | Dispatch _ | Match _
| Br_on_null _ | Br_on_non_null _ | Br_on_cast _ | Br_on_cast_fail _
| Br_on_cast_desc_eq _ | Br_on_cast_desc_eq_fail _ | Return _ | Select _
| If_annotation _ ->
None
let int_literal_value s =
Int64.of_string_opt (String.concat "" (String.split_on_char '_' s))
let rec int_operand_value (e : _ Ast.instr) =
match e.desc with
| Ast.Int s -> int_literal_value s
| UnOp ({ desc = Neg; _ }, { desc = Ast.Int s; _ }) ->
Option.map Int64.neg (int_literal_value s)
| UnOp ({ desc = Pos; _ }, a) -> int_operand_value a
| _ -> None
let int_operand_value_is_zero e = int_operand_value e = Some 0L
let lint_shift ctx op result rhs =
let of_int s =
let bits = String.concat "" (String.split_on_char '_' s) in
if String.starts_with ~prefix:"0x" bits then Int64.of_string_opt bits
else Int64.of_string_opt ("0u" ^ bits)
in
let rec shift_count (e : _ Ast.instr) =
match e.desc with
| Ast.Int s -> of_int s
| UnOp ({ desc = Neg; _ }, { desc = Ast.Int s; _ }) ->
Option.map Int64.neg (of_int s)
| UnOp ({ desc = Pos; _ }, a) -> shift_count a
| _ -> None
in
match op.Annot.desc with
| Shl | Shr _ -> (
match
match Cell.get result with
| Valtype { internal = I32; _ } | Number | Int -> Some 32
| Valtype { internal = I64; _ } | LargeInt -> Some 64
| _ -> None
with
| None -> ()
| Some width -> (
match shift_count rhs with
| Some n when Int64.unsigned_compare n (Int64.of_int width) >= 0 ->
Error.shift_overflow ctx.diagnostics ~location:op.info ~width n
| _ -> ()))
| _ -> ()
let flush_deferred_lints ctx =
List.iter (fun f -> f ()) (List.rev !(ctx.deferred_lints));
ctx.deferred_lints := []
let lint_division ctx (op : (Ast.binop, location) Ast.annotated) rhs =
match op.desc with
| (Div (Some _) | Rem _) when int_operand_value_is_zero rhs ->
Error.division_by_zero ctx.diagnostics ~location:op.info
| _ -> ()
let float_literal_value s =
if String.length s >= 3 && String.equal (String.sub s 0 3) "nan" then
Some Float.nan
else float_of_string_opt (String.concat "" (String.split_on_char '_' s))
let round_to_f32 f = Int32.float_of_bits (Int32.bits_of_float f)
let rec float_operand_value i =
match i.desc with
| Ast.Float s -> float_literal_value s
| UnOp ({ desc = Neg; _ }, { desc = Ast.Float s; _ }) ->
Option.map Float.neg (float_literal_value s)
| UnOp ({ desc = Pos; _ }, e) -> float_operand_value e
| Cast (e, Valtype F32) -> Option.map round_to_f32 (float_operand_value e)
| Cast (e, Valtype F64) -> float_operand_value e
| _ -> None
let float_conversion_traps target signage f =
if not (Float.is_finite f) then true
else
let t = Float.trunc f in
let pow2 n = Float.ldexp 1. n in
match (target, signage) with
| `I32, Signed -> t < -.pow2 31 || t >= pow2 31
| `I32, Unsigned -> t < 0. || t >= pow2 32
| `I64, Signed -> t < -.pow2 63 || t >= pow2 63
| `I64, Unsigned -> t < 0. || t >= pow2 64
let lint_conversion ctx ~location typ operand =
match typ with
| Signedtype { typ = (`I32 | `I64) as target; signage; strict = true } -> (
match float_operand_value operand with
| Some f when float_conversion_traps target signage f ->
Error.conversion_out_of_range ctx.diagnostics ~location
| _ -> ())
| _ -> ()
let identical_operands (l : _ Ast.instr) (r : _ Ast.instr) =
match (l.desc, r.desc) with
| Get a, Get b -> String.equal a.desc b.desc
| _ -> false
let lint_comparison ctx (op : (Ast.binop, location) Ast.annotated)
(l : _ Ast.instr) (r : _ Ast.instr) =
let is_int (e : _ Ast.instr) =
match expression_type_opt e with
| Some c -> (
match Cell.get c with
| Valtype { internal = I32 | I64; _ } -> true
| _ -> false)
| None -> false
in
let tautology =
match op.desc with
| Lt (Some Unsigned) when int_operand_value_is_zero r -> Some false
| Ge (Some Unsigned) when int_operand_value_is_zero r -> Some true
| Gt (Some Unsigned) when int_operand_value_is_zero l -> Some false
| Le (Some Unsigned) when int_operand_value_is_zero l -> Some true
| (Lt (Some _) | Gt (Some _)) when identical_operands l r -> Some false
| (Le (Some _) | Ge (Some _)) when identical_operands l r -> Some true
| Eq when identical_operands l r && is_int l -> Some true
| Ne when identical_operands l r && is_int l -> Some false
| _ -> None
in
match tautology with
| Some value ->
Error.tautological_comparison ctx.diagnostics ~location:op.info ~value
| None -> ()
let is_float_operand e =
match expression_type_opt e with
| Some c -> (
match Cell.get c with
| Valtype { internal = F32 | F64; _ } | Float -> true
| _ -> false)
| None -> false
let lint_redundant ctx (op : (Ast.binop, location) Ast.annotated)
(l : _ Ast.instr) (r : _ Ast.instr) =
let is0 e = int_operand_value e = Some 0L in
let is1 e = int_operand_value e = Some 1L in
let is_int e =
match expression_type_opt e with
| Some c -> (
match Cell.get c with
| Valtype { internal = I32 | I64; _ } -> true
| _ -> false)
| None -> false
in
let is_float = is_float_operand in
let no_effect () =
Error.redundant_operation ctx.diagnostics ~location:op.info
(Wax_utils.Message.text "This operation has no effect on its result.")
in
let always v =
Error.redundant_operation ctx.diagnostics ~location:op.info
Wax_utils.Message.(
(text "This operation always yields" ++ int64 v) ^^ text ".")
in
if is_float l || is_float r then ()
else
match op.desc with
| Add when is0 l || is0 r -> no_effect ()
| (Sub | Shl | Shr _) when is0 r -> no_effect ()
| Mul when is1 l || is1 r -> no_effect ()
| Div (Some _) when is1 r -> no_effect ()
| (Or | Xor) when is0 l || is0 r -> no_effect ()
| (And | Or) when identical_operands l r -> no_effect ()
| Mul when is0 l || is0 r -> always 0L
| And when is0 l || is0 r -> always 0L
| Rem _ when is1 r -> always 0L
| Xor when identical_operands l r -> always 0L
| Sub when identical_operands l r && is_int l -> always 0L
| _ -> ()
let lint_redundant_unop ctx (op : (Ast.unop, location) Ast.annotated)
(e : _ Ast.instr) =
match op.desc with
| Ast.Neg
when (not (is_float_operand e))
&& int_operand_value e = Some 0L
&& match e.Ast.desc with Ast.Int _ -> false | _ -> true ->
Error.redundant_operation ctx.diagnostics ~location:op.info
(Wax_utils.Message.text "This operation has no effect on its result.")
| Neg | Pos | Not -> ()
let lint_condition ctx ?(is_while = false) (cond : _ Ast.instr) =
match int_operand_value cond with
| Some n ->
let value = n <> 0L in
if not (is_while && value) then
Error.constant_condition ctx.diagnostics ~location:cond.info ~value
| None -> ()
let lint_eager_select ctx ~select arm =
match find_eager_hazard arm with
| Some location -> Error.eager_select ctx.diagnostics ~location ~select
| None -> ()
let binop_kind_name = function
| `Shift -> "shift"
| `Arith -> "arithmetic"
| `Bitwise -> "bitwise"
| `Comparison -> "comparison"
let operand_parenthesized ctx ~(op : (Ast.binop, location) Ast.annotated) ~side
(child : _ Ast.instr) =
match Wax_utils.Diagnostic.source ctx.diagnostics with
| None -> true
| Some src ->
let is_space = function ' ' | '\t' | '\n' | '\r' -> true | _ -> false in
let n = String.length src in
let rec skip_fwd i =
if i >= n then i
else if is_space src.[i] then skip_fwd (i + 1)
else if i + 1 < n && src.[i] = '/' && src.[i + 1] = '/' then
let rec eol j = if j >= n || src.[j] = '\n' then j else eol (j + 1) in
skip_fwd (eol (i + 2))
else if i + 1 < n && src.[i] = '/' && src.[i + 1] = '*' then
let rec blk depth j =
if j + 1 >= n then n
else if src.[j] = '/' && src.[j + 1] = '*' then
blk (depth + 1) (j + 2)
else if src.[j] = '*' && src.[j + 1] = '/' then
if depth = 1 then j + 2 else blk (depth - 1) (j + 2)
else blk depth (j + 1)
in
skip_fwd (blk 1 (i + 2))
else i
in
let significant_is c from = from < n && src.[from] = c in
let from =
match side with
| `Right -> skip_fwd op.info.loc_end.pos_cnum
| `Left -> skip_fwd child.info.loc_end.pos_cnum
in
significant_is (match side with `Right -> '(' | `Left -> ')') from
let lint_precedence ctx (op : (binop, location) annotated) e1 e2 =
let outer = Ast_utils.binop_kind op.desc in
List.iter
(fun (child, side) ->
match child.desc with
| BinOp (inner_op, _, _)
when Ast_utils.confusing_precedence outer
(Ast_utils.binop_kind inner_op.desc)
&& not (operand_parenthesized ctx ~op ~side child) ->
let edit =
match Wax_utils.Diagnostic.source ctx.diagnostics with
| Some src ->
let s = child.info.loc_start.pos_cnum
and e = child.info.loc_end.pos_cnum in
if 0 <= s && s <= e && e <= String.length src then
Some
{
Wax_utils.Diagnostic.edit_location = child.info;
new_text = "(" ^ String.sub src s (e - s) ^ ")";
}
else None
| None -> None
in
Error.precedence ?edit ctx.diagnostics ~location:op.info
~inner:inner_op.info ~outer_kind:(binop_kind_name outer)
~inner_kind:(binop_kind_name (Ast_utils.binop_kind inner_op.desc))
| _ -> ())
[ (e1, `Left); (e2, `Right) ]
let rec lint_source ctx (i : _ Ast.instr) =
let list l = List.iter (lint_source ctx) l in
let opt o = Option.iter (lint_source ctx) o in
match i.desc with
| If { cond; if_block; else_block; _ } ->
lint_condition ctx cond;
lint_source ctx cond;
list if_block.desc;
Option.iter (fun b -> list b.Annot.desc) else_block
| While { cond; step; block; _ } ->
lint_condition ctx ~is_while:true cond;
lint_source ctx cond;
opt step;
list block.desc
| Select (c, t, e) ->
lint_condition ctx c;
lint_eager_select ctx ~select:i.info t;
lint_eager_select ctx ~select:i.info e;
lint_source ctx c;
lint_source ctx t;
lint_source ctx e
| Br_if (_, c) ->
let cond =
match c.desc with
| Sequence (_ :: _ as seq) -> List.nth seq (List.length seq - 1)
| _ -> c
in
lint_condition ctx cond;
lint_source ctx c
| Block { block; _ } | Loop { block; _ } | TryTable { block; _ } ->
list block.desc
| Try { block; catches; catch_all; _ } ->
list block.desc;
List.iter (fun (_, b) -> list b.Annot.desc) catches;
Option.iter (fun b -> list b.Annot.desc) catch_all
| TryCatch { block; arms; _ } ->
list block.desc;
List.iter (fun a -> list a.arm_body.desc) arms
| Call (t, args) | TailCall (t, args) ->
lint_source ctx t;
list args
| Set (id, op, e) ->
(match (op, e.desc) with
| None, Get id' when String.equal id.desc id'.desc ->
Error.redundant_operation ctx.diagnostics ~location:i.info
(Wax_utils.Message.text
"This assignment writes the variable back to itself.")
| _ -> ());
lint_source ctx e
| Tee (_, e)
| Labelled (_, e)
| Cast (e, _)
| Test (e, _)
| NonNull e
| StructGet (e, _)
| GetDescriptor e
| StructDefaultDesc e
| UnOp (_, e)
| On (e, _)
| Br_table (_, e)
| Br_on_null (_, e)
| Br_on_non_null (_, e)
| Br_on_cast (_, _, e)
| Br_on_cast_fail (_, _, e)
| ThrowRef e
| ArrayDefault (_, e)
| ContNew (_, e) ->
lint_source ctx e
| Struct (_, fields) ->
List.iter (fun (_, e) -> Option.iter (lint_source ctx) e) fields
| StructDesc (d, fields) ->
lint_source ctx d;
List.iter (fun (_, e) -> Option.iter (lint_source ctx) e) fields
| BinOp (op, e1, e2) ->
lint_precedence ctx op e1 e2;
lint_source ctx e1;
lint_source ctx e2
| CastDesc (e1, _, e2)
| Br_on_cast_desc_eq (_, _, e1, e2)
| Br_on_cast_desc_eq_fail (_, _, e1, e2)
| StructSet (e1, _, e2)
| Array (_, e1, e2)
| ArraySegment (_, _, e1, e2)
| ArrayGet (e1, e2) ->
lint_source ctx e1;
lint_source ctx e2
| ArraySet (e1, e2, e3) ->
lint_source ctx e1;
lint_source ctx e2;
lint_source ctx e3
| ArrayFixed (_, l)
| ContBind (_, _, l)
| Suspend (_, l)
| Resume (_, _, l)
| ResumeThrow (_, _, _, l)
| ResumeThrowRef (_, _, l)
| Switch (_, _, l)
| Throw (_, l)
| Sequence l ->
list l
| Dispatch { index; arms; _ } ->
lint_source ctx index;
List.iter (fun (_, b) -> list b.Annot.desc) arms
| Match { scrutinee; arms; default } ->
lint_source ctx scrutinee;
List.iter (fun (_, b) -> list b.Annot.desc) arms;
list default.desc
| Let (bindings, body) ->
(match (bindings, body) with
| [ (None, _) ], Some e when is_effectless e ->
Error.unused_result ctx.diagnostics ~location:e.info
| _ -> ());
opt body
| Br (_, o) | Return o -> opt o
| If_annotation { then_body; else_body; _ } ->
list then_body.desc;
Option.iter (fun b -> list b.Annot.desc) else_body
| Get _ | Path _ | Unreachable | Nop | Hole | Null | Char _ | String _ | Int _
| Float _ | StructDefault _ ->
()