Skip to content

Commit f1ac541

Browse files
committed
Don't consider delayed bugs for -Ztreat-err-as-bug.
`-Ztreat-err-as-bug` treats normal errors and delayed bugs equally, which can lead to some really surprising results. This commit changes `-Ztreat-err-as-bug` so it ignores delayed bugs, unless they get promoted to proper bugs and are printed. This feels to me much simpler and more logical. And it simplifies the implementation: - The `-Ztreat-err-as-bug` check is removed from in `DiagCtxt::{delayed_bug,span_delayed_bug}`. - `treat_err_as_bug` doesn't need to count delayed bugs. - The `-Ztreat-err-as-bug` panic message is simpler, because it doesn't have to mention delayed bugs. Output of delayed bugs is now more consistent. They're always printed the same way. Previously when they triggered `-Ztreat-err-as-bug` they would be printed slightly differently, via `span_bug` in `span_delayed_bug` or `delayed_bug`. A minor behaviour change: the "no errors encountered even though `span_delayed_bug` issued" printed before delayed bugs is now a note rather than a bug. This is done so it doesn't get counted as an error that might trigger `-Ztreat-err-as-bug`, which would be silly. This means that if you use `-Ztreat-err-as-bug=1` and there are no normal errors but there are delayed bugs, the first delayed bug will be shown (and the panic will happen after it's printed). Also, I have added a second note saying "those delayed bugs will now be shown as internal compiler errors". I think this makes it clearer what is happening, because the whole concept of delayed bugs is non-obvious. There are some test changes. - equality-in-canonical-query.rs: Minor output changes, and the error count reduces by one because the "no errors encountered even though `span_delayed_bug` issued" message is no longer counted as an error. - rpit_tait_equality_in_canonical_query.rs: Ditto. - storage-live.rs: The query stack disappears because these delayed bugs are now printed at the end, rather than when they are created. - storage-return.rs, span_delayed_bug.rs: now need `-Zeagerly-emit-delayed-bugs` because they need the delayed bugs emitted immediately to preserve behaviour.
1 parent 2319be8 commit f1ac541

8 files changed

+51
-68
lines changed

compiler/rustc_errors/src/lib.rs

+23-41
Original file line numberDiff line numberDiff line change
@@ -865,10 +865,6 @@ impl DiagCtxt {
865865
/// directly).
866866
#[track_caller]
867867
pub fn delayed_bug(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
868-
let treat_next_err_as_bug = self.inner.borrow().treat_next_err_as_bug();
869-
if treat_next_err_as_bug {
870-
self.bug(msg);
871-
}
872868
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug(DelayedBugKind::Normal), msg)
873869
.emit()
874870
}
@@ -883,10 +879,6 @@ impl DiagCtxt {
883879
sp: impl Into<MultiSpan>,
884880
msg: impl Into<DiagnosticMessage>,
885881
) -> ErrorGuaranteed {
886-
let treat_next_err_as_bug = self.inner.borrow().treat_next_err_as_bug();
887-
if treat_next_err_as_bug {
888-
self.span_bug(sp, msg);
889-
}
890882
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug(DelayedBugKind::Normal), msg)
891883
.with_span(sp)
892884
.emit()
@@ -1259,10 +1251,6 @@ impl DiagCtxtInner {
12591251
}
12601252

12611253
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1262-
if matches!(diagnostic.level, Error | Fatal) && self.treat_next_err_as_bug() {
1263-
diagnostic.level = Bug;
1264-
}
1265-
12661254
// The `LintExpectationId` can be stable or unstable depending on when it was created.
12671255
// Diagnostics created before the definition of `HirId`s are unstable and can not yet
12681256
// be stored. Instead, they are buffered until the `LintExpectationId` is replaced by
@@ -1298,6 +1286,12 @@ impl DiagCtxtInner {
12981286
_ => {}
12991287
}
13001288

1289+
// This must come after the possible promotion of `DelayedBug` to
1290+
// `Error` above.
1291+
if matches!(diagnostic.level, Error | Fatal) && self.treat_next_err_as_bug() {
1292+
diagnostic.level = Bug;
1293+
}
1294+
13011295
if diagnostic.has_future_breakage() {
13021296
// Future breakages aren't emitted if they're Level::Allow,
13031297
// but they still need to be constructed and stashed below,
@@ -1387,20 +1381,14 @@ impl DiagCtxtInner {
13871381
}
13881382

13891383
fn treat_err_as_bug(&self) -> bool {
1390-
self.flags.treat_err_as_bug.is_some_and(|c| {
1391-
self.err_count + self.lint_err_count + self.delayed_bug_count() >= c.get()
1392-
})
1384+
self.flags.treat_err_as_bug.is_some_and(|c| self.err_count + self.lint_err_count >= c.get())
13931385
}
13941386

13951387
// Use this one before incrementing `err_count`.
13961388
fn treat_next_err_as_bug(&self) -> bool {
1397-
self.flags.treat_err_as_bug.is_some_and(|c| {
1398-
self.err_count + self.lint_err_count + self.delayed_bug_count() + 1 >= c.get()
1399-
})
1400-
}
1401-
1402-
fn delayed_bug_count(&self) -> usize {
1403-
self.span_delayed_bugs.len() + self.good_path_delayed_bugs.len()
1389+
self.flags
1390+
.treat_err_as_bug
1391+
.is_some_and(|c| self.err_count + self.lint_err_count + 1 >= c.get())
14041392
}
14051393

14061394
fn has_errors(&self) -> bool {
@@ -1412,7 +1400,7 @@ impl DiagCtxtInner {
14121400
}
14131401

14141402
fn flush_delayed(&mut self, kind: DelayedBugKind) {
1415-
let (bugs, explanation) = match kind {
1403+
let (bugs, note1) = match kind {
14161404
DelayedBugKind::Normal => (
14171405
std::mem::take(&mut self.span_delayed_bugs),
14181406
"no errors encountered even though `span_delayed_bug` issued",
@@ -1422,6 +1410,7 @@ impl DiagCtxtInner {
14221410
"no warnings or errors encountered even though `good_path_delayed_bugs` issued",
14231411
),
14241412
};
1413+
let note2 = "those delayed bugs will now be shown as internal compiler errors";
14251414

14261415
if bugs.is_empty() {
14271416
return;
@@ -1447,8 +1436,11 @@ impl DiagCtxtInner {
14471436

14481437
if i == 0 {
14491438
// Put the overall explanation before the `DelayedBug`s, to
1450-
// frame them better (e.g. separate warnings from them).
1451-
self.emit_diagnostic(Diagnostic::new(Bug, explanation));
1439+
// frame them better (e.g. separate warnings from them). Also,
1440+
// make it a note so it doesn't count as an error, because that
1441+
// could trigger `-Ztreat-err-as-bug`, which we don't want.
1442+
self.emit_diagnostic(Diagnostic::new(Note, note1));
1443+
self.emit_diagnostic(Diagnostic::new(Note, note2));
14521444
}
14531445

14541446
let mut bug =
@@ -1474,22 +1466,12 @@ impl DiagCtxtInner {
14741466

14751467
fn panic_if_treat_err_as_bug(&self) {
14761468
if self.treat_err_as_bug() {
1477-
match (
1478-
self.err_count + self.lint_err_count,
1479-
self.delayed_bug_count(),
1480-
self.flags.treat_err_as_bug.map(|c| c.get()).unwrap(),
1481-
) {
1482-
(1, 0, 1) => panic!("aborting due to `-Z treat-err-as-bug=1`"),
1483-
(0, 1, 1) => panic!("aborting due delayed bug with `-Z treat-err-as-bug=1`"),
1484-
(count, delayed_count, val) => {
1485-
if delayed_count > 0 {
1486-
panic!(
1487-
"aborting after {count} errors and {delayed_count} delayed bugs due to `-Z treat-err-as-bug={val}`",
1488-
)
1489-
} else {
1490-
panic!("aborting after {count} errors due to `-Z treat-err-as-bug={val}`")
1491-
}
1492-
}
1469+
let n = self.flags.treat_err_as_bug.map(|c| c.get()).unwrap();
1470+
assert_eq!(n, self.err_count + self.lint_err_count);
1471+
if n == 1 {
1472+
panic!("aborting due to `-Z treat-err-as-bug=1`");
1473+
} else {
1474+
panic!("aborting after {n} errors due to `-Z treat-err-as-bug={n}`");
14931475
}
14941476
}
14951477
}
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
error: internal compiler error: no errors encountered even though `span_delayed_bug` issued
1+
note: no errors encountered even though `span_delayed_bug` issued
2+
3+
note: those delayed bugs will now be shown as internal compiler errors
24

35
error: internal compiler error: {OpaqueTypeKey { def_id: DefId(rpit::{opaque#0}), args: [] }: OpaqueTypeDecl { hidden_type: OpaqueHiddenType { span: no-location (#0), ty: Alias(Opaque, AliasTy { args: [], def_id: DefId(foo::{opaque#0}) }) } }}
46
|
57
=
68

79

810
error: internal compiler error: error performing ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: ProvePredicate { predicate: Binder { value: ProjectionPredicate(AliasTy { args: [FnDef(DefId(rpit), []), ()], def_id: DefId(ops::function::FnOnce::Output) }, Term::Ty(Alias(Opaque, AliasTy { args: [], def_id: DefId(foo::{opaque#0}) }))), bound_vars: [] } } }
9-
--> $DIR/equality-in-canonical-query.rs:19:5
11+
--> $DIR/equality-in-canonical-query.rs:21:5
1012
|
1113
LL | same_output(foo, rpit);
1214
| ^^^^^^^^^^^^^^^^^^^^^^
1315
|
1416

15-
--> $DIR/equality-in-canonical-query.rs:19:5
17+
--> $DIR/equality-in-canonical-query.rs:21:5
1618
|
1719
LL | same_output(foo, rpit);
1820
| ^^^^^^^^^^^^^^^^^^^^^^
1921

20-
21-
22-
23-
24-
25-
2622
query stack during panic:
2723
end of query stack
28-
error: aborting due to 3 previous errors
24+
error: aborting due to 2 previous errors
2925

tests/ui/impl-trait/equality-in-canonical-query.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// issue: #116877
22
// revisions: sized clone
33
//[sized] check-pass
4-
54
//[clone] known-bug: #108498
65
//[clone] failure-status: 101
76
//[clone] normalize-stderr-test: "DefId\(.*?\]::" -> "DefId("
8-
//[clone] normalize-stderr-test: "(?m)note: .*$" -> ""
7+
//[clone] normalize-stderr-test: "(?m)note: we would appreciate a bug report.*\n\n" -> ""
8+
//[clone] normalize-stderr-test: "(?m)note: rustc.*running on.*\n\n" -> ""
9+
//[clone] normalize-stderr-test: "(?m)note: compiler flags.*\n\n" -> ""
10+
//[clone] normalize-stderr-test: "(?m)note: delayed at.*$" -> ""
911
//[clone] normalize-stderr-test: "(?m)^ *\d+: .*\n" -> ""
1012
//[clone] normalize-stderr-test: "(?m)^ *at .*\n" -> ""
1113

tests/ui/mir/lint/storage-live.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error: internal compiler error: broken MIR in Item(DefId(0:8 ~ storage_live[HASH
22
StorageLive(_1) which already has storage here
33
--> $DIR/storage-live.rs:22:13
44
|
5+
LL | StorageLive(a);
6+
| ^^^^^^^^^^^^^^
7+
|
8+
note: delayed at compiler/rustc_mir_transform/src/lint.rs:97:26 - disabled backtrace
9+
--> $DIR/storage-live.rs:22:13
10+
|
511
LL | StorageLive(a);
612
| ^^^^^^^^^^^^^^
713

814
aborting due to `-Z treat-err-as-bug=1`
915
error: the compiler unexpectedly panicked. this is a bug.
1016

1117
query stack during panic:
12-
#0 [mir_const] preparing `multiple_storage` for borrow checking
13-
#1 [mir_promoted] promoting constants in MIR for `multiple_storage`
1418
end of query stack

tests/ui/mir/lint/storage-return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Zlint-mir -Ztreat-err-as-bug
1+
// compile-flags: -Zlint-mir -Ztreat-err-as-bug -Zeagerly-emit-delayed-bugs
22
// failure-status: 101
33
// dont-check-compiler-stderr
44
// error-pattern: has storage when returning

tests/ui/treat-err-as-bug/span_delayed_bug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// compile-flags: -Ztreat-err-as-bug
1+
// compile-flags: -Ztreat-err-as-bug -Zeagerly-emit-delayed-bugs
22
// failure-status: 101
33
// error-pattern: aborting due to `-Z treat-err-as-bug=1`
44
// error-pattern: [trigger_span_delayed_bug] triggering a span delayed bug for testing incremental
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
1-
error: internal compiler error: no errors encountered even though `span_delayed_bug` issued
1+
note: no errors encountered even though `span_delayed_bug` issued
2+
3+
note: those delayed bugs will now be shown as internal compiler errors
24

35
error: internal compiler error: {OpaqueTypeKey { def_id: DefId(get_rpit::{opaque#0}), args: [] }: OpaqueTypeDecl { hidden_type: OpaqueHiddenType { span: no-location (#0), ty: Alias(Opaque, AliasTy { args: [], def_id: DefId(Opaque::{opaque#0}) }) } }}
46
|
57
=
68

79

810
error: internal compiler error: error performing ParamEnvAnd { param_env: ParamEnv { caller_bounds: [], reveal: UserFacing }, value: ProvePredicate { predicate: Binder { value: ProjectionPredicate(AliasTy { args: [FnDef(DefId(get_rpit), []), ()], def_id: DefId(ops::function::FnOnce::Output) }, Term::Ty(Alias(Opaque, AliasTy { args: [], def_id: DefId(Opaque::{opaque#0}) }))), bound_vars: [] } } }
9-
--> $DIR/rpit_tait_equality_in_canonical_query.rs:28:5
11+
--> $DIR/rpit_tait_equality_in_canonical_query.rs:31:5
1012
|
1113
LL | query(get_rpit);
1214
| ^^^^^^^^^^^^^^^
1315
|
1416

15-
--> $DIR/rpit_tait_equality_in_canonical_query.rs:28:5
17+
--> $DIR/rpit_tait_equality_in_canonical_query.rs:31:5
1618
|
1719
LL | query(get_rpit);
1820
| ^^^^^^^^^^^^^^^
1921

20-
21-
22-
23-
24-
25-
2622
query stack during panic:
2723
end of query stack
28-
error: aborting due to 3 previous errors
24+
error: aborting due to 2 previous errors
2925

tests/ui/type-alias-impl-trait/rpit_tait_equality_in_canonical_query.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
//[current] known-bug: #108498
1313
//[current] failure-status: 101
1414
//[current] normalize-stderr-test: "DefId\(.*?\]::" -> "DefId("
15-
//[current] normalize-stderr-test: "(?m)note: .*$" -> ""
15+
//[current] normalize-stderr-test: "(?m)note: we would appreciate a bug report.*\n\n" -> ""
16+
//[current] normalize-stderr-test: "(?m)note: rustc.*running on.*\n\n" -> ""
17+
//[current] normalize-stderr-test: "(?m)note: compiler flags.*\n\n" -> ""
18+
//[current] normalize-stderr-test: "(?m)note: delayed at.*$" -> ""
1619
//[current] normalize-stderr-test: "(?m)^ *\d+: .*\n" -> ""
1720
//[current] normalize-stderr-test: "(?m)^ *at .*\n" -> ""
1821

0 commit comments

Comments
 (0)