From cc4cdea3fac026bae72849032bbc5d7770b009ac Mon Sep 17 00:00:00 2001 From: Urgau Date: Sat, 6 Dec 2025 13:02:03 +0100 Subject: [PATCH] Adapt rustfmt to the overhaul filename handling --- src/tools/rustfmt/src/config/file_lines.rs | 10 ++- src/tools/rustfmt/src/parse/session.rs | 80 +++++++++++----------- src/tools/rustfmt/src/source_file.rs | 12 ---- 3 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/tools/rustfmt/src/config/file_lines.rs b/src/tools/rustfmt/src/config/file_lines.rs index 2f2a6c8d5520..f9ae0d16e298 100644 --- a/src/tools/rustfmt/src/config/file_lines.rs +++ b/src/tools/rustfmt/src/config/file_lines.rs @@ -28,7 +28,15 @@ pub enum FileName { impl From for FileName { fn from(name: rustc_span::FileName) -> FileName { match name { - rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(p)) => FileName::Real(p), + rustc_span::FileName::Real(real) => { + if let Some(p) = real.into_local_path() { + FileName::Real(p) + } else { + // rustfmt does not remap filenames; the local path should always + // remain accessible. + unreachable!() + } + } rustc_span::FileName::Custom(ref f) if f == "stdin" => FileName::Stdin, _ => unreachable!(), } diff --git a/src/tools/rustfmt/src/parse/session.rs b/src/tools/rustfmt/src/parse/session.rs index 10e2809e58bf..2fd8bfdaf3e1 100644 --- a/src/tools/rustfmt/src/parse/session.rs +++ b/src/tools/rustfmt/src/parse/session.rs @@ -58,19 +58,19 @@ impl Emitter for SilentOnIgnoredFilesEmitter { } if let Some(primary_span) = &diag.span.primary_span() { let file_name = self.source_map.span_to_filename(*primary_span); - if let rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(ref path)) = - file_name - { - if self - .ignore_path_set - .is_match(&FileName::Real(path.to_path_buf())) - { - if !self.has_non_ignorable_parser_errors { - self.can_reset.store(true, Ordering::Release); + if let rustc_span::FileName::Real(real) = file_name { + if let Some(path) = real.local_path() { + if self + .ignore_path_set + .is_match(&FileName::Real(path.to_path_buf())) + { + if !self.has_non_ignorable_parser_errors { + self.can_reset.store(true, Ordering::Release); + } + return; } - return; } - }; + } } self.handle_non_ignoreable_error(diag, registry); } @@ -181,7 +181,10 @@ impl ParseSess { self.raw_psess .source_map() .get_source_file(&rustc_span::FileName::Real( - rustc_span::RealFileName::LocalPath(path.to_path_buf()), + self.raw_psess + .source_map() + .path_mapping() + .to_real_filename(self.raw_psess.source_map().working_dir(), path), )) .is_some() } @@ -246,10 +249,20 @@ impl ParseSess { ) } - pub(crate) fn get_original_snippet(&self, file_name: &FileName) -> Option> { + pub(crate) fn get_original_snippet(&self, filename: &FileName) -> Option> { + let rustc_filename = match filename { + FileName::Real(path) => rustc_span::FileName::Real( + self.raw_psess + .source_map() + .path_mapping() + .to_real_filename(self.raw_psess.source_map().working_dir(), path), + ), + FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()), + }; + self.raw_psess .source_map() - .get_source_file(&file_name.into()) + .get_source_file(&rustc_filename) .and_then(|source_file| source_file.src.clone()) } } @@ -313,7 +326,7 @@ mod tests { use crate::config::IgnoreList; use crate::utils::mk_sp; use rustc_errors::MultiSpan; - use rustc_span::{FileName as SourceMapFileName, RealFileName}; + use rustc_span::FileName as SourceMapFileName; use std::path::PathBuf; use std::sync::atomic::AtomicU32; @@ -372,6 +385,13 @@ mod tests { .ignore() } + fn filename(sm: &SourceMap, path: &str) -> SourceMapFileName { + SourceMapFileName::Real( + sm.path_mapping() + .to_real_filename(sm.working_dir(), PathBuf::from(path)), + ) + } + #[test] fn handles_fatal_parse_error_in_ignored_file() { let num_emitted_errors = Arc::new(AtomicU32::new(0)); @@ -380,10 +400,7 @@ mod tests { let source_map = Arc::new(SourceMap::new(FilePathMapping::empty())); let source = String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))), - source, - ); + source_map.new_source_file(filename(&source_map, "foo.rs"), source); let registry = Registry::new(&[]); let mut emitter = build_emitter( Arc::clone(&num_emitted_errors), @@ -406,10 +423,7 @@ mod tests { let ignore_list = get_ignore_list(r#"ignore = ["foo.rs"]"#); let source_map = Arc::new(SourceMap::new(FilePathMapping::empty())); let source = String::from(r#"pub fn bar() { 1x; }"#); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))), - source, - ); + source_map.new_source_file(filename(&source_map, "foo.rs"), source); let registry = Registry::new(&[]); let mut emitter = build_emitter( Arc::clone(&num_emitted_errors), @@ -431,10 +445,7 @@ mod tests { let can_reset_errors = Arc::new(AtomicBool::new(false)); let source_map = Arc::new(SourceMap::new(FilePathMapping::empty())); let source = String::from(r#"pub fn bar() { 1x; }"#); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))), - source, - ); + source_map.new_source_file(filename(&source_map, "foo.rs"), source); let registry = Registry::new(&[]); let mut emitter = build_emitter( Arc::clone(&num_emitted_errors), @@ -460,18 +471,9 @@ mod tests { let foo_source = String::from(r#"pub fn foo() { 1x; }"#); let fatal_source = String::from(r#"extern "system" fn jni_symbol!( funcName ) ( ... ) -> {} "#); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("bar.rs"))), - bar_source, - ); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("foo.rs"))), - foo_source, - ); - source_map.new_source_file( - SourceMapFileName::Real(RealFileName::LocalPath(PathBuf::from("fatal.rs"))), - fatal_source, - ); + source_map.new_source_file(filename(&source_map, "bar.rs"), bar_source); + source_map.new_source_file(filename(&source_map, "foo.rs"), foo_source); + source_map.new_source_file(filename(&source_map, "fatal.rs"), fatal_source); let registry = Registry::new(&[]); let mut emitter = build_emitter( Arc::clone(&num_emitted_errors), diff --git a/src/tools/rustfmt/src/source_file.rs b/src/tools/rustfmt/src/source_file.rs index e942058a0a83..b0ec24f3db66 100644 --- a/src/tools/rustfmt/src/source_file.rs +++ b/src/tools/rustfmt/src/source_file.rs @@ -65,18 +65,6 @@ where } } - #[allow(non_local_definitions)] - impl From<&FileName> for rustc_span::FileName { - fn from(filename: &FileName) -> rustc_span::FileName { - match filename { - FileName::Real(path) => { - rustc_span::FileName::Real(rustc_span::RealFileName::LocalPath(path.to_owned())) - } - FileName::Stdin => rustc_span::FileName::Custom("stdin".to_owned()), - } - } - } - // SourceFile's in the SourceMap will always have Unix-style line endings // See: https://github.com/rust-lang/rustfmt/issues/3850 // So if the user has explicitly overridden the rustfmt `newline_style`