@@ -33,7 +33,10 @@ pub type DiagnosticArgName = Cow<'static, str>;
33
33
#[ derive( Clone , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
34
34
pub enum DiagnosticArgValue {
35
35
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 ) ,
37
40
StrListSepByAnd ( Vec < Cow < ' static , str > > ) ,
38
41
}
39
42
@@ -113,7 +116,7 @@ pub struct Diagnostic {
113
116
114
117
/// With `-Ztrack_diagnostics` enabled,
115
118
/// we print where in rustc this error was emitted.
116
- pub emitted_at : DiagnosticLocation ,
119
+ pub ( crate ) emitted_at : DiagnosticLocation ,
117
120
}
118
121
119
122
#[ derive( Clone , Debug , Encodable , Decodable ) ]
@@ -162,10 +165,10 @@ impl DiagnosticStyledString {
162
165
DiagnosticStyledString ( vec ! [ ] )
163
166
}
164
167
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 ) ) ;
166
169
}
167
170
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 ) ) ;
169
172
}
170
173
pub fn push < S : Into < String > > ( & mut self , t : S , highlight : bool ) {
171
174
if highlight {
@@ -175,35 +178,34 @@ impl DiagnosticStyledString {
175
178
}
176
179
}
177
180
pub fn normal < S : Into < String > > ( t : S ) -> DiagnosticStyledString {
178
- DiagnosticStyledString ( vec ! [ StringPart :: Normal ( t . into ( ) ) ] )
181
+ DiagnosticStyledString ( vec ! [ StringPart :: normal ( t ) ] )
179
182
}
180
183
181
184
pub fn highlighted < S : Into < String > > ( t : S ) -> DiagnosticStyledString {
182
- DiagnosticStyledString ( vec ! [ StringPart :: Highlighted ( t . into ( ) ) ] )
185
+ DiagnosticStyledString ( vec ! [ StringPart :: highlighted ( t ) ] )
183
186
}
184
187
185
188
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 > ( )
187
190
}
188
191
}
189
192
190
193
#[ derive( Debug , PartialEq , Eq ) ]
191
- pub enum StringPart {
192
- Normal ( String ) ,
193
- Highlighted ( String ) ,
194
+ pub struct StringPart {
195
+ content : String ,
196
+ style : Style ,
194
197
}
195
198
196
199
impl 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 }
201
206
}
202
207
}
203
208
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.
207
209
impl Diagnostic {
208
210
#[ track_caller]
209
211
pub fn new < M : Into < DiagnosticMessage > > ( level : Level , message : M ) -> Self {
@@ -389,19 +391,16 @@ impl Diagnostic {
389
391
} else {
390
392
( 0 , found_label. len ( ) - expected_label. len ( ) )
391
393
} ;
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}" ) ) ) ;
405
404
406
405
// For now, just attach these as notes.
407
406
self . highlighted_note ( msg) ;
@@ -410,9 +409,9 @@ impl Diagnostic {
410
409
411
410
pub fn note_trait_signature ( & mut self , name : Symbol , signature : String ) -> & mut Self {
412
411
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 ( "`" ) ,
416
415
] ) ;
417
416
self
418
417
}
@@ -424,10 +423,7 @@ impl Diagnostic {
424
423
self
425
424
}
426
425
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 {
431
427
self . sub_with_highlights ( Level :: Note , msg, MultiSpan :: new ( ) ) ;
432
428
self
433
429
}
@@ -496,7 +492,7 @@ impl Diagnostic {
496
492
}
497
493
498
494
/// 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 {
500
496
self . sub_with_highlights ( Level :: Help , msg, MultiSpan :: new ( ) ) ;
501
497
self
502
498
}
@@ -890,15 +886,6 @@ impl Diagnostic {
890
886
self
891
887
}
892
888
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
-
902
889
pub fn primary_message ( & mut self , msg : impl Into < DiagnosticMessage > ) -> & mut Self {
903
890
self . messages [ 0 ] = ( msg. into ( ) , Style :: NoStyle ) ;
904
891
self
@@ -913,7 +900,7 @@ impl Diagnostic {
913
900
914
901
pub fn arg (
915
902
& mut self ,
916
- name : impl Into < Cow < ' static , str > > ,
903
+ name : impl Into < DiagnosticArgName > ,
917
904
arg : impl IntoDiagnosticArg ,
918
905
) -> & mut Self {
919
906
self . args . insert ( name. into ( ) , arg. into_diagnostic_arg ( ) ) ;
@@ -924,10 +911,6 @@ impl Diagnostic {
924
911
self . args = args;
925
912
}
926
913
927
- pub fn messages ( & self ) -> & [ ( DiagnosticMessage , Style ) ] {
928
- & self . messages
929
- }
930
-
931
914
/// Helper function that takes a `SubdiagnosticMessage` and returns a `DiagnosticMessage` by
932
915
/// combining it with the primary message of the diagnostic (if translatable, otherwise it just
933
916
/// passes the user's string along).
@@ -958,15 +941,10 @@ impl Diagnostic {
958
941
959
942
/// Convenience function for internal use, clients should use one of the
960
943
/// 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 ) {
967
945
let messages = messages
968
946
. 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 ) )
970
948
. collect ( ) ;
971
949
let sub = SubDiagnostic { level, messages, span } ;
972
950
self . children . push ( sub) ;
0 commit comments