Skip to content

Commit 986b8a6

Browse files
Auto merge of #149989 - Urgau:filenames-post-improvements, r=<try>
Improve filenames encoding and misc
2 parents 08de25c + 93af682 commit 986b8a6

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

compiler/rustc_span/src/lib.rs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,12 +241,14 @@ bitflags::bitflags! {
241241
}
242242

243243
impl<E: Encoder> Encodable<E> for RemapPathScopeComponents {
244+
#[inline]
244245
fn encode(&self, s: &mut E) {
245246
s.emit_u8(self.bits());
246247
}
247248
}
248249

249250
impl<D: Decoder> Decodable<D> for RemapPathScopeComponents {
251+
#[inline]
250252
fn decode(s: &mut D) -> RemapPathScopeComponents {
251253
RemapPathScopeComponents::from_bits(s.read_u8())
252254
.expect("invalid bits for RemapPathScopeComponents")
@@ -308,12 +310,13 @@ struct InnerRealFileName {
308310
}
309311

310312
impl Hash for RealFileName {
313+
#[inline]
311314
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
312315
// To prevent #70924 from happening again we should only hash the
313316
// remapped path if that exists. This is because remapped paths to
314317
// sysroot crates (/rust/$hash or /rust/$version) remain stable even
315318
// if the corresponding local path changes.
316-
if !self.scopes.is_all() {
319+
if self.was_remapped() {
317320
self.local.hash(state);
318321
}
319322
self.maybe_remapped.hash(state);
@@ -327,6 +330,7 @@ impl RealFileName {
327330
/// ## Panic
328331
///
329332
/// Only one scope components can be given to this function.
333+
#[inline]
330334
pub fn path(&self, scope: RemapPathScopeComponents) -> &Path {
331335
assert!(
332336
scope.bits().count_ones() == 1,
@@ -351,6 +355,7 @@ impl RealFileName {
351355
/// ## Panic
352356
///
353357
/// Only one scope components can be given to this function.
358+
#[inline]
354359
pub fn embeddable_name(&self, scope: RemapPathScopeComponents) -> (&Path, &Path) {
355360
assert!(
356361
scope.bits().count_ones() == 1,
@@ -369,6 +374,9 @@ impl RealFileName {
369374
/// if this information exists.
370375
///
371376
/// May not exists if the filename was imported from another crate.
377+
///
378+
/// Avoid embedding this in build artifacts; prefer `path()` or `embeddable_name()`.
379+
#[inline]
372380
pub fn local_path(&self) -> Option<&Path> {
373381
self.local.as_ref().map(|lp| lp.name.as_ref())
374382
}
@@ -377,18 +385,23 @@ impl RealFileName {
377385
/// if this information exists.
378386
///
379387
/// May not exists if the filename was imported from another crate.
388+
///
389+
/// Avoid embedding this in build artifacts; prefer `path()` or `embeddable_name()`.
390+
#[inline]
380391
pub fn into_local_path(self) -> Option<PathBuf> {
381392
self.local.map(|lp| lp.name)
382393
}
383394

384395
/// Returns whenever the filename was remapped.
396+
#[inline]
385397
pub(crate) fn was_remapped(&self) -> bool {
386398
!self.scopes.is_empty()
387399
}
388400

389401
/// Returns an empty `RealFileName`
390402
///
391403
/// Useful as the working directory input to `SourceMap::to_real_filename`.
404+
#[inline]
392405
pub fn empty() -> RealFileName {
393406
RealFileName {
394407
local: Some(InnerRealFileName {
@@ -420,9 +433,14 @@ impl RealFileName {
420433
/// Update the filename for encoding in the crate metadata.
421434
///
422435
/// Currently it's about removing the local part when the filename
423-
/// is fully remapped.
436+
/// is either fully remapped or not remapped at all.
437+
#[inline]
424438
pub fn update_for_crate_metadata(&mut self) {
425-
if self.scopes.is_all() {
439+
if self.scopes.is_all() || self.scopes.is_empty() {
440+
// NOTE: This works because when the filename is fully
441+
// remapped, we don't care about the `local` part,
442+
// and when the filename is not remapped at all,
443+
// `maybe_remapped` and `local` are equal.
426444
self.local = None;
427445
}
428446
}
@@ -529,6 +547,7 @@ impl FileName {
529547
/// if this information exists.
530548
///
531549
/// Avoid embedding this in build artifacts. Prefer using the `display` method.
550+
#[inline]
532551
pub fn prefer_remapped_unconditionally(&self) -> FileNameDisplay<'_> {
533552
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped }
534553
}
@@ -537,16 +556,19 @@ impl FileName {
537556
/// if this information exists.
538557
///
539558
/// Avoid embedding this in build artifacts. Prefer using the `display` method.
559+
#[inline]
540560
pub fn prefer_local_unconditionally(&self) -> FileNameDisplay<'_> {
541561
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Local }
542562
}
543563

544564
/// Returns a short (either the filename or an empty string).
565+
#[inline]
545566
pub fn short(&self) -> FileNameDisplay<'_> {
546567
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Short }
547568
}
548569

549570
/// Returns a `Display`-able path for the given scope.
571+
#[inline]
550572
pub fn display(&self, scope: RemapPathScopeComponents) -> FileNameDisplay<'_> {
551573
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Scope(scope) }
552574
}

0 commit comments

Comments
 (0)