@@ -33,7 +33,10 @@ pub type DiagnosticArgName = Cow<'static, str>;
3333#[ derive( Clone , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
3434pub enum DiagnosticArgValue {
3535 Str ( Cow < ' static , str > ) ,
36- Number ( i128 ) ,
36+ // This gets converted to a `FluentNumber`, which is an `f64`. An `i32`
37+ // safely fits in an `f64`. Any integers bigger than that will be converted
38+ // to strings in `into_diagnostic_arg` and stored using the `Str` variant.
39+ Number ( i32 ) ,
3740 StrListSepByAnd ( Vec < Cow < ' static , str > > ) ,
3841}
3942
@@ -113,7 +116,7 @@ pub struct Diagnostic {
113116
114117 /// With `-Ztrack_diagnostics` enabled,
115118 /// we print where in rustc this error was emitted.
116- pub emitted_at : DiagnosticLocation ,
119+ pub ( crate ) emitted_at : DiagnosticLocation ,
117120}
118121
119122#[ derive( Clone , Debug , Encodable , Decodable ) ]
@@ -162,10 +165,10 @@ impl DiagnosticStyledString {
162165 DiagnosticStyledString ( vec ! [ ] )
163166 }
164167 pub fn push_normal < S : Into < String > > ( & mut self , t : S ) {
165- self . 0 . push ( StringPart :: Normal ( t . into ( ) ) ) ;
168+ self . 0 . push ( StringPart :: normal ( t ) ) ;
166169 }
167170 pub fn push_highlighted < S : Into < String > > ( & mut self , t : S ) {
168- self . 0 . push ( StringPart :: Highlighted ( t . into ( ) ) ) ;
171+ self . 0 . push ( StringPart :: highlighted ( t ) ) ;
169172 }
170173 pub fn push < S : Into < String > > ( & mut self , t : S , highlight : bool ) {
171174 if highlight {
@@ -175,35 +178,34 @@ impl DiagnosticStyledString {
175178 }
176179 }
177180 pub fn normal < S : Into < String > > ( t : S ) -> DiagnosticStyledString {
178- DiagnosticStyledString ( vec ! [ StringPart :: Normal ( t . into ( ) ) ] )
181+ DiagnosticStyledString ( vec ! [ StringPart :: normal ( t ) ] )
179182 }
180183
181184 pub fn highlighted < S : Into < String > > ( t : S ) -> DiagnosticStyledString {
182- DiagnosticStyledString ( vec ! [ StringPart :: Highlighted ( t . into ( ) ) ] )
185+ DiagnosticStyledString ( vec ! [ StringPart :: highlighted ( t ) ] )
183186 }
184187
185188 pub fn content ( & self ) -> String {
186- self . 0 . iter ( ) . map ( |x| x. content ( ) ) . collect :: < String > ( )
189+ self . 0 . iter ( ) . map ( |x| x. content . as_str ( ) ) . collect :: < String > ( )
187190 }
188191}
189192
190193#[ derive( Debug , PartialEq , Eq ) ]
191- pub enum StringPart {
192- Normal ( String ) ,
193- Highlighted ( String ) ,
194+ pub struct StringPart {
195+ content : String ,
196+ style : Style ,
194197}
195198
196199impl StringPart {
197- pub fn content ( & self ) -> & str {
198- match self {
199- & StringPart :: Normal ( ref s) | & StringPart :: Highlighted ( ref s) => s,
200- }
200+ pub fn normal < S : Into < String > > ( content : S ) -> StringPart {
201+ StringPart { content : content. into ( ) , style : Style :: NoStyle }
202+ }
203+
204+ pub fn highlighted < S : Into < String > > ( content : S ) -> StringPart {
205+ StringPart { content : content. into ( ) , style : Style :: Highlight }
201206 }
202207}
203208
204- // Note: most of these methods are setters that return `&mut Self`. The small
205- // number of simple getter functions all have `get_` prefixes to distinguish
206- // them from the setters.
207209impl Diagnostic {
208210 #[ track_caller]
209211 pub fn new < M : Into < DiagnosticMessage > > ( level : Level , message : M ) -> Self {
@@ -389,19 +391,16 @@ impl Diagnostic {
389391 } else {
390392 ( 0 , found_label. len ( ) - expected_label. len ( ) )
391393 } ;
392- let mut msg: Vec < _ > =
393- vec ! [ ( format!( "{}{} `" , " " . repeat( expected_padding) , expected_label) , Style :: NoStyle ) ] ;
394- msg. extend ( expected. 0 . iter ( ) . map ( |x| match * x {
395- StringPart :: Normal ( ref s) => ( s. to_owned ( ) , Style :: NoStyle ) ,
396- StringPart :: Highlighted ( ref s) => ( s. to_owned ( ) , Style :: Highlight ) ,
397- } ) ) ;
398- msg. push ( ( format ! ( "`{expected_extra}\n " ) , Style :: NoStyle ) ) ;
399- msg. push ( ( format ! ( "{}{} `" , " " . repeat( found_padding) , found_label) , Style :: NoStyle ) ) ;
400- msg. extend ( found. 0 . iter ( ) . map ( |x| match * x {
401- StringPart :: Normal ( ref s) => ( s. to_owned ( ) , Style :: NoStyle ) ,
402- StringPart :: Highlighted ( ref s) => ( s. to_owned ( ) , Style :: Highlight ) ,
403- } ) ) ;
404- msg. push ( ( format ! ( "`{found_extra}" ) , Style :: NoStyle ) ) ;
394+ let mut msg = vec ! [ StringPart :: normal( format!(
395+ "{}{} `" ,
396+ " " . repeat( expected_padding) ,
397+ expected_label
398+ ) ) ] ;
399+ msg. extend ( expected. 0 . into_iter ( ) ) ;
400+ msg. push ( StringPart :: normal ( format ! ( "`{expected_extra}\n " ) ) ) ;
401+ msg. push ( StringPart :: normal ( format ! ( "{}{} `" , " " . repeat( found_padding) , found_label) ) ) ;
402+ msg. extend ( found. 0 . into_iter ( ) ) ;
403+ msg. push ( StringPart :: normal ( format ! ( "`{found_extra}" ) ) ) ;
405404
406405 // For now, just attach these as notes.
407406 self . highlighted_note ( msg) ;
@@ -410,9 +409,9 @@ impl Diagnostic {
410409
411410 pub fn note_trait_signature ( & mut self , name : Symbol , signature : String ) -> & mut Self {
412411 self . highlighted_note ( vec ! [
413- ( format!( "`{name}` from trait: `" ) , Style :: NoStyle ) ,
414- ( signature , Style :: Highlight ) ,
415- ( "`" . to_string ( ) , Style :: NoStyle ) ,
412+ StringPart :: normal ( format!( "`{name}` from trait: `" ) ) ,
413+ StringPart :: highlighted ( signature ) ,
414+ StringPart :: normal ( "`" ) ,
416415 ] ) ;
417416 self
418417 }
@@ -424,10 +423,7 @@ impl Diagnostic {
424423 self
425424 }
426425
427- fn highlighted_note < M : Into < SubdiagnosticMessage > > (
428- & mut self ,
429- msg : Vec < ( M , Style ) > ,
430- ) -> & mut Self {
426+ fn highlighted_note ( & mut self , msg : Vec < StringPart > ) -> & mut Self {
431427 self . sub_with_highlights ( Level :: Note , msg, MultiSpan :: new ( ) ) ;
432428 self
433429 }
@@ -496,7 +492,7 @@ impl Diagnostic {
496492 }
497493
498494 /// Add a help message attached to this diagnostic with a customizable highlighted message.
499- pub fn highlighted_help ( & mut self , msg : Vec < ( String , Style ) > ) -> & mut Self {
495+ pub fn highlighted_help ( & mut self , msg : Vec < StringPart > ) -> & mut Self {
500496 self . sub_with_highlights ( Level :: Help , msg, MultiSpan :: new ( ) ) ;
501497 self
502498 }
@@ -890,15 +886,6 @@ impl Diagnostic {
890886 self
891887 }
892888
893- pub fn clear_code ( & mut self ) -> & mut Self {
894- self . code = None ;
895- self
896- }
897-
898- pub fn get_code ( & self ) -> Option < ErrCode > {
899- self . code
900- }
901-
902889 pub fn primary_message ( & mut self , msg : impl Into < DiagnosticMessage > ) -> & mut Self {
903890 self . messages [ 0 ] = ( msg. into ( ) , Style :: NoStyle ) ;
904891 self
@@ -913,7 +900,7 @@ impl Diagnostic {
913900
914901 pub fn arg (
915902 & mut self ,
916- name : impl Into < Cow < ' static , str > > ,
903+ name : impl Into < DiagnosticArgName > ,
917904 arg : impl IntoDiagnosticArg ,
918905 ) -> & mut Self {
919906 self . args . insert ( name. into ( ) , arg. into_diagnostic_arg ( ) ) ;
@@ -924,10 +911,6 @@ impl Diagnostic {
924911 self . args = args;
925912 }
926913
927- pub fn messages ( & self ) -> & [ ( DiagnosticMessage , Style ) ] {
928- & self . messages
929- }
930-
931914 /// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
932915 /// combining it with the primary message of the diagnostic (if translatable, otherwise it just
933916 /// passes the user's string along).
@@ -958,15 +941,10 @@ impl Diagnostic {
958941
959942 /// Convenience function for internal use, clients should use one of the
960943 /// public methods above.
961- fn sub_with_highlights < M : Into < SubdiagnosticMessage > > (
962- & mut self ,
963- level : Level ,
964- messages : Vec < ( M , Style ) > ,
965- span : MultiSpan ,
966- ) {
944+ fn sub_with_highlights ( & mut self , level : Level , messages : Vec < StringPart > , span : MultiSpan ) {
967945 let messages = messages
968946 . into_iter ( )
969- . map ( |m| ( self . subdiagnostic_message_to_diagnostic_message ( m. 0 ) , m. 1 ) )
947+ . map ( |m| ( self . subdiagnostic_message_to_diagnostic_message ( m. content ) , m. style ) )
970948 . collect ( ) ;
971949 let sub = SubDiagnostic { level, messages, span } ;
972950 self . children . push ( sub) ;
0 commit comments