Repository navigation
Refactor coercion logic for future usage inside codegen - #1837
Conversation
We currently have a few issues with how we are generating code for casting (model-checking#566, and model-checking#1528). The structure of the code is also hard to understand and maintain (see model-checking#1531 for more details). This PR is the first part of the fix I developed. This change moves the coercion specific code to its own module and it introduces an iterator that traverses the coercion path.
| /// - Apply base case to extract the pair `(Wrapper<T>, Wrapper<U>)`. | ||
| /// - Extract the tail element of the struct which are of type `T` and `U`, respectively. | ||
| /// 4. Coercion between smart pointers of wrapper structs. | ||
| /// - Apply the logic from item 2 then item 3. |
There was a problem hiding this comment.
can this nest arbitrarily deep? Smart pointer of wrapper of smart pointer?
There was a problem hiding this comment.
No. The casting stop in the first layer.
There was a problem hiding this comment.
I.e.: You can't do:
let concrete = Rc::new(Box::new(String::from("hi")));
let dynamic : Rc<Box<dyn Display>> = concrete; You need to first do the coercion, then nest things:
let boxed = Box::new(String::from("hi"));
let dynamic : Box<dyn Display> = boxed; // Coercion happens here.
let rc = Rc::new(dynamic); // The external Rc only contains a thin pointer.| /// `T` is sized and `U` is unsized, this iterator will walk over the fields that lead to a | ||
| /// pointer to `T`, which shall be converted from a thin pointer to a fat pointer. | ||
| /// | ||
| /// This iterator will also include the name of the field that differs in the current iteration. |
There was a problem hiding this comment.
It's not stored. It's generated as part of each iteration.
| /// ```rust,ignore | ||
| /// CoerceUnsizedInfo { | ||
| /// field: None, | ||
| /// src_ty: Ty, /* *const RcBox<String> */, |
There was a problem hiding this comment.
So it stops at the box, and doesn't go inside? I guess that makes some sense cause the box is really a pointer?
There was a problem hiding this comment.
This is not a Box though, it's an RcBox which is the internal structure of Rc. And this returns a const pointer to RcBox, which is the actual fat pointer that shall store the metadata.
There was a problem hiding this comment.
I changed to example to use NonNull, so hopefully it's a bit easier to understand.
| custom_coerce_unsize_info(self.tcx, src_ty, dst_ty); | ||
| assert!(coerce_index < src_fields.len()); | ||
|
|
||
| self.src_ty = Some(src_fields[coerce_index].ty(self.tcx, src_substs)); |
There was a problem hiding this comment.
assert that src_type is sized and des_type is unsized?
There was a problem hiding this comment.
Actually, both cases here will always be sized. We are traversing the smart pointers, not the actual DST here.
There was a problem hiding this comment.
I added the assertion in a different function.
|
|
||
| /// Get information about an unsized coercion. | ||
| /// This code was extracted from `rustc_monomorphize` crate. | ||
| /// <https://github.com/rust-lang/rust/blob/4891d57f7aab37b5d6a84f2901c0bb8903111d53/compiler/rustc_monomorphize/src/lib.rs#L25-L46> |
There was a problem hiding this comment.
Should we have a tracking issue to boost this into main rustc?
There was a problem hiding this comment.
We could, but it's fairly low in our priority list.
| substs: tcx.mk_substs_trait(source_ty, &[target_ty.into()]), | ||
| }); | ||
|
|
||
| match tcx.codegen_select_candidate((ParamEnv::reveal_all(), trait_ref)) { |
There was a problem hiding this comment.
This is magic. I'm going to assume it works 🪄
Conflicts: kani-compiler/src/kani_middle/mod.rs
tedinski
left a comment
There was a problem hiding this comment.
I know the answer is "it's too hard to write" but I wish we could write some unit tests for these functions. Oh well.
| //! The more complicated coercions are DST Coercions (aka Unsized Coercions). These coercions | ||
| //! allow rust to create references to dynamically sized types (such as Traits and Slices) by | ||
| //! casting concrete sized types. Unsized coercions can also be used to cast unsized to unsized | ||
| //! types. These casts work not only on the top of references, but it also handle | ||
| //! references inside structures, allowing the unsized coercions of smart pointers. This last | ||
| //! piece was defined by | ||
| //! [RFC 982 DST Coercion](https://rust-lang.github.io/rfcs/0982-dst-coercion.html). |
There was a problem hiding this comment.
Are all unsized coercions constructing fat pointers of some kinds?
Also nit: can you be explicit about "this last piece". It's a stupid writing thing that bugs me, but I like less indirection in technical writing even if it means slightly more repetition.
There was a problem hiding this comment.
Yes. Although I've seen no-ops ones. Done.
| /// Extract the pair (`T`, `U`) for a unsized coercion where type `T` implements `Unsize<U>`. | ||
| /// I.e., `U` is either a trait or a slice. | ||
| /// For more details, please refer to: | ||
| /// <https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions> | ||
| /// | ||
| /// This is used to determine the vtable implementation that must be tracked by the fat pointer. | ||
| /// | ||
| /// For example, if `&u8` is being converted to `&dyn Debug`, this method would return: | ||
| /// `(u8, dyn Debug)`. |
There was a problem hiding this comment.
I wonder if we could rephrase this opening to start with the concrete example instead of only coming to it later.
Given an unsized coercion (e.g. from
&u8to&dyn Debug), extract the pair of coresponding base typesT, U(e.g.u8,dyn Debug), where the source base typeTmust implementUnsize<U>andUis either a trait or slice.
| /// There are a few interesting cases (references / pointers are handled the same way): | ||
| /// 1. Coercion between `&T` to `&U`. | ||
| /// - This is the base case. | ||
| /// - In this case, we extract the types that are pointed to. | ||
| /// 2. Coercion between smart pointers like `NonNull<T>` to `NonNull<U>`. | ||
| /// - Smart pointers implement the `CoerceUnsize` trait. | ||
| /// - Use CustomCoerceUnsized information to traverse the smart pointer structure and find the | ||
| /// underlying pointer. | ||
| /// - Use base case to extract `T` and `U`. | ||
| /// 3. Coercion between a pointer to a structure whose tail is being coerced. |
There was a problem hiding this comment.
This explanation is the clearest I've seen yet! Nice!
| /// This base is used to determine the information that will be stored in the metadata. | ||
| /// E.g.: In order to convert an `Rc<String>` into an `Rc<dyn Debug>`, we need to generate a | ||
| /// vtable that represents the `impl Debug for String`. So this type will carry the `String` type | ||
| /// as the `src_ty` and the `Debug` trait as `dst_ty`. |
There was a problem hiding this comment.
Do we want to be consistent about saying dyn Debug?
I believe the answer is "there is no way to write one". I don't think we can create a custom object of type |
danielsn
left a comment
There was a problem hiding this comment.
Few minor comments, but LGTM
| /// This is used to determine the vtable implementation that must be tracked by the fat pointer. | ||
| /// | ||
| /// For example, if `&u8` is being converted to `&dyn Debug`, this method would return: | ||
| /// `(u8, dyn Debug)`. |
There was a problem hiding this comment.
for NonNull<u8>, would we return (u8, dyn Debug) or (NonNull<u8>, NonNull<dyn Debug>)
| /// `T` is sized and `U` is unsized, this iterator will walk over the fields that lead to a | ||
| /// pointer to `T`, which shall be converted from a thin pointer to a fat pointer. | ||
| /// | ||
| /// This iterator will also include the name of the field that differs in the current iteration |
There was a problem hiding this comment.
Isn't it the Info not the iterator that has the field name? So we should really say "the result of iterating with this iterator will ..."
There was a problem hiding this comment.
Indeed. Let me improve that.
Description of changes:
We currently have a few issues with how we are generating code for casting (#566, and #1528). The structure of the code is also hard to understand and maintain (see #1531 for more details).
This PR is the first part of the fix I developed. This change moves the coercion specific code to its own module and it introduces an iterator that traverses the coercion path.
Resolved issues:
None
Related RFC:
Call-outs:
Testing:
How is this change tested? Current tests
Is this a refactor change? Yes
Checklist
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.