Skip to content

Refactor coercion logic for future usage inside codegen - #1837

Merged
celinval merged 10 commits into
model-checking:mainfrom
celinval:kani-middle
Nov 9, 2022
Merged

celinval merged 10 commits into
model-checking:mainfrom
celinval:kani-middle

Conversation

@celinval

@celinval celinval commented Nov 2, 2022 •

Copy link
Copy Markdown
Contributor

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

  • Each commit message has a non-empty body, explaining why the change was made
  • Methods or procedures are documented
  • Regression or unit tests are included, or existing tests cover the modified code
  • My PR is restricted to a single feature or bugfix

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.

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.
@celinval
celinval requested a review from a team as a code owner November 2, 2022 19:50
@celinval
celinval requested a review from danielsn November 4, 2022 18:48
/// - 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this nest arbitrarily deep? Smart pointer of wrapper of smart pointer?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. The casting stop in the first layer.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=0218b81a56f650edb521a3ec2ef72654

/// `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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is this stored?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not stored. It's generated as part of each iteration.

/// ```rust,ignore
/// CoerceUnsizedInfo {
/// field: None,
/// src_ty: Ty, /* *const RcBox<String> */,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it stops at the box, and doesn't go inside? I guess that makes some sense cause the box is really a pointer?

@celinval celinval Nov 4, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert that src_type is sized and des_type is unsized?

@celinval celinval Nov 4, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, both cases here will always be sized. We are traversing the smart pointers, not the actual DST here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a tracking issue to boost this into main rustc?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is magic. I'm going to assume it works 🪄

@tedinski tedinski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +8 to +14
//! 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).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@celinval celinval Nov 9, 2022 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Although I've seen no-ops ones. Done.

Comment on lines +24 to +32
/// 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)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 &u8 to &dyn Debug), extract the pair of coresponding base types T, U (e.g. u8, dyn Debug), where the source base type T must implement Unsize<U> and U is either a trait or slice.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure

Comment on lines +34 to +43
/// 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to be consistent about saying dyn Debug?

@celinval

celinval commented Nov 8, 2022

Copy link
Copy Markdown
Contributor Author

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.

I believe the answer is "there is no way to write one". I don't think we can create a custom object of type Ty.

@danielsn danielsn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for NonNull<u8>, would we return (u8, dyn Debug) or (NonNull<u8>, NonNull<dyn Debug>)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(u8, 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. Let me improve that.

@celinval
celinval merged commit 2b3a9be into model-checking:main Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants