Skip to content

Commit b954e1f

Browse files
committed
Somewhat retire Options::working_dir in favor SourceMap::working_dir
We can't completely remove it as it's needed for incr comp but direct consumers towards `SourceMap::working_dir` instead.
1 parent 13d9b36 commit b954e1f

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

compiler/rustc_session/src/config.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hashes::Hash64;
2424
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
2525
use rustc_span::edition::{DEFAULT_EDITION, EDITION_NAME_LIST, Edition, LATEST_STABLE_EDITION};
2626
use rustc_span::source_map::FilePathMapping;
27-
use rustc_span::{FileName, SourceFileHashAlgorithm, Symbol, sym};
27+
use rustc_span::{FileName, RealFileName, SourceFileHashAlgorithm, Symbol, sym};
2828
use rustc_target::spec::{
2929
FramePointer, LinkSelfContainedComponents, LinkerFeatures, PanicStrategy, SplitDebuginfo,
3030
Target, TargetTuple,
@@ -1358,6 +1358,17 @@ fn file_path_mapping(
13581358

13591359
impl Default for Options {
13601360
fn default() -> Options {
1361+
let unstable_opts = UnstableOptions::default();
1362+
1363+
// FIXME(Urgau): This is a hack that ideally shouldn't exist, but rustdoc
1364+
// currently uses this `Default` implementation, so we have no choice but
1365+
// to create a default working directory.
1366+
let working_dir = {
1367+
let working_dir = std::env::current_dir().unwrap();
1368+
let file_mapping = file_path_mapping(Vec::new(), &unstable_opts);
1369+
file_mapping.to_real_filename(&RealFileName::empty(), &working_dir)
1370+
};
1371+
13611372
Options {
13621373
assert_incr_state: None,
13631374
crate_types: Vec::new(),
@@ -1374,7 +1385,7 @@ impl Default for Options {
13741385
test: false,
13751386
incremental: None,
13761387
untracked_state_hash: Default::default(),
1377-
unstable_opts: Default::default(),
1388+
unstable_opts,
13781389
prints: Vec::new(),
13791390
cg: Default::default(),
13801391
error_format: ErrorOutputType::default(),
@@ -1398,7 +1409,7 @@ impl Default for Options {
13981409
json_unused_externs: JsonUnusedExterns::No,
13991410
json_future_incompat: false,
14001411
pretty: None,
1401-
working_dir: RealFileName::LocalPath(std::env::current_dir().unwrap()),
1412+
working_dir,
14021413
color: ColorConfig::Auto,
14031414
logical_env: FxIndexMap::default(),
14041415
verbose: false,
@@ -2752,12 +2763,16 @@ pub fn build_session_options(early_dcx: &mut EarlyDiagCtxt, matches: &getopts::M
27522763
.collect()
27532764
};
27542765

2755-
let working_dir = std::env::current_dir().unwrap_or_else(|e| {
2756-
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
2757-
});
2766+
// Ideally we would use `SourceMap::working_dir` instead, but we don't have access to it
2767+
// so we manually create the potentially-remapped working directory
2768+
let working_dir = {
2769+
let working_dir = std::env::current_dir().unwrap_or_else(|e| {
2770+
early_dcx.early_fatal(format!("Current directory is invalid: {e}"));
2771+
});
27582772

2759-
let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2760-
let working_dir = file_mapping.to_real_filename(&working_dir);
2773+
let file_mapping = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
2774+
file_mapping.to_real_filename(&RealFileName::empty(), &working_dir)
2775+
};
27612776

27622777
let verbose = matches.opt_present("verbose") || unstable_opts.verbose_internals;
27632778

compiler/rustc_session/src/options.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_feature::UnstableFeatures;
1212
use rustc_hashes::Hash64;
1313
use rustc_macros::{Decodable, Encodable};
1414
use rustc_span::edition::Edition;
15-
use rustc_span::{RemapPathScopeComponents, SourceFileHashAlgorithm};
15+
use rustc_span::{RealFileName, RemapPathScopeComponents, SourceFileHashAlgorithm};
1616
use rustc_target::spec::{
1717
CodeModel, FramePointer, LinkerFlavorCli, MergeFunctions, OnBrokenPipe, PanicStrategy,
1818
RelocModel, RelroLevel, SanitizerSet, SplitDebuginfo, StackProtector, SymbolVisibility,
@@ -492,7 +492,9 @@ top_level_options!(
492492
pretty: Option<PpMode> [UNTRACKED],
493493

494494
/// The (potentially remapped) working directory
495+
#[rustc_lint_opt_deny_field_access("use `SourceMap::working_dir` instead of this field")]
495496
working_dir: RealFileName [TRACKED],
497+
496498
color: ColorConfig [UNTRACKED],
497499

498500
verbose: bool [TRACKED_NO_CRATE_HASH],

0 commit comments

Comments
 (0)