-
-
Notifications
You must be signed in to change notification settings - Fork 15.7k
Use the entire type of a dropped local to compute variance (edge direction) for Polonius alpha #161305
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
amandasystems
wants to merge
1
commit into
rust-lang:main
Choose a base branch
from
amandasystems:issue-160670
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.
Open
Use the entire type of a dropped local to compute variance (edge direction) for Polonius alpha #161305
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
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,6 +5,7 @@ use rustc_index::interval::SparseIntervalMatrix; | |||||||||
| use rustc_middle::mir::{Body, Location}; | ||||||||||
| use rustc_middle::ty::RegionVid; | ||||||||||
| use rustc_mir_dataflow::points::PointIndex; | ||||||||||
| use tracing::debug; | ||||||||||
|
|
||||||||||
| use crate::BorrowSet; | ||||||||||
| use crate::constraints::OutlivesConstraint; | ||||||||||
|
|
@@ -255,6 +256,7 @@ fn compute_forward_successor( | |||||||||
|
|
||||||||||
| // 2. Otherwise, gather the edges due to explicit region liveness, when applicable. | ||||||||||
| if !live_regions.contains(region, next_point) { | ||||||||||
| debug!("Region {region:?} isn't live at successor {next_point:?}; traversal stops."); | ||||||||||
| return None; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
@@ -275,6 +277,7 @@ fn compute_forward_successor( | |||||||||
| ConstraintDirection::Backward => { | ||||||||||
| // Contravariant cases: loans flow in the inverse direction, but we're only interested | ||||||||||
| // in forward successors and there are none here. | ||||||||||
| debug!("Constraint direction is backwards; {region:?} has no forward successors"); | ||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably should just move above the match: |
||||||||||
| None | ||||||||||
| } | ||||||||||
| ConstraintDirection::Forward | ConstraintDirection::Bidirectional => { | ||||||||||
|
|
@@ -299,6 +302,9 @@ fn compute_backward_successor( | |||||||||
| // Liveness flows into the regions live at the next point. So, in a backwards view, we'll link | ||||||||||
| // the region from the current point, if it's live there, to the previous point. | ||||||||||
| if !live_regions.contains(region, current_point) { | ||||||||||
| debug!( | ||||||||||
| "Backwards successor: {region:?} not live at current point {current_point:?}; bailing out!" | ||||||||||
| ); | ||||||||||
|
Comment on lines
+305
to
+307
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
| return None; | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
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
32 changes: 32 additions & 0 deletions
32
tests/ui/borrowck/alias-liveness/drop-liveness-invariance-issue-160670.rs
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,32 @@ | ||
| // From https://github.com/rust-lang/rust/issues/160670 This issue was | ||
| // discovered when developing Polonius alpha. A live local (the one holding the | ||
| // struct `D`) had its type be drop-live, but only partially. This had not | ||
| // previously triggered any issues because it did not affect region liveness, | ||
| // but it did affect Polonius' region variance computations, since the outer `D` | ||
| // nesting was removed to obtain `fn(&'a T)`, which unlike the associated type | ||
| // isn't invariant. | ||
| // | ||
| // The split declaration/assignment on lines 30--31 is load bearing; without | ||
| // them the bug does not appear. | ||
|
|
||
| struct D<T: HasArg>(T::Arg); | ||
|
|
||
| trait HasArg { | ||
| type Arg; | ||
| } | ||
| impl<'a, T> HasArg for fn(&'a T) { | ||
| type Arg = &'a T; | ||
| } | ||
| impl<T: HasArg> Drop for D<T> { | ||
| fn drop(&mut self) {} | ||
| } | ||
| fn mk<'a, T>(r: &'a T) -> D<fn(&'a T)> { | ||
| D(r) | ||
| } | ||
|
|
||
| fn main() { | ||
| let b = Box::new(0u8); | ||
| let d; | ||
| d = mk(&*b); | ||
| drop(b); //~ ERROR cannot move out of `b` because it is borrowed | ||
| } |
22 changes: 22 additions & 0 deletions
22
tests/ui/borrowck/alias-liveness/drop-liveness-invariance-issue-160670.stderr
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,22 @@ | ||
| error[E0505]: cannot move out of `b` because it is borrowed | ||
| --> $DIR/drop-liveness-invariance-issue-160670.rs:31:10 | ||
| | | ||
| LL | let b = Box::new(0u8); | ||
| | - binding `b` declared here | ||
| LL | let d; | ||
| LL | d = mk(&*b); | ||
| | --- borrow of `*b` occurs here | ||
| LL | drop(b); | ||
| | ^ move out of `b` occurs here | ||
| LL | } | ||
| | - borrow might be used here, when `d` is dropped and runs the `Drop` code for type `D` | ||
| | | ||
| help: consider cloning the value if the performance cost is acceptable | ||
| | | ||
| LL - d = mk(&*b); | ||
| LL + d = mk(&b.clone()); | ||
| | | ||
|
|
||
| error: aborting due to 1 previous error | ||
|
|
||
| For more information about this error, try `rustc --explain E0505`. |
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.