Skip to content

Commit 230e91d

Browse files
committed
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.
1 parent 8188f6c commit 230e91d

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

compiler/rustc_span/src/lib.rs

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -370,22 +370,42 @@ impl RealFileName {
370370
///
371371
/// May not exists if the filename was imported from another crate.
372372
pub fn local_path(&self) -> Option<&Path> {
373-
self.local.as_ref().map(|lp| lp.name.as_ref())
373+
if self.was_not_remapped() {
374+
Some(&self.maybe_remapped.name)
375+
} else {
376+
self.local.as_ref().map(|lp| lp.name.as_ref())
377+
}
374378
}
375379

376380
/// Returns the path suitable for reading from the file system on the local host,
377381
/// if this information exists.
378382
///
379383
/// May not exists if the filename was imported from another crate.
380384
pub fn into_local_path(self) -> Option<PathBuf> {
381-
self.local.map(|lp| lp.name)
385+
if self.was_not_remapped() {
386+
Some(self.maybe_remapped.name)
387+
} else {
388+
self.local.map(|lp| lp.name)
389+
}
382390
}
383391

384392
/// Returns whenever the filename was remapped.
385393
pub(crate) fn was_remapped(&self) -> bool {
386394
!self.scopes.is_empty()
387395
}
388396

397+
/// Returns whenever the filename was fully remapped.
398+
#[inline]
399+
fn was_fully_remapped(&self) -> bool {
400+
self.scopes.is_all()
401+
}
402+
403+
/// Returns whenever the filename was not remapped.
404+
#[inline]
405+
fn was_not_remapped(&self) -> bool {
406+
self.scopes.is_empty()
407+
}
408+
389409
/// Returns an empty `RealFileName`
390410
///
391411
/// Useful as the working directory input to `SourceMap::to_real_filename`.
@@ -420,9 +440,14 @@ impl RealFileName {
420440
/// Update the filename for encoding in the crate metadata.
421441
///
422442
/// Currently it's about removing the local part when the filename
423-
/// is fully remapped.
443+
/// is either fully remapped or not remapped at all.
444+
#[inline]
424445
pub fn update_for_crate_metadata(&mut self) {
425-
if self.scopes.is_all() {
446+
if self.was_fully_remapped() || self.was_not_remapped() {
447+
// NOTE: This works because when the filename is fully
448+
// remapped, we don't care about the `local` part,
449+
// and when the filename is not remapped at all,
450+
// `maybe_remapped` and `local` are equal.
426451
self.local = None;
427452
}
428453
}

0 commit comments

Comments
 (0)