?Uninit types?

uh, anyone remembers a proposal along the lines of:

pub struct Foo {
  a: Bar,
  pub b: Baz,
}

impl ?Uninit for Foo {}

fn partially_init_foo(foo: &mut Foo(!a) -> Foo(a)) {
  foo.a = something;
}

fn partially_uninit_foo(foo: &mut Foo(a) -> Foo(!a)) {
  drop(foo.a);
}

fn those_bangs_can_be_inferred(foo: &mut Foo(!a) -> Foo(a)) {
  foo.a = something;
}

//pub fn no_private_in_public(foo: &mut Foo(a) -> Foo(!a)) { drop(foo.a) } // ERROR: no private in public

pub fn partially_init_public(foo: &mut Foo() -> Foo(b)) {
  foo.b = something;
  //foo.a // ERROR: a may be uninitialized
}

pub fn init_everything(foo: &mut Foo() -> Foo) {
  foo.a = something;
  foo.b = something_else;
}

//impl Drop for Foo(a) {...} // ERROR: no private in public

impl Drop for Foo(b) {
  fn drop(&mut self) {
    // perfectly safe
  }
}

let x = std::mem::uninitialized::<Foo()>(); // safe
//let x = std::mem::uninitialized::<Foo>(); // ERROR: unsafe
let x = unsafe { std::mem::uninitialized::<Foo>() }; // UB

You mean this?

1 Like

Yeah! Hm, why did that discussion die down... :‌(

Probably because the design was really complex, and didn't carry its weight. To be fair, the discussion that spawned it off (my RFC) was also really complex and didn't carry its weight. My views on the topic have changed significantly, and I don't think we need uninitialized memory to be so accessible.

So, for me, there is a significant lack of motivation for either your pre-RFC or my RFC. You can do many of the things in both RFCs already, and you can do field-by-field initialization with the currently nightly raw_ref_op in conjunction with MaybeUninit. This is powerful enough for most use-cases.

2 Likes

Well...

Our design/proposal is meant to be 100% safe Rust. It even involves making mem::uninitialized and mem::zeroed - two functions that are currently straight-up UB - safe. (if specialization ever lands - and so far it doesn't look like it should, as it seems like it'd make adding trait impls a breaking change and we don't want that. altho note that mem::zeroed would do the same as mem::uninitialized due to how our proposal is meant to work. but anyway...)