1
Fork 0

Construct SourceMap at the same time as SessionGlobals.

Currently `SourceMap` is constructed slightly later than
`SessionGlobals`, and inserted. This commit changes things so they are
done at the same time.

Benefits:
- `SessionGlobals::source_map` changes from
  `Lock<Option<Lrc<SourceMap>>>` to `Option<Lrc<SourceMap>>`. It's still
  optional, but mutability isn't required because it's initialized at
  construction.
- `set_source_map` is removed, simplifying `run_compiler`, which is
  good because that's a critical function and it's nice to make it
  simpler.

This requires moving things around a bit, so the necessary inputs are
available when `SessionGlobals` is created, in particular the `loader`
and `hash_kind`, which are no longer computed by `build_session`. These
inputs are captured by the new `SourceMapInputs` type, which is threaded
through various places.
This commit is contained in:
Nicholas Nethercote 2024-03-21 14:17:00 +11:00
parent ff2e4ed1f1
commit 62c32aeeab
10 changed files with 118 additions and 116 deletions

View file

@ -167,9 +167,17 @@ struct SourceMapFiles {
stable_id_to_source_file: UnhashMap<StableSourceFileId, Lrc<SourceFile>>,
}
/// Used to construct a `SourceMap` with `SourceMap::with_inputs`.
pub struct SourceMapInputs {
pub file_loader: Box<dyn FileLoader + Send + Sync>,
pub path_mapping: FilePathMapping,
pub hash_kind: SourceFileHashAlgorithm,
}
pub struct SourceMap {
files: RwLock<SourceMapFiles>,
file_loader: IntoDynSyncSend<Box<dyn FileLoader + Sync + Send>>,
// This is used to apply the file path remapping as specified via
// `--remap-path-prefix` to all `SourceFile`s allocated within this `SourceMap`.
path_mapping: FilePathMapping,
@ -180,17 +188,15 @@ pub struct SourceMap {
impl SourceMap {
pub fn new(path_mapping: FilePathMapping) -> SourceMap {
Self::with_file_loader_and_hash_kind(
Box::new(RealFileLoader),
Self::with_inputs(SourceMapInputs {
file_loader: Box::new(RealFileLoader),
path_mapping,
SourceFileHashAlgorithm::Md5,
)
hash_kind: SourceFileHashAlgorithm::Md5,
})
}
pub fn with_file_loader_and_hash_kind(
file_loader: Box<dyn FileLoader + Sync + Send>,
path_mapping: FilePathMapping,
hash_kind: SourceFileHashAlgorithm,
pub fn with_inputs(
SourceMapInputs { file_loader, path_mapping, hash_kind }: SourceMapInputs,
) -> SourceMap {
SourceMap {
files: Default::default(),
@ -1054,6 +1060,10 @@ impl SourceMap {
}
}
pub fn get_source_map() -> Option<Lrc<SourceMap>> {
with_session_globals(|session_globals| session_globals.source_map.clone())
}
#[derive(Clone)]
pub struct FilePathMapping {
mapping: Vec<(PathBuf, PathBuf)>,