rust-lang / rust-lang/nomicon

Section on coercions fails to mention requirement on trait object creation

Open
#45 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
CSS
Stars
2.3k
Forks
325
PR merge metrics
No merged PRs in 30d

Description

Rust converts a Pointer<T> to a Pointer<Trait> where T implements Trait and Pointer is a type constructor like & or Box, only if Pointer<T> is a thin pointer. The Nomicon suggests otherwise.

According to the "Coercions" section of the Nomicon:

  • Pointer<T>: CoerceUnsized<Pointer<U>> for all pointer types, where T: Unsize<U>.
  • Unsize<U> is automatically implemented for T if T: U.

Suppose I have two traits T1 and T2, and impl T2 for T1. Since T1: Unsize<T2>, the Nomicon claims that Pointer<T1>: CoerceUnsized<Pointer<T2>>. But I can't convert a Box<T1> to a Box<T2>; that would be a doubly-fat pointer, since it needs both the vtable for impl T2 for T1 and whatever vtable Box<T1> was using.

There was a longish thread about this; I think this post is where I finally got it right: https://users.rust-lang.org/t/trait-objects-of-trait-objects/14077/27

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Nomicon's "Coercions" section and compare its statements about CoerceUnsized, Unsize, and trait-object creation with the Box to Box example in the issue. Read the linked Rust Users discussion for the settled explanation, then revise the section to state the missing thin-pointer requirement and verify that the example is accurately covered.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.