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