@@ -11,9 +11,12 @@ use std::slice;
1111use std:: str;
1212use std:: vec;
1313
14- use codespan_reporting:: { Diagnostic , Label } ;
14+ use codespan_reporting:: diagnostic :: { Diagnostic , Label } ;
1515
16- use crate :: pos:: { BytePos , Span , Spanned } ;
16+ use crate :: {
17+ pos:: { BytePos , Spanned } ,
18+ source:: FileId ,
19+ } ;
1720
1821/// An error type which can represent multiple errors.
1922#[ derive( Clone , Debug , Eq , PartialEq , Hash ) ]
@@ -155,7 +158,7 @@ impl<T: fmt::Display + fmt::Debug + Any> StdError for Errors<T> {
155158/// Error type which contains information of which file and where in the file the error occurred
156159#[ derive( Clone , Debug ) ]
157160pub struct InFile < E > {
158- source : :: codespan :: CodeMap ,
161+ source : crate :: source :: CodeMap ,
159162 error : Errors < Spanned < E , BytePos > > ,
160163}
161164
@@ -186,7 +189,7 @@ where
186189impl < E : fmt:: Display > InFile < E > {
187190 /// Creates a new `InFile` error which states that the error occurred in `file` using the file
188191 /// contents in `source` to provide a context to the span.
189- pub fn new ( source : :: codespan :: CodeMap , error : Errors < Spanned < E , BytePos > > ) -> InFile < E > {
192+ pub fn new ( source : crate :: source :: CodeMap , error : Errors < Spanned < E , BytePos > > ) -> InFile < E > {
190193 let err = InFile { source, error } ;
191194 // Verify that the source name can be accessed
192195 debug_assert ! ( {
@@ -196,9 +199,9 @@ impl<E: fmt::Display> InFile<E> {
196199 err
197200 }
198201
199- pub fn source_name ( & self ) -> & :: codespan :: FileName {
202+ pub fn source_name ( & self ) -> & str {
200203 self . source
201- . find_file ( self . error [ 0 ] . span . start ( ) )
204+ . get ( self . error [ 0 ] . span . start ( ) )
202205 . unwrap_or_else ( || {
203206 panic ! (
204207 "Source file does not exist in associated code map. Error: {}" ,
@@ -221,27 +224,34 @@ impl<E: fmt::Display> InFile<E> {
221224 E : AsDiagnostic ,
222225 {
223226 let mut output = Vec :: new ( ) ;
224- self . emit (
225- & mut :: codespan_reporting :: termcolor :: NoColor :: new ( & mut output) ,
226- ) ?;
227+ self . emit ( & mut :: codespan_reporting :: term :: termcolor :: NoColor :: new (
228+ & mut output,
229+ ) ) ?;
227230 Ok ( String :: from_utf8 ( output) . unwrap ( ) )
228231 }
229232
230- pub fn emit < W > ( & self , writer : & mut W ) -> io:: Result < ( ) >
233+ pub fn emit (
234+ & self ,
235+ writer : & mut dyn :: codespan_reporting:: term:: termcolor:: WriteColor ,
236+ ) -> io:: Result < ( ) >
231237 where
232- W : ?Sized + :: codespan_reporting:: termcolor:: WriteColor ,
233238 E : AsDiagnostic ,
234239 {
235240 let iter = self
236241 . error
237242 . iter ( )
238- . map ( AsDiagnostic :: as_diagnostic)
243+ . map ( |error| error . as_diagnostic ( & self . source ) )
239244 . enumerate ( ) ;
240245 for ( i, diagnostic) in iter {
241246 if i != 0 {
242247 writeln ! ( writer) ?;
243248 }
244- :: codespan_reporting:: emit ( & mut * writer, & self . source , & diagnostic) ?;
249+ :: codespan_reporting:: term:: emit (
250+ & mut * writer,
251+ & Default :: default ( ) ,
252+ & self . source ,
253+ & diagnostic,
254+ ) ?;
245255 }
246256 Ok ( ( ) )
247257 }
@@ -251,10 +261,9 @@ impl<E: fmt::Display + AsDiagnostic> fmt::Display for InFile<E> {
251261 fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
252262 let mut buffer = Vec :: new ( ) ;
253263 {
254- let mut writer = :: codespan_reporting:: termcolor:: NoColor :: new ( & mut buffer) ;
264+ let mut writer = :: codespan_reporting:: term :: termcolor:: NoColor :: new ( & mut buffer) ;
255265
256- self . emit ( & mut writer)
257- . map_err ( |_| fmt:: Error ) ?;
266+ self . emit ( & mut writer) . map_err ( |_| fmt:: Error ) ?;
258267 }
259268
260269 write ! ( f, "{}" , str :: from_utf8( & buffer) . unwrap( ) )
@@ -295,16 +304,32 @@ impl<E, H> From<E> for Help<E, H> {
295304}
296305
297306pub trait AsDiagnostic {
298- fn as_diagnostic ( & self ) -> Diagnostic ;
307+ fn as_diagnostic ( & self , map : & crate :: source :: CodeMap ) -> Diagnostic < FileId > ;
299308}
300309
301310impl < E > AsDiagnostic for Spanned < E , BytePos >
302311where
303312 E : AsDiagnostic ,
304313{
305- fn as_diagnostic ( & self ) -> Diagnostic {
306- let mut diagnostic = self . value . as_diagnostic ( ) ;
307- diagnostic. labels . insert ( 0 , Label :: new_primary ( self . span ) ) ;
314+ fn as_diagnostic ( & self , map : & crate :: source:: CodeMap ) -> Diagnostic < FileId > {
315+ let mut diagnostic = self . value . as_diagnostic ( map) ;
316+ for label in & mut diagnostic. labels {
317+ if label. file_id == FileId :: default ( ) {
318+ label. file_id = self . span . start ( ) ;
319+ }
320+ if label. range == ( 0 ..0 ) {
321+ if let Some ( range) = self . span . to_range ( map) {
322+ label. range = range;
323+ }
324+ }
325+ }
326+ if diagnostic. labels . is_empty ( ) {
327+ if let Some ( range) = self . span . to_range ( map) {
328+ diagnostic
329+ . labels
330+ . push ( Label :: primary ( self . span . start ( ) , range) ) ;
331+ }
332+ }
308333 diagnostic
309334 }
310335}
@@ -314,20 +339,27 @@ where
314339 E : AsDiagnostic ,
315340 H : fmt:: Display ,
316341{
317- fn as_diagnostic ( & self ) -> Diagnostic {
318- let mut diagnostic = self . error . as_diagnostic ( ) ;
342+ fn as_diagnostic ( & self , map : & crate :: source :: CodeMap ) -> Diagnostic < FileId > {
343+ let mut diagnostic = self . error . as_diagnostic ( map ) ;
319344 if let Some ( ref help) = self . help {
320345 diagnostic. labels . push (
321- Label :: new_secondary ( Span :: new ( BytePos :: none ( ) , BytePos :: none ( ) ) )
322- . with_message ( help. to_string ( ) ) ,
346+ Label :: secondary (
347+ diagnostic
348+ . labels
349+ . last ( )
350+ . map ( |label| label. file_id )
351+ . unwrap_or_default ( ) ,
352+ 0 ..0 ,
353+ )
354+ . with_message ( help. to_string ( ) ) ,
323355 ) ;
324356 }
325357 diagnostic
326358 }
327359}
328360
329361impl AsDiagnostic for Box < dyn :: std:: error:: Error + Send + Sync > {
330- fn as_diagnostic ( & self ) -> Diagnostic {
331- Diagnostic :: new_error ( self . to_string ( ) )
362+ fn as_diagnostic ( & self , _map : & crate :: source :: CodeMap ) -> Diagnostic < FileId > {
363+ Diagnostic :: error ( ) . with_message ( self . to_string ( ) )
332364 }
333365}
0 commit comments