Adapt rustfmt to the overhaul filename handling

This commit is contained in:
Urgau 2025-12-06 13:02:03 +01:00
parent d5b1a0cfc6
commit cc4cdea3fa
3 changed files with 50 additions and 52 deletions

View file

@ -28,7 +28,15 @@ pub enum FileName {
impl From<rustc_span::FileName> 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!(),
}

View file

@ -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<Arc<String>> {
pub(crate) fn get_original_snippet(&self, filename: &FileName) -> Option<Arc<String>> {
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),

View file

@ -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`