@@ -5,18 +5,18 @@ use crate::Level;
5
5
use crate :: Substitution ;
6
6
use crate :: SubstitutionPart ;
7
7
use crate :: SuggestionStyle ;
8
- use rustc_span:: { MultiSpan , Span , DUMMY_SP } ;
8
+ use rustc_span:: { Multi , SpanLike } ;
9
9
use std:: fmt;
10
10
11
11
#[ must_use]
12
12
#[ derive( Clone , Debug , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
13
- pub struct Diagnostic {
13
+ pub struct Diagnostic < Span > {
14
14
pub level : Level ,
15
15
pub message : Vec < ( String , Style ) > ,
16
16
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 > > ,
20
20
21
21
/// This is not used for highlighting or rendering any error message. Rather, it can be used
22
22
/// 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 {
32
32
33
33
/// For example a note attached to an error.
34
34
#[ derive( Clone , Debug , PartialEq , Hash , RustcEncodable , RustcDecodable ) ]
35
- pub struct SubDiagnostic {
35
+ pub struct SubDiagnostic < Span > {
36
36
pub level : Level ,
37
37
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 > > ,
40
40
}
41
41
42
42
#[ derive( Debug , PartialEq , Eq ) ]
@@ -86,7 +86,7 @@ impl StringPart {
86
86
}
87
87
}
88
88
89
- impl Diagnostic {
89
+ impl < Span : SpanLike > Diagnostic < Span > {
90
90
pub fn new ( level : Level , message : & str ) -> Self {
91
91
Diagnostic :: new_with_code ( level, None , message)
92
92
}
@@ -96,10 +96,10 @@ impl Diagnostic {
96
96
level,
97
97
message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
98
98
code,
99
- span : MultiSpan :: new ( ) ,
99
+ span : Multi :: new ( ) ,
100
100
children : vec ! [ ] ,
101
101
suggestions : vec ! [ ] ,
102
- sort_span : DUMMY_SP ,
102
+ sort_span : Span :: default ( ) ,
103
103
}
104
104
}
105
105
@@ -135,17 +135,18 @@ impl Diagnostic {
135
135
/// all, and you just supplied a `Span` to create the diagnostic,
136
136
/// then the snippet will just include that `Span`, which is
137
137
/// 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 ( ) ) ;
140
140
self
141
141
}
142
142
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 ( ) ;
144
145
let before = self . span . clone ( ) ;
145
- self . set_span ( after) ;
146
+ self . set_span ( after. clone ( ) ) ;
146
147
for span_label in before. span_labels ( ) {
147
148
if let Some ( label) = span_label. label {
148
- self . span_label ( after, label) ;
149
+ self . span_label ( after. clone ( ) , label) ;
149
150
}
150
151
}
151
152
self
@@ -230,54 +231,54 @@ impl Diagnostic {
230
231
}
231
232
232
233
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 ) ;
234
235
self
235
236
}
236
237
237
238
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 ) ;
239
240
self
240
241
}
241
242
242
243
/// 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 {
244
245
self . sub ( Level :: Note , msg, sp. into ( ) , None ) ;
245
246
self
246
247
}
247
248
248
249
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 ) ;
250
251
self
251
252
}
252
253
253
254
/// 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 {
255
256
self . sub ( Level :: Warning , msg, sp. into ( ) , None ) ;
256
257
self
257
258
}
258
259
259
260
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 ) ;
261
262
self
262
263
}
263
264
264
265
/// 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 {
266
267
self . sub ( Level :: Help , msg, sp. into ( ) , None ) ;
267
268
self
268
269
}
269
270
270
271
pub fn multipart_suggestion (
271
272
& mut self ,
272
273
msg : & str ,
273
- suggestion : Vec < ( Span , String ) > ,
274
+ suggestion : impl IntoIterator < Item = ( impl Into < Span > , String ) > ,
274
275
applicability : Applicability ,
275
276
) -> & mut Self {
276
277
self . suggestions . push ( CodeSuggestion {
277
278
substitutions : vec ! [ Substitution {
278
279
parts: suggestion
279
280
. into_iter( )
280
- . map( |( span, snippet) | SubstitutionPart { snippet, span } )
281
+ . map( |( span, snippet) | SubstitutionPart { snippet, span: span . into ( ) } )
281
282
. collect( ) ,
282
283
} ] ,
283
284
msg : msg. to_owned ( ) ,
@@ -296,14 +297,14 @@ impl Diagnostic {
296
297
pub fn tool_only_multipart_suggestion (
297
298
& mut self ,
298
299
msg : & str ,
299
- suggestion : Vec < ( Span , String ) > ,
300
+ suggestion : impl IntoIterator < Item = ( impl Into < Span > , String ) > ,
300
301
applicability : Applicability ,
301
302
) -> & mut Self {
302
303
self . suggestions . push ( CodeSuggestion {
303
304
substitutions : vec ! [ Substitution {
304
305
parts: suggestion
305
306
. into_iter( )
306
- . map( |( span, snippet) | SubstitutionPart { snippet, span } )
307
+ . map( |( span, snippet) | SubstitutionPart { snippet, span: span . into ( ) } )
307
308
. collect( ) ,
308
309
} ] ,
309
310
msg : msg. to_owned ( ) ,
@@ -393,7 +394,9 @@ impl Diagnostic {
393
394
) -> & mut Self {
394
395
self . suggestions . push ( CodeSuggestion {
395
396
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
+ } )
397
400
. collect ( ) ,
398
401
msg : msg. to_owned ( ) ,
399
402
style : SuggestionStyle :: ShowCode ,
@@ -467,7 +470,7 @@ impl Diagnostic {
467
470
self
468
471
}
469
472
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 {
471
474
self . span = sp. into ( ) ;
472
475
if let Some ( span) = self . span . primary_span ( ) {
473
476
self . sort_span = span;
@@ -504,7 +507,7 @@ impl Diagnostic {
504
507
505
508
/// Used by a lint. Copies over all details *but* the "main
506
509
/// message".
507
- pub fn copy_details_not_message ( & mut self , from : & Diagnostic ) {
510
+ pub fn copy_details_not_message ( & mut self , from : & Diagnostic < Span > ) {
508
511
self . span = from. span . clone ( ) ;
509
512
self . code = from. code . clone ( ) ;
510
513
self . children . extend ( from. children . iter ( ) . cloned ( ) )
@@ -516,13 +519,13 @@ impl Diagnostic {
516
519
& mut self ,
517
520
level : Level ,
518
521
message : & str ,
519
- span : MultiSpan ,
520
- render_span : Option < MultiSpan > ,
522
+ span : impl Into < Multi < Span > > ,
523
+ render_span : Option < Multi < Span > > ,
521
524
) {
522
525
let sub = SubDiagnostic {
523
526
level,
524
527
message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
525
- span,
528
+ span : span . into ( ) ,
526
529
render_span,
527
530
} ;
528
531
self . children . push ( sub) ;
@@ -534,15 +537,15 @@ impl Diagnostic {
534
537
& mut self ,
535
538
level : Level ,
536
539
message : Vec < ( String , Style ) > ,
537
- span : MultiSpan ,
538
- render_span : Option < MultiSpan > ,
540
+ span : Multi < Span > ,
541
+ render_span : Option < Multi < Span > > ,
539
542
) {
540
543
let sub = SubDiagnostic { level, message, span, render_span } ;
541
544
self . children . push ( sub) ;
542
545
}
543
546
}
544
547
545
- impl SubDiagnostic {
548
+ impl < Span > SubDiagnostic < Span > {
546
549
pub fn message ( & self ) -> String {
547
550
self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( )
548
551
}
@@ -551,3 +554,28 @@ impl SubDiagnostic {
551
554
& self . message
552
555
}
553
556
}
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