Skip to content

Commit aca22db

Browse files
authored
Merge pull request #1531 from Nadrieril/instantiate-all-clauses
Instantiate all higher-ranked clauses
2 parents 277e748 + 1aebab5 commit aca22db

33 files changed

Lines changed: 494 additions & 282 deletions

‎AGENTS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Guidance for AI agents working on Charon.
4545
- Once you're done with a change, review it from the pov of an architect: does it use the right
4646
abstractions for the job, does it make use or/enforce the important invariants, will the code be
4747
easy to evolve.
48+
- Personal preference: `map`/`and_then` and similar combinators should be avoided unless they really
49+
clarify the code. Use `?` if available, and if-let chains when sensible.
4850

4951
- I will likely work in the same repo in parallel to start cleaning up your changes; I try not to
5052
interfere too much but don't be surprised.

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

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,13 @@ 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_args (env : fmt_env) (fmt : Format.formatter) (args : region_args)
345+
: unit =
346+
if args.regions <> [] then
347+
Format.fprintf fmt "<%a>"
348+
(pp_sep_list ", " pp_string)
349+
(List.map (region_to_string env) args.regions)
350+
344351
let rec region_binder_params_to_string :
345352
'a. (fmt_env -> ty -> string) -> fmt_env -> 'a region_binder -> string =
346353
fun ty_to_string env rb ->
@@ -707,7 +714,7 @@ and dyn_trait_type_constraint_to_string (env : fmt_env)
707714
(ttc : trait_type_constraint) : (trait_clause_id * string) option =
708715
let rec target_clause_and_path path (tref : trait_ref) =
709716
match tref.kind with
710-
| ParentClause (parent, clause_id) ->
717+
| ParentClause (parent, clause_id, _) ->
711718
target_clause_and_path
712719
(trait_clause_id_format_as_implied clause_id :: path)
713720
parent
@@ -837,17 +844,22 @@ and pp_trait_ref_kind (env : fmt_env) (implements : trait_decl_ref option)
837844
Format.fprintf fmt "%s = %a" name (pp_ty env) assoc_ty.value))
838845
types;
839846
pp_string fmt "}"
840-
| Clause (id, _) -> pp_string fmt (trait_db_var_to_string env id)
841-
| ParentClause (tref, clause_id) ->
842-
Format.fprintf fmt "%a::%s" (pp_trait_ref env) tref
847+
| Clause (id, args) ->
848+
Format.fprintf fmt "%s%a"
849+
(trait_db_var_to_string env id)
850+
(pp_region_args env) args
851+
| ParentClause (tref, clause_id, args) ->
852+
Format.fprintf fmt "%a::%s%a" (pp_trait_ref env) tref
843853
(trait_clause_id_format_as_implied clause_id)
844-
| ItemClause (tref, type_id, generics, clause_id) ->
854+
(pp_region_args env) args
855+
| ItemClause (tref, type_id, generics, clause_id, clause_args) ->
845856
let type_name =
846857
GAstUtils.get_assoc_type_name env.crate tref.trait_decl_ref.id type_id
847858
in
848-
Format.fprintf fmt "%a::%s%a::%s" (pp_trait_ref env) tref type_name
859+
Format.fprintf fmt "%a::%s%a::%s%a" (pp_trait_ref env) tref type_name
849860
(pp_generic_args env) generics
850861
(trait_clause_id_format_as_implied clause_id)
862+
(pp_region_args env) clause_args
851863
| Dyn -> pp_trait_decl_ref env fmt (Option.get implements)
852864
| UnknownTrait msg -> Format.fprintf fmt "UNKNOWN(%s)" msg
853865

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,10 +1561,11 @@ and trait_ref_kind_of_json (ctx : of_json_ctx) (js : json) :
15611561
let* _0 = de_bruijn_var_of_json trait_clause_id_of_json ctx _0 in
15621562
let* _1 = region_args_of_json ctx _1 in
15631563
Ok (Clause (_0, _1))
1564-
| `Assoc [ ("ParentClause", `List [ _0; _1 ]) ] ->
1564+
| `Assoc [ ("ParentClause", `List [ _0; _1; _2 ]) ] ->
15651565
let* _0 = trait_ref_of_json ctx _0 in
15661566
let* _1 = trait_clause_id_of_json ctx _1 in
1567-
Ok (ParentClause (_0, _1))
1567+
let* _2 = region_args_of_json ctx _2 in
1568+
Ok (ParentClause (_0, _1, _2))
15681569
| `Assoc
15691570
[
15701571
( "ItemClause",
@@ -1574,13 +1575,15 @@ and trait_ref_kind_of_json (ctx : of_json_ctx) (js : json) :
15741575
("type_id", type_id);
15751576
("generics", generics);
15761577
("clause_id", clause_id);
1578+
("clause_args", clause_args);
15771579
] );
15781580
] ->
15791581
let* trait_ref = trait_ref_of_json ctx trait_ref in
15801582
let* type_id = assoc_type_id_of_json ctx type_id in
15811583
let* generics = generic_args_of_json ctx generics in
15821584
let* clause_id = trait_clause_id_of_json ctx clause_id in
1583-
Ok (ItemClause (trait_ref, type_id, generics, clause_id))
1585+
let* clause_args = region_args_of_json ctx clause_args in
1586+
Ok (ItemClause (trait_ref, type_id, generics, clause_id, clause_args))
15841587
| `String "SelfId" -> Ok Self
15851588
| `Assoc
15861589
[

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,13 +1370,15 @@ and trait_ref_kind_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
13701370
| 2 ->
13711371
let* _0 = trait_ref_of_postcard ctx st in
13721372
let* _1 = trait_clause_id_of_postcard ctx st in
1373-
Ok (ParentClause (_0, _1))
1373+
let* _2 = region_args_of_postcard ctx st in
1374+
Ok (ParentClause (_0, _1, _2))
13741375
| 3 ->
13751376
let* trait_ref = trait_ref_of_postcard ctx st in
13761377
let* type_id = assoc_type_id_of_postcard ctx st in
13771378
let* generics = generic_args_of_postcard ctx st in
13781379
let* clause_id = trait_clause_id_of_postcard ctx st in
1379-
Ok (ItemClause (trait_ref, type_id, generics, clause_id))
1380+
let* clause_args = region_args_of_postcard ctx st in
1381+
Ok (ItemClause (trait_ref, type_id, generics, clause_id, clause_args))
13801382
| 4 -> Ok Self
13811383
| 5 ->
13821384
let* builtin_data = builtin_impl_data_of_postcard ctx st in

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -552,7 +552,7 @@ and trait_ref_kind =
552552
^^^^^^^
553553
Clause(0)
554554
]} *)
555-
| ParentClause of trait_ref * trait_clause_id
555+
| ParentClause of trait_ref * trait_clause_id * region_args
556556
(** A parent clause
557557
558558
Example:
@@ -573,7 +573,8 @@ and trait_ref_kind =
573573
parent clause 1 of clause 0
574574
}
575575
]} *)
576-
| ItemClause of trait_ref * assoc_type_id * generic_args * trait_clause_id
576+
| ItemClause of
577+
trait_ref * assoc_type_id * generic_args * trait_clause_id * region_args
577578
(** A clause defined on an associated type.
578579
579580
Example:
@@ -592,6 +593,7 @@ and trait_ref_kind =
592593
type_id: W, // look at the item [W]
593594
generics: [u32], // with these args
594595
clause_id: 1, // and take the clause 1 of this item
596+
clause_args: [],
595597
}
596598
}
597599
]}
@@ -604,7 +606,9 @@ and trait_ref_kind =
604606
- [trait_ref]
605607
- [type_id]
606608
- [generics]: Generic arguments for the associated type.
607-
- [clause_id]: Id of the clause among those for this item. *)
609+
- [clause_id]: Id of the clause among those for this item.
610+
- [clause_args]: Region arguments that instantiate the higher-ranked
611+
item clause. *)
608612
| Self
609613
(** The implicit [Self: Trait] clause. Present inside trait declarations,
610614
including trait method declarations. Not present in trait

‎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.

‎charon/Cargo.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ tracing = { version = "0.1", features = ["max_level_trace"] }
2727

2828
[package]
2929
name = "charon"
30-
version = "0.1.290"
30+
version = "0.1.291"
3131
authors.workspace = true
3232
edition.workspace = true
3333
license.workspace = true

‎charon/rustc_trait_elaboration/src/elaboration.rs‎

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,6 @@ fn initial_self_pred<'tcx, Id: ItemId>(
3535
}
3636
}
3737

38-
#[tracing::instrument(level = "trace", skip(elab_ctx, state))]
39-
fn parents_trait_predicates<'tcx, Id: ItemId>(
40-
elab_ctx: ElaborationCtx<'tcx, Id>,
41-
state: &Id::State<'tcx>,
42-
self_trait_ref: PolyTraitRef<'tcx>,
43-
) -> Vec<PolyTraitRef<'tcx>> {
44-
let tcx = elab_ctx.tcx;
45-
let def_id = Id::from_rust_def_id(state, self_trait_ref.def_id());
46-
ItemPredicates::implied(elab_ctx, state, def_id)
47-
.iter()
48-
// Substitute with the `self` args so that the clause makes sense in the
49-
// outside context.
50-
.map(|pred| pred.clause.instantiate_supertrait(tcx, self_trait_ref))
51-
.filter_map(|pred| pred.as_trait_clause())
52-
.map(|pred| pred.to_poly_trait_ref())
53-
.collect()
54-
}
55-
5638
/// A trait proof we have in our context.
5739
#[derive(Debug, Clone)]
5840
struct Candidate<'tcx, Id: ItemId = DefId> {
@@ -321,16 +303,34 @@ impl<'tcx, Id: ItemId> PredicateSearcher<'tcx, Id> {
321303
new_candidates: Vec<Candidate<'tcx, Id>>,
322304
) {
323305
let elab_ctx = self.elab_ctx;
306+
let tcx = elab_ctx.tcx;
324307
// Then recursively add their parents. This way ensures a breadth-first order,
325308
// which means we select the shortest path when looking up predicates.
326309
self.insert_candidates(
327310
state,
328311
new_candidates.into_iter().flat_map(|candidate| {
329-
parents_trait_predicates(elab_ctx, state, candidate.pred)
312+
let def_id = Id::from_rust_def_id(state, candidate.pred.def_id());
313+
ItemPredicates::implied(elab_ctx, state, def_id)
314+
.predicates
330315
.into_iter()
316+
.filter(|pred| pred.clause.as_trait_clause().is_some())
331317
.enumerate()
332-
.map(move |(index, parent_pred)| {
333-
candidate.push(elab_ctx, parent_pred, ImpliedPredicate::Parent { index })
318+
.map(|(index, pred)| {
319+
let pred_id = ImpliedPredicate::Parent {
320+
index,
321+
bound_region_count: pred.clause.kind().bound_vars().len(),
322+
};
323+
(pred_id, pred)
324+
})
325+
// Substitute with the `self` args so that the clause makes sense in the
326+
// outside context. This merges nested region binders.
327+
.map(move |(pred_id, pred)| {
328+
let pred = pred.clause.instantiate_supertrait(tcx, candidate.pred);
329+
(pred_id, pred)
330+
})
331+
.map(move |(pred_id, pred)| {
332+
let pred = pred.as_trait_clause().unwrap().to_poly_trait_ref();
333+
candidate.push(elab_ctx, pred, pred_id)
334334
})
335335
}),
336336
);
@@ -372,26 +372,26 @@ impl<'tcx, Id: ItemId> PredicateSearcher<'tcx, Id> {
372372
ItemPredicates::implied(self.elab_ctx, state, Id::from_rust_def_id(state, *def_id));
373373
let item_bounds = item_bounds
374374
.iter_trait_clauses()
375-
// Substitute the item generics
376-
.map(|(_, tref)| {
377-
EarlyBinder::bind(tcx, tref)
375+
.enumerate()
376+
.map(|(index, (_, tref))| {
377+
let bound_region_count = tref.bound_vars().len();
378+
// Substitute the item generics
379+
let pred = EarlyBinder::bind(tcx, tref)
378380
.instantiate(tcx, args)
379-
.skip_normalization()
380-
})
381-
.enumerate();
381+
.skip_normalization();
382+
let pred_id = ImpliedPredicate::AssocItem {
383+
item: item_ref.clone(),
384+
index,
385+
bound_region_count,
386+
};
387+
(pred_id, pred)
388+
});
382389

383390
// Add all the bounds on the corresponding associated item.
384391
self.insert_candidates(
385392
state,
386-
item_bounds.map(|(index, pred)| {
387-
Candidate::from_trait_proof(proof.push(
388-
elab_ctx,
389-
pred,
390-
ImpliedPredicate::AssocItem {
391-
item: item_ref.clone(),
392-
index,
393-
},
394-
))
393+
item_bounds.map(|(pred_id, pred)| {
394+
Candidate::from_trait_proof(proof.push(elab_ctx, pred, pred_id))
395395
}),
396396
);
397397
}

‎charon/rustc_trait_elaboration/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,14 @@ pub enum ImpliedPredicate<'tcx, Id: ItemId = DefId> {
7373
item: ItemRef<'tcx, Id>,
7474
/// The index of this predicate among the trait predicates returned by `ItemPredicates::Implied`.
7575
index: usize,
76+
/// Number of regions bound by the corresponding clause.
77+
bound_region_count: usize,
7678
},
7779
Parent {
7880
/// The index of this predicate among the trait predicates returned by `ItemPredicates::Implied`.
7981
index: usize,
82+
/// Number of regions bound by the corresponding clause.
83+
bound_region_count: usize,
8084
},
8185
}
8286

0 commit comments

Comments
 (0)