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

Re-investigate alignment and size #9

Open
skade opened this issue Dec 17, 2020 · 1 comment
Open

Re-investigate alignment and size #9

skade opened this issue Dec 17, 2020 · 1 comment

Comments

@skade
Copy link
Owner

skade commented Dec 17, 2020

While the original implementation (rust-lang/rust#44295) seems to document a size of 0 and an alignment of 1 for extern types, the current state seems to be that they are unsized (rust-lang/rust#49708).

It even goes a little further by not considering them values.

#![feature(extern_types)]

extern "C" {
    type foo;
    
}

fn main() {
    std::mem::size_of::<foo>();
    std::mem::align_of::<foo>();
    std::mem::size_of_val(&foo);
    std::mem::align_of_val(&foo);
}
Compiling playground v0.0.1 (/playground)
error[E0423]: expected value, found foreign type `foo`
  --> src/main.rs:11:28
   |
11 |     std::mem::size_of_val(&foo);
   |                            ^^^ not a value

error[E0423]: expected value, found foreign type `foo`
  --> src/main.rs:12:29
   |
12 |     std::mem::align_of_val(&foo);
   |                             ^^^ not a value

error[E0277]: the size for values of type `foo` cannot be known at compilation time
   --> src/main.rs:9:25
    |
9   |     std::mem::size_of::<foo>();
    |                         ^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `foo`

error[E0277]: the size for values of type `foo` cannot be known at compilation time
   --> src/main.rs:10:26
    |
10  |     std::mem::align_of::<foo>();
    |                          ^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `foo`

This is hard to emulate, though, as unsized types are not considered FFI-safe.

@nvzqz
Copy link
Contributor

nvzqz commented Jan 12, 2021

You're getting these errors:

error[E0423]: expected value, found foreign type `foo`
  --> src/main.rs:11:28
   |
11 |     std::mem::size_of_val(&foo);
   |                            ^^^ not a value

error[E0423]: expected value, found foreign type `foo`
  --> src/main.rs:12:29
   |
12 |     std::mem::align_of_val(&foo);
   |                             ^^^ not a value

Because you're treating it like struct foo; where the initializer uses the same name. extern type foo; isn't meant to be instantiated in normal safe Rust code.

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

2 participants