Skip to content

Commit 40f206e

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. - tests/ui/mir/lint/*.rs: These tests involved delayed bugs that get shown. The query stack disappears because these delayed bugs are now printed at the end, rather than when they are created. - storage-return.rs: This is the only test with a big change. It triggers a delayed bug and then a normal error. It used to use `-Ztreat-err-as-bug=1` to abort on the delayed bug. That is no longer possible, and the delayed bug doesn't show up because of the normal error. So I changed the test to use the normal error. The normal error does seem related to the delayed bug, so hopefully it's good enough.
1 parent 3330940 commit 40f206e

10 files changed

+76
-73
lines changed

compiler/rustc_errors/src/lib.rs

+18-37
Original file line numberDiff line numberDiff line change
@@ -861,10 +861,6 @@ impl DiagCtxt {
861861
/// directly).
862862
#[track_caller]
863863
pub fn delayed_bug(&self, msg: impl Into<DiagnosticMessage>) -> ErrorGuaranteed {
864-
let treat_next_err_as_bug = self.inner.borrow().treat_next_err_as_bug();
865-
if treat_next_err_as_bug {
866-
self.bug(msg);
867-
}
868864
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).emit()
869865
}
870866

@@ -878,10 +874,6 @@ impl DiagCtxt {
878874
sp: impl Into<MultiSpan>,
879875
msg: impl Into<DiagnosticMessage>,
880876
) -> ErrorGuaranteed {
881-
let treat_next_err_as_bug = self.inner.borrow().treat_next_err_as_bug();
882-
if treat_next_err_as_bug {
883-
self.span_bug(sp, msg);
884-
}
885877
DiagnosticBuilder::<ErrorGuaranteed>::new(self, DelayedBug, msg).with_span(sp).emit()
886878
}
887879

@@ -1370,20 +1362,15 @@ impl DiagCtxtInner {
13701362
}
13711363

13721364
fn treat_err_as_bug(&self) -> bool {
1373-
self.flags.treat_err_as_bug.is_some_and(|c| {
1374-
self.err_count + self.lint_err_count + self.delayed_bug_count() >= c.get()
1375-
})
1365+
self.flags.treat_err_as_bug.is_some_and(|c| self.err_count + self.lint_err_count >= c.get())
13761366
}
13771367

13781368
// Use this one before incrementing `err_count`.
1369+
#[allow(unused)] // njn: temp
13791370
fn treat_next_err_as_bug(&self) -> bool {
1380-
self.flags.treat_err_as_bug.is_some_and(|c| {
1381-
self.err_count + self.lint_err_count + self.delayed_bug_count() + 1 >= c.get()
1382-
})
1383-
}
1384-
1385-
fn delayed_bug_count(&self) -> usize {
1386-
self.span_delayed_bugs.len() + self.good_path_delayed_bugs.len()
1371+
self.flags
1372+
.treat_err_as_bug
1373+
.is_some_and(|c| self.err_count + self.lint_err_count + 1 >= c.get())
13871374
}
13881375

13891376
fn has_errors(&self) -> bool {
@@ -1395,7 +1382,7 @@ impl DiagCtxtInner {
13951382
}
13961383

13971384
fn flush_delayed(&mut self, kind: DelayedBugKind) {
1398-
let (bugs, explanation) = match kind {
1385+
let (bugs, note1) = match kind {
13991386
DelayedBugKind::Normal => (
14001387
std::mem::take(&mut self.span_delayed_bugs),
14011388
"no errors encountered even though `span_delayed_bug` issued",
@@ -1405,6 +1392,7 @@ impl DiagCtxtInner {
14051392
"no warnings or errors encountered even though `good_path_delayed_bugs` issued",
14061393
),
14071394
};
1395+
let note2 = "those delayed bugs will now be shown as internal compiler errors";
14081396

14091397
if bugs.is_empty() {
14101398
return;
@@ -1430,8 +1418,11 @@ impl DiagCtxtInner {
14301418

14311419
if i == 0 {
14321420
// Put the overall explanation before the `DelayedBug`s, to
1433-
// frame them better (e.g. separate warnings from them).
1434-
self.emit_diagnostic(Diagnostic::new(Bug, explanation));
1421+
// frame them better (e.g. separate warnings from them). Also,
1422+
// make it a note so it doesn't count as an error, because that
1423+
// could trigger `-Ztreat-err-as-bug`, which we don't want.
1424+
self.emit_diagnostic(Diagnostic::new(Note, note1));
1425+
self.emit_diagnostic(Diagnostic::new(Note, note2));
14351426
}
14361427

14371428
let mut bug =
@@ -1457,22 +1448,12 @@ impl DiagCtxtInner {
14571448

14581449
fn panic_if_treat_err_as_bug(&self) {
14591450
if self.treat_err_as_bug() {
1460-
match (
1461-
self.err_count + self.lint_err_count,
1462-
self.delayed_bug_count(),
1463-
self.flags.treat_err_as_bug.map(|c| c.get()).unwrap(),
1464-
) {
1465-
(1, 0, 1) => panic!("aborting due to `-Z treat-err-as-bug=1`"),
1466-
(0, 1, 1) => panic!("aborting due delayed bug with `-Z treat-err-as-bug=1`"),
1467-
(count, delayed_count, val) => {
1468-
if delayed_count > 0 {
1469-
panic!(
1470-
"aborting after {count} errors and {delayed_count} delayed bugs due to `-Z treat-err-as-bug={val}`",
1471-
)
1472-
} else {
1473-
panic!("aborting after {count} errors due to `-Z treat-err-as-bug={val}`")
1474-
}
1475-
}
1451+
let n = self.flags.treat_err_as_bug.map(|c| c.get()).unwrap();
1452+
assert_eq!(n, self.err_count + self.lint_err_count);
1453+
if n == 1 {
1454+
panic!("aborting due to `-Z treat-err-as-bug=1`");
1455+
} else {
1456+
panic!("aborting after {n} errors due to `-Z treat-err-as-bug={n}`");
14761457
}
14771458
}
14781459
}
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
|
5-
=
7+
= note: delayed at compiler/rustc_infer/src/infer/opaque_types/table.rs:42:43
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:20:5
1012
|
1113
LL | same_output(foo, rpit);
1214
| ^^^^^^^^^^^^^^^^^^^^^^
1315
|
14-
15-
--> $DIR/equality-in-canonical-query.rs:19:5
16+
note: delayed at /home/njn/dev/rust1/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs:157:29
17+
--> $DIR/equality-in-canonical-query.rs:20: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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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" -> ""
910
//[clone] normalize-stderr-test: "(?m)^ *\d+: .*\n" -> ""
1011
//[clone] normalize-stderr-test: "(?m)^ *at .*\n" -> ""
1112

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-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// compile-flags: -Zlint-mir -Ztreat-err-as-bug
2-
// failure-status: 101
3-
// dont-check-compiler-stderr
4-
// error-pattern: has storage when returning
51
#![feature(custom_mir, core_intrinsics)]
62
extern crate core;
73
use core::intrinsics::mir::*;
@@ -12,7 +8,7 @@ fn main() {
128
let a: ();
139
{
1410
StorageLive(a);
15-
RET = a;
11+
RET = a; //~ ERROR used binding isn't initialized
1612
Return()
1713
}
1814
)
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0381]: used binding isn't initialized
2+
--> $DIR/storage-return.rs:11:13
3+
|
4+
LL | / mir!(
5+
LL | | let a: ();
6+
LL | | {
7+
LL | | StorageLive(a);
8+
LL | | RET = a;
9+
| | ^^^^^^^ value used here but it isn't initialized
10+
LL | | Return()
11+
LL | | }
12+
LL | | )
13+
| |_____- binding declared here but left uninitialized
14+
|
15+
help: consider assigning a value
16+
|
17+
LL | let a: () = todo!();
18+
| +++++++++
19+
20+
error: aborting due to 1 previous error
21+
22+
For more information about this error, try `rustc --explain E0381`.

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// compile-flags: -Ztreat-err-as-bug
22
// failure-status: 101
33
// error-pattern: aborting due to `-Z treat-err-as-bug=1`
4-
// error-pattern: [trigger_span_delayed_bug] triggering a span delayed bug for testing incremental
4+
// error-pattern: delayed span bug triggered by #[rustc_error(span_delayed_bug_from_inside_query)]
55
// normalize-stderr-test "note: .*\n\n" -> ""
66
// normalize-stderr-test "thread 'rustc' panicked.*:\n.*\n" -> ""
77
// rustc-env:RUST_BACKTRACE=0
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
error: internal compiler error: delayed span bug triggered by #[rustc_error(span_delayed_bug_from_inside_query)]
22
--> $DIR/span_delayed_bug.rs:12:1
33
|
4+
LL | fn main() {}
5+
| ^^^^^^^^^
6+
|
7+
note: delayed at compiler/rustc_middle/src/util/bug.rs:45:15 - disabled backtrace
8+
--> $DIR/span_delayed_bug.rs:12:1
9+
|
410
LL | fn main() {}
511
| ^^^^^^^^^
612

713
error: the compiler unexpectedly panicked. this is a bug.
814

915
query stack during panic:
10-
#0 [trigger_span_delayed_bug] triggering a span delayed bug for testing incremental
1116
end of query stack
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
|
5-
=
7+
= note: delayed at compiler/rustc_infer/src/infer/opaque_types/table.rs:42:43
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:30:5
1012
|
1113
LL | query(get_rpit);
1214
| ^^^^^^^^^^^^^^^
1315
|
14-
15-
--> $DIR/rpit_tait_equality_in_canonical_query.rs:28:5
16+
note: delayed at /home/njn/dev/rust1/compiler/rustc_trait_selection/src/traits/query/type_op/mod.rs:157:29
17+
--> $DIR/rpit_tait_equality_in_canonical_query.rs:30: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

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
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" -> ""
1618
//[current] normalize-stderr-test: "(?m)^ *\d+: .*\n" -> ""
1719
//[current] normalize-stderr-test: "(?m)^ *at .*\n" -> ""
1820

0 commit comments

Comments
 (0)