Skip to content

Commit d147e65

Browse files
authored
Merge pull request #1599 from Nadrieril/refactor-annotations-repr
Rework the representation of user type annotations
2 parents c04983b + 454586e commit d147e65

94 files changed

Lines changed: 592 additions & 505 deletions

File tree

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.314"
3+
let supported_charon_version = "0.1.315"

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,24 +1776,28 @@ let pp_assertion (env : fmt_env) (fmt : Format.formatter) (a : assertion) : unit
17761776
(Bool.to_string a.expected)
17771777
check_kind
17781778

1779+
let pp_place_type_constraint (env : fmt_env) (fmt : Format.formatter)
1780+
(constraint_ : place_type_constraint) : unit =
1781+
let { place; ty; variance } = constraint_ in
1782+
let relation =
1783+
match variance with
1784+
| Covariant -> ":<"
1785+
| Invariant -> ":="
1786+
| Contravariant -> ":>"
1787+
| Bivariant | VaUnknown -> assert false
1788+
in
1789+
Format.fprintf fmt "%a %s %s" (pp_place env) place relation
1790+
(ty_to_string env ty)
1791+
17791792
let pp_borrowck_statement (env : fmt_env) (fmt : Format.formatter)
17801793
(statement : borrowck_statement) : unit =
17811794
match statement with
17821795
| FakeRead place -> Format.fprintf fmt "fake_read(%a)" (pp_place env) place
1783-
| SetType (place, ty, variance, temporary) ->
1784-
let relation =
1785-
match variance with
1786-
| Covariant -> ":<"
1787-
| Invariant -> ":="
1788-
| Contravariant -> ":>"
1789-
| Bivariant | VaUnknown -> assert false
1790-
in
1791-
let name = if temporary then "set_here" else "set" in
1792-
Format.fprintf fmt "%s %a %s %s" name (pp_place env) place relation
1793-
(ty_to_string env ty)
1794-
| SetOutlives (lhs, rhs) ->
1795-
Format.fprintf fmt "set %s :> %s" (region_to_string env lhs)
1796-
(region_to_string env rhs)
1796+
| SetType constraint_ ->
1797+
Format.fprintf fmt "set %a" (pp_place_type_constraint env) constraint_
1798+
| SetOutlives pred ->
1799+
Format.fprintf fmt "set %s"
1800+
(outlives_to_string_with ty_to_string env pred)
17971801

17981802
let pp_abort_kind (env : fmt_env) (fmt : Format.formatter) (a : abort_kind) :
17991803
unit =
@@ -1906,7 +1910,13 @@ let pp_locals (env : fmt_env) (indent : string) (fmt : Format.formatter)
19061910
(if var.is_mutable then "mut " else "")
19071911
(local_id_to_string env var.index)
19081912
(ty_to_string env var.local_ty)
1909-
kind drop_flag_for
1913+
kind drop_flag_for;
1914+
List.iter
1915+
(fun constraint_ ->
1916+
Format.fprintf fmt "\n%s where %a;" indent
1917+
(pp_place_type_constraint env)
1918+
constraint_)
1919+
var.borrowck_constraints
19101920
in
19111921
pp_sep_list "\n" pp_local_decl fmt locals.locals
19121922

‎charon-ml/src/Substitute.ml‎

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -439,22 +439,13 @@ let call_substitute (subst : subst) (call : call) : call =
439439
let block_substitute (subst : subst) (blk : block) : block =
440440
st_substitute_visitor#visit_block subst blk
441441

442-
(** Apply a type substitution to a function body. Return the local variables and
443-
the body. *)
444-
let expr_body_substitute (subst : subst) (body : expr_body) : local list * block
445-
=
446-
let locals =
447-
List.map
448-
(fun (v : local) ->
449-
{
450-
v with
451-
local_ty = ty_substitute subst v.local_ty;
452-
drop_flag_for = Option.map (place_substitute subst) v.drop_flag_for;
453-
})
454-
body.locals.locals
455-
in
456-
let body = block_substitute subst body.body in
457-
(locals, body)
442+
(** Apply a type substitution to a function body. *)
443+
let expr_body_substitute (subst : subst) (body : expr_body) : expr_body =
444+
{
445+
body with
446+
locals = st_substitute_visitor#visit_locals subst body.locals;
447+
body = block_substitute subst body.body;
448+
}
458449

