-
Notifications
You must be signed in to change notification settings - Fork 14k
Fix the issue of unused assignment from MIR liveness checking #149072
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
chenyukang
wants to merge
2
commits into
rust-lang:main
Choose a base branch
from
chenyukang:yukang-fix-unused-148960
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+200
−22
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| warning: value assigned to `x` is never read | ||
| --> $DIR/or-pattern-drop-order.rs:43:9 | ||
| | | ||
| LL | x = LogDrop(o, 2); | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
| = note: `#[warn(unused_assignments)]` (part of `#[warn(unused)]`) on by default | ||
|
|
||
| warning: value assigned to `y` is never read | ||
| --> $DIR/or-pattern-drop-order.rs:44:9 | ||
| | | ||
| LL | y = LogDrop(o, 1); | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| warning: value assigned to `x` is never read | ||
| --> $DIR/or-pattern-drop-order.rs:38:9 | ||
| | | ||
| LL | x = LogDrop(o, 3); | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| warning: value assigned to `y` is never read | ||
| --> $DIR/or-pattern-drop-order.rs:37:9 | ||
| | | ||
| LL | y = LogDrop(o, 2); | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| warning: value assigned to `z` is never read | ||
| --> $DIR/or-pattern-drop-order.rs:36:9 | ||
| | | ||
| LL | z = LogDrop(o, 1); | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| warning: 5 warnings emitted | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //@ check-fail | ||
| #![deny(unused)] | ||
| #![allow(dead_code)] | ||
|
|
||
| fn test_one_extra_assign() { | ||
| let mut value = b"0".to_vec(); //~ ERROR value assigned to `value` is never read | ||
| value = b"1".to_vec(); | ||
| println!("{:?}", value); | ||
| } | ||
|
|
||
| fn test_two_extra_assign() { | ||
| let mut x = 1; //~ ERROR value assigned to `x` is never read | ||
| x = 2; //~ ERROR value assigned to `x` is never read | ||
| x = 3; | ||
| println!("{}", x); | ||
| } | ||
|
|
||
| struct Point { | ||
| x: i32, | ||
| y: i32, | ||
| } | ||
|
|
||
| fn test_indirect_assign() { | ||
| let mut p = Point { x: 1, y: 1 }; //~ ERROR value assigned to `p` is never read | ||
| p = Point { x: 2, y: 2 }; | ||
| p.x = 3; | ||
| println!("{}", p.y); | ||
| } | ||
|
|
||
| struct Foo; | ||
|
|
||
| impl Drop for Foo { | ||
| fn drop(&mut self) {} | ||
| } | ||
|
|
||
| // testcase for issue #148418 | ||
| fn test_unused_variable() { | ||
| let mut foo = Foo; //~ ERROR variable `foo` is assigned to, but never used | ||
| foo = Foo; //~ ERROR value assigned to `foo` is never read | ||
| } | ||
|
|
||
| fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| error: value assigned to `value` is never read | ||
| --> $DIR/unused-assign-148960.rs:6:21 | ||
| | | ||
| LL | let mut value = b"0".to_vec(); | ||
| | ^^^^^^^^^^^^^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
| note: the lint level is defined here | ||
| --> $DIR/unused-assign-148960.rs:2:9 | ||
| | | ||
| LL | #![deny(unused)] | ||
| | ^^^^^^ | ||
| = note: `#[deny(unused_assignments)]` implied by `#[deny(unused)]` | ||
|
|
||
| error: value assigned to `x` is never read | ||
| --> $DIR/unused-assign-148960.rs:12:17 | ||
| | | ||
| LL | let mut x = 1; | ||
| | ^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| error: value assigned to `x` is never read | ||
| --> $DIR/unused-assign-148960.rs:13:5 | ||
| | | ||
| LL | x = 2; | ||
| | ^^^^^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| error: value assigned to `p` is never read | ||
| --> $DIR/unused-assign-148960.rs:24:17 | ||
| | | ||
| LL | let mut p = Point { x: 1, y: 1 }; | ||
| | ^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| error: variable `foo` is assigned to, but never used | ||
| --> $DIR/unused-assign-148960.rs:38:9 | ||
| | | ||
| LL | let mut foo = Foo; | ||
| | ^^^^^^^ | ||
| | | ||
| = note: consider using `_foo` instead | ||
| = note: `#[deny(unused_variables)]` implied by `#[deny(unused)]` | ||
| help: you might have meant to pattern match on the similarly named struct `Foo` | ||
| | | ||
| LL - let mut foo = Foo; | ||
| LL + let Foo = Foo; | ||
| | | ||
|
|
||
| error: value assigned to `foo` is never read | ||
| --> $DIR/unused-assign-148960.rs:39:5 | ||
| | | ||
| LL | foo = Foo; | ||
| | ^^^ | ||
| | | ||
| = help: maybe it is overwritten before being read? | ||
|
|
||
| error: aborting due to 6 previous errors | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is for avoiding report
unused_assignfor this line of code:https://github.com/rust-lang/rust/blob/main/library/std/src/io/mod.rs#L393
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would there be an
unused_assignfor that line irrespective of whether there is aDropimpl?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If there's no subsequent code that explicitly reads
g.len, this assignment appears "unused".I think the liveness checking in MIR only knows that the entire
gwill be dropped, but doesn't know the drop process will readg.len. Theis_directhere is used to distinguish this case and avoid false positives for field assignments that Drop may use.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past fields weren't checked, only whole variables, right? Why was that changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indirect statement is added here:
https://github.com/chenyukang/rust/blob/4930d3e6129b17538e687f97c3b3f3e0dcaea76f/compiler/rustc_mir_transform/src/liveness.rs#L656-L661
it seems to be trying to record the access for a variable with a field:
@cjgillot correct me if I'm wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
another issue which related to Drop, but this PR does not fix it:
#148418
maybe we need a more general fix.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a new commit for this PR to fix #148418
00f3155
I double checked the changes for testcase
tests/ui/drop/or-pattern-drop-order.stderr, it's now the same with stable branch.