# Can unsafe smart pointer downcasts be correct?

**URL:** <https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229>\
**Category:** libs\
**Created:** [January 28, 2024, 5:33am UTC](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229 "2024-01-28T05:33:51Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Nemo157](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nemo157/32/11585_2.png) [@Nemo157](https://internals.rust-lang.org/u/Nemo157)\
**Post date:** [January 28, 2024, 12:02pm UTC](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229/2 "2024-01-28T12:02:11Z")

</div>

There was some discussion on whether `Box::<dyn Any>::downcast_unchecked` should instead be `Box::<impl ?Sized>::cast_unchecked`, which if applied to `Arc`s API too would allow this:

> <https://github.com/rust-lang/rust/issues/90850#issuecomment-1269293549>
>
> Feature gate: \`#!\[feature(downcast\_unchecked)\]\`
> 
> This is a tracking issue for …the \`downcast\_unchecked\`, \`downcast\_ref\_unchecked\`, and \`downcast\_mut\_unchecked\` methods.
> 
> \### Public API
> 
> \`\`\`rust
> impl dyn Any (+ Send + Sync) {
> pub unsafe fn downcast\_ref\_unchecked\<T: Any\>(&self) -\> &T;
> pub unsafe fn downcast\_mut\_unchecked\<T: Any\>(&mut self) -\> &mut T;
> }
> 
> impl\<A: Allocator\> Box\<dyn Any (+ Send + Sync), A\> {
> pub unsafe fn downcast\_unchecked\<T: Any\>(&self) -\> Box\<T, A\>;
> }
> \`\`\`
> 
> \### Steps / History
> 
> \- \[x\] Implementation: #90851
> \- \[\] Final comment period (FCP)
> \- \[\] Stabilization PR
> 
> \### Unresolved Questions
> 
> \- None yet.

My feeling is that `CoerceUnsized` does force this to be valid, since there is no code run during the coercion there's no way `Arc` could make it invalid; but I don't know that there's enough documentation about what unsizing-coercions do to actually make that a firm guarantee.

---

_[View the full topic](https://internals.rust-lang.org/t/can-unsafe-smart-pointer-downcasts-be-correct/20229)._
