Skip to content

Commit a71492e

Browse files
committed
Unify non-void C-type checks
array_shift, RW/W (Owned) and sizeof now use the exact same check and error message in wellTyped.ml. Curiously, whilst this does fix the test case in rems-project/cerberus#716, the error message is wholly unsatisfactory because of how steps for quantified resources are defined.
1 parent aa02658 commit a71492e

9 files changed

+39
-63
lines changed

lib/check.ml

+4-2
Original file line numberDiff line numberDiff line change
@@ -1927,7 +1927,8 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
19271927
| To_from_bytes ((To | From), { name = PName _; _ }) ->
19281928
fail (fun _ -> { loc; msg = Byte_conv_needs_owned })
19291929
| To_from_bytes (To, { name = Owned (ct, init); pointer; _ }) ->
1930-
let@ () = WellTyped.owned_ct_ok loc (ct, init) in
1930+
let ctxt = match init with Init -> `RW | Uninit -> `W in
1931+
let@ () = WellTyped.err_if_ct_void loc ctxt ct in
19311932
let@ pointer = WellTyped.infer_term pointer in
19321933
let@ (_, O value), _ =
19331934
RI.Special.predicate_request
@@ -1947,7 +1948,8 @@ let rec check_expr labels (e : BT.t Mu.expr) (k : IT.t -> unit m) : unit m =
19471948
let@ constr = bytes_constraints ~value ~byte_arr ct in
19481949
add_c loc (LC.T constr))
19491950
| To_from_bytes (From, { name = Owned (ct, init); pointer; _ }) ->
1950-
let@ () = WellTyped.owned_ct_ok loc (ct, init) in
1951+
let ctxt = match init with Init -> `RW | Uninit -> `W in
1952+
let@ () = WellTyped.err_if_ct_void loc ctxt ct in
19511953
let@ pointer = WellTyped.infer_term pointer in
19521954
let q_sym = Sym.fresh_named "from_bytes" in
19531955
let@ (_, O byte_arr), _ =

lib/typeErrors.ml

+9-2
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,15 @@ let pp_welltyped = function
315315
| Missing_member m ->
316316
let short = !^"Missing member" ^^^ Id.pp m in
317317
{ short; descr = None; state = None }
318-
| Size_of_void ->
319-
let short = !^"Cannot take the sizeof of a void expression" in
318+
| Void_ctype ctxt ->
319+
let ctxt =
320+
match ctxt with
321+
| `RW -> "RW"
322+
| `W -> "W"
323+
| `Array_shift -> "array_shift"
324+
| `Sizeof -> "sizeof"
325+
in
326+
let short = !^"C-type must not be void in a" ^^^ !^ctxt ^^ angles underscore in
320327
{ short; descr = None; state = None }
321328

322329

lib/wellTyped.ml

+11-27
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type message =
3636
| Empty_pattern
3737
| Redundant_pattern of Pp.document
3838
| Unknown_variable of Sym.t
39-
| Size_of_void
39+
| Void_ctype of [ `Array_shift | `Sizeof | `RW | `W ]
4040

4141
type error =
4242
{ loc : Locations.t;
@@ -232,6 +232,10 @@ let correct_members_sorted_annotated loc spec have =
232232
return have_annotated
233233

234234

235+
let err_if_ct_void loc context ct =
236+
match ct with Sctypes.Void -> fail { loc; msg = Void_ctype context } | _ -> return ()
237+
238+
235239
module WBT = struct
236240
open BT
237241

@@ -837,6 +841,7 @@ module WIT = struct
837841
(* looking at solver mapping *)
838842
return (IT (MemberShift (t, tag, member), BT.Loc (), loc))
839843
| ArrayShift { base; ct; index } ->
844+
let@ () = err_if_ct_void loc `Array_shift ct in
840845
let@ () = WCT.is_ct loc ct in
841846
let@ base = check loc (Loc ()) base in
842847
let@ index = infer index in
@@ -850,11 +855,7 @@ module WIT = struct
850855
let@ ptr = check loc (Loc ()) ptr in
851856
return (IT (HasAllocId ptr, BT.Bool, loc))
852857
| SizeOf ct ->
853-
let@ () =
854-
match ct with
855-
| Sctypes.Void -> fail { loc; msg = Size_of_void }
856-
| _ -> return ()
857-
in
858+
let@ () = err_if_ct_void loc `Sizeof ct in
858859
let@ () = WCT.is_ct loc ct in
859860
let sz = Memory.size_of_ctype ct in
860861
let rs = Option.get (BT.is_bits_bt Memory.size_bt) in
@@ -1069,25 +1070,6 @@ let warn_when_not_quantifier_bt
10691070
^^ dot)
10701071

10711072

1072-
let owned_ct_ok loc (ct, init) =
1073-
let@ () = WCT.is_ct loc ct in
1074-
let pp_resource ct =
1075-
match init with
1076-
| Request.Init -> !^"Owned" ^^ Pp.angles !^ct
1077-
| Request.Uninit -> !^"Block" ^^ Pp.angles !^ct
1078-
in
1079-
match ct with
1080-
| Void ->
1081-
let msg =
1082-
pp_resource "void"
1083-
^^^ !^"is not a valid resource,"
1084-
^^^ !^"please specify another C-type"
1085-
^^^ Pp.parens (!^"using" ^^^ pp_resource "YOURTYPE")
1086-
in
1087-
fail { loc; msg = Generic msg [@alert "-deprecated"] }
1088-
| _ -> return ()
1089-
1090-
10911073
module WReq = struct
10921074
module Req = Request
10931075
open IndexTerms
@@ -1097,7 +1079,9 @@ module WReq = struct
10971079
let@ spec_iargs =
10981080
match Req.get_name r with
10991081
| Owned (ct, init) ->
1100-
let@ () = owned_ct_ok loc (ct, init) in
1082+
let tag = match init with Req.Init -> `RW | Uninit -> `W in
1083+
let@ () = err_if_ct_void loc tag ct in
1084+
let@ () = WCT.is_ct loc ct in
11011085
return []
11021086
| PName name ->
11031087
let@ def = get_resource_predicate_def loc name in
@@ -2642,7 +2626,7 @@ module Lift (M : ErrorReader) : WellTyped_intf.S with type 'a t := 'a M.t = stru
26422626

26432627
let check_ct = lift2 check_ct
26442628

2645-
let owned_ct_ok = lift2 owned_ct_ok
2629+
let err_if_ct_void = lift3 err_if_ct_void
26462630

26472631
let ensure_same_argument_number loc type_ n ~expect =
26482632
let ( let@ ) = M.bind in

lib/wellTyped.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type message =
2626
| Empty_pattern
2727
| Redundant_pattern of Pp.document
2828
| Unknown_variable of Sym.t
29-
| Size_of_void
29+
| Void_ctype of [ `Sizeof | `Array_shift | `RW | `W ]
3030

