Skip to content

Consider whether the use of static muts in constant expressions should not require unsafe blocks #9852

Closed
@bstrie

Description

@bstrie

Take a look at this program, which fails to compile:

struct Foo { a: int }

static mut x: int = 3;
static mut f: Foo = Foo { a: x };  // error: use of mutable static requires unsafe function or block

fn main() {}

The failure is obviously because the rule is that static mut items have to be wrapped in an unsafe block to be used, which I presume would look like the following:

struct Foo { a: int }

static mut x: int = 3;
static mut f: Foo = unsafe { Foo { a: x } };  // error: constant contains unimplemented expression type

fn main() {}

...but as you can see, using unsafe blocks in constant expressions isn't implemented yet.

The question I want to raise here is whether the unsafe block should be required at all. Currently, constant expressions cannot mutate anything, so x might as well be just static in this case. But if we ever expand our constant expressions in the future to include mutation, allowing people to omit them here might present a backwards-compat hazard.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsP-lowLow priority

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions