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