From 230e91d2f6a6cb495bff8db9bbfc1530c97294d5 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 14 Dec 2025 13:29:59 +0100 Subject: [PATCH 1/4] Prevent double encoding when the filename wasn't remapped at all This is done by making the `local` part of `RealFileName` none. This works because `maybe_remapped` is equal to `local` when no remapping happened. --- compiler/rustc_span/src/lib.rs | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 49b2e0c1ff1a7..63c51164761d9 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -370,7 +370,11 @@ impl RealFileName { /// /// May not exists if the filename was imported from another crate. pub fn local_path(&self) -> Option<&Path> { - self.local.as_ref().map(|lp| lp.name.as_ref()) + if self.was_not_remapped() { + Some(&self.maybe_remapped.name) + } else { + self.local.as_ref().map(|lp| lp.name.as_ref()) + } } /// Returns the path suitable for reading from the file system on the local host, @@ -378,7 +382,11 @@ impl RealFileName { /// /// May not exists if the filename was imported from another crate. pub fn into_local_path(self) -> Option { - self.local.map(|lp| lp.name) + if self.was_not_remapped() { + Some(self.maybe_remapped.name) + } else { + self.local.map(|lp| lp.name) + } } /// Returns whenever the filename was remapped. @@ -386,6 +394,18 @@ impl RealFileName { !self.scopes.is_empty() } + /// Returns whenever the filename was fully remapped. + #[inline] + fn was_fully_remapped(&self) -> bool { + self.scopes.is_all() + } + + /// Returns whenever the filename was not remapped. + #[inline] + fn was_not_remapped(&self) -> bool { + self.scopes.is_empty() + } + /// Returns an empty `RealFileName` /// /// Useful as the working directory input to `SourceMap::to_real_filename`. @@ -420,9 +440,14 @@ impl RealFileName { /// Update the filename for encoding in the crate metadata. /// /// Currently it's about removing the local part when the filename - /// is fully remapped. + /// is either fully remapped or not remapped at all. + #[inline] pub fn update_for_crate_metadata(&mut self) { - if self.scopes.is_all() { + if self.was_fully_remapped() || self.was_not_remapped() { + // NOTE: This works because when the filename is fully + // remapped, we don't care about the `local` part, + // and when the filename is not remapped at all, + // `maybe_remapped` and `local` are equal. self.local = None; } } From 7227bfed0847703f65e0628f60a860f5cad09594 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 14 Dec 2025 13:30:25 +0100 Subject: [PATCH 2/4] Prefer using `was_fully_remapped` helper function --- compiler/rustc_span/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 63c51164761d9..53363fae933ee 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -313,7 +313,7 @@ impl Hash for RealFileName { // remapped path if that exists. This is because remapped paths to // sysroot crates (/rust/$hash or /rust/$version) remain stable even // if the corresponding local path changes. - if !self.scopes.is_all() { + if !self.was_fully_remapped() { self.local.hash(state); } self.maybe_remapped.hash(state); From c877536864fc011dfcbcbde60f5d21698c6eee71 Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 14 Dec 2025 13:32:16 +0100 Subject: [PATCH 3/4] Add `#[inline]` to a bunch of filename related functions --- compiler/rustc_span/src/lib.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 53363fae933ee..630656f9ec5c6 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -241,12 +241,14 @@ bitflags::bitflags! { } impl Encodable for RemapPathScopeComponents { + #[inline] fn encode(&self, s: &mut E) { s.emit_u8(self.bits()); } } impl Decodable for RemapPathScopeComponents { + #[inline] fn decode(s: &mut D) -> RemapPathScopeComponents { RemapPathScopeComponents::from_bits(s.read_u8()) .expect("invalid bits for RemapPathScopeComponents") @@ -308,6 +310,7 @@ struct InnerRealFileName { } impl Hash for RealFileName { + #[inline] fn hash(&self, state: &mut H) { // To prevent #70924 from happening again we should only hash the // remapped path if that exists. This is because remapped paths to @@ -327,6 +330,7 @@ impl RealFileName { /// ## Panic /// /// Only one scope components can be given to this function. + #[inline] pub fn path(&self, scope: RemapPathScopeComponents) -> &Path { assert!( scope.bits().count_ones() == 1, @@ -351,6 +355,7 @@ impl RealFileName { /// ## Panic /// /// Only one scope components can be given to this function. + #[inline] pub fn embeddable_name(&self, scope: RemapPathScopeComponents) -> (&Path, &Path) { assert!( scope.bits().count_ones() == 1, @@ -369,6 +374,7 @@ impl RealFileName { /// if this information exists. /// /// May not exists if the filename was imported from another crate. + #[inline] pub fn local_path(&self) -> Option<&Path> { if self.was_not_remapped() { Some(&self.maybe_remapped.name) @@ -381,6 +387,7 @@ impl RealFileName { /// if this information exists. /// /// May not exists if the filename was imported from another crate. + #[inline] pub fn into_local_path(self) -> Option { if self.was_not_remapped() { Some(self.maybe_remapped.name) @@ -390,6 +397,7 @@ impl RealFileName { } /// Returns whenever the filename was remapped. + #[inline] pub(crate) fn was_remapped(&self) -> bool { !self.scopes.is_empty() } @@ -409,6 +417,7 @@ impl RealFileName { /// Returns an empty `RealFileName` /// /// Useful as the working directory input to `SourceMap::to_real_filename`. + #[inline] pub fn empty() -> RealFileName { RealFileName { local: Some(InnerRealFileName { @@ -554,6 +563,7 @@ impl FileName { /// if this information exists. /// /// Avoid embedding this in build artifacts. Prefer using the `display` method. + #[inline] pub fn prefer_remapped_unconditionally(&self) -> FileNameDisplay<'_> { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped } } @@ -562,16 +572,19 @@ impl FileName { /// if this information exists. /// /// Avoid embedding this in build artifacts. Prefer using the `display` method. + #[inline] pub fn prefer_local_unconditionally(&self) -> FileNameDisplay<'_> { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local } } /// Returns a short (either the filename or an empty string). + #[inline] pub fn short(&self) -> FileNameDisplay<'_> { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Short } } /// Returns a `Display`-able path for the given scope. + #[inline] pub fn display(&self, scope: RemapPathScopeComponents) -> FileNameDisplay<'_> { FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Scope(scope) } } From 075f4cd57f3c2b175510c25638fc23bc87d7633b Mon Sep 17 00:00:00 2001 From: Urgau Date: Sun, 14 Dec 2025 13:44:14 +0100 Subject: [PATCH 4/4] Restore embedding warning and simply `{,into_}local_path` methods --- compiler/rustc_span/src/lib.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_span/src/lib.rs b/compiler/rustc_span/src/lib.rs index 630656f9ec5c6..3d641905d3251 100644 --- a/compiler/rustc_span/src/lib.rs +++ b/compiler/rustc_span/src/lib.rs @@ -374,12 +374,16 @@ impl RealFileName { /// if this information exists. /// /// May not exists if the filename was imported from another crate. + /// + /// Avoid embedding this in build artifacts; prefer `path()` or `embeddable_name()`. #[inline] pub fn local_path(&self) -> Option<&Path> { if self.was_not_remapped() { Some(&self.maybe_remapped.name) + } else if let Some(local) = &self.local { + Some(&local.name) } else { - self.local.as_ref().map(|lp| lp.name.as_ref()) + None } } @@ -387,12 +391,16 @@ impl RealFileName { /// if this information exists. /// /// May not exists if the filename was imported from another crate. + /// + /// Avoid embedding this in build artifacts; prefer `path()` or `embeddable_name()`. #[inline] pub fn into_local_path(self) -> Option { if self.was_not_remapped() { Some(self.maybe_remapped.name) + } else if let Some(local) = self.local { + Some(local.name) } else { - self.local.map(|lp| lp.name) + None } }