459450
let trait_type_constraint_substitute (subst : subst)
460451
(ttc : trait_type_constraint) : trait_type_constraint =

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,10 @@ and assertion = {
156156
(** Statements that only affect borrow-checking. They are no-ops at runtime. *)
157157
and borrowck_statement =
158158
| FakeRead of place (** Acts like a read of the place. *)
159-
| SetType of place * ty * variance * bool
160-
(** Relate the type of a place to the provided type. For example,
161-
[let x: Self = value] produces [SetType(x == Self)].
162-
163-
Fields:
164-
- [place]
165-
- [ty]: This type may contain body regions shared with subsequent
166-
[SetOutlives] statements.
167-
- [variance]
168-
- [temporary]: Whether this type relation holds for the whole rest of
169-
the function, or just at this point. *)
159+
| SetType of place_type_constraint
160+
(** Apply a constraint to this place at this point in the function. For
161+
example, [x = Option::<&'static u32>::None;] emits a
162+
[tmp := Option::<&'static u32>] constraint. *)
170163
| SetOutlives of outlives_pred (** Require an outlives predicate to hold. *)
171164

172165
and branch_id = (BranchId.id[@visitors.opaque])
@@ -345,6 +338,10 @@ and local = {
345338
is_mutable : bool;
346339
(** Whether this local was declared [mut]. Anonymous locals are always
347340
mutable. *)
341+
borrowck_constraints : place_type_constraint list;
342+
(** List of constraints that apply to this local throughout the function
343+
body. For example, [let x: Self = ...;] produces a [x == Self]
344+
constraint. *)
348345
drop_flag_for : place option;
349346
(** If this local is a drop flag, this is the place whose initialization
350347
state it tracks. *)
@@ -361,6 +358,10 @@ and locals = {
361358
- the remaining locals, used for the intermediate computations *)
362359
}
363360

361+
(** Borrow-checking constraint that enforces a relation between the type of the
362+
given place and the provided type. *)
363+
and place_type_constraint = { place : place; ty : ty; variance : variance }
364+
364365
(** A branching operation. *)
365366
and switch_data = {
366367
scrutinee : switch_scrutinee; (** The value to branch over. *)

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -359,22 +359,9 @@ and borrowck_statement_of_json (ctx : of_json_ctx) (js : json) :
359359
| `Assoc [ ("FakeRead", _0) ] ->
360360
let* _0 = place_of_json ctx _0 in
361361
Ok (FakeRead _0)
362-
| `Assoc
363-
[
364-
( "SetType",
365-
`Assoc
366-
[
367-
("place", place);
368-
("ty", ty);
369-
("variance", variance);
370-
("temporary", temporary);
371-
] );
372-
] ->
373-
let* place = place_of_json ctx place in
374-
let* ty = ty_of_json ctx ty in
375-
let* variance = variance_of_json ctx variance in
376-
let* temporary = bool_of_json ctx temporary in
377-
Ok (SetType (place, ty, variance, temporary))
362+
| `Assoc [ ("SetType", _0) ] ->
363+
let* _0 = place_type_constraint_of_json ctx _0 in
364+
Ok (SetType _0)
378365
| `Assoc [ ("SetOutlives", _0) ] ->
379366
let* _0 = outlives_pred_of_json ctx _0 in
380367
Ok (SetOutlives _0)
@@ -1239,6 +1226,17 @@ and place_kind_of_json (ctx : of_json_ctx) (js : json) :
12391226
Ok (PlaceGlobal _0)
12401227
| _ -> Error "")
12411228

1229+
and place_type_constraint_of_json (ctx : of_json_ctx) (js : json) :
1230+
(place_type_constraint, string) result =
1231+
combine_error_msgs js __FUNCTION__
1232+
(match js with
1233+
| `Assoc [ ("place", place); ("ty", ty); ("variance", variance) ] ->
1234+
let* place = place_of_json ctx place in
1235+
let* ty = ty_of_json ctx ty in
1236+
let* variance = variance_of_json ctx variance in
1237+
Ok ({ place; ty; variance } : place_type_constraint)
1238+
| _ -> Error "")
1239+
12421240
and poly_trait_ref_of_json (ctx : of_json_ctx) (js : json) :
12431241
(poly_trait_ref, string) result =
12441242
combine_error_msgs js __FUNCTION__
@@ -3771,15 +3769,29 @@ and local_of_json (ctx : of_json_ctx) (js : json) : (local, string) result =
37713769
("span", span);
37723770
("ty", ty);
37733771
("mutable", mutable_);
3772+
("borrowck_constraints", borrowck_constraints);
37743773
("drop_flag_for", drop_flag_for);
37753774
] ->
37763775
let* index = local_id_of_json ctx index in
37773776
let* name = option_of_json string_of_json ctx name in
37783777
let* span = span_of_json ctx span in
37793778
let* local_ty = ty_of_json ctx ty in
37803779
let* is_mutable = bool_of_json ctx mutable_ in
3780+
let* borrowck_constraints =
3781+
list_of_json place_type_constraint_of_json ctx borrowck_constraints
3782+
in
37813783
let* drop_flag_for = option_of_json place_of_json ctx drop_flag_for in
3782-
Ok ({ index; name; span; local_ty; is_mutable; drop_flag_for } : local)
3784+
Ok
3785+
({
3786+
index;
3787+
name;
3788+
span;
3789+
local_ty;
3790+
is_mutable;
3791+
borrowck_constraints;
3792+
drop_flag_for;
3793+
}
3794+
: local)
37833795
| _ -> Error "")
37843796

37853797
and locals_of_json (ctx : of_json_ctx) (js : json) : (locals, string) result =

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,11 +315,8 @@ and borrowck_statement_of_postcard (ctx : of_postcard_ctx) (st : postcard_state)
315315
let* _0 = place_of_postcard ctx st in
316316
Ok (FakeRead _0)
317317
| 1 ->
318-
let* place = place_of_postcard ctx st in
319-
let* ty = ty_of_postcard ctx st in
320-
let* variance = variance_of_postcard ctx st in
321-
let* temporary = bool_of_postcard ctx st in
322-
Ok (SetType (place, ty, variance, temporary))
318+
let* _0 = place_type_constraint_of_postcard ctx st in
319+
Ok (SetType _0)
323320
| 2 ->
324321
let* _0 = outlives_pred_of_postcard ctx st in
325322
Ok (SetOutlives _0)
@@ -1107,6 +1104,14 @@ and place_kind_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
11071104
Ok (PlaceGlobal _0)
11081105
| _ -> Error ("unknown enum variant tag: " ^ string_of_int __tag))
11091106

1107+
and place_type_constraint_of_postcard (ctx : of_postcard_ctx)
1108+
(st : postcard_state) : (place_type_constraint, string) result =
1109+
combine_error_msgs st __FUNCTION__
1110+
(let* place = place_of_postcard ctx st in
1111+
let* ty = ty_of_postcard ctx st in
1112+
let* variance = variance_of_postcard ctx st in
1113+
Ok ({ place; ty; variance } : place_type_constraint))
1114+
11101115
and poly_trait_ref_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
11111116
(poly_trait_ref, string) result =
11121117
combine_error_msgs st __FUNCTION__
@@ -3095,8 +3100,21 @@ and local_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
30953100
let* span = span_of_postcard ctx st in
30963101
let* local_ty = ty_of_postcard ctx st in
30973102
let* is_mutable = bool_of_postcard ctx st in
3103+
let* borrowck_constraints =
3104+
list_of_postcard place_type_constraint_of_postcard ctx st
3105+
in
30983106
let* drop_flag_for = option_of_postcard place_of_postcard ctx st in
3099-
Ok ({ index; name; span; local_ty; is_mutable; drop_flag_for } : local))
3107+
Ok
3108+
({
3109+
index;
3110+
name;
3111+
span;
3112+
local_ty;
3113+
is_mutable;
3114+
borrowck_constraints;
3115+
drop_flag_for;
3116+
}
3117+
: local))
31003118

31013119
and locals_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
31023120
(locals, string) result =

‎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
@@ -26,7 +26,7 @@ tracing = { version = "0.1", features = ["max_level_trace"] }
2626

2727
[package]
2828
name = "charon"
29-
version = "0.1.314"
29+
version = "0.1.315"
3030
authors.workspace = true
3131
edition.workspace = true
3232
license.workspace = true

‎charon/src/ast/bodies.rs‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ pub struct Local {
119119
/// Whether this local was declared `mut`. Anonymous locals are always mutable.
120120
#[cfg_attr(feature = "charon_on_charon", charon::rename("is_mutable"))]
121121
pub mutable: bool,
122+
/// List of constraints that apply to this local throughout the function body. For example,
123+
/// `let x: Self = ...;` produces a `x == Self` constraint.
124+
pub borrowck_constraints: Vec<PlaceTypeConstraint>,
122125
/// If this local is a drop flag, this is the place whose initialization state it tracks.
123126
pub drop_flag_for: Option<Place>,
124127
}
@@ -212,24 +215,26 @@ pub enum CallSafety {
212215
Unsafe,
213216
}
214217

218+
/// Borrow-checking constraint that enforces a relation between the type of the given place and the
219+
/// provided type.
220+
#[derive(Debug, Clone, PartialEq, Eq)]
221+
#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
222+
pub struct PlaceTypeConstraint {
223+
pub place: Place,
224+
pub ty: Ty,
225+
#[serde_state(stateless)]
226+
pub variance: Variance,
227+
}
228+
215229
/// Statements that only affect borrow-checking. They are no-ops at runtime.
216230
#[derive(Debug, Clone, PartialEq, Eq)]
217231
#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]
218232
pub enum BorrowckStatement {
219233
/// Acts like a read of the place.
220234
FakeRead(Place),
221-
/// Relate the type of a place to the provided type. For example, `let x: Self = value`
222-
/// produces `SetType(x == Self)`.
223-
SetType {
224-
place: Place,
225-
/// This type may contain body regions shared with subsequent `SetOutlives` statements.
226-
ty: Ty,
227-
#[serde_state(stateless)]
228-
variance: Variance,
229-
/// Whether this type relation holds for the whole rest of the function, or just at this
230-
/// point.
231-
temporary: bool,
232-
},
235+
/// Apply a constraint to this place at this point in the function. For example, `x =
236+
/// Option::<&'static u32>::None;` emits a `tmp := Option::<&'static u32>` constraint.
237+
SetType(PlaceTypeConstraint),
233238
/// Require an outlives predicate to hold.
234239
SetOutlives(OutlivesPred),
235240
}
@@ -362,6 +367,7 @@ impl Locals {
362367
span: Span::dummy(),
363368
ty: ty.clone(),
364369
mutable: true,
370+
borrowck_constraints: Vec::new(),
365371
drop_flag_for: None,
366372
});
367373
Place::new(local_id, ty)

‎charon/src/ast/bodies/safety.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl HasSafety for BorrowckStatement {
315315
fn safety(&self, krate: &TranslatedCrate) -> Safety {
316316
match self {
317317
BorrowckStatement::FakeRead(place) => place.read_safety(krate),
318-
BorrowckStatement::SetType { .. } | BorrowckStatement::SetOutlives(..) => Safety::Safe,
318+
BorrowckStatement::SetType(..) | BorrowckStatement::SetOutlives(..) => Safety::Safe,
319319
}
320320
}
321321
}

0 commit comments

Comments
 (0)