@@ -202,6 +202,23 @@ impl RealFileName {
202
202
| RealFileName :: Remapped { local_path : _, virtual_name : p } => & p,
203
203
}
204
204
}
205
+
206
+ fn to_string_lossy ( & self , prefer_local : bool ) -> Cow < ' _ , str > {
207
+ use RealFileName :: * ;
208
+ if prefer_local {
209
+ match self {
210
+ LocalPath ( path)
211
+ | Remapped { local_path : None , virtual_name : path }
212
+ | Remapped { local_path : Some ( path) , virtual_name : _ } => path. to_string_lossy ( ) ,
213
+ }
214
+ } else {
215
+ match self {
216
+ LocalPath ( path) | Remapped { local_path : _, virtual_name : path } => {
217
+ path. to_string_lossy ( )
218
+ }
219
+ }
220
+ }
221
+ }
205
222
}
206
223
207
224
/// Differentiates between real files and common virtual files.
@@ -228,16 +245,24 @@ pub enum FileName {
228
245
InlineAsm ( u64 ) ,
229
246
}
230
247
231
- impl std:: fmt:: Display for FileName {
248
+ impl From < PathBuf > for FileName {
249
+ fn from ( p : PathBuf ) -> Self {
250
+ assert ! ( !p. to_string_lossy( ) . ends_with( '>' ) ) ;
251
+ FileName :: Real ( RealFileName :: LocalPath ( p) )
252
+ }
253
+ }
254
+
255
+ pub struct FileNameDisplay < ' a > {
256
+ inner : & ' a FileName ,
257
+ prefer_local : bool ,
258
+ }
259
+
260
+ impl fmt:: Display for FileNameDisplay < ' _ > {
232
261
fn fmt ( & self , fmt : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
233
262
use FileName :: * ;
234
- match * self {
235
- Real ( RealFileName :: Named ( ref path) ) => write ! ( fmt, "{}" , path. display( ) ) ,
236
- // FIXME: might be nice to display both components of Devirtualized.
237
- // But for now (to backport fix for issue #70924), best to not
238
- // perturb diagnostics so its obvious test suite still works.
239
- Real ( RealFileName :: Devirtualized { ref local_path, virtual_name : _ } ) => {
240
- write ! ( fmt, "{}" , local_path. display( ) )
263
+ match * self . inner {
264
+ Real ( ref name) => {
265
+ write ! ( fmt, "{}" , name. to_string_lossy( self . prefer_local) )
241
266
}
242
267
QuoteExpansion ( _) => write ! ( fmt, "<quote expansion>" ) ,
243
268
MacroExpansion ( _) => write ! ( fmt, "<macro expansion>" ) ,
@@ -252,10 +277,12 @@ impl std::fmt::Display for FileName {
252
277
}
253
278
}
254
279
255
- impl From < PathBuf > for FileName {
256
- fn from ( p : PathBuf ) -> Self {
257
- assert ! ( !p. to_string_lossy( ) . ends_with( '>' ) ) ;
258
- FileName :: Real ( RealFileName :: LocalPath ( p) )
280
+ impl FileNameDisplay < ' _ > {
281
+ pub fn to_string_lossy ( & self ) -> Cow < ' _ , str > {
282
+ match self . inner {
283
+ FileName :: Real ( ref inner) => inner. to_string_lossy ( self . prefer_local ) ,
284
+ _ => Cow :: from ( format ! ( "{}" , self ) ) ,
285
+ }
259
286
}
260
287
}
261
288
0 commit comments