Skip to content

Commit 0f4ebf9

Browse files
committed
Auto merge of #47146 - ereslibre:issue-42106, r=estebank
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).
2 parents 687d3d1 + 063607e commit 0f4ebf9

8 files changed

+14
-20
lines changed

src/librustc_errors/diagnostic.rs

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ impl Diagnostic {
100100

101101
/// Cancel the diagnostic (a structured diagnostic must either be emitted or
102102
/// canceled or it will panic when dropped).
103-
/// BEWARE: if this DiagnosticBuilder is an error, then creating it will
104-
/// bump the error count on the Handler and canceling it won't undo that.
105-
/// If you want to decrement the error count you should use `Handler::cancel`.
106103
pub fn cancel(&mut self) {
107104
self.level = Level::Cancelled;
108105
}

src/librustc_errors/diagnostic_builder.rs

+6-12
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,12 @@ impl<'a> DiagnosticBuilder<'a> {
8383
return;
8484
}
8585

86-
let is_error = match self.level {
86+
self.handler.emit_db(&self);
87+
self.cancel();
88+
}
89+
90+
pub fn is_error(&self) -> bool {
91+
match self.level {
8792
Level::Bug |
8893
Level::Fatal |
8994
Level::PhaseFatal |
@@ -97,18 +102,7 @@ impl<'a> DiagnosticBuilder<'a> {
97102
Level::Cancelled => {
98103
false
99104
}
100-
};
101-
102-
self.handler.emit_db(&self);
103-
self.cancel();
104-
105-
if is_error {
106-
self.handler.bump_err_count();
107105
}
108-
109-
// if self.is_fatal() {
110-
// panic!(FatalError);
111-
// }
112106
}
113107

114108
/// Convenience function for internal use, clients should use one of the

src/librustc_errors/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ impl Handler {
588588
// one:
589589
if self.emitted_diagnostics.borrow_mut().insert(diagnostic_hash) {
590590
self.emitter.borrow_mut().emit(db);
591+
if db.is_error() {
592+
self.bump_err_count();
593+
}
591594
}
592595
}
593596
}

src/test/ui/feature-gate/issue-43106-gating-of-rustc_deprecated.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ error: stability attributes may not be used outside of the standard library
4242
35 | #[rustc_deprecated = "1500"] impl S { }
4343
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4444

45-
error: aborting due to 9 previous errors
45+
error: aborting due to 8 previous errors
4646

src/test/ui/feature-gate/issue-43106-gating-of-stable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ error: stability attributes may not be used outside of the standard library
4242
35 | #[stable = "1300"] impl S { }
4343
| ^^^^^^^^^^^^^^^^^^
4444

45-
error: aborting due to 9 previous errors
45+
error: aborting due to 8 previous errors
4646

src/test/ui/feature-gate/issue-43106-gating-of-unstable.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ error: stability attributes may not be used outside of the standard library
4242
35 | #[unstable = "1200"] impl S { }
4343
| ^^^^^^^^^^^^^^^^^^^^
4444

45-
error: aborting due to 9 previous errors
45+
error: aborting due to 8 previous errors
4646

src/test/ui/issue-42106.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ error[E0502]: cannot borrow `*collection` as mutable because `collection` is als
88
14 | }
99
| - immutable borrow ends here
1010

11-
error: aborting due to 2 previous errors
11+
error: aborting due to previous error
1212

src/test/ui/span/macro-ty-params.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ error: generic arguments in macro path
2222
20 | m!(MyTrait<>); //~ ERROR generic arguments in macro path
2323
| ^^
2424

25-
error: aborting due to 5 previous errors
25+
error: aborting due to 4 previous errors
2626

0 commit comments

Comments
 (0)