Skip to content

Can write to fields of uninitialized mutable struct #46791

Closed
@sander2

Description

@sander2

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions