Skip to content
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

Fix missing explanation of where the borrowed reference is used when the same borrow occurs multiple times due to loop iterations #102080

Merged
merged 1 commit into from
Oct 16, 2022

Conversation

yanchen4791
Copy link
Contributor

Fix #99824.

Problem of the issue:
If a borrow occurs in a loop, the borrowed reference could be invalidated at the same place at next iteration of the loop. When this happens, the point where the borrow occurs is the same as the intervening point that might invalidate the reference in the loop. This causes a problem for the current code finding the point where the resulting reference is used, so that the explanation of the cause will be missing. As the second point of "explain all errors in terms of three points" (see leveraging intuition framing errors in terms of points", this explanation is very helpful for user to understand the error.

In the current implementation, the searching region for finding the location where the borrowed reference is used is limited to between the place where the borrow occurs and the place where the reference is invalidated. If those two places happen to be the same, which indicates that the borrow and invalidation occur at the same place in a loop, the search will fail.

One solution to the problem is when these two places are the same, find the terminator of the loop, and then use the location of the loop terminator instead of the location of the borrow for the region to find the place where the borrowed reference is used.

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 21, 2022
@rust-highfive
Copy link
Collaborator

r? @davidtwco

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 21, 2022
@yanchen4791
Copy link
Contributor Author

@rustbot label +A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Sep 21, 2022
compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
src/test/ui/nll/closures-in-loops.stderr Outdated Show resolved Hide resolved
src/test/ui/borrowck/two-phase-across-loop.stderr Outdated Show resolved Hide resolved
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

A few more comments..

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

Some more changes, going to re-assign this to double-check it because it's been a while since I've worked on the borrow checker.

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
@davidtwco
Copy link
Member

r? @cjgillot

Copy link
Contributor

@cjgillot cjgillot left a comment

Choose a reason for hiding this comment

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

Thanks @yanchen4791. The simplification looks good. I have a few questions.

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/region_infer/mod.rs Outdated Show resolved Hide resolved
| ^^ - borrows occur due to use of `x` in closure
| |
| `x` was mutably borrowed here in the previous iteration of the loop
| -------^^-------------------
Copy link
Contributor

Choose a reason for hiding this comment

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

The spans are a bit unfortunate. Is there anything to do here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean the format of using "-------^^-------------------" is not clear?

Copy link
Contributor

Choose a reason for hiding this comment

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

I just realised: the longer --- is meant to point to loop_terminator_location. It should point to some } below, shouldn't it?

src/test/ui/nll/closures-in-loops.stderr Outdated Show resolved Hide resolved
@yanchen4791
Copy link
Contributor Author

@cjgillot Any other comments?

@cjgillot
Copy link
Contributor

Sorry for the wait @yanchen4791. It took me a bit of time to understand why this PR was doing the right thing.
@bors r+

@bors
Copy link
Contributor

bors commented Oct 15, 2022

📌 Commit b3bf931 has been approved by cjgillot

It is now in the queue for this repository.

@bors bors 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 Oct 15, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2022
…illot

Fix missing explanation of where the borrowed reference is used when the same borrow occurs multiple times due to loop iterations

Fix rust-lang#99824.

Problem of the issue:
If a borrow occurs in a loop, the borrowed reference could be invalidated at the same place at next iteration of the loop. When this happens, the point where the borrow occurs is the same as the intervening point that might invalidate the reference in the loop. This causes a problem for the current code finding the point where the resulting reference is used, so that the explanation of the cause will be missing. As the second point of "explain all errors in terms of three points" (see [leveraging intuition framing errors in terms of points"](https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points), this explanation is very helpful for user to understand the error.

In the current implementation, the searching region for finding the location where the borrowed reference is used is limited to between the place where the borrow occurs and the place where the reference is invalidated. If those two places happen to be the same, which indicates that the borrow and invalidation occur at the same place in a loop, the search will fail.

One solution to the problem is when these two places are the same,  find the terminator of the loop, and then use the location of the loop terminator instead of the location of the borrow for the region to find the place where the borrowed reference is used.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2022
…illot

Fix missing explanation of where the borrowed reference is used when the same borrow occurs multiple times due to loop iterations

Fix rust-lang#99824.

Problem of the issue:
If a borrow occurs in a loop, the borrowed reference could be invalidated at the same place at next iteration of the loop. When this happens, the point where the borrow occurs is the same as the intervening point that might invalidate the reference in the loop. This causes a problem for the current code finding the point where the resulting reference is used, so that the explanation of the cause will be missing. As the second point of "explain all errors in terms of three points" (see [leveraging intuition framing errors in terms of points"](https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points), this explanation is very helpful for user to understand the error.

In the current implementation, the searching region for finding the location where the borrowed reference is used is limited to between the place where the borrow occurs and the place where the reference is invalidated. If those two places happen to be the same, which indicates that the borrow and invalidation occur at the same place in a loop, the search will fail.

One solution to the problem is when these two places are the same,  find the terminator of the loop, and then use the location of the loop terminator instead of the location of the borrow for the region to find the place where the borrowed reference is used.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Oct 15, 2022
…illot

Fix missing explanation of where the borrowed reference is used when the same borrow occurs multiple times due to loop iterations

Fix rust-lang#99824.

Problem of the issue:
If a borrow occurs in a loop, the borrowed reference could be invalidated at the same place at next iteration of the loop. When this happens, the point where the borrow occurs is the same as the intervening point that might invalidate the reference in the loop. This causes a problem for the current code finding the point where the resulting reference is used, so that the explanation of the cause will be missing. As the second point of "explain all errors in terms of three points" (see [leveraging intuition framing errors in terms of points"](https://rust-lang.github.io/rfcs/2094-nll.html#leveraging-intuition-framing-errors-in-terms-of-points), this explanation is very helpful for user to understand the error.

In the current implementation, the searching region for finding the location where the borrowed reference is used is limited to between the place where the borrow occurs and the place where the reference is invalidated. If those two places happen to be the same, which indicates that the borrow and invalidation occur at the same place in a loop, the search will fail.

One solution to the problem is when these two places are the same,  find the terminator of the loop, and then use the location of the loop terminator instead of the location of the borrow for the region to find the place where the borrowed reference is used.
@bors
Copy link
Contributor

bors commented Oct 16, 2022

⌛ Testing commit b3bf931 with merge 11432fe...

@bors
Copy link
Contributor

bors commented Oct 16, 2022

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 11432fe to master...

1 similar comment
@bors
Copy link
Contributor

bors commented Oct 16, 2022

☀️ Test successful - checks-actions
Approved by: cjgillot
Pushing 11432fe to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 16, 2022
@bors bors merged commit 11432fe into rust-lang:master Oct 16, 2022
@rustbot rustbot added this to the 1.66.0 milestone Oct 16, 2022
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (11432fe): comparison URL.

Overall result: ✅ improvements - no action needed

@rustbot label: -perf-regression

Instruction count

This is a highly reliable metric that was used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-2.5% [-2.5%, -2.5%] 1
All ❌✅ (primary) - - 0

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-3.4% [-3.4%, -3.4%] 1
Improvements ✅
(secondary)
-3.6% [-10.6%, -2.0%] 11
All ❌✅ (primary) -3.4% [-3.4%, -3.4%] 1

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean1 range count2
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.7% [3.7%, 3.7%] 1
Improvements ✅
(primary)
-2.0% [-2.0%, -2.0%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -2.0% [-2.0%, -2.0%] 1

Footnotes

  1. the arithmetic mean of the percent change 2 3

  2. number of relevant changes 2 3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints merged-by-bors This PR was explicitly merged by bors. 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.

Borrow checker gives E0499 in loops with mutable borrows, but it is misleading
8 participants