Skip to content

Commit 44ffb20

Browse files
authored
Merge pull request #1540 from Nadrieril/map-regions
Cleanup casts
2 parents 5bdd500 + de8736f commit 44ffb20

128 files changed

Lines changed: 1431 additions & 806 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.292"
3+
let supported_charon_version = "0.1.293"

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -551,8 +551,8 @@ and pp_constant_expr (env : fmt_env) (fmt : Format.formatter)
551551
args
552552
| CFnDef fn_ptr -> pp_fn_ptr env fmt fn_ptr
553553
| CFnPtr fn_ptr -> Format.fprintf fmt "fnptr(%a)" (pp_fn_ptr env) fn_ptr
554-
| CCast (value, ty) ->
555-
Format.fprintf fmt "cast<%a>(%a)" (pp_ty env) ty (pp_constant_expr env)
554+
| CCast (value, kind) ->
555+
Format.fprintf fmt "%a(%a)" (pp_cast_kind env) kind (pp_constant_expr env)
556556
value
557557
| CSizeOf ty -> Format.fprintf fmt "size_of::<%a>()" (pp_ty env) ty
558558
| CAlignOf ty -> Format.fprintf fmt "align_of::<%a>()" (pp_ty env) ty
@@ -606,6 +606,23 @@ and pp_constant_expr (env : fmt_env) (fmt : Format.formatter)
606606
meta)
607607
meta
608608

609+
and pp_cast_kind (env : fmt_env) (fmt : Format.formatter) (cast : cast_kind) :
610+
unit =
611+
match cast with
612+
| CastScalar tgt -> Format.fprintf fmt "cast<%a>" pp_scalar_type tgt
613+
| CastPtrExposeProvenance -> Format.pp_print_string fmt "cast_expose"
614+
| CastPtrWithExposedProvenance tgt ->
615+
Format.fprintf fmt "cast_with_exposed<%a>" (pp_ty env) tgt
616+
| CastFnDefToFnPtr -> Format.pp_print_string fmt "fn_def_to_fn_ptr"
617+
| CastFnPtrToUnsafe -> Format.pp_print_string fmt "fn_ptr_to_unsafe"
618+
| CastSubtype tgt -> Format.fprintf fmt "subtype<%a>" (pp_ty env) tgt
619+
| CastRawPtr tgt -> Format.fprintf fmt "cast<%a>" (pp_ty env) tgt
620+
| CastTransmute tgt -> Format.fprintf fmt "transmute<%a>" (pp_ty env) tgt
621+
| CastUnsize (tgt, meta) ->
622+
Format.fprintf fmt "unsize_cast<%a, %a>" (pp_ty env) tgt
623+
(pp_unsizing_metadata env) meta
624+
| CastConcretize tgt -> Format.fprintf fmt "concretize<%a>" (pp_ty env) tgt
625+
609626
and pp_match_pattern (env : fmt_env) (fmt : Format.formatter)
610627
(cv : constant_expr) : unit =
611628
match cv.kind with
@@ -1495,27 +1512,6 @@ and pp_place (env : fmt_env) (fmt : Format.formatter) (p : place) : unit =
14951512

14961513
and place_to_string env p = pp_to_string (fun fmt -> pp_place env fmt p)
14971514

1498-
and pp_cast_kind (env : fmt_env) (fmt : Format.formatter) (cast : cast_kind) :
1499-
unit =
1500-
match cast with
1501-
| CastScalar (src, tgt) ->
1502-
Format.fprintf fmt "cast<%a, %a>" pp_scalar_type src pp_scalar_type tgt
1503-
| CastPtrExposeProvenance (src, tgt) ->
1504-
Format.fprintf fmt "cast_expose<%a, %a>" (pp_ty env) src pp_scalar_type
1505-
tgt
1506-
| CastPtrWithExposedProvenance (src, tgt) ->
1507-
Format.fprintf fmt "cast_with_exposed<%a, %a>" pp_scalar_type src
1508-
(pp_ty env) tgt
1509-
| CastFnPtr (src, tgt) | CastRawPtr (src, tgt) ->
1510-
Format.fprintf fmt "cast<%a, %a>" (pp_ty env) src (pp_ty env) tgt
1511-
| CastTransmute (src, tgt) ->
1512-
Format.fprintf fmt "transmute<%a, %a>" (pp_ty env) src (pp_ty env) tgt
1513-
| CastUnsize (src, tgt, meta) ->
1514-
Format.fprintf fmt "unsize_cast<%a, %a, %a>" (pp_ty env) src (pp_ty env)
1515-
tgt (pp_unsizing_metadata env) meta
1516-
| CastConcretize (src, tgt) ->
1517-
Format.fprintf fmt "concretize<%a, %a>" (pp_ty env) src (pp_ty env) tgt
1518-
15191515
and pp_nullop (env : fmt_env) (fmt : Format.formatter) (op : nullop) : unit =
15201516
match op with
15211517
| UbChecks -> pp_string fmt "ub_checks"

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

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -121,58 +121,6 @@ and borrow_kind =
121121
<https://doc.rust-lang.org/beta/nightly-rustc/rustc_middle/mir/enum.MutBorrowKind.html#variant.ClosureCapture>.
122122
*)
123123

124-
(** For all the variants: the first type gives the source type, the second one
125-
gives the destination type. *)
126-
and cast_kind =
127-
| CastScalar of scalar_type * scalar_type
128-
(** Conversion between scalar types. See
129-
<https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric>
130-
*)
131-
| CastRawPtr of ty * ty
132-
(** A conversion between pointer and function pointer types. *)
133-
| CastPtrExposeProvenance of ty * scalar_type
134-
(** Converts a pointer or function pointer to an address, exposing its
135-
provenance. See
136-
<https://doc.rust-lang.org/std/primitive.pointer.html#method.expose_provenance>.
137-
*)
138-
| CastPtrWithExposedProvenance of scalar_type * ty
139-
(** Converts an address to a pointer, which picks up exposed provenance.
140-
See
141-
<https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html>.
142-
*)
143-
| CastFnPtr of ty * ty
144-
(** Cast into a function pointer. The source may be a function item or an
145-
unsafe function pointer that is made safe. *)
146-
| CastUnsize of ty * ty * unsizing_metadata
147-
(** [Unsize coercion](https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html).
148-
This is either [[T; N]] -> [[T]] or [T: Trait] -> [dyn Trait]
149-
coercions, behind a pointer (reference, [Box], or other type that
150-
implements [CoerceUnsized]).
151-
152-
The special case of [&[T; N]] -> [&[T]] coercion is caught by
153-
[UnOp::ArrayToSlice]. *)
154-
| CastTransmute of ty * ty
155-
(** Reinterprets the bits of a value of one type as another type, i.e.
156-
exactly what [[std::mem::transmute]] does. *)
157-
| CastConcretize of ty * ty
158-
(** Converts a receiver type with [dyn Trait<...>] to a concrete type [T],
159-
used in vtable method shims. Valid conversions are references, raw
160-
pointers, and (optionally) boxes:
161-
- [&[mut] dyn Trait<...>] -> [&[mut] T]
162-
- [*[mut] dyn Trait<...>] -> [*[mut] T]
163-
- [Box<dyn Trait<...>>] -> [Box<T>] when no [--treat-box-as-builtin]
164-
165-
For possible receivers, see:
166-
<https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>.
167-
Other receivers, e.g., [Rc] should be unpacked before the cast and
168-
re-boxed after. FIXME(ssyram): but this is not implemented yet,
169-
namely, there may still be something like
170-
[Rc<dyn Trait<...>> -> Rc<T>] in the types.
171-
172-
With [--index-to-function-calls], this also converts [&[T]] to
173-
[&[T; N]] (or the mutable equivalents) when an array subslice
174-
projection determines the length statically. *)
175-
176124
and local_id = (LocalId.id[@visitors.opaque])
177125

178126
(** Nullary operation *)

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

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -525,39 +525,31 @@ and cast_kind_of_json (ctx : of_json_ctx) (js : json) :
525525
(cast_kind, string) result =
526526
combine_error_msgs js __FUNCTION__
527527
(match js with
528-
| `Assoc [ ("Scalar", `List [ _0; _1 ]) ] ->
528+
| `Assoc [ ("Scalar", _0) ] ->
529529
let* _0 = scalar_type_of_json ctx _0 in
530-
let* _1 = scalar_type_of_json ctx _1 in
531-
Ok (CastScalar (_0, _1))
532-
| `Assoc [ ("RawPtr", `List [ _0; _1 ]) ] ->
530+
Ok (CastScalar _0)
531+
| `Assoc [ ("RawPtr", _0) ] ->
533532
let* _0 = ty_of_json ctx _0 in
534-
let* _1 = ty_of_json ctx _1 in
535-
Ok (CastRawPtr (_0, _1))
536-
| `Assoc [ ("PtrExposeProvenance", `List [ _0; _1 ]) ] ->
533+
Ok (CastRawPtr _0)
534+
| `String "PtrExposeProvenance" -> Ok CastPtrExposeProvenance
535+
| `Assoc [ ("PtrWithExposedProvenance", _0) ] ->
537536
let* _0 = ty_of_json ctx _0 in
538-
let* _1 = scalar_type_of_json ctx _1 in
539-
Ok (CastPtrExposeProvenance (_0, _1))
540-
| `Assoc [ ("PtrWithExposedProvenance", `List [ _0; _1 ]) ] ->
541-
let* _0 = scalar_type_of_json ctx _0 in
542-
let* _1 = ty_of_json ctx _1 in
543-
Ok (CastPtrWithExposedProvenance (_0, _1))
544-
| `Assoc [ ("FnPtr", `List [ _0; _1 ]) ] ->
537+
Ok (CastPtrWithExposedProvenance _0)
538+
| `String "FnDefToFnPtr" -> Ok CastFnDefToFnPtr
539+
| `String "FnPtrToUnsafe" -> Ok CastFnPtrToUnsafe
540+
| `Assoc [ ("Subtype", _0) ] ->
545541
let* _0 = ty_of_json ctx _0 in
546-
let* _1 = ty_of_json ctx _1 in
547-
Ok (CastFnPtr (_0, _1))
548-
| `Assoc [ ("Unsize", `List [ _0; _1; _2 ]) ] ->
542+
Ok (CastSubtype _0)
543+
| `Assoc [ ("Unsize", `List [ _0; _1 ]) ] ->
549544
let* _0 = ty_of_json ctx _0 in
550-
let* _1 = ty_of_json ctx _1 in
551-
let* _2 = unsizing_metadata_of_json ctx _2 in
552-
Ok (CastUnsize (_0, _1, _2))
553-
| `Assoc [ ("Transmute", `List [ _0; _1 ]) ] ->
545+
let* _1 = unsizing_metadata_of_json ctx _1 in
546+
Ok (CastUnsize (_0, _1))
547+
| `Assoc [ ("Transmute", _0) ] ->
554548
let* _0 = ty_of_json ctx _0 in
555-
let* _1 = ty_of_json ctx _1 in
556-
Ok (CastTransmute (_0, _1))
557-
| `Assoc [ ("Concretize", `List [ _0; _1 ]) ] ->
549+
Ok (CastTransmute _0)
550+
| `Assoc [ ("Concretize", _0) ] ->
558551
let* _0 = ty_of_json ctx _0 in
559-
let* _1 = ty_of_json ctx _1 in
560-
Ok (CastConcretize (_0, _1))
552+
Ok (CastConcretize _0)
561553
| _ -> Error "")
562554

563555
and const_generic_param_of_json (ctx : of_json_ctx) (js : json) :
@@ -641,7 +633,7 @@ and constant_expr_kind_of_json (ctx : of_json_ctx) (js : json) :
641633
Ok (CFnPtr _0)
642634
| `Assoc [ ("Cast", `List [ _0; _1 ]) ] ->
643635
let* _0 = constant_expr_of_json ctx _0 in
644-
let* _1 = ty_of_json ctx _1 in
636+
let* _1 = cast_kind_of_json ctx _1 in
645637
Ok (CCast (_0, _1))
646638
| `Assoc [ ("PtrNoProvenance", _0) ] ->
647639
let* _0 = big_int_of_json ctx _0 in
@@ -1306,12 +1298,14 @@ and region_param_of_json (ctx : of_json_ctx) (js : json) :
13061298
("name", name);
13071299
("variance", variance);
13081300
("mutability", mutability);
1301+
("late_bound", late_bound);
13091302
] ->
13101303
let* index = region_id_of_json ctx index in
13111304
let* name = option_of_json string_of_json ctx name in
13121305
let* variance = variance_of_json ctx variance in
13131306
let* mutability = lifetime_mutability_of_json ctx mutability in
1314-
Ok ({ index; name; variance; mutability } : region_param)
1307+
let* late_bound = bool_of_json ctx late_bound in
1308+
Ok ({ index; name; variance; mutability; late_bound } : region_param)
13151309
| _ -> Error "")
13161310

13171311
and rvalue_of_json (ctx : of_json_ctx) (js : json) : (rvalue, string) result =

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

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -475,37 +475,29 @@ and cast_kind_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
475475
match __tag with
476476
| 0 ->
477477
let* _0 = scalar_type_of_postcard ctx st in
478-
let* _1 = scalar_type_of_postcard ctx st in
479-
Ok (CastScalar (_0, _1))
478+
Ok (CastScalar _0)
480479
| 1 ->
481480
let* _0 = ty_of_postcard ctx st in
482-
let* _1 = ty_of_postcard ctx st in
483-
Ok (CastRawPtr (_0, _1))
484-
| 2 ->
485-
let* _0 = ty_of_postcard ctx st in
486-
let* _1 = scalar_type_of_postcard ctx st in
487-
Ok (CastPtrExposeProvenance (_0, _1))
481+
Ok (CastRawPtr _0)
482+
| 2 -> Ok CastPtrExposeProvenance
488483
| 3 ->
489-
let* _0 = scalar_type_of_postcard ctx st in
490-
let* _1 = ty_of_postcard ctx st in
491-
Ok (CastPtrWithExposedProvenance (_0, _1))
492-
| 4 ->
493-
let* _0 = ty_of_postcard ctx st in
494-
let* _1 = ty_of_postcard ctx st in
495-
Ok (CastFnPtr (_0, _1))
496-
| 5 ->
497484
let* _0 = ty_of_postcard ctx st in
498-
let* _1 = ty_of_postcard ctx st in
499-
let* _2 = unsizing_metadata_of_postcard ctx st in
500-
Ok (CastUnsize (_0, _1, _2))
485+
Ok (CastPtrWithExposedProvenance _0)
486+
| 4 -> Ok CastFnDefToFnPtr
487+
| 5 -> Ok CastFnPtrToUnsafe
501488
| 6 ->
502489
let* _0 = ty_of_postcard ctx st in
503-
let* _1 = ty_of_postcard ctx st in
504-
Ok (CastTransmute (_0, _1))
490+
Ok (CastSubtype _0)
505491
| 7 ->
506492
let* _0 = ty_of_postcard ctx st in
507-
let* _1 = ty_of_postcard ctx st in
508-
Ok (CastConcretize (_0, _1))
493+
let* _1 = unsizing_metadata_of_postcard ctx st in
494+
Ok (CastUnsize (_0, _1))
495+
| 8 ->
496+
let* _0 = ty_of_postcard ctx st in
497+
Ok (CastTransmute _0)
498+
| 9 ->
499+
let* _0 = ty_of_postcard ctx st in
500+
Ok (CastConcretize _0)
509501
| _ -> Error ("unknown enum variant tag: " ^ string_of_int __tag))
510502

511503
and const_generic_param_of_postcard (ctx : of_postcard_ctx)
@@ -582,7 +574,7 @@ and constant_expr_kind_of_postcard (ctx : of_postcard_ctx) (st : postcard_state)
582574
Ok (CFnPtr _0)
583575
| 12 ->
584576
let* _0 = constant_expr_of_postcard ctx st in
585-
let* _1 = ty_of_postcard ctx st in
577+
let* _1 = cast_kind_of_postcard ctx st in
586578
Ok (CCast (_0, _1))
587579
| 13 ->
588580
let* _0 = big_uint_of_postcard ctx st in
@@ -1178,7 +1170,8 @@ and region_param_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
11781170
let* name = option_of_postcard string_of_postcard ctx st in
11791171
let* variance = variance_of_postcard ctx st in
11801172
let* mutability = lifetime_mutability_of_postcard ctx st in
1181-
Ok ({ index; name; variance; mutability } : region_param))
1173+
let* late_bound = bool_of_postcard ctx st in
1174+
Ok ({ index; name; variance; mutability; late_bound } : region_param))
11821175

11831176
and rvalue_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
11841177
(rvalue, string) result =

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

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,62 @@ and byte =
258258
within the pointer. Note that we do not have an actual value for this
259259
pointer byte, unlike MiniRust, as that is non-deterministic. *)
260260

261+
(** Cast the operand to a new type. *)
262+
and cast_kind =
263+
| CastScalar of scalar_type
264+
(** Conversion between scalar types. See
265+
<https://doc.rust-lang.org/reference/expressions/operator-expr.html#r-expr.as.numeric>
266+
*)
267+
| CastRawPtr of ty
268+
(** Convert a raw pointer or function pointer to a raw pointer. *)
269+
| CastPtrExposeProvenance
270+
(** Converts a pointer or function pointer to an address, exposing its
271+
provenance. See
272+
<https://doc.rust-lang.org/std/primitive.pointer.html#method.expose_provenance>.
273+
*)
274+
| CastPtrWithExposedProvenance of ty
275+
(** Converts an address to a pointer, which picks up exposed provenance.
276+
See
277+
<https://doc.rust-lang.org/std/ptr/fn.with_exposed_provenance.html>.
278+
*)
279+
| CastFnDefToFnPtr
280+
(** Turn a function item into a function pointer, binding exactly its
281+
late-bound regions. *)
282+
| CastFnPtrToUnsafe
283+
(** Convert a safe function pointer to an unsafe function pointer with the
284+
same signature. *)
285+
| CastSubtype of ty
286+
(** Convert a value to a supertype; this has no runtime effect. *)
287+
| CastUnsize of ty * unsizing_metadata
288+
(** [Unsize coercion](https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html).
289+
This is either [[T; N]] -> [[T]] or [T: Trait] -> [dyn Trait]
290+
coercions, behind a pointer (reference, [Box], or other type that
291+
implements [CoerceUnsized]).
292+
293+
The special case of [&[T; N]] -> [&[T]] coercion is caught by
294+
[UnOp::ArrayToSlice]. *)
295+
| CastTransmute of ty
296+
(** Reinterprets the bits of a value of one type as another type, i.e.
297+
exactly what [[std::mem::transmute]] does. *)
298+
| CastConcretize of ty
299+
(** Converts a receiver type with [dyn Trait<...>] to a concrete type [T],
300+
used in vtable method shims. Valid conversions are references, raw
301+
pointers, and (optionally) boxes:
302+
- [&[mut] dyn Trait<...>] -> [&[mut] T]
303+
- [*[mut] dyn Trait<...>] -> [*[mut] T]
304+
- [Box<dyn Trait<...>>] -> [Box<T>] when no [--treat-box-as-builtin]
305+
306+
For possible receivers, see:
307+
<https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>.
308+
Other receivers, e.g., [Rc] should be unpacked before the cast and
309+
re-boxed after. FIXME(ssyram): but this is not implemented yet,
310+
namely, there may still be something like
311+
[Rc<dyn Trait<...>> -> Rc<T>] in the types.
312+
313+
With [--index-to-function-calls], this also converts [&[T]] to
314+
[&[T; N]] (or the mutable equivalents) when an array subslice
315+
projection determines the length statically. *)
316+
261317
(** A const generic variable in a signature or binder. *)
262318
and const_generic_param = {
263319
index : const_generic_var_id;
@@ -302,7 +358,7 @@ and constant_expr_kind =
302358
(** A function pointer value; this is a pointer (i.e. an address).
303359
304360
This is eliminated inside functions if [--raw-consts] is off. *)
305-
| CCast of constant_expr * ty
361+
| CCast of constant_expr * cast_kind
306362
(** Cast a constant value to another type (e.g. erase a vtable method
307363
pointer to [*const ()]). *)
308364
| CPtrNoProvenance of big_int
@@ -485,6 +541,10 @@ and region_param = {
485541
Only [true] if this lifetime parameter belongs to an ADT. This is a
486542
global analysis that looks even into opaque items. When unsure, err on
487543
the side of assuming mutability. *)
544+
late_bound : bool;
545+
(** Whether this region is "late bound", which means that it is
546+
unconstrained by the trait system and thus may be used in a [for<..>]
547+
binder. *)
488548
}
489549

490550
(** The value of a trait associated type. *)

‎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.292"
30+
version = "0.1.293"
3131
authors.workspace = true
3232
edition.workspace = true
3333
license.workspace = true

0 commit comments

Comments
 (0)