1
Fork 0

Auto merge of #115214 - Urgau:rfc-3127-trim-paths, r=compiler-errors

Implement rustc part of RFC 3127 trim-paths

This PR implements (or at least tries to) [RFC 3127 trim-paths](https://github.com/rust-lang/rust/issues/111540), the rustc part. That is `-Zremap-path-scope` with all of it's components/scopes.

`@rustbot` label: +F-trim-paths
This commit is contained in:
bors 2023-10-19 19:09:29 +00:00
commit 94c4e5c411
34 changed files with 580 additions and 92 deletions

View file

@ -370,7 +370,7 @@ impl FileName {
}
}
pub fn prefer_remapped(&self) -> FileNameDisplay<'_> {
pub fn prefer_remapped_unconditionaly(&self) -> FileNameDisplay<'_> {
FileNameDisplay { inner: self, display_pref: FileNameDisplayPreference::Remapped }
}

View file

@ -1124,16 +1124,13 @@ pub struct FilePathMapping {
impl FilePathMapping {
pub fn empty() -> FilePathMapping {
FilePathMapping::new(Vec::new())
FilePathMapping::new(Vec::new(), FileNameDisplayPreference::Local)
}
pub fn new(mapping: Vec<(PathBuf, PathBuf)>) -> FilePathMapping {
let filename_display_for_diagnostics = if mapping.is_empty() {
FileNameDisplayPreference::Local
} else {
FileNameDisplayPreference::Remapped
};
pub fn new(
mapping: Vec<(PathBuf, PathBuf)>,
filename_display_for_diagnostics: FileNameDisplayPreference,
) -> FilePathMapping {
FilePathMapping { mapping, filename_display_for_diagnostics }
}
@ -1287,6 +1284,27 @@ impl FilePathMapping {
}
}
/// Expand a relative path to an absolute path **without** remapping taken into account.
///
/// The resulting `RealFileName` will have its `virtual_path` portion erased if
/// possible (i.e. if there's also a remapped path).
pub fn to_local_embeddable_absolute_path(
&self,
file_path: RealFileName,
working_directory: &RealFileName,
) -> RealFileName {
let file_path = file_path.local_path_if_available();
if file_path.is_absolute() {
// No remapping has applied to this path and it is absolute,
// so the working directory cannot influence it either, so
// we are done.
return RealFileName::LocalPath(file_path.to_path_buf());
}
debug_assert!(file_path.is_relative());
let working_directory = working_directory.local_path_if_available();
RealFileName::LocalPath(Path::new(working_directory).join(file_path))
}
/// Attempts to (heuristically) reverse a prefix mapping.
///
/// Returns [`Some`] if there is exactly one mapping where the "to" part is

View file

@ -351,7 +351,10 @@ fn reverse_map_prefix(mapping: &FilePathMapping, p: &str) -> Option<String> {
fn path_prefix_remapping() {
// Relative to relative
{
let mapping = &FilePathMapping::new(vec![(path("abc/def"), path("foo"))]);
let mapping = &FilePathMapping::new(
vec![(path("abc/def"), path("foo"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(map_path_prefix(mapping, "abc/def/src/main.rs"), path_str("foo/src/main.rs"));
assert_eq!(map_path_prefix(mapping, "abc/def"), path_str("foo"));
@ -359,7 +362,10 @@ fn path_prefix_remapping() {
// Relative to absolute
{
let mapping = &FilePathMapping::new(vec![(path("abc/def"), path("/foo"))]);
let mapping = &FilePathMapping::new(
vec![(path("abc/def"), path("/foo"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(map_path_prefix(mapping, "abc/def/src/main.rs"), path_str("/foo/src/main.rs"));
assert_eq!(map_path_prefix(mapping, "abc/def"), path_str("/foo"));
@ -367,7 +373,10 @@ fn path_prefix_remapping() {
// Absolute to relative
{
let mapping = &FilePathMapping::new(vec![(path("/abc/def"), path("foo"))]);
let mapping = &FilePathMapping::new(
vec![(path("/abc/def"), path("foo"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(map_path_prefix(mapping, "/abc/def/src/main.rs"), path_str("foo/src/main.rs"));
assert_eq!(map_path_prefix(mapping, "/abc/def"), path_str("foo"));
@ -375,7 +384,10 @@ fn path_prefix_remapping() {
// Absolute to absolute
{
let mapping = &FilePathMapping::new(vec![(path("/abc/def"), path("/foo"))]);
let mapping = &FilePathMapping::new(
vec![(path("/abc/def"), path("/foo"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(map_path_prefix(mapping, "/abc/def/src/main.rs"), path_str("/foo/src/main.rs"));
assert_eq!(map_path_prefix(mapping, "/abc/def"), path_str("/foo"));
@ -385,8 +397,10 @@ fn path_prefix_remapping() {
#[test]
fn path_prefix_remapping_expand_to_absolute() {
// "virtual" working directory is relative path
let mapping =
&FilePathMapping::new(vec![(path("/foo"), path("FOO")), (path("/bar"), path("BAR"))]);
let mapping = &FilePathMapping::new(
vec![(path("/foo"), path("FOO")), (path("/bar"), path("BAR"))],
FileNameDisplayPreference::Remapped,
);
let working_directory = path("/foo");
let working_directory = RealFileName::Remapped {
local_path: Some(working_directory.clone()),
@ -487,8 +501,10 @@ fn path_prefix_remapping_expand_to_absolute() {
fn path_prefix_remapping_reverse() {
// Ignores options without alphanumeric chars.
{
let mapping =
&FilePathMapping::new(vec![(path("abc"), path("/")), (path("def"), path("."))]);
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/")), (path("def"), path("."))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(reverse_map_prefix(mapping, "/hello.rs"), None);
assert_eq!(reverse_map_prefix(mapping, "./hello.rs"), None);
@ -496,20 +512,20 @@ fn path_prefix_remapping_reverse() {
// Returns `None` if multiple options match.
{
let mapping = &FilePathMapping::new(vec![
(path("abc"), path("/redacted")),
(path("def"), path("/redacted")),
]);
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/redacted")), (path("def"), path("/redacted"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(reverse_map_prefix(mapping, "/redacted/hello.rs"), None);
}
// Distinct reverse mappings.
{
let mapping = &FilePathMapping::new(vec![
(path("abc"), path("/redacted")),
(path("def/ghi"), path("/fake/dir")),
]);
let mapping = &FilePathMapping::new(
vec![(path("abc"), path("/redacted")), (path("def/ghi"), path("/fake/dir"))],
FileNameDisplayPreference::Remapped,
);
assert_eq!(
reverse_map_prefix(mapping, "/redacted/path/hello.rs"),