Skip to content

Commit 20406cb

Browse files
authored
Merge pull request #1527 from Nadrieril/hrtb1
Distinguish higher-ranked trait proofs
2 parents 41ad011 + 8492d0c commit 20406cb

51 files changed

Lines changed: 679 additions & 435 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎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.287"
3+
let supported_charon_version = "0.1.288"

‎charon-ml/src/NameMatcher.ml‎

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -641,18 +641,15 @@ and match_expr_with_trait_impl_id (ctx : ctx) (c : match_config) (ptr : expr)
641641
| EPrimAdt _ | ERef _ | EVar _ | ENever | EArrow _ | ERawPtr _ -> false
642642

643643
and match_trait_decl_ref (ctx : ctx) (c : match_config) (m : maps)
644-
(pid : pattern) (tr : T.trait_decl_ref T.region_binder) : bool =
644+
(pid : pattern) (tr : T.trait_decl_ref) : bool =
645645
(* Lookup the trait declaration *)
646-
let d = T.TraitDeclId.Map.find tr.binder_value.id ctx.crate.trait_decls in
647-
(* Push a region group in the map, if necessary - TODO: make this more precise *)
648-
let m = maps_push_bound_regions_group_if_nonempty m tr.binder_regions in
646+
let d = T.TraitDeclId.Map.find tr.id ctx.crate.trait_decls in
649647
(* Match the trait decl ref *)
650-
match_name_with_generics ctx c ~m pid d.item_meta.name
651-
tr.binder_value.generics
648+
match_name_with_generics ctx c ~m pid d.item_meta.name tr.generics
652649

653650
and match_trait_decl_ref_item (ctx : ctx) (c : match_config) (m : maps)
654-
(pid : pattern) (tr : T.trait_decl_ref T.region_binder)
655-
(item_id : T.assoc_item_id) (generics : T.generic_args) : bool =
651+
(pid : pattern) (tr : T.trait_decl_ref) (item_id : T.assoc_item_id)
652+
(generics : T.generic_args) : bool =
656653
if c.match_with_trait_decl_refs then
657654
(* We match the trait decl ref *)
658655
(* We split the pattern between the trait decl ref and the associated item name *)
@@ -663,9 +660,7 @@ and match_trait_decl_ref_item (ctx : ctx) (c : match_config) (m : maps)
663660
(* Match the item name *)
664661
match pitem_name with
665662
| PIdent (pitem_name, pd, pgenerics) ->
666-
let item_name =
667-
GAstUtils.get_assoc_item_name ctx.crate tr.binder_value.id item_id
668-
in
663+
let item_name = GAstUtils.get_assoc_item_name ctx.crate tr.id item_id in
669664
pitem_name = item_name && pd = 0
670665
&& match_generic_args ctx c (mk_empty_maps ()) pgenerics generics
671666
| PWild -> true
@@ -746,13 +741,7 @@ let match_fn_ptr (ctx : ctx) (c : match_config) (p : pattern) (func : T.fn_ptr)
746741
in
747742
(* TODO: recover the method generics somehow *)
748743
let method_generics = TypesUtils.empty_generic_args in
749-
match_trait_decl_ref_item ctx c (mk_empty_maps ()) p
750-
{
751-
binder_value = trait_ref;
752-
binder_regions = [];
753-
regions_outlive = [];
754-
types_outlive = [];
755-
}
744+
match_trait_decl_ref_item ctx c (mk_empty_maps ()) p trait_ref
756745
(T.AssocIdMethod item_id) method_generics
757746
| _ -> false
758747
in
@@ -1009,8 +998,8 @@ and ty_to_pattern_aux (ctx : ctx) (c : to_pat_config) (m : constraints)
1009998
ref_kind_to_pattern rk )
1010999
| TTraitType (trait_ref, type_id, generics) ->
10111000
let type_name =
1012-
GAstUtils.get_assoc_type_name ctx.crate
1013-
trait_ref.trait_decl_ref.binder_value.id type_id
1001+
GAstUtils.get_assoc_type_name ctx.crate trait_ref.trait_decl_ref.id
1002+
type_id
10141003
in
10151004
let name =
10161005
trait_ref_item_with_generics_to_pattern ctx c m trait_ref type_name
@@ -1055,18 +1044,10 @@ and trait_ref_item_with_generics_to_pattern (ctx : ctx) (c : to_pat_config)
10551044
(item_generics : T.generic_args) : pattern =
10561045
if c.use_trait_decl_refs then
10571046
let trait_decl_ref = trait_ref.trait_decl_ref in
1058-
let d =
1059-
T.TraitDeclId.Map.find trait_decl_ref.binder_value.id
1060-
ctx.crate.trait_decls
1061-
in
1062-
(* Push a regions map if necessary - TODO: make this more precise *)
1063-
let m =
1064-
constraints_map_push_regions_map_if_nonempty m
1065-
trait_decl_ref.binder_regions
1066-
in
1047+
let d = T.TraitDeclId.Map.find trait_decl_ref.id ctx.crate.trait_decls in
10671048
let name =
10681049
name_with_generics_to_pattern_aux ctx c m d.item_meta.name
1069-
trait_decl_ref.binder_value.generics
1050+
trait_decl_ref.generics
10701051
in
10711052
let item_generics = generic_args_to_pattern ctx c m item_generics in
10721053
let name = name @ [ PIdent (item_name, 0, item_generics) ] in
@@ -1167,8 +1148,7 @@ let fn_ptr_to_pattern (ctx : ctx) (c : to_pat_config)
11671148
name_with_generics_to_pattern_aux ctx c m d.item_meta.name func.generics
11681149
| TraitMethod (tr, method_id) ->
11691150
let method_name =
1170-
GAstUtils.get_method_name ctx.crate tr.trait_decl_ref.binder_value.id
1171-
method_id
1151+
GAstUtils.get_method_name ctx.crate tr.trait_decl_ref.id method_id
11721152
in
11731153
trait_ref_item_with_generics_to_pattern ctx c m tr method_name
11741154
func.generics

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -530,12 +530,14 @@ and pp_constant_expr (env : fmt_env) (fmt : Format.formatter)
530530
| CVar var -> pp_string fmt (const_generic_db_var_to_string env var)
531531
| CTraitConst (trait_ref, const_id) ->
532532
let name =
533-
GAstUtils.get_assoc_const_name env.crate
534-
trait_ref.trait_decl_ref.binder_value.id const_id
533+
GAstUtils.get_assoc_const_name env.crate trait_ref.trait_decl_ref.id
534+
const_id
535535
in
536536
Format.fprintf fmt "%a::%s" (pp_trait_ref env) trait_ref name
537537
| CVTableRef trait_ref ->
538-
Format.fprintf fmt "&vtable_of(%a)" (pp_trait_ref env) trait_ref
538+
Format.fprintf fmt "&vtable_of(%a)"
539+
(pp_region_binder ty_to_string pp_trait_ref env)
540+
trait_ref
539541
| CCall (fn_ptr, args) ->
540542
Format.fprintf fmt "%a(%a)" (pp_fn_ptr env) fn_ptr
541543
(pp_sep_list ", " (pp_constant_expr env))
@@ -612,8 +614,8 @@ and pp_fn_ptr_kind (env : fmt_env) (fmt : Format.formatter) (r : fn_ptr_kind) :
612614
match r with
613615
| TraitMethod (trait_ref, method_id) ->
614616
let method_name =
615-
GAstUtils.get_method_name env.crate
616-
trait_ref.trait_decl_ref.binder_value.id method_id
617+
GAstUtils.get_method_name env.crate trait_ref.trait_decl_ref.id
618+
method_id
617619
in
618620
Format.fprintf fmt "%a::%s" (pp_trait_ref env) trait_ref method_name
619621
| Fun fid -> pp_fun_decl_id env fmt fid
@@ -643,8 +645,8 @@ and pp_ty (env : fmt_env) (fmt : Format.formatter) (ty : ty) : unit =
643645
Format.fprintf fmt "%a is %a" (pp_ty env) ty (pp_type_pattern env) pat
644646
| TTraitType (trait_ref, type_id, generics) ->
645647
let type_name =
646-
GAstUtils.get_assoc_type_name env.crate
647-
trait_ref.trait_decl_ref.binder_value.id type_id
648+
GAstUtils.get_assoc_type_name env.crate trait_ref.trait_decl_ref.id
649+
type_id
648650
in
649651
Format.fprintf fmt "%a::%s%a" (pp_trait_ref env) trait_ref type_name
650652
(pp_generic_args env) generics
@@ -717,8 +719,8 @@ and dyn_trait_type_constraint_to_string (env : fmt_env)
717719
| None -> None
718720
| Some (clause_id, path) ->
719721
let type_name =
720-
GAstUtils.get_assoc_type_name env.crate
721-
ttc.trait_ref.trait_decl_ref.binder_value.id ttc.type_id
722+
GAstUtils.get_assoc_type_name env.crate ttc.trait_ref.trait_decl_ref.id
723+
ttc.type_id
722724
in
723725
let path =
724726
match path with
@@ -799,7 +801,7 @@ and generic_args_to_strings (env : fmt_env) (generics : generic_args) :
799801
let types = List.map (ty_to_string env) types in
800802
let cgs = List.map (constant_expr_to_string env) const_generics in
801803
let params = List.flatten [ regions; types; cgs ] in
802-
let trait_refs = List.map (trait_ref_to_string env) trait_refs in
804+
let trait_refs = List.map (poly_trait_ref_to_string env) trait_refs in
803805
(params, trait_refs)
804806

