-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Repeated "mutable/immutable borrow" error messages #42106
Comments
Related: #40067 |
|
I'll fix this, thanks for the instruction! |
I would happily have a go at this, if that's ok with @tommyip 😄 |
Sure @lochsh go for it! |
@lochsh have you had time to work on this? If not could I give it a go? :) |
@joshleeb I totally thought I was going to have time, but I had a crazy week at work this week. Go ahead and give it a go :) I'll find another bug to work on when I definitely have time 👍 |
Fix duplicate display of error E0502 Ref. Repeated "mutable/immutable borrow" error messages #42106. This PR modifies the return type of [`report_error_if_loan_conflicts_with_restriction`](https://github.com/rust-lang/rust/blob/0f0f5db465de96b6c12e71f0c7d3e475f618b104/src/librustc_borrowck/borrowck/check_loans.rs#L398-L403) so the result can be checked in [`report_error_if_loans_conflict`](https://github.com/rust-lang/rust/blob/0f0f5db465de96b6c12e71f0c7d3e475f618b104/src/librustc_borrowck/borrowck/check_loans.rs#L377-L396). This is done to prevent displaying a duplicate of the error message E0502 which is referenced in #42106. The output of compiling: ```rust fn do_something<T>(collection: &mut Vec<T>) { let _a = &collection; collection.swap(1, 2); } fn main() {} ``` is now ```bash $ rustc src/test/compile-fail/issue-42106.rs error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable --> src/test/compile-fail/issue-42106.rs:13:5 | 12 | let _a = &collection; | ---------- immutable borrow occurs here 13 | collection.swap(1, 2); | ^^^^^^^^^^ mutable borrow occurs here 14 | } | - immutable borrow ends here error: aborting due to 2 previous errors ``` r? @estebank
It looks like the PR to fix this was merged but has a few dangling threads. There seems to be some confusion about short-circuiting functionality and the necessity of the patch and also the error count still reads 2 rather than 1. Should a new issue be opened with those issues and this issue closed? |
Only bump error count when we are sure that the diagnostic is not a repetition This ensures that if we emit the same diagnostic twice, the error count will match the real number of errors shown to the user. Fixes #42106 This is a followup of #45603 as stated in #42106 (comment). This program, for example: ```rust fn do_something<T>(collection: &mut Vec<T>) { let _a = &collection; collection.swap(1, 2); } fn main() {} ``` without this patch, produces: ``` error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable --> $DIR/issue-42106.rs:13:5 | 12 | let _a = &collection; | ---------- immutable borrow occurs here 13 | collection.swap(1, 2); //~ ERROR also borrowed as immutable | ^^^^^^^^^^ mutable borrow occurs here 14 | } | - immutable borrow ends here error: aborting due to 2 previous errors ``` The number of errors do not match the diagnostics reported. This PR fixes this problem. The output is now in this case: ``` error[E0502]: cannot borrow `*collection` as mutable because `collection` is also borrowed as immutable --> $DIR/issue-42106.rs:13:5 | 12 | let _a = &collection; | ---------- immutable borrow occurs here 13 | collection.swap(1, 2); //~ ERROR also borrowed as immutable | ^^^^^^^^^^ mutable borrow occurs here 14 | } | - immutable borrow ends here error: aborting due to previous error ``` Also, some other tests outputs have been adapted because their count didn't really match the number of diagnostics reported. As an aside, an outdated comment has been removed (`Handler::cancel` will only call to the `Diagnostic::cancel` method and will not decrease the count of errors). All tests are passing with this PR (`x.py test` is successful).
Minimal example:
Error message (ran in playground):
Tested on nightly as well (
rustc 1.19.0-nightly (0ed1ec9f9 2017-05-18)
)The text was updated successfully, but these errors were encountered: