Skip to content

Cannot dyn upcast to target w/ auto traits implied by source's principal's supertraits #119335

Closed

Description

@compiler-errors

Originally posted by @Ekleog in #65991 (comment)

I just tried using this feature to upcast to Any + Send + Sync, and it seems to me like this is currently not allowed:

trait DynSized: 'static + Any + Send + Sync + DeepSizeOf {}
impl<T: 'static + Any + Send + Sync + DeepSizeOf> DynSized for T {}

fn main() {
    let ptr = Arc::new(()) as Arc<dyn DynSized>;
    let ptr_any = ptr as Arc<dyn Any + Send + Sync>;
    // my goal:
    let res = Arc::<dyn Any + Send + Sync>::downcast::<()>(ptr);
}

(playground link)

With references, the situation does not seem better than with Arc: this does not compile either

fn main() {
    let ptr = &() as &'static dyn DynSized;
    let ptr_any = ptr as &'static (dyn Any + Send + Sync);
}

(playground link)

Is this expected? I can't find any reference to this limitation, neither explicitly in the RFC text nor in this tracking issue. The RFC text does mention not supporting upcasting to multiple traits, but I'd have expected lifetimes as well as auto traits to not be included in the limitation.

If it is expected that this feature is stabilized without this support, does anyone know where I should watch, to know of the news on that front?


Minimized:

trait Source: Send + Target {}

fn upcast(x: &dyn Source) -> &(dyn Target + Send) { x }

We should be able to upcast to a dyn type w/ auto-traits that show up in the principal's supertraits too.

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 27, 2023
  2. compiler-errors commented on Dec 27, 2023

    @compiler-errors
    ContributorAuthor

    I'll fix it.

    @rustbot claim

  3. Ekleog commented on Dec 27, 2023

    @Ekleog

    This would be awesome, thank you so much for looking into it! ❤️

  4. added
    C-bugCategory: This is a bug.
    T-typesRelevant to the types team, which will review and decide on the PR/issue.
    and removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Dec 27, 2023
  5. crlf0710 commented on Dec 27, 2023

    @crlf0710
    Member

    This includes the previously unsupported dyn Source => dyn Source + Send coercion, right? I think it's a good enhancement.

  6. added
    C-enhancementCategory: An issue proposing an enhancement or a PR with one.
    and removed
    C-bugCategory: This is a bug.
    on Dec 27, 2023
  7. compiler-errors commented on Dec 27, 2023

    @compiler-errors
    ContributorAuthor

    @crlf0710: Yes, if Source has Send as one of its supertraits.

  8. PoignardAzur commented on Jan 25, 2024

    @PoignardAzur
    Contributor

    A more minimal example:

    trait AnySendSync: Any + Send + Sync {}
    impl<T: Any + Send + Sync> AnySendSync for T {}
    
    fn main() {
        let ptr = &() as &dyn AnySendSync;
        // Compile error, unless you remove the "+ Send + Sync" part
        let ptr = ptr as &(dyn Any + Send + Sync);
    }
  9. compiler-errors commented on Jan 3, 2025

    @compiler-errors
    ContributorAuthor

    This got fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions