Skip to content

Commit 98c8e4a

Browse files
committed
Adapt rustfmt to the overhaul filename handling
1 parent c8dfbf5 commit 98c8e4a

File tree

3 files changed

+48
-52
lines changed

3 files changed

+48
-52
lines changed

src/tools/rustfmt/src/config/file_lines.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ pub enum FileName {
2828
impl From<rustc_span::FileName> for FileName {
2929
fn from(name: rustc_span::FileName) -> FileName {
3030
match name {
31-
rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(p)) => FileName::Real(p),
31+
rustc_span::FileName::Real(real) => {
32+
if let Some(p) = real.into_local_path() {
33+
FileName::Real(p)
34+
} else {
35+
unreachable!()
36+
}
37+
}
3238
rustc_span::FileName::Custom(ref f) if f == "stdin" => FileName::Stdin,
3339
_ => unreachable!(),
3440
}

src/tools/rustfmt/src/parse/session.rs

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -58,19 +58,19 @@ impl Emitter for SilentOnIgnoredFilesEmitter {
5858
}
5959
if let Some(primary_span) = &diag.span.primary_span() {
6060
let file_name = self.source_map.span_to_filename(*primary_span);
61-
if let rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(ref path)) =
62-
file_name
63-
{
64-
if self
65-
.ignore_path_set
66-
.is_match(&FileName::Real(path.to_path_buf()))
67-
{
68-
if !self.has_non_ignorable_parser_errors {
69-
self.can_reset.store(true, Ordering::Release);
61+
if let rustc_span::FileName::Real(real) = file_name {
62+
if let Some(path) = real.local_path() {
63+
if self
64+
.ignore_path_set
65+
.is_match(&FileName::Real(path.to_path_buf()))
66+
{
67+
if !self.has_non_ignorable_parser_errors {
68+
self.can_reset.store(true, Ordering::Release);
69+
}
70+
return;
7071
}
71-
return;
7272
}
73-
};
73+
}
7474
}
7575
self.handle_non_ignoreable_error(diag, registry);
7676
}
@@ -181,7 +181,10 @@ impl ParseSess {
181181
self.raw_psess
182182
.source_map()
183183
.get_source_file(&rustc_span::FileName::Real(
184-
rustc_span::RealFileName::LocalPath(path.to_path_buf()),
184+
self.raw_psess
185+
.source_map()
186+
.path_mapping()
187+
.to_real_filename(self.raw_psess.source_map().working_dir(), path),
185188
))
186189
.is_some()
187190
}
@@ -246,10 +249,20 @@ impl ParseSess {
246249
)
247250
}
248251

249-
pub(crate) fn get_original_snippet(&self, file_name: &FileName) -> Option<Arc<String>> {
252+
pub(crate) fn get_original_snippet(&self, filename: &FileName) -> Option<Arc<String>> {
253+
let rustc_filename = match filename {
254+
FileName::Real(path) => rustc_span::FileName::Real(
255+
self.raw_psess
256+
.source_map()
257+
.path_mapping()
258+
.to_real_filename(self.raw_psess.source_map().working_dir(), path),
259+
),
260+
FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
261+
};
262+
250263
self.raw_psess
251264
.source_map()
252-
.get_source_file(&file_name.into())
265+
.get_source_file(&rustc_filename)
253266
.and_then(|source_file| source_file.src.clone())
254267
}
255268
}
@@ -313,7 +326,7 @@ mod tests {
313326
use crate::config::IgnoreList;
314327
use crate::utils::mk_sp;
315328
use rustc_errors::MultiSpan;
316-
use rustc_span::{FileName as SourceMapFileName, RealFileName};
329+
use rustc_span::FileName as SourceMapFileName;
317330
use std::path::PathBuf;
318331
use std::sync::atomic::AtomicU32;
319332

@@ -372,6 +385,13 @@ mod tests {
372385
.ignore()
373386
}
374387

388+
fn filename(sm: &SourceMap, path: &str) -> SourceMapFileName {
389+
SourceMapFileName::Real(
390+
sm.path_mapping()
391+
.to_real_filename(sm.working_dir(), PathBuf::from(path)),
392+
)
393+
}
394+
375395
#[test]
376396
fn handles_fatal_parse_error_in_ignored_file() {
377397
let num_emitted_errors = Arc::new(AtomicU32::new(0));
@@ -380,10 +400,7 @@ mod tests {
380400
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
381401
let source =
382402
String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#);
383-
source_map.new_source_file(
384-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))),
385-
source,
386-
);
403+
source_map.new_source_file(filename(&source_map, "foo.rs"), source);
387404
let registry = Registry::new(&[]);
388405
let mut emitter = build_emitter(
389406
Arc::clone(&num_emitted_errors),
@@ -406,10 +423,7 @@ mod tests {
406423
let ignore_list = get_ignore_list(r#"ignore = ["foo.rs"]"#);
407424
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
408425
let source = String::from(r#"pub fn bar() { 1x; }"#);
409-
source_map.new_source_file(
410-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))),
411-
source,
412-
);
426+
source_map.new_source_file(filename(&source_map, "foo.rs"), source);
413427
let registry = Registry::new(&[]);
414428
let mut emitter = build_emitter(
415429
Arc::clone(&num_emitted_errors),
@@ -431,10 +445,7 @@ mod tests {
431445
let can_reset_errors = Arc::new(AtomicBool::new(false));
432446
let source_map = Arc::new(SourceMap::new(FilePathMapping::empty()));
433447
let source = String::from(r#"pub fn bar() { 1x; }"#);
434-
source_map.new_source_file(
435-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))),
436-
source,
437-
);
448+
source_map.new_source_file(filename(&source_map, "foo.rs"), source);
438449
let registry = Registry::new(&[]);
439450
let mut emitter = build_emitter(
440451
Arc::clone(&num_emitted_errors),
@@ -460,18 +471,9 @@ mod tests {
460471
let foo_source = String::from(r#"pub fn foo() { 1x; }"#);
461472
let fatal_source =
462473
String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#);
463-
source_map.new_source_file(
464-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("bar.rs"))),
465-
bar_source,
466-
);
467-
source_map.new_source_file(
468-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))),
469-
foo_source,
470-
);
471-
source_map.new_source_file(
472-
SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("fatal.rs"))),
473-
fatal_source,
474-
);
474+
source_map.new_source_file(filename(&source_map, "bar.rs"), bar_source);
475+
source_map.new_source_file(filename(&source_map, "foo.rs"), foo_source);
476+
source_map.new_source_file(filename(&source_map, "fatal.rs"), fatal_source);
475477
let registry = Registry::new(&[]);
476478
let mut emitter = build_emitter(
477479
Arc::clone(&num_emitted_errors),

src/tools/rustfmt/src/source_file.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,6 @@ where
6565
}
6666
}
6767

68-
#[allow(non_local_definitions)]
69-
impl From<&FileName> for rustc_span::FileName {
70-
fn from(filename: &FileName) -> rustc_span::FileName {
71-
match filename {
72-
FileName::Real(path) => {
73-
rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned()))
74-
}
75-
FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()),
76-
}
77-
}
78-
}
79-
8068
// SourceFile's in the SourceMap will always have Unix-style line endings
8169
// See: https://github.com/rust-lang/rustfmt/issues/3850
8270
// So if the user has explicitly overridden the rustfmt `newline_style`

0 commit comments

Comments
 (0)