3131
type error =
3232
{ loc : Locations.t;

lib/wellTyped_intf.ml

+5-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ module type S = sig
1414

1515
val check_ct : Locations.t -> Sctypes.ctype -> unit t
1616

17-
val owned_ct_ok : Locations.t -> Sctypes.ctype * Request.init -> unit t
17+
val err_if_ct_void
18+
: Locations.t ->
19+
[ `Sizeof | `Array_shift | `RW | `W ] ->
20+
Sctypes.ctype ->
21+
unit t
1822

1923
val infer_term : 'bt IndexTerms.annot -> IndexTerms.t t
2024

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,7 @@
1-
return code: 125
1+
return code: 1
22
tests/cn/array_shift_mismatch.error.c:8:19: warning: annotation on array_shift suggests p has type void* but it has type signed int*.
33
ptr_eq(return,array_shift<void>(p, 1u64));
44
^~~~~~~~~~~~~~~~~~~~~~~~~~
5-
internal error: size_of_ctype applied to void
6-
cn: internal error, uncaught exception:
7-
Failure("internal error: size_of_ctype applied to void")
8-
Raised at Stdlib.failwith in file "stdlib.ml", line 29, characters 17-33
9-
Called from Cn__Solver.translate_term in file "lib/solver.ml", line 947, characters 32-57
10-
Called from Cn__Solver.translate_term in file "lib/solver.ml", line 753, characters 13-32
11-
Called from Cn__Solver.add_assumption in file "lib/solver.ml", line 1105, characters 46-68
12-
Called from Cn__Typing.add_c_internal in file "lib/typing.ml", line 409, characters 11-51
13-
Called from Cn__Typing.bind.(fun) in file "lib/typing.ml", line 49, characters 17-20
14-
Called from Cn__Typing.bind.(fun) in file "lib/typing.ml", line 49, characters 17-20
15-
Called from Cn__Typing.pure.(fun) in file "lib/typing.ml", line 76, characters 22-25
16-
Called from Cn__Typing.pure.(fun) in file "lib/typing.ml", line 76, characters 22-25
17-
Called from Cn__Typing.pure.(fun) in file "lib/typing.ml", line 76, characters 22-25
18-
Called from Cn__Typing.pure.(fun) in file "lib/typing.ml", line 76, characters 22-25
19-
Called from Cn__Typing.pure.(fun) in file "lib/typing.ml", line 76, characters 22-25
20-
Called from Cn__Typing.bind.(fun) in file "lib/typing.ml", line 49, characters 17-20
21-
Called from Cn__Typing.bind.(fun) in file "lib/typing.ml", line 49, characters 17-20
22-
Called from Cn__Typing.bind.(fun) in file "lib/typing.ml", line 49, characters 17-20
23-
Called from Cn__Typing.run_from_pause in file "lib/typing.ml", line 68, characters 50-55
24-
Called from Dune__exe__Main.with_well_formedness_check in file "bin/main.ml", line 187, characters 15-69
25-
Called from Dune__exe__Main.with_well_formedness_check in file "bin/main.ml", lines 176-204, characters 6-15
26-
Re-raised at Dune__exe__Main.with_well_formedness_check in file "bin/main.ml", line 212, characters 4-69
27-
Called from Cmdliner_term.app.(fun) in file "cmdliner_term.ml", line 24, characters 19-24
28-
Called from Cmdliner_eval.run_parser in file "cmdliner_eval.ml", line 35, characters 37-44
5+
tests/cn/array_shift_mismatch.error.c:8:19: error: C-type must not be void in a array_shift<_>
6+
ptr_eq(return,array_shift<void>(p, 1u64));
7+
^~~~~~~~~~~~~~~~~~~~~~~~~~
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
return code: 1
2-
other location (File "lib/compile.ml", line 1144, characters 33-40) error: Cannot take the sizeof of a void expression
2+
other location (File "lib/compile.ml", line 1145, characters 33-40) error: C-type must not be void in a sizeof<_>
33

tests/cn/tree16/as_mutual_dt/tree16.c.verify

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ tests/cn/tree16/as_mutual_dt/tree16.c:111:19: warning: 'each' prefers a 'u64', b
1111
tests/cn/tree16/as_mutual_dt/tree16.c:121:18: warning: 'each' prefers a 'u64', but 'j' has type 'i32'.
1212
take Xs2 = each (i32 j; (0i32 <= j) && (j < path_len))
1313
^
14-
other location (File "lib/compile.ml", line 1630, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&i1' has type 'i32'.
14+
other location (File "lib/compile.ml", line 1631, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&i1' has type 'i32'.
1515

16-
other location (File "lib/compile.ml", line 1630, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&idx0' has type 'i32'.
16+
other location (File "lib/compile.ml", line 1631, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&idx0' has type 'i32'.
1717

1818
[1/1]: lookup_rec -- pass

tests/cn/tree16/as_partial_map/tree16.c.verify

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ tests/cn/tree16/as_partial_map/tree16.c:137:19: warning: 'each' prefers a 'u64',
1717
tests/cn/tree16/as_partial_map/tree16.c:146:18: warning: 'each' prefers a 'u64', but 'j' has type 'i32'.
1818
take Xs2 = each (i32 j; (0i32 <= j) && (j < path_len))
1919
^
20-
other location (File "lib/compile.ml", line 1630, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&i2' has type 'i32'.
20+
other location (File "lib/compile.ml", line 1631, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&i2' has type 'i32'.
2121

22-
other location (File "lib/compile.ml", line 1630, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&idx0' has type 'i32'.
22+
other location (File "lib/compile.ml", line 1631, characters 38-45) warning: 'focus' prefers a 'u64', but 'read_&idx0' has type 'i32'.
2323

2424
[1/2]: cn_get_num_nodes -- pass
2525
[2/2]: lookup_rec -- pass

0 commit comments

Comments
 (0)