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

Type inference complains about unknown size for dyn Any #127005

Closed
Wasabi375 opened this issue Jun 26, 2024 · 3 comments
Closed

Type inference complains about unknown size for dyn Any #127005

Wasabi375 opened this issue Jun 26, 2024 · 3 comments

Comments

@Wasabi375
Copy link

Inferring the resulting type of Box::downcast_unsafe complains about dyn Any not being sized. However if the type is specified directly, everything works as expected.

I tried this code:

pub struct BoxWrapper<T: Any> {
    data: Box<dyn Any>,

    _phantom: PhantomData<T>,
}

impl<T: Any> BoxWrapper<T> {

    pub fn downcast<TTarget: Any>(self) -> Result<BoxWrapper<TTarget>, Self> {
        if !self.data.is::<TTarget>() {
            return Err(self);
        }

        // let data: Box<TTarget> works fine
        let data: Box<_> = unsafe { self.data.downcast_unchecked() };

        Ok(BoxWrapper {
            data,
            _phantom: PhantomData,
        })
    }
}

This creates the following error message:

error[E0277]: the size for values of type `dyn Any` cannot be known at compilation time
    --> src/lib.rs:22:47
     |
22   |         let data: Box<_> = unsafe { self.data.downcast_unchecked() };
     |                                               ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
     |
     = help: the trait `Sized` is not implemented for `dyn Any`
note: required by an implicit `Sized` bound in `Box::<(dyn Any + 'static), A>::downcast_unchecked`
    --> /playground/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1820:38
     |
1820 |     pub unsafe fn downcast_unchecked<T: Any>(self) -> Box<T, A> {
     |                                      ^ required by the implicit `Sized` requirement on this type parameter in `Box::<dyn Any, A>::downcast_unchecked`

However specifying the type directly (let data: Box<TTarget> = ... ) does not produce an error.

I would expect to either get no error message, or an error stating that the type can not be inferred.

Meta

rustc --version --verbose:

cargo 1.79.0-nightly (48eca1b16 2024-04-12)
release: 1.79.0-nightly
commit-hash: 48eca1b164695022295ce466b64b44e4e0228b08
commit-date: 2024-04-12
host: x86_64-unknown-linux-gnu
libgit2: 1.7.2 (sys:0.18.3 vendored)
libcurl: 8.6.0-DEV (sys:0.4.72+curl-8.6.0 vendored ssl:OpenSSL/1.1.1w)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: NixOS 23.11.0 [64-bit]
@Wasabi375 Wasabi375 added the C-bug Category: This is a bug. label Jun 26, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 26, 2024
@marc0246
Copy link
Contributor

The type you defined in your struct is Box<dyn Any>. This is the type that gets correctly inferred. It also correctly can't be downcast to because it's unsized. If you specify a concrete type to downcast to, what you are doing is downcasting and then unsizing again, which is really a no-op.

@marc0246
Copy link
Contributor

@Wasabi375 Do you see why this is working as intended?

@Wasabi375
Copy link
Author

Yeah, not sure how I got myself confused here. Thanks for the help anyways.

@saethlin saethlin removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants