Skip to content

Commit

Permalink
Auto merge of #47146 - ereslibre:issue-42106, r=estebank
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
bors committed Jan 3, 2018
2 parents 687d3d1 + 063607e commit 0f4ebf9
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 20 deletions.
3 changes: 0 additions & 3 deletions src/librustc_errors/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ impl Diagnostic {

/// Cancel the diagnostic (a structured diagnostic must either be emitted or
/// canceled or it will panic when dropped).
/// BEWARE: if this DiagnosticBuilder is an error, then creating it will
/// bump the error count on the Handler and canceling it won't undo that.
/// If you want to decrement the error count you should use `Handler::cancel`.
pub fn cancel(&mut self) {
self.level = Level::Cancelled;
}
Expand Down
18 changes: 6 additions & 12 deletions src/librustc_errors/diagnostic_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ impl<'a> DiagnosticBuilder<'a> {
return;
}

let is_error = match self.level {
self.handler.emit_db(&self);
self.cancel();
}

pub fn is_error(&self) -> bool {
match self.level {
Level::Bug |
Level::Fatal |
Level::PhaseFatal |
Expand All @@ -97,18 +102,7 @@ impl<'a> DiagnosticBuilder<'a> {
Level::Cancelled => {
false
}
};

self.handler.emit_db(&self);
self.cancel();

if is_error {
self.handler.bump_err_count();
}

// if self.is_fatal() {
// panic!(FatalError);
// }
}

/// Convenience function for internal use, clients should use one of the
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_errors/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,9 @@ impl Handler {
// one:
if self.emitted_diagnostics.borrow_mut().insert(diagnostic_hash) {
self.emitter.borrow_mut().emit(db);
if db.is_error() {
self.bump_err_count();
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@ error: stability attributes may not be used outside of the standard library
35 | #[rustc_deprecated = "1500"] impl S { }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 9 previous errors
error: aborting due to 8 previous errors

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

error: aborting due to 9 previous errors
error: aborting due to 8 previous errors

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

error: aborting due to 9 previous errors
error: aborting due to 8 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/issue-42106.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ error[E0502]: cannot borrow `*collection` as mutable because `collection` is als
14 | }
| - immutable borrow ends here

error: aborting due to 2 previous errors
error: aborting due to previous error

2 changes: 1 addition & 1 deletion src/test/ui/span/macro-ty-params.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ error: generic arguments in macro path
20 | m!(MyTrait<>); //~ ERROR generic arguments in macro path
| ^^

error: aborting due to 5 previous errors
error: aborting due to 4 previous errors

0 comments on commit 0f4ebf9

Please sign in to comment.