Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can write to fields of uninitialized mutable struct #46791

Closed
sander2 opened this issue Dec 17, 2017 · 4 comments
Closed

Can write to fields of uninitialized mutable struct #46791

sander2 opened this issue Dec 17, 2017 · 4 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sander2
Copy link

sander2 commented Dec 17, 2017

I'm not 100% sure this is a bug, but the behavior seems very strange to me. You can write to fields of an uninitialized mutable struct.

I tried this code:

struct X { } 
struct Y { 
    x : X , 
}
fn main ( ) { 
    let mut y:Y;
    y.x = X{};  // this line compiles
    // y.x; // this line would throw an error
}

I expected to see this happen: the assignment throws a compiler error.

Instead, this happened: no error is generated.

Alternatively, it would be ok to have the assignment work, but then I'd expect to be able to refer to it, like in the line below it.

Link to playground

@S0urc3C0de
Copy link

I think what happens here is rather simple to explain:

Since the size of struct Y is known at compile-time and is allocated on the stack, it is, theoretically, perfectly legal to assign a value to it's fields, basically initializing them. It's essentially the same as:

let x: i32;
x = 42;

I'm neither a professional, nor do I know much about the compiler in detail, but in contrast to heap-allocated stuff, you should theoretically be able to read and write to stuff on the stack without explicitly initializing it since the stack will be setup anyway. Its initial values are undefined, but assigning a value essentially initializes the field.
The reason why the behavior of the compiler is so strange is that the compiler apparently doesn't check whether the struct has been initialized when assigning a value to a field of it, but does so when trying to read it.

There are two ways to solve this that come to my mind:

  1. Make reading single struct fields legal if that specific field has been initialized before, but not when it hasn't
  2. Make both assigning and reading illegal until the struct has been initialized the way it is meant to be

Looking at the way that Rust usually enforces you to initialize all fields of a struct when initializing it, the only solution that makes sense is the second one.

@retep998
Copy link
Member

This is intended behavior. It is perfectly legal to initialize a field at a time of a struct in this manner. The only actual issue at the moment is that the overall struct itself remains uninitialized, even if you've initialized every single field like this.

@scottmcm
Copy link
Member

Related: #21232

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 30, 2018
@Centril
Copy link
Contributor

Centril commented Aug 6, 2019

Fixed in #63230.

@Centril Centril closed this as completed Aug 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants