Skip to content

Introduce &uninit pointers#22

Open
dingxiangfei2009 wants to merge 1 commit intorust-lang:mainfrom
dingxiangfei2009:out-pointer
Open

Introduce &uninit pointers#22
dingxiangfei2009 wants to merge 1 commit intorust-lang:mainfrom
dingxiangfei2009:out-pointer

Conversation

@dingxiangfei2009
Copy link
Copy Markdown
Contributor

Let's put a summary of the out-pointer/uninit-pointer in the wiki.

cc @cramertj, please feel free to critic and review!

Signed-off-by: Xiangfei Ding <dingxiangfei2009@protonmail.ch>
Comment thread src/out-pointers.md
Comment thread src/out-pointers.md
Comment on lines +88 to +93
```rust
let init_x: &own<'_> MyStruct = uninit_x;
x <- init_x; // Mark x as initialised.
// Now x is initialised
dbg!(x.id); // OK
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no x in this example. And the "marking" of being initialized only works when the &own reference points to that local variable, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops. I will fix that.

Comment thread src/out-pointers.md
and the corresponding `&own` as `&'a uninit MyStruct` and `&'b own<'c> MyStruct`.
When coercion happens on `uninit_x`, the relationship `'a == 'c` is established.
Later, when `<-` is used, the lifetime for which the `x` remains uninitialised
is matched against the so-called origin lifetime on the `&own` and the statement
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
is matched against the so-called origin lifetime on the `&own` and the statement
is matched against the so-called origin lifetime `'c` on the `&'b own<'c>` and the statement

Comment thread src/out-pointers.md
Comment on lines +100 to +101
is only accepted when the two lifetime matches. This is true in this case
because the origin lifetime `'c` indeed matches the lifetime `'a` on `uninit_x`.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works for this locals, since they do not have a lifetime. Consider:

let x: Struct;
let y: Struct;
let r: &uninit Struct = &uninit x;
r.a = 42;
r.b = 24;
let r: &own Struct = r; // `r` is fully initialized
y <- r; // oops used the wrong local variable with the `&own`

There is no lifetime that can differentiate x from y.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, this needs compiler magic to produce a generativity-style unique invariant lifetime for x and y.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I realised that this section is very badly written. Let me sort something out and update it later.

In general, one needs a more detailed spelling of the borrow checking rules for this to work admittedly.

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

Successfully merging this pull request may close these issues.

3 participants