805807
and pp_generic_args (env : fmt_env) (fmt : Format.formatter)
@@ -814,25 +816,23 @@ and pp_generic_args_for_fn (env : fmt_env) (fmt : Format.formatter)
814816
(generics : generic_args) : unit =
815817
pp_generic_args env fmt generics
816818

817-
and pp_trait_ref_kind (env : fmt_env)
818-
(implements : trait_decl_ref region_binder option) (fmt : Format.formatter)
819-
(kind : trait_ref_kind) : unit =
819+
and pp_trait_ref_kind (env : fmt_env) (implements : trait_decl_ref option)
820+
(fmt : Format.formatter) (kind : trait_ref_kind) : unit =
820821
match kind with
821822
| Self -> pp_string fmt "Self"
822823
| TraitImpl impl_ref -> pp_trait_impl_ref env fmt impl_ref
823824
| BuiltinOrAuto (_, _, types, _) ->
824825
let implements = Option.get implements in
825826
let types = AssocTypeId.Map.to_list types in
826827
Format.fprintf fmt "{built_in impl %a"
827-
(pp_region_binder ty_to_string pp_trait_decl_ref_as_impl env)
828+
(pp_trait_decl_ref_as_impl env)
828829
implements;
829830
if types <> [] then
830831
Format.fprintf fmt " where %a"
831832
(pp_sep_list ", "
832833
(fun fmt (type_id, (assoc_ty : trait_assoc_ty_impl)) ->
833834
let name =
834-
GAstUtils.get_assoc_type_name env.crate
835-
implements.binder_value.id type_id
835+
GAstUtils.get_assoc_type_name env.crate implements.id type_id
836836
in
837837
Format.fprintf fmt "%s = %a" name (pp_ty env) assoc_ty.value))
838838
types;
@@ -843,24 +843,25 @@ and pp_trait_ref_kind (env : fmt_env)
843843
(trait_clause_id_format_as_implied clause_id)
844844
| ItemClause (tref, type_id, generics, clause_id) ->
845845
let type_name =
846-
GAstUtils.get_assoc_type_name env.crate
847-
tref.trait_decl_ref.binder_value.id type_id
846+
GAstUtils.get_assoc_type_name env.crate tref.trait_decl_ref.id type_id
848847
in
849848
Format.fprintf fmt "%a::%s%a::%s" (pp_trait_ref env) tref type_name
850849
(pp_generic_args env) generics
851850
(trait_clause_id_format_as_implied clause_id)
852-
| Dyn ->
853-
pp_region_binder ty_to_string pp_trait_decl_ref env fmt
854-
(Option.get implements)
851+
| Dyn -> pp_trait_decl_ref env fmt (Option.get implements)
855852
| UnknownTrait msg -> Format.fprintf fmt "UNKNOWN(%s)" msg
856853

857854
and pp_trait_ref (env : fmt_env) (fmt : Format.formatter) (tr : trait_ref) :
858855
unit =
859856
pp_trait_ref_kind env (Some tr.trait_decl_ref) fmt tr.kind
860857

861-
and trait_ref_to_string env tr =
858+
and trait_ref_to_string env (tr : trait_ref) =
862859
pp_to_string (fun fmt -> pp_trait_ref env fmt tr)
863860

861+
and poly_trait_ref_to_string env (tr : poly_trait_ref) =
862+
pp_to_string (fun fmt ->
863+
pp_region_binder ty_to_string pp_trait_ref env fmt tr)
864+
864865
and pp_trait_decl_ref (env : fmt_env) (fmt : Format.formatter)
865866
(tr : trait_decl_ref) : unit =
866867
Format.fprintf fmt "%a%a" (pp_trait_decl_id env) tr.id (pp_generic_args env)
@@ -902,6 +903,16 @@ and pp_trait_proof (env : fmt_env) (fmt : Format.formatter)
902903
| None -> ()
903904
| Some value -> Format.fprintf fmt " = %a" (pp_trait_ref env) value
904905

906+
and pp_poly_trait_proof (env : fmt_env) (fmt : Format.formatter)
907+
(clause_id : trait_clause_id) (trait_ref : poly_trait_ref) : unit =
908+
pp_region_binder ty_to_string
909+
(fun env fmt trait_ref ->
910+
Format.fprintf fmt "proof %s: (%a) = %a"
911+
(trait_clause_id_format_as_implied clause_id)
912+
(pp_trait_decl_ref_as_pred env)
913+
trait_ref.trait_decl_ref (pp_trait_ref env) trait_ref)
914+
env fmt trait_ref
915+
905916
and pp_impl_elem (env : fmt_env) (fmt : Format.formatter) (elem : impl_elem) :
906917
unit =
907918
match elem with
@@ -1066,8 +1077,7 @@ let pp_trait_type_constraint (env : fmt_env) (fmt : Format.formatter)
10661077
(ttc : trait_type_constraint) : unit =
10671078
let { trait_ref; type_id; ty } = ttc in
10681079
let type_name =
1069-
GAstUtils.get_assoc_type_name env.crate
1070-
trait_ref.trait_decl_ref.binder_value.id type_id
1080+
GAstUtils.get_assoc_type_name env.crate trait_ref.trait_decl_ref.id type_id
10711081
in
10721082
Format.fprintf fmt "%s::%s = %s"
10731083
(trait_ref_to_string env trait_ref)
@@ -1697,7 +1707,9 @@ let pp_borrowck_statement (env : fmt_env) (fmt : Format.formatter)
16971707
Format.fprintf fmt "set_outlives(%s, %s)" (ty_to_string env ty)
16981708
(region_to_string env region)
16991709
| PredicateHolds predicate ->
1700-
Format.fprintf fmt "predicate_holds(%a)" (pp_trait_ref env) predicate
1710+
Format.fprintf fmt "predicate_holds(%a)"
1711+
(pp_region_binder ty_to_string pp_trait_ref env)
1712+
predicate
17011713

17021714
let pp_abort_kind (env : fmt_env) (fmt : Format.formatter) (a : abort_kind) :
17031715
unit =
@@ -1929,8 +1941,7 @@ let pp_trait_impl (env : fmt_env) (indent : string) (indent_incr : string)
19291941
(fun i trait_ref ->
19301942
Format.fprintf fmt "%s%a\n" indent1
19311943
(fun fmt (clause_id, trait_ref) ->
1932-
pp_trait_proof env fmt clause_id trait_ref.trait_decl_ref
1933-
(Some trait_ref))
1944+
pp_poly_trait_proof env fmt clause_id trait_ref)
19341945
(TraitClauseId.of_int i, trait_ref))
19351946
def.implied_trait_refs;
19361947
AssocConstId.Map.to_list def.consts
@@ -1955,8 +1966,8 @@ let pp_trait_impl (env : fmt_env) (indent : string) (indent_incr : string)
19551966
@ List.mapi
19561967
(fun i trait_ref ->
19571968
pp_to_string (fun fmt ->
1958-
pp_trait_proof env fmt (TraitClauseId.of_int i)
1959-
trait_ref.trait_decl_ref (Some trait_ref)))
1969+
pp_poly_trait_proof env fmt (TraitClauseId.of_int i)
1970+
trait_ref))
19601971
bound_ty.binder_value.implied_trait_refs
19611972
in
19621973
Format.fprintf fmt "%stype %s%s = %s%s\n" indent1 name params

‎charon-ml/src/Substitute.ml‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,13 @@ let trait_ref_kind_erase_regions (tr : trait_ref_kind) : trait_ref_kind =
291291
let generic_args_erase_regions (tr : generic_args) : generic_args =
292292
generic_args_substitute erase_regions_subst tr
293293

294+
let poly_trait_ref_erase_binder (tr : poly_trait_ref) : trait_ref =
295+
let subst =
296+
subst_remove_binder_zero
297+
{ error_sb_subst with r_sb_subst = (fun _ -> RErased) }
298+
in
299+
trait_ref_substitute subst tr.binder_value
300+
294301
(** Erase the regions in a type and perform a substitution *)
295302
let erase_regions_substitute_types (subst : subst) (ty : ty) : ty =
296303
let subst = { subst with r_subst = (fun _ -> RErased) } in
@@ -349,10 +356,10 @@ let make_trait_subst (var_ids : TraitClauseId.id list)
349356
fun varid -> TraitClauseId.Map.find varid map
350357

351358
let make_trait_subst_from_clauses (clauses : trait_param list)
352-
(trs : trait_ref list) : TraitClauseId.id -> trait_ref_kind =
359+
(trs : poly_trait_ref list) : TraitClauseId.id -> trait_ref_kind =
353360
make_trait_subst
354361
(List.map (fun (x : trait_param) -> x.clause_id) clauses)
355-
(List.map (fun (x : trait_ref) -> x.kind) trs)
362+
(List.map (fun x -> (poly_trait_ref_erase_binder x).kind) trs)
356363

357364
let make_sb_subst_from_generics (params : generic_params) (args : generic_args)
358365
(tr_self : trait_ref_kind) : single_binder_subst =
@@ -640,7 +647,7 @@ let instantiate_method (trait_self : trait_ref_kind)
640647

641648
(** Helper *)
642649
let instantiate_trait_method (trait_ref : trait_ref) =
643-
let trait_generics = trait_ref.trait_decl_ref.binder_value.generics in
650+
let trait_generics = trait_ref.trait_decl_ref.generics in
644651
let trait_self = trait_ref.kind in
645652
instantiate_method trait_self trait_generics
646653

@@ -742,7 +749,14 @@ let bound_identity_args (params : generic_params) : generic_args =
742749
List.map
743750
(fun (clause : trait_param) ->
744751
let kind = s.tr_sb_subst clause.clause_id in
745-
{ kind; trait_decl_ref = clause.trait })
752+
{
753+
clause.trait with
754+
binder_value =
755+
{
756+
kind = trait_ref_kind_substitute move_under_binder_subst kind;
757+
trait_decl_ref = clause.trait.binder_value;
758+
};
759+
})
746760
params.trait_clauses;
747761
}
748762

‎charon-ml/src/TypesUtils.ml‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ let empty_generic_args : generic_args =
213213
{ regions = []; types = []; const_generics = []; trait_refs = [] }
214214

215215
let mk_generic_args (regions : region list) (types : ty list)
216-
(const_generics : constant_expr list) (trait_refs : trait_ref list) :
216+
(const_generics : constant_expr list) (trait_refs : poly_trait_ref list) :
217217
generic_args =
218218
{ regions; types; const_generics; trait_refs }
219219

@@ -247,7 +247,14 @@ let generic_args_of_params span (generics : generic_params) : generic_args =
247247
let trait_refs =
248248
List.map
249249
(fun (c : trait_param) ->
250-
{ kind = Clause (Free c.clause_id); trait_decl_ref = c.trait })
250+
{
251+
c.trait with
252+
binder_value =
253+
{
254+
kind = Clause (Free c.clause_id);
255+
trait_decl_ref = c.trait.binder_value;
256+
};
257+
})
251258
generics.trait_clauses
252259
in
253260
{ regions; types; const_generics; trait_refs }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ and borrowck_statement =
168168
(** Require a type to outlive a region. For example, the ['a] bound in
169169
[let x: impl Copy + 'a = value] produces [SetOutlives(typeof(x), 'a)].
170170
*)
171-
| PredicateHolds of trait_ref
171+
| PredicateHolds of poly_trait_ref
172172
(** Require a trait predicate to hold. For example, the [Copy] bound in
173173
[let x: impl Copy = value] produces [PredicateHolds(typeof(x): Copy)].
174174
*)
@@ -664,7 +664,7 @@ and trait_impl = {
664664
(** Whether this is a negative impl ([impl !Trait for Type]). *)
665665
is_unsafe : bool; (** Whether this is an [unsafe impl]. *)
666666
generics : generic_params;
667-
implied_trait_refs : trait_ref list;
667+
implied_trait_refs : poly_trait_ref list;
668668
(** The trait references for the parent clauses (see [TraitDecl]). *)
669669
consts : global_decl_ref assoc_const_id_map;
670670
(** The implemented associated constants. *)

0 commit comments

Comments
 (0)