Skip to content

fix const_item_mutation lint to use needs_drop instead of has_dtor - #160097

Open
Rachit2323 wants to merge 1 commit into
rust-lang:mainfrom
Rachit2323:const-mutation-needs-drop
Open

Rachit2323 wants to merge 1 commit into
rust-lang:mainfrom
Rachit2323:const-mutation-needs-drop

Conversation

@Rachit2323

Copy link
Copy Markdown
Contributor

Description:
The const_item_mutation lint was only checking if the outer type has a direct Drop impl (has_dtor). But if a field inside the type has a Drop impl, that drop logic can also observe the mutation — so the warning should be suppressed in that case too.

This fixes a false positive where the lint would warn on code like:

O.inner.val = 42;

even when Inner has a Drop impl that prints the value — meaning the mutation IS observable and the warning is wrong.

Fix: replace has_dtor check with needs_drop, which checks the whole type including all fields inside it.

@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@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 Jul 28, 2026
@rustbot

rustbot commented Jul 28, 2026

Copy link
Copy Markdown
Collaborator

r? @jackh726

rustbot has assigned @jackh726.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 74 candidates
  • Random selection from 17 candidates

@rabindra789

Copy link
Copy Markdown
Contributor

Nice fix! While testing this branch, I noticed one behaviour change. on the base commit this lint warns for traits associated consts whose type mentions Self, but on this branch it doesn't. Is that expected, or should this case still lint?

@Rachit2323

Copy link
Copy Markdown
Contributor Author

Thanks for catching this! It was unintended. Fixed it — for types containing type parameters like Self, needs_drop is too conservative so I now skip the suppression and let the lint fire as before.

@rabindra789 rabindra789 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for checking and fixing it! Glad we caught that regression.

View changes since this review

Comment thread compiler/rustc_mir_transform/src/check_const_item_mutation.rs Outdated
@rust-bors

This comment has been minimized.

@Rachit2323
Rachit2323 force-pushed the const-mutation-needs-drop branch from b47f970 to 4326ae2 Compare August 22, 2026 15:33
@rustbot

rustbot commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@jackh726

Copy link
Copy Markdown
Member

If you can squash, r=me.

@Rachit2323
Rachit2323 force-pushed the const-mutation-needs-drop branch from 4326ae2 to 940498d Compare August 24, 2026 06:55
@jackh726

Copy link
Copy Markdown
Member

@bors r+

@rust-bors

rust-bors Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

📌 Commit 940498d has been approved by jackh726

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 19, 2026
jhpratt added a commit to jhpratt/rust that referenced this pull request Sep 19, 2026
…op, r=jackh726

fix const_item_mutation lint to use needs_drop instead of has_dtor

Description:
The const_item_mutation lint was only checking if the outer type has a direct Drop impl (has_dtor). But if a field inside the type has a Drop impl, that drop logic can also observe the mutation — so the warning should be suppressed in that case too.

This fixes a false positive where the lint would warn on code like:

```O.inner.val = 42;```

even when Inner has a Drop impl that prints the value — meaning the mutation IS observable and the warning is wrong.

Fix: replace has_dtor check with needs_drop, which checks the whole type including all fields inside it.
rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
Rollup of 13 pull requests

Successful merges:

 - #158515 (Make let-else respect macro_rules expr metavariable grouping)
 - #160097 (fix const_item_mutation lint to use needs_drop instead of has_dtor)
 - #161435 (Provide a `supertrait_def_ids()` function in rustc_type_ir's interner)
 - #161894 (Do not suppress the fn item uniqueness note for late bound lifetimes)
 - #162990 (post GH comment on types nominations)
 - #154665 (add safety section for mem::zeroed)
 - #162700 ( Remove incorrect parse error recovery code that mistakes `as` casts for the long removed type ascription)
 - #162705 (Trigger "C array" parse error recovery in far fewer cases)
 - #162875 (Add .seek_read_exact(), .seek_write_all() to std::os::windows::fs::FileExt)
 - #162988 (recover `true` and `false` in type position as `bool`)
 - #162995 (Constify `impl FromStr for NonZero<T>`)
 - #163006 (Use `end_point` for trailing brace in `let...else` diagnostics)
 - #163007 (add Dir::try_clone)
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Sep 19, 2026
…op, r=jackh726

fix const_item_mutation lint to use needs_drop instead of has_dtor

Description:
The const_item_mutation lint was only checking if the outer type has a direct Drop impl (has_dtor). But if a field inside the type has a Drop impl, that drop logic can also observe the mutation — so the warning should be suppressed in that case too.

This fixes a false positive where the lint would warn on code like:

```O.inner.val = 42;```

even when Inner has a Drop impl that prints the value — meaning the mutation IS observable and the warning is wrong.

Fix: replace has_dtor check with needs_drop, which checks the whole type including all fields inside it.
rust-bors Bot pushed a commit that referenced this pull request Sep 19, 2026
…uwer

Rollup of 15 pull requests

Successful merges:

 - #158515 (Make let-else respect macro_rules expr metavariable grouping)
 - #162726 (std: fix unix socket address panic on a full sun_path)
 - #160028 (Better account for `Self` that might be a typo of `self`)
 - #160097 (fix const_item_mutation lint to use needs_drop instead of has_dtor)
 - #161435 (Provide a `supertrait_def_ids()` function in rustc_type_ir's interner)
 - #161894 (Do not suppress the fn item uniqueness note for late bound lifetimes)
 - #162990 (post GH comment on types nominations)
 - #154665 (add safety section for mem::zeroed)
 - #162700 ( Remove incorrect parse error recovery code that mistakes `as` casts for the long removed type ascription)
 - #162705 (Trigger "C array" parse error recovery in far fewer cases)
 - #162875 (Add .seek_read_exact(), .seek_write_all() to std::os::windows::fs::FileExt)
 - #162988 (recover `true` and `false` in type position as `bool`)
 - #162995 (Constify `impl FromStr for NonZero<T>`)
 - #163006 (Use `end_point` for trailing brace in `let...else` diagnostics)
 - #163007 (add Dir::try_clone)
mu001999 added a commit to mu001999/rust that referenced this pull request Sep 20, 2026
…op, r=jackh726

fix const_item_mutation lint to use needs_drop instead of has_dtor

Description:
The const_item_mutation lint was only checking if the outer type has a direct Drop impl (has_dtor). But if a field inside the type has a Drop impl, that drop logic can also observe the mutation — so the warning should be suppressed in that case too.

This fixes a false positive where the lint would warn on code like:

```O.inner.val = 42;```

even when Inner has a Drop impl that prints the value — meaning the mutation IS observable and the warning is wrong.

Fix: replace has_dtor check with needs_drop, which checks the whole type including all fields inside it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

4 participants