@@ -16,8 +16,8 @@ use rustc_data_structures::sync::Lrc;
16
16
use rustc_errors:: emitter:: Emitter ;
17
17
use rustc_errors:: translation:: Translate ;
18
18
use rustc_errors:: {
19
- DiagCtxt , DiagnosticArgName , DiagnosticArgValue , DiagnosticBuilder , DiagnosticMessage , ErrCode ,
20
- FatalError , FluentBundle , Level , Style ,
19
+ DiagCtxt , DiagnosticArgMap , DiagnosticBuilder , DiagnosticMessage , ErrCode , FatalError ,
20
+ FluentBundle , Level , MultiSpan , Style ,
21
21
} ;
22
22
use rustc_fs_util:: link_or_copy;
23
23
use rustc_hir:: def_id:: { CrateNum , LOCAL_CRATE } ;
@@ -999,11 +999,29 @@ pub(crate) enum Message<B: WriteBackendMethods> {
999
999
/// process another codegen unit.
1000
1000
pub struct CguMessage ;
1001
1001
1002
+ // A cut-down version of `rustc_errors::Diagnostic` that impls `Send`, which
1003
+ // can be used to send diagnostics from codegen threads to the main thread.
1004
+ // It's missing the following fields from `rustc_errors::Diagnostic`.
1005
+ // - `span`: it doesn't impl `Send`.
1006
+ // - `suggestions`: it doesn't impl `Send`, and isn't used for codegen
1007
+ // diagnostics.
1008
+ // - `sort_span`: it doesn't impl `Send`.
1009
+ // - `is_lint`: lints aren't relevant during codegen.
1010
+ // - `emitted_at`: not used for codegen diagnostics.
1002
1011
struct Diagnostic {
1003
- msgs : Vec < ( DiagnosticMessage , Style ) > ,
1004
- args : FxIndexMap < DiagnosticArgName , DiagnosticArgValue > ,
1012
+ level : Level ,
1013
+ messages : Vec < ( DiagnosticMessage , Style ) > ,
1005
1014
code : Option < ErrCode > ,
1006
- lvl : Level ,
1015
+ children : Vec < Subdiagnostic > ,
1016
+ args : DiagnosticArgMap ,
1017
+ }
1018
+
1019
+ // A cut-down version of `rustc_errors::SubDiagnostic` that impls `Send`. It's
1020
+ // missing the following fields from `rustc_errors::SubDiagnostic`.
1021
+ // - `span`: it doesn't impl `Send`.
1022
+ pub struct Subdiagnostic {
1023
+ level : Level ,
1024
+ messages : Vec < ( DiagnosticMessage , Style ) > ,
1007
1025
}
1008
1026
1009
1027
#[ derive( PartialEq , Clone , Copy , Debug ) ]
@@ -1766,7 +1784,6 @@ fn spawn_work<'a, B: ExtraBackendMethods>(
1766
1784
enum SharedEmitterMessage {
1767
1785
Diagnostic ( Diagnostic ) ,
1768
1786
InlineAsmError ( u32 , String , Level , Option < ( String , Vec < InnerSpan > ) > ) ,
1769
- AbortIfErrors ,
1770
1787
Fatal ( String ) ,
1771
1788
}
1772
1789
@@ -1812,24 +1829,29 @@ impl Translate for SharedEmitter {
1812
1829
}
1813
1830
1814
1831
impl Emitter for SharedEmitter {
1815
- fn emit_diagnostic ( & mut self , diag : rustc_errors:: Diagnostic ) {
1816
- let args: FxIndexMap < DiagnosticArgName , DiagnosticArgValue > =
1817
- diag. args ( ) . map ( |( name, arg) | ( name. clone ( ) , arg. clone ( ) ) ) . collect ( ) ;
1818
- drop ( self . sender . send ( SharedEmitterMessage :: Diagnostic ( Diagnostic {
1819
- msgs : diag. messages . clone ( ) ,
1820
- args : args. clone ( ) ,
1821
- code : diag. code ,
1822
- lvl : diag. level ( ) ,
1823
- } ) ) ) ;
1824
- for child in & diag. children {
1825
- drop ( self . sender . send ( SharedEmitterMessage :: Diagnostic ( Diagnostic {
1826
- msgs : child. messages . clone ( ) ,
1827
- args : args. clone ( ) ,
1828
- code : None ,
1829
- lvl : child. level ,
1830
- } ) ) ) ;
1831
- }
1832
- drop ( self . sender . send ( SharedEmitterMessage :: AbortIfErrors ) ) ;
1832
+ fn emit_diagnostic ( & mut self , mut diag : rustc_errors:: Diagnostic ) {
1833
+ // Check that we aren't missing anything interesting when converting to
1834
+ // the cut-down local `Diagnostic`.
1835
+ assert_eq ! ( diag. span, MultiSpan :: new( ) ) ;
1836
+ assert_eq ! ( diag. suggestions, Ok ( vec![ ] ) ) ;
1837
+ assert_eq ! ( diag. sort_span, rustc_span:: DUMMY_SP ) ;
1838
+ assert_eq ! ( diag. is_lint, None ) ;
1839
+ // No sensible check for `diag.emitted_at`.
1840
+
1841
+ let args = mem:: replace ( & mut diag. args , DiagnosticArgMap :: default ( ) ) ;
1842
+ drop (
1843
+ self . sender . send ( SharedEmitterMessage :: Diagnostic ( Diagnostic {
1844
+ level : diag. level ( ) ,
1845
+ messages : diag. messages ,
1846
+ code : diag. code ,
1847
+ children : diag
1848
+ . children
1849
+ . into_iter ( )
1850
+ . map ( |child| Subdiagnostic { level : child. level , messages : child. messages } )
1851
+ . collect ( ) ,
1852
+ args,
1853
+ } ) ) ,
1854
+ ) ;
1833
1855
}
1834
1856
1835
1857
fn source_map ( & self ) -> Option < & Lrc < SourceMap > > {
@@ -1854,11 +1876,24 @@ impl SharedEmitterMain {
1854
1876
1855
1877
match message {
1856
1878
Ok ( SharedEmitterMessage :: Diagnostic ( diag) ) => {
1879
+ // The diagnostic has been received on the main thread.
1880
+ // Convert it back to a full `Diagnostic` and emit.
1857
1881
let dcx = sess. dcx ( ) ;
1858
- let mut d = rustc_errors:: Diagnostic :: new_with_messages ( diag. lvl , diag. msgs ) ;
1882
+ let mut d =
1883
+ rustc_errors:: Diagnostic :: new_with_messages ( diag. level , diag. messages ) ;
1859
1884
d. code = diag. code ; // may be `None`, that's ok
1860
- d. replace_args ( diag. args ) ;
1885
+ d. children = diag
1886
+ . children
1887
+ . into_iter ( )
1888
+ . map ( |sub| rustc_errors:: SubDiagnostic {
1889
+ level : sub. level ,
1890
+ messages : sub. messages ,
1891
+ span : MultiSpan :: new ( ) ,
1892
+ } )
1893
+ . collect ( ) ;
1894
+ d. args = diag. args ;
1861
1895
dcx. emit_diagnostic ( d) ;
1896
+ sess. dcx ( ) . abort_if_errors ( ) ;
1862
1897
}
1863
1898
Ok ( SharedEmitterMessage :: InlineAsmError ( cookie, msg, level, source) ) => {
1864
1899
assert ! ( matches!( level, Level :: Error | Level :: Warning | Level :: Note ) ) ;
@@ -1891,9 +1926,6 @@ impl SharedEmitterMain {
1891
1926
1892
1927
err. emit ( ) ;
1893
1928
}
1894
- Ok ( SharedEmitterMessage :: AbortIfErrors ) => {
1895
- sess. dcx ( ) . abort_if_errors ( ) ;
1896
- }
1897
1929
Ok ( SharedEmitterMessage :: Fatal ( msg) ) => {
1898
1930
sess. dcx ( ) . fatal ( msg) ;
1899
1931
}
0 commit comments