@@ -5,18 +5,18 @@ use crate::Level;
55use crate :: Substitution ;
66use crate :: SubstitutionPart ;
77use crate :: SuggestionStyle ;
8- use rustc_span:: { MultiSpan , Span , DUMMY_SP } ;
8+ use rustc_span:: { Multi , SpanLike } ;
99use std:: fmt;
1010
1111#[ must_use]
1212#[ derive( Clone , Debug , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
13- pub struct Diagnostic {
13+ pub struct Diagnostic < Span > {
1414 pub level : Level ,
1515 pub message : Vec < ( String , Style ) > ,
1616 pub code : Option < DiagnosticId > ,
17- pub span : MultiSpan ,
18- pub children : Vec < SubDiagnostic > ,
19- pub suggestions : Vec < CodeSuggestion > ,
17+ pub span : Multi < Span > ,
18+ pub children : Vec < SubDiagnostic < Span > > ,
19+ pub suggestions : Vec < CodeSuggestion < Span > > ,
2020
2121 /// This is not used for highlighting or rendering any error message. Rather, it can be used
2222 /// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
@@ -32,11 +32,11 @@ pub enum DiagnosticId {
3232
3333/// For example a note attached to an error.
3434#[ derive( Clone , Debug , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
35- pub struct SubDiagnostic {
35+ pub struct SubDiagnostic < Span > {
3636 pub level : Level ,
3737 pub message : Vec < ( String , Style ) > ,
38- pub span : MultiSpan ,
39- pub render_span : Option < MultiSpan > ,
38+ pub span : Multi < Span > ,
39+ pub render_span : Option < Multi < Span > > ,
4040}
4141
4242#[ derive( Debug , PartialEq , Eq ) ]
@@ -86,7 +86,7 @@ impl StringPart {
8686 }
8787}
8888
89- impl Diagnostic {
89+ impl < Span : SpanLike > Diagnostic < Span > {
9090 pub fn new ( level : Level , message : & str ) -> Self {
9191 Diagnostic :: new_with_code ( level, None , message)
9292 }
@@ -96,10 +96,10 @@ impl Diagnostic {
9696 level,
9797 message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
9898 code,
99- span : MultiSpan :: new ( ) ,
99+ span : Multi :: new ( ) ,
100100 children : vec ! [ ] ,
101101 suggestions : vec ! [ ] ,
102- sort_span : DUMMY_SP ,
102+ sort_span : Span :: default ( ) ,
103103 }
104104 }
105105
@@ -135,17 +135,18 @@ impl Diagnostic {
135135 /// all, and you just supplied a `Span` to create the diagnostic,
136136 /// then the snippet will just include that `Span`, which is
137137 /// called the primary span.
138- pub fn span_label < T : Into < String > > ( & mut self , span : Span , label : T ) -> & mut Self {
139- self . span . push_span_label ( span, label. into ( ) ) ;
138+ pub fn span_label < T : Into < String > > ( & mut self , span : impl Into < Span > , label : T ) -> & mut Self {
139+ self . span . push_span_label ( span. into ( ) , label. into ( ) ) ;
140140 self
141141 }
142142
143- pub fn replace_span_with ( & mut self , after : Span ) -> & mut Self {
143+ pub fn replace_span_with ( & mut self , after : impl Into < Span > ) -> & mut Self {
144+ let after = after. into ( ) ;
144145 let before = self . span . clone ( ) ;
145- self . set_span ( after) ;
146+ self . set_span ( after. clone ( ) ) ;
146147 for span_label in before. span_labels ( ) {
147148 if let Some ( label) = span_label. label {
148- self . span_label ( after, label) ;
149+ self . span_label ( after. clone ( ) , label) ;
149150 }
150151 }
151152 self
@@ -230,54 +231,54 @@ impl Diagnostic {
230231 }
231232
232233 pub fn note ( & mut self , msg : & str ) -> & mut Self {
233- self . sub ( Level :: Note , msg, MultiSpan :: new ( ) , None ) ;
234+ self . sub ( Level :: Note , msg, Multi :: new ( ) , None ) ;
234235 self
235236 }
236237
237238 pub fn highlighted_note ( & mut self , msg : Vec < ( String , Style ) > ) -> & mut Self {
238- self . sub_with_highlights ( Level :: Note , msg, MultiSpan :: new ( ) , None ) ;
239+ self . sub_with_highlights ( Level :: Note , msg, Multi :: new ( ) , None ) ;
239240 self
240241 }
241242
242243 /// Prints the span with a note above it.
243- pub fn span_note < S : Into < MultiSpan > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
244+ pub fn span_note < S : Into < Multi < Span > > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
244245 self . sub ( Level :: Note , msg, sp. into ( ) , None ) ;
245246 self
246247 }
247248
248249 pub fn warn ( & mut self , msg : & str ) -> & mut Self {
249- self . sub ( Level :: Warning , msg, MultiSpan :: new ( ) , None ) ;
250+ self . sub ( Level :: Warning , msg, Multi :: new ( ) , None ) ;
250251 self
251252 }
252253
253254 /// Prints the span with a warn above it.
254- pub fn span_warn < S : Into < MultiSpan > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
255+ pub fn span_warn < S : Into < Multi < Span > > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
255256 self . sub ( Level :: Warning , msg, sp. into ( ) , None ) ;
256257 self
257258 }
258259
259260 pub fn help ( & mut self , msg : & str ) -> & mut Self {
260- self . sub ( Level :: Help , msg, MultiSpan :: new ( ) , None ) ;
261+ self . sub ( Level :: Help , msg, Multi :: new ( ) , None ) ;
261262 self
262263 }
263264
264265 /// Prints the span with some help above it.
265- pub fn span_help < S : Into < MultiSpan > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
266+ pub fn span_help < S : Into < Multi < Span > > > ( & mut self , sp : S , msg : & str ) -> & mut Self {
266267 self . sub ( Level :: Help , msg, sp. into ( ) , None ) ;
267268 self
268269 }
269270
270271 pub fn multipart_suggestion (
271272 & mut self ,
272273 msg : & str ,
273- suggestion : Vec < ( Span , String ) > ,
274+ suggestion : impl IntoIterator < Item = ( impl Into < Span > , String ) > ,
274275 applicability : Applicability ,
275276 ) -> & mut Self {
276277 self . suggestions . push ( CodeSuggestion {
277278 substitutions : vec ! [ Substitution {
278279 parts: suggestion
279280 . into_iter( )
280- . map( |( span, snippet) | SubstitutionPart { snippet, span } )
281+ . map( |( span, snippet) | SubstitutionPart { snippet, span: span . into ( ) } )
281282 . collect( ) ,
282283 } ] ,
283284 msg : msg. to_owned ( ) ,
@@ -296,14 +297,14 @@ impl Diagnostic {
296297 pub fn tool_only_multipart_suggestion (
297298 & mut self ,
298299 msg : & str ,
299- suggestion : Vec < ( Span , String ) > ,
300+ suggestion : impl IntoIterator < Item = ( impl Into < Span > , String ) > ,
300301 applicability : Applicability ,
301302 ) -> & mut Self {
302303 self . suggestions . push ( CodeSuggestion {
303304 substitutions : vec ! [ Substitution {
304305 parts: suggestion
305306 . into_iter( )
306- . map( |( span, snippet) | SubstitutionPart { snippet, span } )
307+ . map( |( span, snippet) | SubstitutionPart { snippet, span: span . into ( ) } )
307308 . collect( ) ,
308309 } ] ,
309310 msg : msg. to_owned ( ) ,
@@ -393,7 +394,9 @@ impl Diagnostic {
393394 ) -> & mut Self {
394395 self . suggestions . push ( CodeSuggestion {
395396 substitutions : suggestions
396- . map ( |snippet| Substitution { parts : vec ! [ SubstitutionPart { snippet, span: sp } ] } )
397+ . map ( |snippet| Substitution {
398+ parts : vec ! [ SubstitutionPart { snippet, span: sp. clone( ) } ] ,
399+ } )
397400 . collect ( ) ,
398401 msg : msg. to_owned ( ) ,
399402 style : SuggestionStyle :: ShowCode ,
@@ -467,7 +470,7 @@ impl Diagnostic {
467470 self
468471 }
469472
470- pub fn set_span < S : Into < MultiSpan > > ( & mut self , sp : S ) -> & mut Self {
473+ pub fn set_span < S : Into < Multi < Span > > > ( & mut self , sp : S ) -> & mut Self {
471474 self . span = sp. into ( ) ;
472475 if let Some ( span) = self . span . primary_span ( ) {
473476 self . sort_span = span;
@@ -504,7 +507,7 @@ impl Diagnostic {
504507
505508 /// Used by a lint. Copies over all details *but* the "main
506509 /// message".
507- pub fn copy_details_not_message ( & mut self , from : & Diagnostic ) {
510+ pub fn copy_details_not_message ( & mut self , from : & Diagnostic < Span > ) {
508511 self . span = from. span . clone ( ) ;
509512 self . code = from. code . clone ( ) ;
510513 self . children . extend ( from. children . iter ( ) . cloned ( ) )
@@ -516,13 +519,13 @@ impl Diagnostic {
516519 & mut self ,
517520 level : Level ,
518521 message : & str ,
519- span : MultiSpan ,
520- render_span : Option < MultiSpan > ,
522+ span : impl Into < Multi < Span > > ,
523+ render_span : Option < Multi < Span > > ,
521524 ) {
522525 let sub = SubDiagnostic {
523526 level,
524527 message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
525- span,
528+ span : span . into ( ) ,
526529 render_span,
527530 } ;
528531 self . children . push ( sub) ;
@@ -534,15 +537,15 @@ impl Diagnostic {
534537 & mut self ,
535538 level : Level ,
536539 message : Vec < ( String , Style ) > ,
537- span : MultiSpan ,
538- render_span : Option < MultiSpan > ,
540+ span : Multi < Span > ,
541+ render_span : Option < Multi < Span > > ,
539542 ) {
540543 let sub = SubDiagnostic { level, message, span, render_span } ;
541544 self . children . push ( sub) ;
542545 }
543546}
544547
545- impl SubDiagnostic {
548+ impl < Span > SubDiagnostic < Span > {
546549 pub fn message ( & self ) -> String {
547550 self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( )
548551 }
@@ -551,3 +554,28 @@ impl SubDiagnostic {
551554 & self . message
552555 }
553556}
557+
558+ impl < S : Clone > Diagnostic < S > {
559+ pub fn map_span < S2 > ( self , f : impl Fn ( S ) -> S2 ) -> Diagnostic < S2 > {
560+ Diagnostic {
561+ level : self . level ,
562+ message : self . message ,
563+ code : self . code ,
564+ span : self . span . map_span ( & f) ,
565+ children : self . children . into_iter ( ) . map ( |c| c. map_span ( & f) ) . collect ( ) ,
566+ suggestions : self . suggestions . into_iter ( ) . map ( |s| s. map_span ( & f) ) . collect ( ) ,
567+ sort_span : f ( self . sort_span ) ,
568+ }
569+ }
570+ }
571+
572+ impl < S : Clone > SubDiagnostic < S > {
573+ pub fn map_span < S2 > ( self , f : impl Fn ( S ) -> S2 ) -> SubDiagnostic < S2 > {
574+ SubDiagnostic {
575+ level : self . level ,
576+ message : self . message ,
577+ span : self . span . map_span ( & f) ,
578+ render_span : self . render_span . map ( |ms| ms. map_span ( f) ) ,
579+ }
580+ }
581+ }
0 commit comments