Skip to content

Commit b593ab4

Browse files
committed
Add current working to source map and FileLoader
This is preparation for the filename handling overhaul where having access to the working directory is mandatory.
1 parent 66428d9 commit b593ab4

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

compiler/rustc_span/src/source_map.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ pub trait FileLoader {
105105
/// Read the contents of a potentially non-UTF-8 file into memory.
106106
/// We don't normalize binary files, so we can start in an Arc.
107107
fn read_binary_file(&self, path: &Path) -> io::Result<Arc<[u8]>>;
108+
109+
/// Current working directory
110+
fn current_directory(&self) -> io::Result<PathBuf>;
108111
}
109112

110113
/// A FileLoader that uses std::fs to load real files.
@@ -170,6 +173,10 @@ impl FileLoader for RealFileLoader {
170173
file.read_to_end(&mut bytes)?;
171174
Ok(bytes.into())
172175
}
176+
177+
fn current_directory(&self) -> io::Result<PathBuf> {
178+
std::env::current_dir()
179+
}
173180
}
174181

175182
// _____________________________________________________________________________
@@ -198,6 +205,9 @@ pub struct SourceMap {
198205
// `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
199206
path_mapping: FilePathMapping,
200207

208+
/// Current working directory
209+
working_dir: RealFileName,
210+
201211
/// The algorithm used for hashing the contents of each source file.
202212
hash_kind: SourceFileHashAlgorithm,
203213

@@ -221,8 +231,14 @@ impl SourceMap {
221231
pub fn with_inputs(
222232
SourceMapInputs { file_loader, path_mapping, hash_kind, checksum_hash_kind }: SourceMapInputs,
223233
) -> SourceMap {
234+
let cwd = file_loader
235+
.current_directory()
236+
.expect("expecting a current working directory to exist");
237+
let working_dir = cwd.to_path_buf().into();
238+
debug!(?working_dir);
224239
SourceMap {
225240
files: Default::default(),
241+
working_dir,
226242
file_loader: IntoDynSyncSend(file_loader),
227243
path_mapping,
228244
hash_kind,
@@ -234,6 +250,10 @@ impl SourceMap {
234250
&self.path_mapping
235251
}
236252

253+
pub fn working_dir(&self) -> &RealFileName {
254+
&self.working_dir
255+
}
256+
237257
pub fn file_exists(&self, path: &Path) -> bool {
238258
self.file_loader.file_exists(path)
239259
}

0 commit comments

Comments
 (0)