Skip to content

Re-investigate alignment and size #9

@skade

Description

@skade

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions