Skip to content

static_mut_refs: Automatic edition migration #123061

Closed

Description

@ehuss

#117556 implemented the static_mut_refs change for the 2024 Edition. However, the migration code is marked as maybe-incorrect, which prevents automatic migration. Is it possible to have an automatic migration for this change? If the addr_of! change is unlikely to work (see #123059), what is the scope of manual migration? How many crates will be affected by this change?

If this has a very widespread impact, we should be very certain that we want to move forward without that migration.

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-langRelevant to the language team
    on Mar 25, 2024
  2. traviscross commented on Mar 25, 2024

    @traviscross
    Contributor

    Here's what @scottmcm earlier had to say about this:

    As mentioned in #114447 (comment), you can always change &BLAH to &*ptr::addr_of!(BLAH), which will compile, but still be just as UB-prone.

    The problem with taking references to static mut is that it's most often insta-UB, so there's no local change that can fix that, and yes, larger rework will typically be needed.

    For more reading, there's been discussion and slow movement for at least 5 years towards limiting static mut. See #53639 for example.

  3. RalfJung commented on Mar 25, 2024

    @RalfJung
    Member

    Yeah, this is a case of "please use this opportunity to carefully review your code, based on today's understanding of how references work". There's no sensible automatic migration.

  4. est31 commented on Mar 26, 2024

    @est31
    Member

    Yeah indeed edition changes are required to have automatic migrations implemented, but this is not a change where such automatic migration is easy to do (unlike say Trait to dyn Trait). It's similar to discussions about deprecating other UB-prone code like forbidding mem::uninitialized in new editions or such.

    Maybe that policy should be evolved into: if the API/pattern is UB-prone and easy to misuse, it is okay to not offer automatic edition migrations.

  5. ryanavella commented on Apr 2, 2024

    @ryanavella

    Forgive me if this has been suggested before, but is there room for a --allow-unsound flag for cargo fix?

    I'm imagining that something like this:

    static mut X: i32 = 0;
    let _y = unsafe { &mut X };

    Could be transformed into this:

    static mut X: i32 = 0;
    let _y = unsafe {
        // TODO: this was rewritten by `cargo fix`, and needs to be manually audited.
        // See [https://github.com/rust-lang/rust/issues/114447] for more information.
        &mut *std::ptr::addr_of_mut!(X)
    };
  6. traviscross commented on May 14, 2024

    @traviscross
    Contributor

    We're not necessarily going to add any specific migration lints here as we want people to carefully review any code that is affected by this. So let's go ahead and close this as we'll be willing to mark this as ready for Rust 2024 without any lints here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-edition-2024Area: The 2024 editionT-langRelevant to the language team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions