Skip to content

Commit c58132a

Browse files
authored
Issue misplaced attribute warning earlier (#2155)
1 parent 547f134 commit c58132a

12 files changed

+103
-2
lines changed

driver/optcompile.ml

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ let compile i typed ~transl_style ~unix ~pipeline =
3939
|> Compiler_hooks.execute_and_pipe Compiler_hooks.Raw_lambda
4040
|> Profile.(record generate)
4141
(fun program ->
42+
Builtin_attributes.warn_unused ();
4243
let code = Simplif.simplify_lambda program.Lambda.code in
4344
{ program with Lambda.code }
4445
|> print_if i.ppf_dump Clflags.dump_lambda Printlambda.program

ocaml/driver/compile.ml

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ let to_bytecode i Typedtree.{structure; coercion; _} =
3636
(Translmod.transl_implementation i.module_name ~style:Set_global_to_block)
3737
|> Profile.(record ~accumulate:true generate)
3838
(fun { Lambda.code = lambda; required_globals } ->
39+
Builtin_attributes.warn_unused ();
3940
lambda
4041
|> print_if i.ppf_dump Clflags.dump_rawlambda Printlambda.lambda
4142
|> Simplif.simplify_lambda

ocaml/driver/compile_common.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,6 @@ let implementation ~hook_parse_tree ~hook_typed_tree info ~backend =
139139
backend info typed
140140
end;
141141
end;
142-
Builtin_attributes.warn_unused ();
142+
Builtin_attributes.warn_unchecked_property ();
143143
Warnings.check_fatal ();
144144
)

ocaml/driver/optcompile.ml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ let compile i ~backend ~middle_end ~transl_style
4040
|> print_if i.ppf_dump Clflags.dump_rawlambda Printlambda.program
4141
|> Profile.(record generate)
4242
(fun program ->
43+
Builtin_attributes.warn_unused ();
4344
let code = Simplif.simplify_lambda program.Lambda.code in
4445
{ program with Lambda.code }
4546
|> print_if i.ppf_dump Clflags.dump_lambda Printlambda.program

ocaml/parsing/builtin_attributes.ml

+4-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ let mark_property_checked txt loc =
3838
let register_property attr =
3939
Attribute_table.replace unchecked_properties attr ()
4040
let warn_unchecked_property () =
41+
(* When using -i, attributes will not have been translated, so we can't
42+
warn about missing ones. *)
43+
if !Clflags.print_types then ()
44+
else
4145
let keys = List.of_seq (Attribute_table.to_seq_keys unchecked_properties) in
4246
let keys = List.sort attr_order keys in
4347
List.iter (fun sloc ->
@@ -50,7 +54,6 @@ let warn_unused () =
5054
if !Clflags.print_types then ()
5155
else
5256
begin
53-
warn_unchecked_property ();
5457
let keys = List.of_seq (Attribute_table.to_seq_keys unused_attrs) in
5558
let keys = List.sort attr_order keys in
5659
List.iter (fun sloc ->

ocaml/parsing/builtin_attributes.mli

+1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ val mark_payload_attrs_used : Parsetree.payload -> unit
9292
(** Issue misplaced attribute warnings for all attributes created with
9393
[mk_internal] but not yet marked used. *)
9494
val warn_unused : unit -> unit
95+
val warn_unchecked_property : unit -> unit
9596

9697
val check_alerts: Location.t -> Parsetree.attributes -> string -> unit
9798
val check_alerts_inclusion:

tests/backend/checkmach/dune.inc

+38
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,41 @@
597597
(enabled_if (= %{context_name} "main"))
598598
(deps test_assume_fail.output test_assume_fail.output.corrected)
599599
(action (diff test_assume_fail.output test_assume_fail.output.corrected)))
600+
601+
(rule
602+
(enabled_if (= %{context_name} "main"))
603+
(targets test_assume_on_call.output.corrected)
604+
(deps (:ml test_assume_on_call.ml) filter.sh)
605+
(action
606+
(with-outputs-to test_assume_on_call.output.corrected
607+
(pipe-outputs
608+
(with-accepted-exit-codes 2
609+
(run %{bin:ocamlopt.opt} %{ml} -g -color never -error-style short -c
610+
-zero-alloc-check default -checkmach-details-cutoff 20 -O3))
611+
(run "./filter.sh")
612+
))))
613+
614+
(rule
615+
(alias runtest)
616+
(enabled_if (= %{context_name} "main"))
617+
(deps test_assume_on_call.output test_assume_on_call.output.corrected)
618+
(action (diff test_assume_on_call.output test_assume_on_call.output.corrected)))
619+
620+
(rule
621+
(enabled_if (= %{context_name} "main"))
622+
(targets test_misplaced_assume.output.corrected)
623+
(deps (:ml test_misplaced_assume.ml) filter.sh)
624+
(action
625+
(with-outputs-to test_misplaced_assume.output.corrected
626+
(pipe-outputs
627+
(with-accepted-exit-codes 2
628+
(run %{bin:ocamlopt.opt} %{ml} -g -color never -error-style short -c
629+
-zero-alloc-check default -checkmach-details-cutoff 20 -O3))
630+
(run "./filter.sh")
631+
))))
632+
633+
(rule
634+
(alias runtest)
635+
(enabled_if (= %{context_name} "main"))
636+
(deps test_misplaced_assume.output test_misplaced_assume.output.corrected)
637+
(action (diff test_misplaced_assume.output test_misplaced_assume.output.corrected)))

tests/backend/checkmach/gen/gen_dune.ml

+2
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,6 @@ let () =
125125
print_test ~extra_flags:"-zero-alloc-check opt" ~flambda_only:false "test_zero_alloc_opt1.ml";
126126
print_test ~extra_flags:"-zero-alloc-check opt" ~flambda_only:false "test_zero_alloc_opt2.ml";
127127
print_test_expected_output ~cutoff:default_cutoff ~flambda_only:false ~extra_dep:None ~exit_code:2 "test_assume_fail";
128+
print_test_expected_output ~cutoff:default_cutoff ~flambda_only:false ~extra_dep:None ~exit_code:2 "test_assume_on_call";
129+
print_test_expected_output ~cutoff:default_cutoff ~flambda_only:false ~extra_dep:None ~exit_code:2 "test_misplaced_assume";
128130
()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
let[@inline always] bar x = (x,x)
2+
3+
(* The following functions currently report misplaced attribute warnings.
4+
They are expected to compile successfully and pass the check
5+
when zero_alloc annotations on function calls are supported. *)
6+
7+
let[@zero_alloc] test1 x =
8+
(bar[@zero_alloc]) x
9+
10+
let[@zero_alloc] test2 x =
11+
(bar[@zero_alloc assume strict]) x
12+
13+
let[@zero_alloc] test3 x =
14+
(bar[@zero_alloc assume never_returns_normally]) x
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
File "test_assume_on_call.ml", line 8, characters 8-18:
2+
Warning 53 [misplaced-attribute]: the "zero_alloc" attribute cannot appear in this context
3+
4+
File "test_assume_on_call.ml", line 11, characters 8-18:
5+
Warning 53 [misplaced-attribute]: the "zero_alloc" attribute cannot appear in this context
6+
7+
File "test_assume_on_call.ml", line 14, characters 8-18:
8+
Warning 53 [misplaced-attribute]: the "zero_alloc" attribute cannot appear in this context
9+
10+
File "test_assume_on_call.ml", line 7, characters 5-15:
11+
Error: Annotation check for zero_alloc failed on function Test_assume_on_call.test1 (camlTest_assume_on_call.test1_HIDE_STAMP)
12+
13+
File "test_assume_on_call.ml", line 8, characters 2-22:
14+
Error: Unexpected allocation of 24 bytes (test_assume_on_call.ml:8,2--22;test_assume_on_call.ml:1,28--33)
15+
16+
File "test_assume_on_call.ml", line 10, characters 5-15:
17+
Error: Annotation check for zero_alloc failed on function Test_assume_on_call.test2 (camlTest_assume_on_call.test2_HIDE_STAMP)
18+
19+
File "test_assume_on_call.ml", line 11, characters 2-36:
20+
Error: Unexpected allocation of 24 bytes (test_assume_on_call.ml:11,2--36;test_assume_on_call.ml:1,28--33)
21+
22+
File "test_assume_on_call.ml", line 13, characters 5-15:
23+
Error: Annotation check for zero_alloc failed on function Test_assume_on_call.test3 (camlTest_assume_on_call.test3_HIDE_STAMP)
24+
25+
File "test_assume_on_call.ml", line 14, characters 2-52:
26+
Error: Unexpected allocation of 24 bytes (test_assume_on_call.ml:14,2--52;test_assume_on_call.ml:1,28--33)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[@@@warnings "+53"]
2+
3+
let[@inline never] bar x = (x,x)
4+
5+
let[@zero_alloc] foo x =
6+
((bar x)[@zero_alloc assume])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
File "test_misplaced_assume.ml", line 6, characters 12-22:
2+
Warning 53 [misplaced-attribute]: the "zero_alloc" attribute cannot appear in this context
3+
4+
File "test_misplaced_assume.ml", line 5, characters 5-15:
5+
Error: Annotation check for zero_alloc failed on function Test_misplaced_assume.foo (camlTest_misplaced_assume.foo_HIDE_STAMP)
6+
7+
File "test_misplaced_assume.ml", line 6, characters 2-31:
8+
Error: Unexpected direct tailcall camlTest_misplaced_assume.bar_HIDE_STAMP

0 commit comments

Comments
 (0)