Skip to content

Using "mem::unitialized()" can lead to "Attempted to instantiate uninhabited type" #139

Closed
@tomaka

Description

@tomaka

The following code panics:

fn main() {
    enum Void {}
    let sv = smallvec::SmallVec::<[Void; 8]>::new();
}

With:

thread 'main' panicked at 'Attempted to instantiate uninhabited type [main::Void; 8]', /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libcore/mem.rs:630:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:70
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:58
             at src/libstd/panicking.rs:200
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:215
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:478
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:385
   6: rust_begin_unwind
             at src/libstd/panicking.rs:312
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::panicking::panic
             at src/libcore/panicking.rs:49
   9: core::mem::uninitialized
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libcore/mem.rs:630
  10: <smallvec::SmallVec<A>>::new
             at /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.8/lib.rs:402
  11: playpen::main
             at src/main.rs:3
  12: std::rt::lang_start::{{closure}}
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libstd/rt.rs:64
  13: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:297
  14: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:92
  15: std::rt::lang_start_internal
             at src/libstd/panicking.rs:276
             at src/libstd/panic.rs:388
             at src/libstd/rt.rs:48
  16: std::rt::lang_start
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libstd/rt.rs:64
  17: main
  18: __libc_start_main
  19: _start

The culprit is here:

data: SmallVecData::from_inline(mem::uninitialized()),

"Why would someone create a SmallVec of Void?" you may ask.
In my situation, the type (Void here) is a template parameter of one of my structs.
When the user uses a Void, the SmallVec will of course always remain empty. However creating an empty SmallVec shouldn't itself lead to a panic.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions