1
Fork 0

[RFC 3127 - Trim Paths]: Condition remapped filepath on remap scopes

This commit is contained in:
Urgau 2023-08-23 15:46:58 +02:00
parent 30f94717ca
commit eccc9e6628
16 changed files with 328 additions and 82 deletions

View file

@ -21,8 +21,8 @@ use rustc_feature::UnstableFeatures;
use rustc_span::edition::{Edition, DEFAULT_EDITION, EDITION_NAME_LIST, LATEST_STABLE_EDITION};
use rustc_span::source_map::{FileName, FilePathMapping};
use rustc_span::symbol::{sym, Symbol};
use rustc_span::RealFileName;
use rustc_span::SourceFileHashAlgorithm;
use rustc_span::{FileNameDisplayPreference, RealFileName};
use rustc_errors::emitter::HumanReadableErrorType;
use rustc_errors::{ColorConfig, DiagnosticArgValue, HandlerFlags, IntoDiagnosticArg};
@ -1056,6 +1056,22 @@ pub fn host_triple() -> &'static str {
(option_env!("CFG_COMPILER_HOST_TRIPLE")).expect("CFG_COMPILER_HOST_TRIPLE")
}
fn file_path_mapping(
remap_path_prefix: Vec<(PathBuf, PathBuf)>,
unstable_opts: &UnstableOptions,
) -> FilePathMapping {
FilePathMapping::new(
remap_path_prefix.clone(),
if unstable_opts.remap_path_scope.contains(RemapPathScopeComponents::DIAGNOSTICS)
&& !remap_path_prefix.is_empty()
{
FileNameDisplayPreference::Remapped
} else {
FileNameDisplayPreference::Local
},
)
}
impl Default for Options {
fn default() -> Options {
Options {
@ -1111,7 +1127,7 @@ impl Options {
}
pub fn file_path_mapping(&self) -> FilePathMapping {
FilePathMapping::new(self.remap_path_prefix.clone())
file_path_mapping(self.remap_path_prefix.clone(), &self.unstable_opts)
}
/// Returns `true` if there will be an output file generated.
@ -2893,7 +2909,7 @@ pub fn build_session_options(
handler.early_error(format!("Current directory is invalid: {e}"));
});
let remap = FilePathMapping::new(remap_path_prefix.clone());
let remap = file_path_mapping(remap_path_prefix.clone(), &unstable_opts);
let (path, remapped) = remap.map_prefix(&working_dir);
let working_dir = if remapped {
RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) }

View file

@ -1,7 +1,8 @@
use crate::code_stats::CodeStats;
pub use crate::code_stats::{DataTypeKind, FieldInfo, FieldKind, SizeKind, VariantInfo};
use crate::config::{
self, CrateType, InstrumentCoverage, OptLevel, OutFileName, OutputType, SwitchWithOptPath,
self, CrateType, InstrumentCoverage, OptLevel, OutFileName, OutputType,
RemapPathScopeComponents, SwitchWithOptPath,
};
use crate::config::{ErrorOutputType, Input};
use crate::errors;
@ -254,7 +255,11 @@ impl Session {
pub fn local_crate_source_file(&self) -> Option<PathBuf> {
let path = self.io.input.opt_path()?;
Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned())
if self.should_prefer_remapped_for_codegen() {
Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned())
} else {
Some(path.to_path_buf())
}
}
fn check_miri_unleashed_features(&self) {
@ -1243,6 +1248,53 @@ impl Session {
pub fn link_dead_code(&self) -> bool {
self.opts.cg.link_dead_code.unwrap_or(false)
}
pub fn should_prefer_remapped_for_codegen(&self) -> bool {
// bail out, if any of the requested crate types aren't:
// "compiled executables or libraries"
for crate_type in &self.opts.crate_types {
match crate_type {
CrateType::Executable
| CrateType::Dylib
| CrateType::Rlib
| CrateType::Staticlib
| CrateType::Cdylib => continue,
CrateType::ProcMacro => return false,
}
}
let has_split_debuginfo = match self.split_debuginfo() {
SplitDebuginfo::Off => false,
SplitDebuginfo::Packed => true,
SplitDebuginfo::Unpacked => true,
};
let remap_path_scopes = &self.opts.unstable_opts.remap_path_scope;
let mut prefer_remapped = false;
if remap_path_scopes.contains(RemapPathScopeComponents::UNSPLIT_DEBUGINFO) {
prefer_remapped |= !has_split_debuginfo;
}
if remap_path_scopes.contains(RemapPathScopeComponents::SPLIT_DEBUGINFO) {
prefer_remapped |= has_split_debuginfo;
}
prefer_remapped
}
pub fn should_prefer_remapped_for_split_debuginfo_paths(&self) -> bool {
let has_split_debuginfo = match self.split_debuginfo() {
SplitDebuginfo::Off => false,
SplitDebuginfo::Packed | SplitDebuginfo::Unpacked => true,
};
self.opts
.unstable_opts
.remap_path_scope
.contains(RemapPathScopeComponents::SPLIT_DEBUGINFO_PATH)
&& has_split_debuginfo
}
}
// JUSTIFICATION: part of session construction
@ -1752,3 +1804,53 @@ fn mk_emitter(output: ErrorOutputType) -> Box<DynEmitter> {
};
emitter
}
pub trait RemapFileNameExt {
type Output<'a>
where
Self: 'a;
fn for_scope(&self, sess: &Session, scopes: RemapPathScopeComponents) -> Self::Output<'_>;
fn for_codegen(&self, sess: &Session) -> Self::Output<'_>;
}
impl RemapFileNameExt for rustc_span::FileName {
type Output<'a> = rustc_span::FileNameDisplay<'a>;
fn for_scope(&self, sess: &Session, scopes: RemapPathScopeComponents) -> Self::Output<'_> {
if sess.opts.unstable_opts.remap_path_scope.contains(scopes) {
self.prefer_remapped_unconditionaly()
} else {
self.prefer_local()
}
}
fn for_codegen(&self, sess: &Session) -> Self::Output<'_> {
if sess.should_prefer_remapped_for_codegen() {
self.prefer_remapped_unconditionaly()
} else {
self.prefer_local()
}
}
}
impl RemapFileNameExt for rustc_span::RealFileName {
type Output<'a> = &'a Path;
fn for_scope(&self, sess: &Session, scopes: RemapPathScopeComponents) -> Self::Output<'_> {
if sess.opts.unstable_opts.remap_path_scope.contains(scopes) {
self.remapped_path_if_available()
} else {
self.local_path_if_available()
}
}
fn for_codegen(&self, sess: &Session) -> Self::Output<'_> {
if sess.should_prefer_remapped_for_codegen() {
self.remapped_path_if_available()
} else {
self.local_path_if_available()
}
}
}