@@ -1054,7 +1054,7 @@ impl Handler {
1054
1054
}
1055
1055
let mut diagnostic = Diagnostic :: new ( Level :: DelayedBug , msg) ;
1056
1056
diagnostic. set_span ( sp) ;
1057
- inner. emit_diagnostic ( & mut diagnostic) . unwrap ( )
1057
+ inner. emit_diagnostic ( diagnostic) . unwrap ( )
1058
1058
}
1059
1059
1060
1060
// FIXME(eddyb) note the comment inside `impl Drop for HandlerInner`, that's
@@ -1064,15 +1064,17 @@ impl Handler {
1064
1064
1065
1065
let mut diagnostic = Diagnostic :: new ( Level :: DelayedBug , msg) ;
1066
1066
if inner. flags . report_delayed_bugs {
1067
- inner. emit_diagnostic ( & mut diagnostic) ;
1067
+ inner. emit_diagnostic_without_consuming ( & mut diagnostic) ;
1068
1068
}
1069
1069
let backtrace = std:: backtrace:: Backtrace :: capture ( ) ;
1070
1070
inner. good_path_delayed_bugs . push ( DelayedDiagnostic :: with_backtrace ( diagnostic, backtrace) ) ;
1071
1071
}
1072
1072
1073
1073
#[ track_caller]
1074
1074
pub fn span_bug_no_panic ( & self , span : impl Into < MultiSpan > , msg : impl Into < DiagnosticMessage > ) {
1075
- self . emit_diagnostic ( Diagnostic :: new ( Bug , msg) . set_span ( span) ) ;
1075
+ let mut diag = Diagnostic :: new ( Bug , msg) ;
1076
+ diag. set_span ( span) ;
1077
+ self . emit_diagnostic ( diag) ;
1076
1078
}
1077
1079
1078
1080
#[ track_caller]
@@ -1185,10 +1187,10 @@ impl Handler {
1185
1187
DiagnosticMessage :: Str ( warnings) ,
1186
1188
) ) ,
1187
1189
( _, 0 ) => {
1188
- inner. emit_diagnostic ( & mut Diagnostic :: new ( Fatal , errors) ) ;
1190
+ inner. emit_diagnostic ( Diagnostic :: new ( Fatal , errors) ) ;
1189
1191
}
1190
1192
( _, _) => {
1191
- inner. emit_diagnostic ( & mut Diagnostic :: new ( Fatal , format ! ( "{errors}; {warnings}" ) ) ) ;
1193
+ inner. emit_diagnostic ( Diagnostic :: new ( Fatal , format ! ( "{errors}; {warnings}" ) ) ) ;
1192
1194
}
1193
1195
}
1194
1196
@@ -1255,8 +1257,17 @@ impl Handler {
1255
1257
self . inner . borrow_mut ( ) . emitter . emit_diagnostic ( & db) ;
1256
1258
}
1257
1259
1258
- pub fn emit_diagnostic ( & self , diagnostic : & mut Diagnostic ) -> Option < ErrorGuaranteed > {
1259
- self . inner . borrow_mut ( ) . emit_diagnostic ( diagnostic)
1260
+ pub fn emit_diagnostic ( & self , mut diagnostic : Diagnostic ) -> Option < ErrorGuaranteed > {
1261
+ self . emit_diagnostic_without_consuming ( & mut diagnostic)
1262
+ }
1263
+
1264
+ // It's unfortunate this exists. `emit_diagnostic` is preferred, because it
1265
+ // consumes the diagnostic, thus ensuring it is emitted just once.
1266
+ pub ( crate ) fn emit_diagnostic_without_consuming (
1267
+ & self ,
1268
+ diagnostic : & mut Diagnostic ,
1269
+ ) -> Option < ErrorGuaranteed > {
1270
+ self . inner . borrow_mut ( ) . emit_diagnostic_without_consuming ( diagnostic)
1260
1271
}
1261
1272
1262
1273
pub fn emit_err < ' a > ( & ' a self , err : impl IntoDiagnostic < ' a > ) -> ErrorGuaranteed {
@@ -1370,7 +1381,7 @@ impl Handler {
1370
1381
// Here the diagnostic is given back to `emit_diagnostic` where it was first
1371
1382
// intercepted. Now it should be processed as usual, since the unstable expectation
1372
1383
// id is now stable.
1373
- inner. emit_diagnostic ( & mut diag) ;
1384
+ inner. emit_diagnostic ( diag) ;
1374
1385
}
1375
1386
}
1376
1387
@@ -1412,7 +1423,7 @@ impl HandlerInner {
1412
1423
let has_errors = self . has_errors ( ) ;
1413
1424
let diags = self . stashed_diagnostics . drain ( ..) . map ( |x| x. 1 ) . collect :: < Vec < _ > > ( ) ;
1414
1425
let mut reported = None ;
1415
- for mut diag in diags {
1426
+ for diag in diags {
1416
1427
// Decrement the count tracking the stash; emitting will increment it.
1417
1428
if diag. is_error ( ) {
1418
1429
if matches ! ( diag. level, Level :: Error { lint: true } ) {
@@ -1432,14 +1443,20 @@ impl HandlerInner {
1432
1443
}
1433
1444
}
1434
1445
}
1435
- let reported_this = self . emit_diagnostic ( & mut diag) ;
1446
+ let reported_this = self . emit_diagnostic ( diag) ;
1436
1447
reported = reported. or ( reported_this) ;
1437
1448
}
1438
1449
reported
1439
1450
}
1440
1451
1441
- // FIXME(eddyb) this should ideally take `diagnostic` by value.
1442
- fn emit_diagnostic ( & mut self , diagnostic : & mut Diagnostic ) -> Option < ErrorGuaranteed > {
1452
+ fn emit_diagnostic ( & mut self , mut diagnostic : Diagnostic ) -> Option < ErrorGuaranteed > {
1453
+ self . emit_diagnostic_without_consuming ( & mut diagnostic)
1454
+ }
1455
+
1456
+ fn emit_diagnostic_without_consuming (
1457
+ & mut self ,
1458
+ diagnostic : & mut Diagnostic ,
1459
+ ) -> Option < ErrorGuaranteed > {
1443
1460
if matches ! ( diagnostic. level, Level :: Error { .. } | Level :: Fatal ) && self . treat_err_as_bug ( )
1444
1461
{
1445
1462
diagnostic. level = Level :: Bug ;
@@ -1576,12 +1593,14 @@ impl HandlerInner {
1576
1593
1577
1594
#[ track_caller]
1578
1595
fn span_bug ( & mut self , sp : impl Into < MultiSpan > , msg : impl Into < DiagnosticMessage > ) -> ! {
1579
- self . emit_diagnostic ( Diagnostic :: new ( Bug , msg) . set_span ( sp) ) ;
1596
+ let mut diag = Diagnostic :: new ( Bug , msg) ;
1597
+ diag. set_span ( sp) ;
1598
+ self . emit_diagnostic ( diag) ;
1580
1599
panic:: panic_any ( ExplicitBug ) ;
1581
1600
}
1582
1601
1583
1602
fn failure_note ( & mut self , msg : impl Into < DiagnosticMessage > ) {
1584
- self . emit_diagnostic ( & mut Diagnostic :: new ( FailureNote , msg) ) ;
1603
+ self . emit_diagnostic ( Diagnostic :: new ( FailureNote , msg) ) ;
1585
1604
}
1586
1605
1587
1606
fn flush_delayed (
@@ -1613,7 +1632,7 @@ impl HandlerInner {
1613
1632
if no_bugs {
1614
1633
// Put the overall explanation before the `DelayedBug`s, to
1615
1634
// frame them better (e.g. separate warnings from them).
1616
- self . emit_diagnostic ( & mut Diagnostic :: new ( Bug , explanation) ) ;
1635
+ self . emit_diagnostic ( Diagnostic :: new ( Bug , explanation) ) ;
1617
1636
no_bugs = false ;
1618
1637
}
1619
1638
@@ -1628,7 +1647,7 @@ impl HandlerInner {
1628
1647
}
1629
1648
bug. level = Level :: Bug ;
1630
1649
1631
- self . emit_diagnostic ( & mut bug) ;
1650
+ self . emit_diagnostic ( bug) ;
1632
1651
}
1633
1652
1634
1653
// Panic with `DelayedBugPanic` to avoid "unexpected panic" messages.
0 commit comments