Boxy
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Arbitrary Self Types, VTable calls, and Object Safety disclaimer: when i wrote this I did not think through the fact that we allow for `<Self as SuperTrait>::Assoc` in dyn-compatible traits' methods. I don't think this really matters because the exact type the alias normalizes to is preserved in the dyn type and is in an invariant position. ## Soundness requirements of VTable calls Calling trait methods on trait objects involves converting the reciever involving the trait object, to the equivalent underling type. For example when calling some method on `&dyn Trait` it is converted to a `&Foo` at some point as that is the reciever that was used in the trait implementation. This operation is not visible to the type checker as (rather fundamentally to the concept of trait objects) we do not actually know what the underlying type of a trait object is. As we never see the underlying type of a trait object when calling methods, we are unable to check that any where clauses on the method involving `Self` hold for the underlying type. Due to this there is a fundamental property of the language that must be upheld in order for trait objects to be sound: Given some impl of `Trait` for `SelfTy`, for every method on the trait, the where clauses on `dyn Trait`'s builtin implementation's method must be sufficient to prove all where clauses on `SelfTy`'s method and that the reciever is "well formed". If this is not upheld then it is not possible for type checking to ensure that calling a trait method on a trait object is sound. It *must* be true that it being valid to call a trait method on a trait object implies it would be valid to call the underlying type's trait method. It is the job of the object safety rules to ensure this rule is upheld. The combination of the `arbitrary_self_types` and `derive_coerce_pointee` features means that we now have to be sure that the object safety rules are sufficient for any possible ADT that can be written in the language not *just* the "well behaved" types in std. Specifically, what we are looking to determine is that given some arbitrary user defined type `Ptr<P1...PN, P>` and some type `Bar<...>` that implements `Trait<...>`, where `Trait` defines a method with a reciever of `Ptr<..., Self>`, is the following guaranteed: All bounds on `<dyn Trait<...> as Trait<...>>::method::<...>` holding implies that all bounds hold on `<Underlying as Trait<...>>::method::<...>` and that in turn implies that `Ptr<..., Self>` is well formed. ## Does this requirement hold There are ~4 ways for bounds to be introduced that we must ensure hold for the underlying type when calling `<dyn Trait as Trait>::bar`: ```rust impl<...> Trait<...> for Bar</* ... */> where // *1 /* ... */, { fn bar( self: Ptr</* ... */, Self> ) where // *2 Self: SomeTrait, // *3 Self: 'a, // *4 Self: AutoTrait, } ``` - `*1` is checked when coercing `Foo<...>` to `dyn Trait`. Trait objects are invariant over the trait parameters so it is not possible to use subtyping to change a type/lifetime present in `Trait<...>` to one that would result in bounds no longer holding. The self type is erased so it is not possible to use subtyping to change any lifetimes or types present in `Foo<...>`. Are there any asteriks here involving raw pointer recievers as those would allow arbitrarily changing trait parameters. E.g. `*mut dyn Trait<'static> as *mut dyn Trait<'a>`? No, we forbid pointer casts involving arguments to object types' traits - `*2` Arbitrary where clauses on `bar` involving `Self` are outright forbidden, there are however two exceptions to this rule, lifetime bounds and auto trait bounds are allowed to reference `Self`. Whether `Self` is `dyn Trait` or `Foo<...>` does not affect the behaviour of a bound not referencing `Self` in any way. Are there any asteriks here involving `T: Trait<Assoc = Self>` bounds on the impl? Don't think so, predicates of the trait are required to be proven when calling methods on trait objects which requires `T: Trait<Assoc = dyn Trait>` to hold which is not possible if `T: Trait<Assoc = Underlying>` also holds (which it must). - `*3` Type outlives bounds involving `Self` are okay if an object type's lifetime is always outlived by the underlying type, as outlives are transitive. `dyn Trait<P1..PN> + 'a: 'b` holding implies `Underlying: 'b` if `Underlying: 'a` holds. Object types are covariant over the object lifetime so it is only possible to shrink the lifetime of the object type which cannot cause more `Self: '...` bounds to hold. If an object type is used in a contravariant position this is not necessarily true as `fn(&dyn Trait + 'a)` can be turned into `fn(&dyn Trait + 'static)`. It is therefore important that `DispatchFromDyn` does not allow dispatching from trait objects in contravariant positions. I do not believe it is possible to write a type that is contravariant over a type parameter while also using it in a way that allows accessing a vtable. Having a vtable requires a pointer/reference/ownership of the object type which forces covariance (which turns to invariance in the presence of a PhantomData asking for contravariance) Unfortunately, raw pointers allow for *safe* casting of object type lifetimes which allows for an object type to have its underlying type *not* outlive the object type's lifetime bound. It must be `unsafe` to cast the lifetime of an object type, i.e. `*const dyn Trait + 'a -> *const dyn Trait + 'static` cannot be a safe operation in the general case as there may be a `Self: 'static` bound on a method. [#136702](https://github.com/rust-lang/rust/issues/136702) - `*4` Auto trait bounds with `Self` as the the self type are also allowed. This is only okay if an object type implements auto traits if (and only if) the underlying type also implements the auto trait. This is enforced by forbidding manual implementations of auto traits for object types, i.e. `unsafe impl Send for dyn Trait` is a hard error. Also, when coercing from the underlying type to the object type, auto traits are checked for the underlying type. This, however, can by bypassed with raw pointer recievers as you may safely cast from `*const dyn Trait` to `*const dyn Trait + Send` and then call a method with a `Self: Send` bound. [#127323](https://github.com/rust-lang/rust/issues/127323) Having gone through all of that I feel somewhat convinced that the object safety rules (once fixed for pointers) do behave correctly under `arbitrary_self_types/derive_coerce_pointee` and allow us to ensure all where clauses on the underlying type's method hold. Then all that is left is to actually check that in the context of `<Bar<...> as Trait<...>>::method`, `Ptr<..., Bar<...>>` is actually well formed. If it is then, in theory, proving all bounds on `<dyn Trait<...> as Trait>::method` implies that all bounds on `<Underlying as Trait>::method` hold which implies that `Ptr<..., Bar<...>>` is well formed. It being required *for soundness* to check in the impl (or trait definition) that the reciever is well formed is a new constraint on the language as far as I know. Previously we could just look at the list of types in std that are valid types to do vtable calls from and tell that none of them bound `Self` in any interesting ways. Now, however, types with *arbitrary* where clauses including those that may hold for `dyn Trait` but not the underlying type of the object type are able to exist and be used for vtable calls. I do not believe this poses any kind of problem for the language but I do think it is *interesting*. ## Why are pointer casts only a problem *now* Something that is interesting to think about is why raw pointers allowing safe casting of object types hasn't caused problems before `arbitrary_self_types/derive_coerce_pointee` ([#127323](https://github.com/rust-lang/rust/issues/127323), [#136702](https://github.com/rust-lang/rust/issues/136702)). The answer to this, I believe, is just that all types in std that allow performing vtable calls also require unsafe to be constructed from a raw pointer. For example while you *can*: - Create some `Box<Foo>` - Coerce it to `Box<dyn Trait>` - Call `Box::into_raw` - Cast to`*const dyn Trait + Send + Sync + 'static` - Call `Box::from_raw` - Do a vtable call to some method that required `Self: Send + 'static` The `Box::from_raw` step requires unsafe, and this is true of all smart pointers in std that allow for vtable calls. What `arbitrary_self_types`/`derive_coerce_pointee` do is allow you to *safely* construct a smart pointer type that can make vtable calls. If it's going to be safe to make vtable calls, and safe to construct types that can make vtable calls, then it just has to be *unsafe* to have an object type that allows methods to be called on its vtable that shouldnt be called[^1]. In some sense raw pointers and vtable calls have *never* been sound, but as the unsoundness only affects implementors of `std`/`core` and the traits involved were relatively obviously "special" it just didn't matter very much. [^1]: An alternative might be for `derive(CoercePointee)` to require the field containing the vtable to be unsafe (though such a feature is not even implemented unstabley). This would effectively force construction of the smart pointer to be unsafe (slash safely encapsulated)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully