Skip to content

Commit 8937893

Browse files
authored
Merge pull request #1544 from N1ark/dyn-receiver-ptr-metadata
Fix unsizing through smart pointers
2 parents 81bc117 + 40ca4ab commit 8937893

25 files changed

Lines changed: 625 additions & 82 deletions

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

‎charon-ml/src/PrintFmt.ml‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,10 +628,11 @@ and pp_cast_kind (env : fmt_env) (fmt : Format.formatter) (cast : cast_kind) :
628628
| CastSubtype tgt -> Format.fprintf fmt "subtype<%a>" (pp_ty env) tgt
629629
| CastRawPtr tgt -> Format.fprintf fmt "cast<%a>" (pp_ty env) tgt
630630
| CastTransmute tgt -> Format.fprintf fmt "transmute<%a>" (pp_ty env) tgt
631-
| CastUnsize (tgt, meta) ->
631+
| CastUnsize (tgt, meta, _) ->
632632
Format.fprintf fmt "unsize_cast<%a, %a>" (pp_ty env) tgt
633633
(pp_unsizing_metadata env) meta
634-
| CastConcretize tgt -> Format.fprintf fmt "concretize<%a>" (pp_ty env) tgt
634+
| CastConcretize (tgt, _) ->
635+
Format.fprintf fmt "concretize<%a>" (pp_ty env) tgt
635636

636637
and pp_match_pattern (env : fmt_env) (fmt : Format.formatter)
637638
(cv : constant_expr) : unit =

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -540,16 +540,18 @@ and cast_kind_of_json (ctx : of_json_ctx) (js : json) :
540540
| `Assoc [ ("Subtype", _0) ] ->
541541
let* _0 = ty_of_json ctx _0 in
542542
Ok (CastSubtype _0)
543-
| `Assoc [ ("Unsize", `List [ _0; _1 ]) ] ->
543+
| `Assoc [ ("Unsize", `List [ _0; _1; _2 ]) ] ->
544544
let* _0 = ty_of_json ctx _0 in
545545
let* _1 = unsizing_metadata_of_json ctx _1 in
546-
Ok (CastUnsize (_0, _1))
546+
let* _2 = list_of_json field_id_of_json ctx _2 in
547+
Ok (CastUnsize (_0, _1, _2))
547548
| `Assoc [ ("Transmute", _0) ] ->
548549
let* _0 = ty_of_json ctx _0 in
549550
Ok (CastTransmute _0)
550-
| `Assoc [ ("Concretize", _0) ] ->
551+
| `Assoc [ ("Concretize", `List [ _0; _1 ]) ] ->
551552
let* _0 = ty_of_json ctx _0 in
552-
Ok (CastConcretize _0)
553+
let* _1 = list_of_json field_id_of_json ctx _1 in
554+
Ok (CastConcretize (_0, _1))
553555
| _ -> Error "")
554556

555557
and const_generic_param_of_json (ctx : of_json_ctx) (js : json) :
@@ -4512,9 +4514,10 @@ and v_table_field_of_json (ctx : of_json_ctx) (js : json) :
45124514
| `String "Size" -> Ok VTableSize
45134515
| `String "Align" -> Ok VTableAlign
45144516
| `String "Drop" -> Ok VTableDrop
4515-
| `Assoc [ ("Method", _0) ] ->
4517+
| `Assoc [ ("Method", `List [ _0; _1 ]) ] ->
45164518
let* _0 = trait_method_id_of_json ctx _0 in
4517-
Ok (VTableMethod _0)
4519+
let* _1 = list_of_json field_id_of_json ctx _1 in
4520+
Ok (VTableMethod (_0, _1))
45184521
| `Assoc [ ("SuperTrait", _0) ] ->
45194522
let* _0 = trait_clause_id_of_json ctx _0 in
45204523
Ok (VTableSuperTrait _0)

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,15 @@ and cast_kind_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
491491
| 7 ->
492492
let* _0 = ty_of_postcard ctx st in
493493
let* _1 = unsizing_metadata_of_postcard ctx st in
494-
Ok (CastUnsize (_0, _1))
494+
let* _2 = list_of_postcard field_id_of_postcard ctx st in
495+
Ok (CastUnsize (_0, _1, _2))
495496
| 8 ->
496497
let* _0 = ty_of_postcard ctx st in
497498
Ok (CastTransmute _0)
498499
| 9 ->
499500
let* _0 = ty_of_postcard ctx st in
500-
Ok (CastConcretize _0)
501+
let* _1 = list_of_postcard field_id_of_postcard ctx st in
502+
Ok (CastConcretize (_0, _1))
501503
| _ -> Error ("unknown enum variant tag: " ^ string_of_int __tag))
502504

503505
and const_generic_param_of_postcard (ctx : of_postcard_ctx)
@@ -3750,7 +3752,8 @@ and v_table_field_of_postcard (ctx : of_postcard_ctx) (st : postcard_state) :
37503752
| 2 -> Ok VTableDrop
37513753
| 3 ->
37523754
let* _0 = trait_method_id_of_postcard ctx st in
3753-
Ok (VTableMethod _0)
3755+
let* _1 = list_of_postcard field_id_of_postcard ctx st in
3756+
Ok (VTableMethod (_0, _1))
37543757
| 4 ->
37553758
let* _0 = trait_clause_id_of_postcard ctx st in
37563759
Ok (VTableSuperTrait _0)

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ and cast_kind =
285285
same signature. *)
286286
| CastSubtype of ty
287287
(** Convert a value to a supertype; this has no runtime effect. *)
288-
| CastUnsize of ty * unsizing_metadata
288+
| CastUnsize of ty * unsizing_metadata * field_id list
289289
(** [Unsize coercion](https://doc.rust-lang.org/std/ops/trait.CoerceUnsized.html).
290290
This is either [[T; N]] -> [[T]] or [T: Trait] -> [dyn Trait]
291291
coercions, behind a pointer (reference, [Box], or other type that
@@ -296,20 +296,18 @@ and cast_kind =
296296
| CastTransmute of ty
297297
(** Reinterprets the bits of a value of one type as another type, i.e.
298298
exactly what [[std::mem::transmute]] does. *)
299-
| CastConcretize of ty
299+
| CastConcretize of ty * field_id list
300300
(** Converts a receiver type with [dyn Trait<...>] to a concrete type [T],
301301
used in vtable method shims. Valid conversions are references, raw
302302
pointers, and (optionally) boxes:
303303
- [&[mut] dyn Trait<...>] -> [&[mut] T]
304304
- [*[mut] dyn Trait<...>] -> [*[mut] T]
305305
- [Box<dyn Trait<...>>] -> [Box<T>] when no [--treat-box-as-builtin]
306+
- smart-pointers, e.g. [Rc<dyn Trait<...>>] -> [Rc<T>] or
307+
[Pin<&mut dyn Trait<...>>] -> [Pin<&mut T>]
306308
307309
For possible receivers, see:
308310
<https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>.
309-
Other receivers, e.g., [Rc] should be unpacked before the cast and
310-
re-boxed after. FIXME(ssyram): but this is not implemented yet,
311-
namely, there may still be something like
312-
[Rc<dyn Trait<...>> -> Rc<T>] in the types.
313311
314312
With [--index-to-function-calls], this also converts [&[T]] to
315313
[&[T; N]] (or the mutable equivalents) when an array subslice
@@ -1623,7 +1621,7 @@ and v_table_field =
16231621
| VTableSize
16241622
| VTableAlign
16251623
| VTableDrop
1626-
| VTableMethod of trait_method_id
1624+
| VTableMethod of trait_method_id * field_id list
16271625
| VTableSuperTrait of trait_clause_id
16281626

16291627
and variant = {

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

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ pub enum CastKind {
196196
/// (reference, `Box`, or other type that implements `CoerceUnsized`).
197197
///
198198
/// The special case of `&[T; N]` -> `&[T]` coercion is caught by `UnOp::ArrayToSlice`.
199-
Unsize(Ty, UnsizingMetadata),
199+
Unsize(Ty, UnsizingMetadata, MetadataPath),
200200
/// Reinterprets the bits of a value of one type as another type, i.e. exactly what
201201
/// [`std::mem::transmute`] does.
202202
Transmute(Ty),
@@ -205,15 +205,16 @@ pub enum CastKind {
205205
/// - `&[mut] dyn Trait<...>` -> `&[mut] T`
206206
/// - `*[mut] dyn Trait<...>` -> `*[mut] T`
207207
/// - `Box<dyn Trait<...>>` -> `Box<T>` when no `--treat-box-as-builtin`
208+
/// - smart-pointers, e.g. `Rc<dyn Trait<...>>` -> `Rc<T>` or
209+
/// `Pin<&mut dyn Trait<...>>` -> `Pin<&mut T>`
208210
///
209211
/// For possible receivers, see: <https://doc.rust-lang.org/reference/items/traits.html#dyn-compatibility>.
210-
/// Other receivers, e.g., `Rc` should be unpacked before the cast and re-boxed after.
211-
/// FIXME(ssyram): but this is not implemented yet, namely, there may still be
212-
/// something like `Rc<dyn Trait<...>> -> Rc<T>` in the types.
213212
///
214213
/// With `--index-to-function-calls`, this also converts `&[T]` to `&[T; N]` (or the mutable
215214
/// equivalents) when an array subslice projection determines the length statically.
216-
Concretize(Ty),
215+
// FIXME(#855): smart-pointer receivers should rather be unpacked before the cast and re-packed after,
216+
// so that the cast only ever applies to built-in pointers.
217+
Concretize(Ty, MetadataPath),
217218
}
218219

219220
/// Nullary operation
@@ -267,6 +268,11 @@ impl BorrowKind {
267268
}
268269
}
269270

271+
/// For `Unsize` and `Concretize` casts, the fields to follow from the cast value to the built-in
272+
/// pointer whose metadata is changed, e.g. `[ptr, pointer]` for `Rc<T>`. Empty for built-in
273+
/// pointers.
274+
pub type MetadataPath = Vec<FieldId>;
275+
270276
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
271277
#[derive(EnumIsA, VariantName)]
272278
#[derive(SerializeState, DeserializeState, Drive, DriveMut, DriveTwo)]

‎charon/src/ast/items/type_decl.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub enum VTableField {
167167
Size,
168168
Align,
169169
Drop,
170-
Method(TraitMethodId),
170+
Method(TraitMethodId, MetadataPath),
171171
SuperTrait(TraitClauseId),
172172
}
173173

‎charon/src/bin/charon-driver/hax/constant_utils/uneval.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ fn unsizing_metadata<'tcx, S: UnderOwnerState<'tcx>>(
151151
) -> UnsizingMetadata<'tcx> {
152152
let from = ty::Ty::new_imm_ptr(s.base().tcx, from);
153153
let to = ty::Ty::new_imm_ptr(s.base().tcx, to);
154-
compute_unsizing_metadata(s, from, to)
154+
let (path, metadata) = compute_unsizing_metadata(s, from, to);
155+
assert!(path.is_empty(), "Non-empty unsize path in const ptr");
156+
metadata
155157
}
156158

157159
/// The provenance to give to the bytes of a pointer into the given allocation.

0 commit comments

Comments
 (0)