Skip to content

Disallow reference to static mut and adding static_mut_ref lint#117556

Merged
bors merged 6 commits intorust-lang:masterfrom
obeis:static-mut-ref-lint
Jan 9, 2024
Merged

Disallow reference to static mut and adding static_mut_ref lint#117556
bors merged 6 commits intorust-lang:masterfrom
obeis:static-mut-ref-lint

Conversation

@obeis
Copy link
Contributor

@obeis obeis commented Nov 3, 2023

Closes #114447

r? @scottmcm

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 3, 2023
@rust-log-analyzer

This comment has been minimized.

@obeis obeis force-pushed the static-mut-ref-lint branch from 743f1ee to f3bfb1d Compare November 3, 2023 19:47
@est31
Copy link
Member

est31 commented Nov 4, 2023

I wonder if you could also implement the error part for edition 2024, similar to how it's done here. Asking because then it might not be done in a lint pass any more? E.g. maybe_lint_bare_trait is called during the hir::Ty -> rustc_middle::ty::Ty lowering.

@obeis obeis marked this pull request as draft November 7, 2023 18:33
@obeis obeis force-pushed the static-mut-ref-lint branch from f3bfb1d to de71124 Compare November 10, 2023 17:43
@obeis obeis marked this pull request as ready for review November 10, 2023 17:43
@rustbot
Copy link
Collaborator

rustbot commented Nov 10, 2023

Some changes occurred in diagnostic error codes

cc @GuillaumeGomez

@obeis obeis changed the title Add static_mut_ref lint Disallow reference to static mut and adding static_mut_ref lint Nov 10, 2023
@rust-log-analyzer

This comment has been minimized.

@obeis obeis force-pushed the static-mut-ref-lint branch from de71124 to 2389d7d Compare November 10, 2023 19:04
@rust-log-analyzer

This comment has been minimized.

@obeis obeis force-pushed the static-mut-ref-lint branch from 2389d7d to e0abb31 Compare November 10, 2023 19:41
@est31
Copy link
Member

est31 commented Nov 10, 2023

Some additional points:

  • could you add a test that ptr::addr_of_mut still works?
  • could you add a test that uses dot notation so there is no explicit syntax including &? Maybe like static mut foo: [u8; 3] = [1, 2, 3]; let _ = FOO.len();?
  • this seems like a good use for test revisions where in one revision you enable the lint, and in the other revision you enable edition 2024. then that can cut down on duplication.
  • the messaging around the lint/error is that all uses are illegal, but it's only the references and mutable references that are. In fact, the lint/error could suggest adopting addr_of_mut.

@est31
Copy link
Member

est31 commented Nov 10, 2023

the messaging around the lint/error is that all uses are illegal, but it's only the references and mutable references that are.

Note that I'm not arguing here that we should never make all of static mut deprecated (maybe a good idea, maybe not, in any case it's right now it's too early because SyncUnsafeCell is still unstable). But there is an inconsistency between what the lint does and its name on one hand, and what its messaging is on the other.

@scottmcm
Copy link
Member

Thanks for making a PR for this! Unfortunately, I'm not personally familiar with the lint system, so while I'm happy to look at the tests to see what it's doing (est31's suggestions in #117556 (comment) sound good), I'm not a good reviewer for it technically.

r? compiler

@rustbot rustbot assigned davidtwco and unassigned scottmcm Nov 11, 2023
@obeis obeis force-pushed the static-mut-ref-lint branch from e0abb31 to b4f1f06 Compare November 14, 2023 20:49
@rustbot
Copy link
Collaborator

rustbot commented Nov 14, 2023

These commits modify the Cargo.lock file. Unintentional changes to Cargo.lock can be introduced when switching branches and rebasing PRs.

If this was unintentional then you should revert the changes before this PR is merged.
Otherwise, you can ignore this comment.

@obeis
Copy link
Contributor Author

obeis commented Nov 15, 2023

cc @RalfJung because it was your idea and unsafe is your area

@est31
Copy link
Member

est31 commented Jan 7, 2024

@bors r=davidtwco

@bors
Copy link
Collaborator

bors commented Jan 7, 2024

📌 Commit a4a52d5 has been approved by davidtwco

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 7, 2024

⌛ Testing commit a4a52d5 with merge 5088709...

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jan 7, 2024

💔 Test failed - checks-actions

@est31
Copy link
Member

est31 commented Jan 8, 2024

@bors r=davidtwco

@bors
Copy link
Collaborator

bors commented Jan 8, 2024

📌 Commit a8aa687 has been approved by davidtwco

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jan 8, 2024

⌛ Testing commit a8aa687 with merge 092c722...

@bors
Copy link
Collaborator

bors commented Jan 8, 2024

💥 Test timed out

@rust-log-analyzer
Copy link
Collaborator

A job failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

@fmease
Copy link
Member

fmease commented Jan 8, 2024

Hmm, let's retry that?
@bors retry

@ehuss
Copy link
Contributor

ehuss commented Jan 9, 2024

Some questions about this:

  • Is there an intended migration path for this, or is the user expected to rewrite their code if it is encountered?
  • The current suggestion of adding addr_of_mut! does not compile. At a minimum, I would think it needs to be qualified with core::ptr::. Can that be fixed?
  • Is there an explanation of why that is a "maybe incorrect" suggestion?
  • Was there a crater run done to see how much of an impact this will have?
  • Was there an RFC for this, or any public discussion about the lang team's decision? It seems unusual to make language changes without an RFC.

@scottmcm
Copy link
Member

scottmcm commented Jan 9, 2024

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.

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

Labels

A-edition-2024 Area: The 2024 edition A-testsuite Area: The testsuite used to check the correctness of rustc O-unix Operating system: Unix-like relnotes Marks issues that should be documented in the release notes of the next release. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-bootstrap Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap) T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disallow *references* to static mut [Edition Idea]