1
Fork 0

Rename RealFileName::Named to LocalPath and Devirtualized to Remapped

This commit is contained in:
Andy Wang 2021-04-03 16:59:31 +01:00
parent d66506d353
commit 6720a37042
No known key found for this signature in database
GPG key ID: 181B49F9F38F3374
13 changed files with 82 additions and 132 deletions

View file

@ -1072,7 +1072,7 @@ impl<'a> ExtCtxt<'a> {
// after macro expansion (that is, they are unhygienic). // after macro expansion (that is, they are unhygienic).
if !path.is_absolute() { if !path.is_absolute() {
let callsite = span.source_callsite(); let callsite = span.source_callsite();
let mut result = match self.source_map().span_to_unmapped_path(callsite) { let mut result = match self.source_map().span_to_filename(callsite) {
FileName::Real(name) => name.into_local_path(), FileName::Real(name) => name.into_local_path(),
FileName::DocTest(path, _) => path, FileName::DocTest(path, _) => path,
other => { other => {

View file

@ -361,7 +361,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
// FIXME: Avoid visiting the crate as a `Mod` item, // FIXME: Avoid visiting the crate as a `Mod` item,
// make crate a first class expansion target instead. // make crate a first class expansion target instead.
pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { pub fn expand_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
let file_path = match self.cx.source_map().span_to_unmapped_path(krate.span) { let file_path = match self.cx.source_map().span_to_filename(krate.span) {
FileName::Real(name) => name.into_local_path(), FileName::Real(name) => name.into_local_path(),
other => PathBuf::from(other.to_string()), other => PathBuf::from(other.to_string()),
}; };

View file

@ -725,7 +725,7 @@ fn ident_name_compatibility_hack(
if let ExpnKind::Macro(_, macro_name) = orig_span.ctxt().outer_expn_data().kind { if let ExpnKind::Macro(_, macro_name) = orig_span.ctxt().outer_expn_data().kind {
let source_map = rustc.sess.source_map(); let source_map = rustc.sess.source_map();
let filename = source_map.span_to_filename(orig_span); let filename = source_map.span_to_filename(orig_span);
if let FileName::Real(RealFileName::Named(path)) = filename { if let FileName::Real(RealFileName::LocalPath(path)) = filename {
let matches_prefix = |prefix, filename| { let matches_prefix = |prefix, filename| {
// Check for a path that ends with 'prefix*/src/<filename>' // Check for a path that ends with 'prefix*/src/<filename>'
let mut iter = path.components().rev(); let mut iter = path.components().rev();
@ -788,7 +788,7 @@ fn ident_name_compatibility_hack(
if macro_name == sym::tuple_from_req && matches_prefix("actix-web", "extract.rs") { if macro_name == sym::tuple_from_req && matches_prefix("actix-web", "extract.rs") {
let snippet = source_map.span_to_snippet(orig_span); let snippet = source_map.span_to_snippet(orig_span);
if snippet.as_deref() == Ok("$T") { if snippet.as_deref() == Ok("$T") {
if let FileName::Real(RealFileName::Named(macro_path)) = if let FileName::Real(RealFileName::LocalPath(macro_path)) =
source_map.span_to_filename(rustc.def_site) source_map.span_to_filename(rustc.def_site)
{ {
if macro_path.to_string_lossy().contains("pin-project-internal-0.") { if macro_path.to_string_lossy().contains("pin-project-internal-0.") {

View file

@ -575,7 +575,7 @@ fn write_out_deps(
.iter() .iter()
.filter(|fmap| fmap.is_real_file()) .filter(|fmap| fmap.is_real_file())
.filter(|fmap| !fmap.is_imported()) .filter(|fmap| !fmap.is_imported())
.map(|fmap| escape_dep_filename(&fmap.unmapped_path.as_ref().unwrap_or(&fmap.name))) .map(|fmap| escape_dep_filename(&fmap.name))
.collect(); .collect();
if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend { if let Some(ref backend) = sess.opts.debugging_opts.codegen_backend {
@ -587,15 +587,15 @@ fn write_out_deps(
for cnum in resolver.cstore().crates_untracked() { for cnum in resolver.cstore().crates_untracked() {
let source = resolver.cstore().crate_source_untracked(cnum); let source = resolver.cstore().crate_source_untracked(cnum);
if let Some((path, _)) = source.dylib { if let Some((path, _)) = source.dylib {
let file_name = FileName::Real(RealFileName::Named(path)); let file_name = FileName::Real(RealFileName::LocalPath(path));
files.push(escape_dep_filename(&file_name)); files.push(escape_dep_filename(&file_name));
} }
if let Some((path, _)) = source.rlib { if let Some((path, _)) = source.rlib {
let file_name = FileName::Real(RealFileName::Named(path)); let file_name = FileName::Real(RealFileName::LocalPath(path));
files.push(escape_dep_filename(&file_name)); files.push(escape_dep_filename(&file_name));
} }
if let Some((path, _)) = source.rmeta { if let Some((path, _)) = source.rmeta {
let file_name = FileName::Real(RealFileName::Named(path)); let file_name = FileName::Real(RealFileName::LocalPath(path));
files.push(escape_dep_filename(&file_name)); files.push(escape_dep_filename(&file_name));
} }
} }

View file

@ -1635,9 +1635,11 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
if let Some(virtual_dir) = virtual_rust_source_base_dir { if let Some(virtual_dir) = virtual_rust_source_base_dir {
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir { if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
if let rustc_span::FileName::Real(old_name) = name { if let rustc_span::FileName::Real(old_name) = name {
if let rustc_span::RealFileName::Named(one_path) = old_name { if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
if let Ok(rest) = one_path.strip_prefix(virtual_dir) { old_name
let virtual_name = one_path.clone(); {
if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) {
let virtual_name = virtual_name.clone();
// The std library crates are in // The std library crates are in
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
@ -1673,7 +1675,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
virtual_name.display(), virtual_name.display(),
new_path.display(), new_path.display(),
); );
let new_name = rustc_span::RealFileName::Devirtualized { let new_name = rustc_span::RealFileName::Remapped {
local_path: new_path, local_path: new_path,
virtual_name, virtual_name,
}; };
@ -1694,7 +1696,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
// containing the information we need. // containing the information we need.
let rustc_span::SourceFile { let rustc_span::SourceFile {
mut name, mut name,
name_was_remapped,
src_hash, src_hash,
start_pos, start_pos,
end_pos, end_pos,
@ -1709,8 +1710,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
// If this file's path has been remapped to `/rustc/$hash`, // If this file's path has been remapped to `/rustc/$hash`,
// we might be able to reverse that (also see comments above, // we might be able to reverse that (also see comments above,
// on `try_to_translate_virtual_to_real`). // on `try_to_translate_virtual_to_real`).
// FIXME(eddyb) we could check `name_was_remapped` here,
// but in practice it seems to be always `false`.
try_to_translate_virtual_to_real(&mut name); try_to_translate_virtual_to_real(&mut name);
let source_length = (end_pos - start_pos).to_usize(); let source_length = (end_pos - start_pos).to_usize();
@ -1735,7 +1734,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
let local_version = sess.source_map().new_imported_source_file( let local_version = sess.source_map().new_imported_source_file(
name, name,
name_was_remapped,
src_hash, src_hash,
name_hash, name_hash,
source_length, source_length,

View file

@ -28,9 +28,12 @@ use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
use rustc_serialize::{opaque, Encodable, Encoder}; use rustc_serialize::{opaque, Encodable, Encoder};
use rustc_session::config::CrateType; use rustc_session::config::CrateType;
use rustc_span::hygiene::{ExpnDataEncodeMode, HygieneEncodeContext, MacroKind};
use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext}; use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext};
use rustc_span::{
hygiene::{ExpnDataEncodeMode, HygieneEncodeContext, MacroKind},
RealFileName,
};
use rustc_target::abi::VariantIdx; use rustc_target::abi::VariantIdx;
use std::hash::Hash; use std::hash::Hash;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
@ -485,18 +488,22 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
}) })
.map(|(_, source_file)| { .map(|(_, source_file)| {
let mut adapted = match source_file.name { let mut adapted = match source_file.name {
// This path of this SourceFile has been modified by FileName::Real(ref name) => {
// path-remapping, so we use it verbatim (and avoid // Expand all local paths to absolute paths because
// cloning the whole map in the process).
_ if source_file.name_was_remapped => source_file.clone(),
// Otherwise expand all paths to absolute paths because
// any relative paths are potentially relative to a // any relative paths are potentially relative to a
// wrong directory. // wrong directory.
FileName::Real(ref name) => {
let name = name.stable_name();
let mut adapted = (**source_file).clone(); let mut adapted = (**source_file).clone();
adapted.name = Path::new(&working_dir).join(name).into(); adapted.name = match name {
RealFileName::LocalPath(local_path) => {
Path::new(&working_dir).join(local_path).into()
}
RealFileName::Remapped { local_path, virtual_name } => {
FileName::Real(RealFileName::Remapped {
local_path: Path::new(&working_dir).join(local_path),
virtual_name: virtual_name.clone(),
})
}
};
adapted.name_hash = { adapted.name_hash = {
let mut hasher: StableHasher = StableHasher::new(); let mut hasher: StableHasher = StableHasher::new();
adapted.name.hash(&mut hasher); adapted.name.hash(&mut hasher);

View file

@ -61,8 +61,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
let SourceFile { let SourceFile {
name: _, // We hash the smaller name_hash instead of this name: _, // We hash the smaller name_hash instead of this
name_hash, name_hash,
name_was_remapped,
unmapped_path: _,
cnum, cnum,
// Do not hash the source as it is not encoded // Do not hash the source as it is not encoded
src: _, src: _,
@ -77,7 +75,6 @@ impl<'a> HashStable<StableHashingContext<'a>> for SourceFile {
} = *self; } = *self;
(name_hash as u64).hash_stable(hcx, hasher); (name_hash as u64).hash_stable(hcx, hasher);
name_was_remapped.hash_stable(hcx, hasher);
src_hash.hash_stable(hcx, hasher); src_hash.hash_stable(hcx, hasher);

View file

@ -16,8 +16,7 @@ impl<'a> SpanUtils<'a> {
pub fn make_filename_string(&self, file: &SourceFile) -> String { pub fn make_filename_string(&self, file: &SourceFile) -> String {
match &file.name { match &file.name {
FileName::Real(name) if !file.name_was_remapped => { FileName::Real(RealFileName::LocalPath(path)) => {
let path = name.local_path();
if path.is_absolute() { if path.is_absolute() {
self.sess self.sess
.source_map() .source_map()
@ -30,8 +29,11 @@ impl<'a> SpanUtils<'a> {
self.sess.working_dir.0.join(&path).display().to_string() self.sess.working_dir.0.join(&path).display().to_string()
} }
} }
// If the file name is already remapped, we assume the user // If the file name was remapped, we assume the user
// configured it the way they wanted to, so use that directly // configured it the way they wanted to, so use that directly
FileName::Real(RealFileName::Remapped { local_path: _, virtual_name }) => {
virtual_name.display().to_string()
}
filename => filename.to_string(), filename => filename.to_string(),
} }
} }

View file

@ -113,17 +113,16 @@ pub fn with_default_session_globals<R>(f: impl FnOnce() -> R) -> R {
// deserialization. // deserialization.
scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals); scoped_tls::scoped_thread_local!(pub static SESSION_GLOBALS: SessionGlobals);
// FIXME: Perhaps this should not implement Rustc{Decodable, Encodable}
//
// FIXME: We should use this enum or something like it to get rid of the // FIXME: We should use this enum or something like it to get rid of the
// use of magic `/rust/1.x/...` paths across the board. // use of magic `/rust/1.x/...` paths across the board.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)] #[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
#[derive(HashStable_Generic, Decodable, Encodable)] #[derive(HashStable_Generic, Decodable, Encodable)]
pub enum RealFileName { pub enum RealFileName {
Named(PathBuf), LocalPath(PathBuf),
/// For de-virtualized paths (namely paths into libstd that have been mapped /// For remapped paths (namely paths into libstd that have been mapped
/// to the appropriate spot on the local host's file system), /// to the appropriate spot on the local host's file system, and local file
Devirtualized { /// system paths that have been remapped with `FilePathMapping`),
Remapped {
/// `local_path` is the (host-dependent) local path to the file. /// `local_path` is the (host-dependent) local path to the file.
local_path: PathBuf, local_path: PathBuf,
/// `virtual_name` is the stable path rustc will store internally within /// `virtual_name` is the stable path rustc will store internally within
@ -137,8 +136,8 @@ impl RealFileName {
/// Avoid embedding this in build artifacts; see `stable_name()` for that. /// Avoid embedding this in build artifacts; see `stable_name()` for that.
pub fn local_path(&self) -> &Path { pub fn local_path(&self) -> &Path {
match self { match self {
RealFileName::Named(p) RealFileName::LocalPath(p)
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => &p, | RealFileName::Remapped { local_path: p, virtual_name: _ } => &p,
} }
} }
@ -146,19 +145,19 @@ impl RealFileName {
/// Avoid embedding this in build artifacts; see `stable_name()` for that. /// Avoid embedding this in build artifacts; see `stable_name()` for that.
pub fn into_local_path(self) -> PathBuf { pub fn into_local_path(self) -> PathBuf {
match self { match self {
RealFileName::Named(p) RealFileName::LocalPath(p)
| RealFileName::Devirtualized { local_path: p, virtual_name: _ } => p, | RealFileName::Remapped { local_path: p, virtual_name: _ } => p,
} }
} }
/// Returns the path suitable for embedding into build artifacts. Note that /// Returns the path suitable for embedding into build artifacts. Note that
/// a virtualized path will not correspond to a valid file system path; see /// a remapped path will not correspond to a valid file system path; see
/// `local_path()` for something that is more likely to return paths into the /// `local_path()` for something that is more likely to return paths into the
/// local host file system. /// local host file system.
pub fn stable_name(&self) -> &Path { pub fn stable_name(&self) -> &Path {
match self { match self {
RealFileName::Named(p) RealFileName::LocalPath(p)
| RealFileName::Devirtualized { local_path: _, virtual_name: p } => &p, | RealFileName::Remapped { local_path: _, virtual_name: p } => &p,
} }
} }
} }
@ -214,7 +213,7 @@ impl std::fmt::Display for FileName {
impl From<PathBuf> for FileName { impl From<PathBuf> for FileName {
fn from(p: PathBuf) -> Self { fn from(p: PathBuf) -> Self {
assert!(!p.to_string_lossy().ends_with('>')); assert!(!p.to_string_lossy().ends_with('>'));
FileName::Real(RealFileName::Named(p)) FileName::Real(RealFileName::LocalPath(p))
} }
} }
@ -1124,11 +1123,6 @@ pub struct SourceFile {
/// originate from files has names between angle brackets by convention /// originate from files has names between angle brackets by convention
/// (e.g., `<anon>`). /// (e.g., `<anon>`).
pub name: FileName, pub name: FileName,
/// `true` if the `name` field above has been modified by `--remap-path-prefix`.
pub name_was_remapped: bool,
/// The unmapped path of the file that the source came from.
/// Set to `None` if the `SourceFile` was imported from an external crate.
pub unmapped_path: Option<FileName>,
/// The complete source code. /// The complete source code.
pub src: Option<Lrc<String>>, pub src: Option<Lrc<String>>,
/// The source code's hash. /// The source code's hash.
@ -1158,7 +1152,6 @@ impl<S: Encoder> Encodable<S> for SourceFile {
fn encode(&self, s: &mut S) -> Result<(), S::Error> { fn encode(&self, s: &mut S) -> Result<(), S::Error> {
s.emit_struct("SourceFile", 8, |s| { s.emit_struct("SourceFile", 8, |s| {
s.emit_struct_field("name", 0, |s| self.name.encode(s))?; s.emit_struct_field("name", 0, |s| self.name.encode(s))?;
s.emit_struct_field("name_was_remapped", 1, |s| self.name_was_remapped.encode(s))?;
s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?; s.emit_struct_field("src_hash", 2, |s| self.src_hash.encode(s))?;
s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?; s.emit_struct_field("start_pos", 3, |s| self.start_pos.encode(s))?;
s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?; s.emit_struct_field("end_pos", 4, |s| self.end_pos.encode(s))?;
@ -1233,8 +1226,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
fn decode(d: &mut D) -> Result<SourceFile, D::Error> { fn decode(d: &mut D) -> Result<SourceFile, D::Error> {
d.read_struct("SourceFile", 8, |d| { d.read_struct("SourceFile", 8, |d| {
let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?; let name: FileName = d.read_struct_field("name", 0, |d| Decodable::decode(d))?;
let name_was_remapped: bool =
d.read_struct_field("name_was_remapped", 1, |d| Decodable::decode(d))?;
let src_hash: SourceFileHash = let src_hash: SourceFileHash =
d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?; d.read_struct_field("src_hash", 2, |d| Decodable::decode(d))?;
let start_pos: BytePos = let start_pos: BytePos =
@ -1278,8 +1269,6 @@ impl<D: Decoder> Decodable<D> for SourceFile {
let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?; let cnum: CrateNum = d.read_struct_field("cnum", 10, |d| Decodable::decode(d))?;
Ok(SourceFile { Ok(SourceFile {
name, name,
name_was_remapped,
unmapped_path: None,
start_pos, start_pos,
end_pos, end_pos,
src: None, src: None,
@ -1307,8 +1296,6 @@ impl fmt::Debug for SourceFile {
impl SourceFile { impl SourceFile {
pub fn new( pub fn new(
name: FileName, name: FileName,
name_was_remapped: bool,
unmapped_path: FileName,
mut src: String, mut src: String,
start_pos: BytePos, start_pos: BytePos,
hash_kind: SourceFileHashAlgorithm, hash_kind: SourceFileHashAlgorithm,
@ -1330,8 +1317,6 @@ impl SourceFile {
SourceFile { SourceFile {
name, name,
name_was_remapped,
unmapped_path: Some(unmapped_path),
src: Some(Lrc::new(src)), src: Some(Lrc::new(src)),
src_hash, src_hash,
external_src: Lock::new(ExternalSource::Unneeded), external_src: Lock::new(ExternalSource::Unneeded),

View file

@ -15,11 +15,11 @@ pub use crate::*;
use rustc_data_structures::fx::FxHashMap; use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::stable_hasher::StableHasher;
use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock}; use rustc_data_structures::sync::{AtomicU32, Lrc, MappedReadGuard, ReadGuard, RwLock};
use std::cmp;
use std::convert::TryFrom;
use std::hash::Hash; use std::hash::Hash;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use std::{clone::Clone, cmp};
use std::{convert::TryFrom, unreachable};
use std::fs; use std::fs;
use std::io; use std::io;
@ -127,30 +127,16 @@ pub struct StableSourceFileId(u128);
// StableSourceFileId, perhaps built atop source_file.name_hash. // StableSourceFileId, perhaps built atop source_file.name_hash.
impl StableSourceFileId { impl StableSourceFileId {
pub fn new(source_file: &SourceFile) -> StableSourceFileId { pub fn new(source_file: &SourceFile) -> StableSourceFileId {
StableSourceFileId::new_from_pieces( StableSourceFileId::new_from_name(&source_file.name)
&source_file.name,
source_file.name_was_remapped,
source_file.unmapped_path.as_ref(),
)
} }
fn new_from_pieces( fn new_from_name(name: &FileName) -> StableSourceFileId {
name: &FileName,
name_was_remapped: bool,
unmapped_path: Option<&FileName>,
) -> StableSourceFileId {
let mut hasher = StableHasher::new(); let mut hasher = StableHasher::new();
if let FileName::Real(real_name) = name { // If name was remapped, we need to take both the local path
// rust-lang/rust#70924: Use the stable (virtualized) name when // and stablised path into account, in case two different paths were
// available. (We do not want artifacts from transient file system // mapped to the same
// paths for libstd to leak into our build artifacts.)
real_name.stable_name().hash(&mut hasher)
} else {
name.hash(&mut hasher); name.hash(&mut hasher);
}
name_was_remapped.hash(&mut hasher);
unmapped_path.hash(&mut hasher);
StableSourceFileId(hasher.finish()) StableSourceFileId(hasher.finish())
} }
@ -283,35 +269,15 @@ impl SourceMap {
fn try_new_source_file( fn try_new_source_file(
&self, &self,
mut filename: FileName, filename: FileName,
src: String, src: String,
) -> Result<Lrc<SourceFile>, OffsetOverflowError> { ) -> Result<Lrc<SourceFile>, OffsetOverflowError> {
// The path is used to determine the directory for loading submodules and
// include files, so it must be before remapping.
// Note that filename may not be a valid path, eg it may be `<anon>` etc, // Note that filename may not be a valid path, eg it may be `<anon>` etc,
// but this is okay because the directory determined by `path.pop()` will // but this is okay because the directory determined by `path.pop()` will
// be empty, so the working directory will be used. // be empty, so the working directory will be used.
let unmapped_path = filename.clone(); let (filename, _) = self.path_mapping.map_filename_prefix(&filename);
let was_remapped; let file_id = StableSourceFileId::new_from_name(&filename);
if let FileName::Real(real_filename) = &mut filename {
match real_filename {
RealFileName::Named(path_to_be_remapped)
| RealFileName::Devirtualized {
local_path: path_to_be_remapped,
virtual_name: _,
} => {
let mapped = self.path_mapping.map_prefix(path_to_be_remapped.clone());
was_remapped = mapped.1;
*path_to_be_remapped = mapped.0;
}
}
} else {
was_remapped = false;
}
let file_id =
StableSourceFileId::new_from_pieces(&filename, was_remapped, Some(&unmapped_path));
let lrc_sf = match self.source_file_by_stable_id(file_id) { let lrc_sf = match self.source_file_by_stable_id(file_id) {
Some(lrc_sf) => lrc_sf, Some(lrc_sf) => lrc_sf,
@ -320,8 +286,6 @@ impl SourceMap {
let source_file = Lrc::new(SourceFile::new( let source_file = Lrc::new(SourceFile::new(
filename, filename,
was_remapped,
unmapped_path,
src, src,
Pos::from_usize(start_pos), Pos::from_usize(start_pos),
self.hash_kind, self.hash_kind,
@ -345,7 +309,6 @@ impl SourceMap {
pub fn new_imported_source_file( pub fn new_imported_source_file(
&self, &self,
filename: FileName, filename: FileName,
name_was_remapped: bool,
src_hash: SourceFileHash, src_hash: SourceFileHash,
name_hash: u128, name_hash: u128,
source_len: usize, source_len: usize,
@ -382,8 +345,6 @@ impl SourceMap {
let source_file = Lrc::new(SourceFile { let source_file = Lrc::new(SourceFile {
name: filename, name: filename,
name_was_remapped,
unmapped_path: None,
src: None, src: None,
src_hash, src_hash,
external_src: Lock::new(ExternalSource::Foreign { external_src: Lock::new(ExternalSource::Foreign {
@ -474,14 +435,6 @@ impl SourceMap {
self.lookup_char_pos(sp.lo()).file.name.clone() self.lookup_char_pos(sp.lo()).file.name.clone()
} }
pub fn span_to_unmapped_path(&self, sp: Span) -> FileName {
self.lookup_char_pos(sp.lo())
.file
.unmapped_path
.clone()
.expect("`SourceMap::span_to_unmapped_path` called for imported `SourceFile`?")
}
pub fn is_multiline(&self, sp: Span) -> bool { pub fn is_multiline(&self, sp: Span) -> bool {
let lo = self.lookup_char_pos(sp.lo()); let lo = self.lookup_char_pos(sp.lo());
let hi = self.lookup_char_pos(sp.hi()); let hi = self.lookup_char_pos(sp.hi());
@ -1046,9 +999,26 @@ impl FilePathMapping {
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) { fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
match file { match file {
FileName::Real(realfile) => { FileName::Real(realfile) => {
let path = realfile.local_path(); // If the file is the Name variant with only local_path, then clearly we want to map that
let (path, mapped) = self.map_prefix(path.to_path_buf()); // to a virtual_name
(FileName::Real(RealFileName::Named(path)), mapped) // If the file is already remapped, then we want to map virtual_name further
// but we leave local_path alone
let path = realfile.stable_name();
let (mapped_path, mapped) = self.map_prefix(path.to_path_buf());
if mapped {
let mapped_realfile = match realfile {
RealFileName::LocalPath(local_path)
| RealFileName::Remapped { local_path, virtual_name: _ } => {
RealFileName::Remapped {
local_path: local_path.clone(),
virtual_name: mapped_path,
}
}
};
(FileName::Real(mapped_realfile), mapped)
} else {
unreachable!("attempted to remap an already remapped filename");
}
} }
other => (other.clone(), false), other => (other.clone(), false),
} }

View file

@ -229,7 +229,6 @@ fn t10() {
let SourceFile { let SourceFile {
name, name,
name_was_remapped,
src_hash, src_hash,
start_pos, start_pos,
end_pos, end_pos,
@ -243,7 +242,6 @@ fn t10() {
let imported_src_file = sm.new_imported_source_file( let imported_src_file = sm.new_imported_source_file(
name, name,
name_was_remapped,
src_hash, src_hash,
name_hash, name_hash,
(end_pos - start_pos).to_usize(), (end_pos - start_pos).to_usize(),

View file

@ -883,8 +883,6 @@ impl Tester for Collector {
self.compiling_test_count.fetch_add(1, Ordering::SeqCst); self.compiling_test_count.fetch_add(1, Ordering::SeqCst);
} }
// FIXME(#44940): if doctests ever support path remapping, then this filename
// needs to be the result of `SourceMap::span_to_unmapped_path`.
let path = match &filename { let path = match &filename {
FileName::Real(path) => path.local_path().to_path_buf(), FileName::Real(path) => path.local_path().to_path_buf(),
_ => PathBuf::from(r"doctest.rs"), _ => PathBuf::from(r"doctest.rs"),

View file

@ -66,12 +66,7 @@ impl JsonRenderer<'_> {
let hi = span.hi(self.sess()); let hi = span.hi(self.sess());
let lo = span.lo(self.sess()); let lo = span.lo(self.sess());
Some(Span { Some(Span {
filename: match name { filename: name.into_local_path(),
rustc_span::RealFileName::Named(path) => path,
rustc_span::RealFileName::Devirtualized { local_path, virtual_name: _ } => {
local_path
}
},
begin: (lo.line, lo.col.to_usize()), begin: (lo.line, lo.col.to_usize()),
end: (hi.line, hi.col.to_usize()), end: (hi.line, hi.col.to_usize()),
}) })