Skip to content

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

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

Closed
compiler-errors opened this issue Dec 27, 2023 · 6 comments
Assignees
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@compiler-errors
Copy link
Member

compiler-errors commented Dec 27, 2023

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.

@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Dec 27, 2023
@compiler-errors
Copy link
Member Author

I'll fix it.

@rustbot claim

@Ekleog
Copy link

Ekleog commented Dec 27, 2023

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

@saethlin saethlin added C-bug Category: This is a bug. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 27, 2023
@crlf0710
Copy link
Member

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

@compiler-errors compiler-errors added C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Dec 27, 2023
@compiler-errors
Copy link
Member Author

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

@PoignardAzur
Copy link
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);
}

@compiler-errors
Copy link
Member Author

This got fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants