Skip to content

Commit 2dbe82d

Browse files
authored
Merge pull request #1525 from Nadrieril/predicates-on-binders
Add assumptions on binders
2 parents 93a0a63 + 26abff0 commit 2dbe82d

33 files changed

Lines changed: 661 additions & 197 deletions

‎AGENTS.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ Guidance for AI agents working on Charon.
8181
- Prefer precise flags over using `--preset` as this makes more obvious which pass is at fault.
8282
- Changes to the generated output are to be double-checked: they can often indicate a bug.
8383
Well-motivated changes are however totally fine.
84+
- Do not add unit or `crate_data` tests for things that can be observed in the ui tests outputs.
8485
- Always do a full `make test && make clippy` at the end of a change.
8586

8687
## OCaml vs Rust

‎charon-ml/src/CharonVersion.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
(* This is an automatically generated file, generated from `charon/Cargo.toml`. *)
22
(* To re-generate this file, rune `make` in the root directory *)
3-
let supported_charon_version = "0.1.286"
3+
let supported_charon_version = "0.1.287"

‎charon-ml/src/NameMatcher.ml‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -747,7 +747,12 @@ let match_fn_ptr (ctx : ctx) (c : match_config) (p : pattern) (func : T.fn_ptr)
747747
(* TODO: recover the method generics somehow *)
748748
let method_generics = TypesUtils.empty_generic_args in
749749
match_trait_decl_ref_item ctx c (mk_empty_maps ()) p
750-
{ binder_value = trait_ref; binder_regions = [] }
750+
{
751+
binder_value = trait_ref;
752+
binder_regions = [];
753+
regions_outlive = [];
754+
types_outlive = [];
755+
}
751756
(T.AssocIdMethod item_id) method_generics
752757
| _ -> false
753758
in

‎charon-ml/src/Print.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ let region_to_string env r = PrintFmt.region_to_string env r
116116

117117
let region_binder_to_string value_to_string env rb =
118118
PrintFmt.pp_to_string (fun fmt ->
119-
PrintFmt.pp_region_binder
119+
PrintFmt.pp_region_binder PrintFmt.ty_to_string
120120
(fun env fmt value ->
121121
Format.pp_print_string fmt (value_to_string env value))
122122
env fmt rb)

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -341,23 +341,55 @@ let region_to_string (env : fmt_env) (r : region) : string =
341341
| RBody id -> "'" ^ RegionId.to_string id
342342
| RVar var -> region_db_var_to_string env var
343343

344-
let pp_region_binder (pp_value : fmt_env -> Format.formatter -> 'c -> unit)
345-
(env : fmt_env) (fmt : Format.formatter) (rb : 'c region_binder) : unit =
344+
let rec region_binder_params_to_string :
345+
'a. (fmt_env -> ty -> string) -> fmt_env -> 'a region_binder -> string =
346+
fun ty_to_string env rb ->
347+
let regions = List.map (region_param_to_string env) rb.binder_regions in
348+
let types_outlive =
349+
List.map
350+
(region_binder_to_string ty_to_string
351+
(fun env (ty, region) ->
352+
ty_to_string env ty ^ ": " ^ region_to_string env region)
353+
env)
354+
rb.types_outlive
355+
in
356+
let regions_outlive =
357+
List.map
358+
(region_binder_to_string ty_to_string
359+
(fun env (r1, r2) ->
360+
region_to_string env r1 ^ ": " ^ region_to_string env r2)
361+
env)
362+
rb.regions_outlive
363+
in
364+
let predicates = types_outlive @ regions_outlive in
365+
String.concat ", " regions
366+
^
367+
if predicates = [] then ""
368+
else
369+
(if regions = [] then "" else " ")
370+
^ "where "
371+
^ String.concat ", " predicates
372+
373+
and region_binder_to_string :
374+
'a.
375+
(fmt_env -> ty -> string) ->
376+
(fmt_env -> 'a -> string) ->
377+
fmt_env ->
378+
'a region_binder ->
379+
string =
380+
fun ty_to_string value_to_string env rb ->
381+
let env = fmt_env_push_regions env rb.binder_regions in
382+
let params = region_binder_params_to_string ty_to_string env rb in
383+
(if params = "" then "" else "for<" ^ params ^ "> ")
384+
^ value_to_string env rb.binder_value
385+
386+
let pp_region_binder ty_to_string
387+
(pp_value : fmt_env -> Format.formatter -> 'c -> unit) (env : fmt_env)
388+
(fmt : Format.formatter) (rb : 'c region_binder) : unit =
346389
let env = fmt_env_push_regions env rb.binder_regions in
347-
match rb.binder_regions with
348-
| [] -> pp_value env fmt rb.binder_value
349-
| _ ->
350-
Format.fprintf fmt "for<%a> %a"
351-
(pp_sep_list ", " (fun fmt region ->
352-
pp_string fmt (region_param_to_string env region)))
353-
rb.binder_regions (pp_value env) rb.binder_value
354-
355-
let region_binder_to_string (value_to_string : fmt_env -> 'c -> string)
356-
(env : fmt_env) (rb : 'c region_binder) : string =
357-
pp_to_string (fun fmt ->
358-
pp_region_binder
359-
(fun env fmt value -> pp_string fmt (value_to_string env value))
360-
env fmt rb)
390+
let params = region_binder_params_to_string ty_to_string env rb in
391+
if params <> "" then Format.fprintf fmt "for<%s> " params;
392+
pp_value env fmt rb.binder_value
361393

362394
let abi_name (abi : abi) : string =
363395
match abi with
@@ -635,11 +667,8 @@ and pp_ty (env : fmt_env) (fmt : Format.formatter) (ty : ty) : unit =
635667
let unsafe = if is_unsafe then "unsafe " else "" in
636668
let abi = abi_prefix abi in
637669
Format.fprintf fmt "%s%sfn" unsafe abi;
638-
if binder.binder_regions <> [] then
639-
Format.fprintf fmt "<%a>"
640-
(pp_sep_list ", " (fun fmt region ->
641-
pp_string fmt (region_param_to_string env region)))
642-
binder.binder_regions;
670+
let params = region_binder_params_to_string ty_to_string env binder in
671+
if params <> "" then Format.fprintf fmt "<%s>" params;
643672
if is_variadic && inputs = [] then Format.fprintf fmt "(...)"
644673
else if is_variadic then
645674
Format.fprintf fmt "(%a, ...)" (pp_sep_list ", " (pp_ty env)) inputs
@@ -648,11 +677,8 @@ and pp_ty (env : fmt_env) (fmt : Format.formatter) (ty : ty) : unit =
648677
Format.fprintf fmt " -> %a" (pp_ty env) output
649678
| TFnDef f ->
650679
let env = fmt_env_push_regions env f.binder_regions in
651-
if f.binder_regions <> [] then
652-
Format.fprintf fmt "for<%a> "
653-
(pp_sep_list ", " (fun fmt region ->
654-
pp_string fmt (region_param_to_string env region)))
655-
f.binder_regions;
680+
let params = region_binder_params_to_string ty_to_string env f in
681+
if params <> "" then Format.fprintf fmt "for<%s> " params;
656682
pp_fn_ptr env fmt f.binder_value
657683
| TDynTrait pred -> Format.fprintf fmt "(dyn %a)" (pp_dyn_predicate env) pred
658684
| TArray (ty, len, _) ->
@@ -722,7 +748,7 @@ and pp_dyn_trait_clause (env : fmt_env)
722748
if params <> [] then
723749
Format.fprintf fmt "<%a>" (pp_sep_list ", " pp_string) params
724750
in
725-
pp_region_binder pp_trait env fmt clause.trait
751+
pp_region_binder ty_to_string pp_trait env fmt clause.trait
726752

727753
and dyn_trait_clause_to_string env constraints clause =
728754
pp_to_string (fun fmt -> pp_dyn_trait_clause env constraints clause fmt)
@@ -733,7 +759,7 @@ and dyn_types_outlive_to_string (env : fmt_env)
733759
| _, RErased -> None
734760
| _, region ->
735761
Some
736-
(region_binder_to_string
762+
(region_binder_to_string ty_to_string
737763
(fun env (_, region) -> region_to_string env region)
738764
env rb)
739765

@@ -798,7 +824,7 @@ and pp_trait_ref_kind (env : fmt_env)
798824
let implements = Option.get implements in
799825
let types = AssocTypeId.Map.to_list types in
800826
Format.fprintf fmt "{built_in impl %a"
801-
(pp_region_binder pp_trait_decl_ref_as_impl env)
827+
(pp_region_binder ty_to_string pp_trait_decl_ref_as_impl env)
802828
implements;
803829
if types <> [] then
804830
Format.fprintf fmt " where %a"
@@ -823,7 +849,9 @@ and pp_trait_ref_kind (env : fmt_env)
823849
Format.fprintf fmt "%a::%s%a::%s" (pp_trait_ref env) tref type_name
824850
(pp_generic_args env) generics
825851
(trait_clause_id_format_as_implied clause_id)
826-
| Dyn -> pp_region_binder pp_trait_decl_ref env fmt (Option.get implements)
852+
| Dyn ->
853+
pp_region_binder ty_to_string pp_trait_decl_ref env fmt
854+
(Option.get implements)
827855
| UnknownTrait msg -> Format.fprintf fmt "UNKNOWN(%s)" msg
828856

829857
and pp_trait_ref (env : fmt_env) (fmt : Format.formatter) (tr : trait_ref) :
@@ -858,7 +886,7 @@ and pp_trait_decl_ref_as_pred (env : fmt_env) (fmt : Format.formatter)
858886

859887
and pp_poly_trait_decl_ref_as_pred (env : fmt_env) (fmt : Format.formatter)
860888
(tr : trait_decl_ref region_binder) : unit =
861-
pp_region_binder
889+
pp_region_binder ty_to_string
862890
(fun env fmt tr ->
863891
Format.fprintf fmt "(%a)" (pp_trait_decl_ref_as_pred env) tr)
864892
env fmt tr
@@ -1058,7 +1086,7 @@ let generic_clauses_to_strings (env : fmt_env) (generics : generic_params) :
10581086
List.mapi
10591087
(fun i rb ->
10601088
"RegionOutlives" ^ string_of_int i ^ ": "
1061-
^ region_binder_to_string outlive_to_string env rb)
1089+
^ region_binder_to_string ty_to_string outlive_to_string env rb)
10621090
generics.regions_outlive
10631091
in
10641092
let types_outlive =
@@ -1068,14 +1096,15 @@ let generic_clauses_to_strings (env : fmt_env) (generics : generic_params) :
10681096
List.mapi
10691097
(fun i rb ->
10701098
"TypeOutlives" ^ string_of_int i ^ ": "
1071-
^ region_binder_to_string outlive_to_string env rb)
1099+
^ region_binder_to_string ty_to_string outlive_to_string env rb)
10721100
generics.types_outlive
10731101
in
10741102
let trait_type_constraints =
10751103
List.mapi
10761104
(fun i rb ->
10771105
"TypeConstraint" ^ string_of_int i ^ ": "
1078-
^ region_binder_to_string trait_type_constraint_to_string env rb)
1106+
^ region_binder_to_string ty_to_string trait_type_constraint_to_string
1107+
env rb)
10791108
generics.trait_type_constraints
10801109
in
10811110
List.concat

‎charon-ml/src/Substitute.ml‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,14 +186,28 @@ let st_substitute_visitor =
186186

187187
method! visit_region_binder visit_value (subst : subst) x =
188188
(* Note that we don't visit the bound variables. *)
189-
let { binder_regions; binder_value } = x in
189+
let { binder_regions; regions_outlive; types_outlive; binder_value } =
190+
x
191+
in
190192
(* Crucial: we shift the substitution to be valid under this binder. *)
191193
let subst = shift_subst subst in
192194
let binder_regions =
193195
self#visit_list self#visit_region_param subst binder_regions
194196
in
197+
let regions_outlive =
198+
self#visit_list
199+
(self#visit_region_binder
200+
(self#visit_outlives_pred self#visit_region self#visit_region))
201+
subst regions_outlive
202+
in
203+
let types_outlive =
204+
self#visit_list
205+
(self#visit_region_binder
206+
(self#visit_outlives_pred self#visit_ty self#visit_region))
207+
subst types_outlive
208+
in
195209
let binder_value = visit_value subst binder_value in
196-
{ binder_regions; binder_value }
210+
{ binder_regions; regions_outlive; types_outlive; binder_value }
197211

198212
method! visit_RVar (subst : subst) var = subst.r_subst var
199213
method! visit_TVar (subst : subst) var = subst.ty_subst var
@@ -694,7 +708,13 @@ let lookup_fndef_sig (crate : crate) (fn_ptr : fn_ptr region_binder) :
694708
fun_decl.signature
695709
in
696710
(* Rebind everything *)
697-
Some { binder_regions = fn_ptr.binder_regions; binder_value = fn_sig }
711+
Some
712+
{
713+
binder_regions = fn_ptr.binder_regions;
714+
regions_outlive = fn_ptr.regions_outlive;
715+
types_outlive = fn_ptr.types_outlive;
716+
binder_value = fn_sig;
717+
}
698718
| _ -> None
699719

700720
(* Construct a set of generic arguments in the scope of `params` that matches
@@ -733,4 +753,6 @@ let trait_decl_ref_to_poly_trait_decl_ref (pred : trait_decl_ref) :
733753
binder_value =
734754
st_substitute_visitor#visit_trait_decl_ref move_under_binder_subst pred;
735755
binder_regions = [];
756+
regions_outlive = [];
757+
types_outlive = [];
736758
}

‎charon-ml/src/generated/Generated_OfJson.ml‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,12 +1242,32 @@ and region_binder_of_json :
12421242
fun arg0_of_json ctx js ->
12431243
combine_error_msgs js __FUNCTION__
12441244
(match js with
1245-
| `Assoc [ ("regions", regions); ("skip_binder", skip_binder) ] ->
1245+
| `Assoc
1246+
[
1247+
("regions", regions);
1248+
("regions_outlive", regions_outlive);
1249+
("types_outlive", types_outlive);
1250+
("skip_binder", skip_binder);
1251+
] ->
12461252
let* binder_regions =
12471253
index_vec_of_json region_id_of_json region_param_of_json ctx regions
12481254
in
1255+
let* regions_outlive =
1256+
list_of_json
1257+
(region_binder_of_json
1258+
(outlives_pred_of_json region_of_json region_of_json))
1259+
ctx regions_outlive
1260+
in
1261+
let* types_outlive =
1262+
list_of_json
1263+
(region_binder_of_json
1264+
(outlives_pred_of_json ty_of_json region_of_json))
1265+
ctx types_outlive
1266+
in
12491267
let* binder_value = arg0_of_json ctx skip_binder in
1250-
Ok ({ binder_regions; binder_value } : _ region_binder)
1268+
Ok
1269+
({ binder_regions; regions_outlive; types_outlive; binder_value }
1270+
: _ region_binder)
12511271
| _ -> Error "")
12521272

12531273
and region_id_of_json (ctx : of_json_ctx) (js : json) :

‎charon-ml/src/generated/Generated_OfPostcard.ml‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1136,8 +1136,22 @@ and region_binder_of_postcard :
11361136
index_vec_of_postcard region_id_of_postcard region_param_of_postcard ctx
11371137
st
11381138
in
1139+
let* regions_outlive =
1140+
list_of_postcard
1141+
(region_binder_of_postcard
1142+
(outlives_pred_of_postcard region_of_postcard region_of_postcard))
1143+
ctx st
1144+
in
1145+
let* types_outlive =
1146+
list_of_postcard
1147+
(region_binder_of_postcard
1148+
(outlives_pred_of_postcard ty_of_postcard region_of_postcard))
1149+
ctx st
1150+
in
11391151
let* binder_value = arg0_of_postcard ctx st in
1140-
Ok ({ binder_regions; binder_value } : _ region_binder))
1152+
Ok
1153+
({ binder_regions; regions_outlive; types_outlive; binder_value }
1154+
: _ region_binder))
11411155

11421156
and region_id_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
11431157
(region_id, string) result =

‎charon-ml/src/generated/Generated_Types.ml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,10 @@ and region =
456456
this causes name clash issues in the derived ocaml visitors. *)
457457
and 'a0 region_binder = {
458458
binder_regions : region_param list;
459+
regions_outlive : (region, region) outlives_pred region_binder list;
460+
(** Outlives relations involving the bound regions. *)
461+
types_outlive : (ty, region) outlives_pred region_binder list;
462+
(** Outlives relations involving the bound regions. *)
459463
binder_value : 'a0;
460464
(** Named this way to highlight accesses to the inner value that might be
461465
handling parameters incorrectly. Prefer using helper methods. *)

‎charon/Cargo.lock‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)