Stream: t-compiler/help

Topic: What is "uninhabited"


Derek (May 10 2020 at 15:03, on Zulip):

I'm reading document, I see it use word "uninhabited", what exactly "uninhabited" means here. e.g. for panic_if_uninhabited, how to trigger a panic in following example? Thanks.

#![feature(core_intrinsics)]
use std::intrinsics;

fn main() {
    unsafe {
        intrinsics::panic_if_uninhabited::<i32>();
        intrinsics::panic_if_uninhabited::<ManuallyDrop<i32>>();
    }
    println!("Hello, world!");
}
Matthew Jasper (May 10 2020 at 15:07, on Zulip):

"uninhabited" means a type with no valid values, e.g., ! or enum Empty {}

Derek (May 10 2020 at 15:22, on Zulip):

Thanks, I got panic enum Empty.

#![feature(core_intrinsics)]
use std::intrinsics;

  enum Empty {}
  fn main() {
      unsafe {
          intrinsics::panic_if_uninhabited::<Empty>();
   }
}

But with !, got a compile error,

$ cargo +nightly run
error[E0658]: The `!` type is experimental
   |
9 |         intrinsics::panic_if_uninhabited::<!>();
bjorn3 (May 10 2020 at 15:25, on Zulip):

When you want to use ! as a type, you need to enable #![feature(never_type)].

Derek (May 10 2020 at 15:26, on Zulip):

:+1: @Matthew Jasper @bjorn3 Thanks!!

Last update: Sep 28 2020 at 16:45UTC