Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tracking issue for release notes of #134367: Stabilize feature(trait_upcasting) #134540

Open
1 of 3 tasks
rustbot opened this issue Dec 20, 2024 · 0 comments
Open
1 of 3 tasks
Assignees
Labels
A-trait-objects Area: trait objects, vtable layout F-trait_upcasting `#![feature(trait_upcasting)]` release-blog-post Marks issues tracking what text to put in the release blog post. relnotes Marks issues that should be documented in the release notes of the next release. relnotes-tracking-issue Marks issues tracking what text to put in release notes. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Milestone

Comments

@rustbot
Copy link
Collaborator

rustbot commented Dec 20, 2024

This issue tracks the release notes text for #134367.

Steps

  • Proposed text is drafted by PR author (or team) making the noteworthy change.
  • Issue is nominated for release team review of clarity for wider audience.
  • Release team includes text in release notes/blog posts.

Release notes text

The responsible team for the underlying change should edit this section to replace the automatically generated link with a succinct description of what changed, drawing upon text proposed by the author (either in discussion or through direct editing).

# Language
- [Stabilize the ability to upcast a trait object to one of its supertraits.](https://github.com/rust-lang/rust/pull/134367)

Tip

Use the previous releases categories to help choose which one(s) to use.
The category will be de-duplicated with all the other ones by the release team.

More than one section can be included if needed.

Release blog section

If the change is notable enough for inclusion in the blog post, the responsible team should add content to this section.
Otherwise leave it empty.

# Trait upcasting

This release includes a long awaited feature — the ability to upcast trait objects.
If a trait has a [supertrait](https://doc.rust-lang.org/reference/items/traits.html#supertraits) you can coerce a reference to said trait object to a reference to a trait object of the super trait:

```rust
trait Trait: Supertrait {}
trait Supertrait {}

fn upcast(x: &dyn Trait) -> &dyn Supertrait {
    x
}
```

The same would work with any other kind of (smart)-pointer, like `Arc<dyn Trait> -> Arc<dyn Supertrait>` or `*const dyn Trait -> *const dyn Supertrait`.

Previously this would require a workaround in the form of an `upcast` method in the `Trait` itself (and that would work only for one kind of reference/pointer). Such workarounds are not necessary anymore. 

Note that this adds a new _safety invariant_  to raw pointers — if a pointer points to a trait object, vtable must be valid for that trait. This means that "leaking" a raw pointer to a trait object with an invalid vtable into safe code may lead to undefined behavior.

Trait upcasting may be especially useful with the `Any` trait, as it allows upcasting your trait object to `dyn Any` to call the downcast methods. Whereas before you'd have to write workarounds for this or use external crates.

```rust
use std::any::Any;

trait MyAny: Any {}

impl dyn MyAny {
    fn downcast_ref<T>(&self) -> Option<&T> {
        (self as &dyn Any).downcast_ref()
    }
}
```

You can [learn more about trait upcasting in the Rust reference](https://doc.rust-lang.org/reference/type-coercions.html#unsized-coercions).

cc @WaffleLapkin, @compiler-errors -- origin issue/PR authors and assignees for starting to draft text

@rustbot rustbot added F-trait_upcasting `#![feature(trait_upcasting)]` relnotes Marks issues that should be documented in the release notes of the next release. relnotes-tracking-issue Marks issues tracking what text to put in release notes. T-lang Relevant to the language team, which will review and decide on the PR/issue. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 20, 2024
@WaffleLapkin WaffleLapkin self-assigned this Dec 20, 2024
@WaffleLapkin WaffleLapkin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 21, 2024
@rustbot rustbot added this to the 1.86.0 milestone Feb 7, 2025
HactarCE added a commit to HactarCE/Hyperspeedcube that referenced this issue Mar 10, 2025
I need Rust v1.86.0 (currently beta) for rust-lang/rust#134540
@cuviper cuviper added the release-blog-post Marks issues tracking what text to put in the release blog post. label Mar 21, 2025
@cuviper cuviper marked this as a duplicate of #138807 Mar 21, 2025
@jieyouxu jieyouxu added T-types Relevant to the types team, which will review and decide on the PR/issue. A-trait-objects Area: trait objects, vtable layout labels Mar 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-trait-objects Area: trait objects, vtable layout F-trait_upcasting `#![feature(trait_upcasting)]` release-blog-post Marks issues tracking what text to put in the release blog post. relnotes Marks issues that should be documented in the release notes of the next release. relnotes-tracking-issue Marks issues tracking what text to put in release notes. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants