Skip to content

Commit 1355559

Browse files
committed
Avoid an unnecessary allocation
1 parent 44ef075 commit 1355559

File tree

5 files changed

+28
-27
lines changed

5 files changed

+28
-27
lines changed

Diff for: compiler/rustc_save_analysis/src/span_utils.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@ impl<'a> SpanUtils<'a> {
1818
match &file.name {
1919
FileName::Real(RealFileName::LocalPath(path)) => {
2020
if path.is_absolute() {
21-
self.sess
22-
.source_map()
23-
.path_mapping()
24-
.map_prefix(path.into())
25-
.0
26-
.display()
27-
.to_string()
21+
self.sess.source_map().path_mapping().map_prefix(path).0.display().to_string()
2822
} else {
2923
self.sess
3024
.opts

Diff for: compiler/rustc_session/src/config.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -592,19 +592,19 @@ impl Input {
592592
}
593593
}
594594

595-
pub fn opt_path(&self) -> Option<PathBuf> {
595+
pub fn opt_path(&self) -> Option<&Path> {
596596
match self {
597-
Input::File(file) => Some(file.clone()),
597+
Input::File(file) => Some(file),
598598
Input::Str { name, .. } => match name {
599-
FileName::Real(real) => real.local_path().map(|p| p.to_owned()),
599+
FileName::Real(real) => real.local_path(),
600600
FileName::QuoteExpansion(_) => None,
601601
FileName::Anon(_) => None,
602602
FileName::MacroExpansion(_) => None,
603603
FileName::ProcMacroSourceCode(_) => None,
604604
FileName::CfgSpec(_) => None,
605605
FileName::CliCrateAttr(_) => None,
606606
FileName::Custom(_) => None,
607-
FileName::DocTest(path, _) => Some(path.to_owned()),
607+
FileName::DocTest(path, _) => Some(path),
608608
FileName::InlineAsm(_) => None,
609609
},
610610
}
@@ -2509,12 +2509,12 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options {
25092509
early_error(error_format, &format!("Current directory is invalid: {e}"));
25102510
});
25112511

2512-
let (path, remapped) =
2513-
FilePathMapping::new(remap_path_prefix.clone()).map_prefix(working_dir.clone());
2512+
let remap = FilePathMapping::new(remap_path_prefix.clone());
2513+
let (path, remapped) = remap.map_prefix(&working_dir);
25142514
let working_dir = if remapped {
2515-
RealFileName::Remapped { local_path: Some(working_dir), virtual_name: path }
2515+
RealFileName::Remapped { virtual_name: path.into_owned(), local_path: Some(working_dir) }
25162516
} else {
2517-
RealFileName::LocalPath(path)
2517+
RealFileName::LocalPath(path.into_owned())
25182518
};
25192519

25202520
Options {

Diff for: compiler/rustc_session/src/session.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl Session {
237237

238238
pub fn local_crate_source_file(&self) -> Option<PathBuf> {
239239
let path = self.io.input.opt_path()?;
240-
Some(self.opts.file_path_mapping().map_prefix(path).0)
240+
Some(self.opts.file_path_mapping().map_prefix(path).0.into_owned())
241241
}
242242

243243
fn check_miri_unleashed_features(&self) {

Diff for: compiler/rustc_span/src/source_map.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,8 @@ impl FilePathMapping {
11381138
/// Applies any path prefix substitution as defined by the mapping.
11391139
/// The return value is the remapped path and a boolean indicating whether
11401140
/// the path was affected by the mapping.
1141-
pub fn map_prefix(&self, path: PathBuf) -> (PathBuf, bool) {
1141+
pub fn map_prefix<'a>(&'a self, path: impl Into<Cow<'a, Path>>) -> (Cow<'a, Path>, bool) {
1142+
let path = path.into();
11421143
if path.as_os_str().is_empty() {
11431144
// Exit early if the path is empty and therefore there's nothing to remap.
11441145
// This is mostly to reduce spam for `RUSTC_LOG=[remap_path_prefix]`.
@@ -1148,7 +1149,10 @@ impl FilePathMapping {
11481149
return remap_path_prefix(&self.mapping, path);
11491150

11501151
#[instrument(level = "debug", skip(mapping), ret)]
1151-
fn remap_path_prefix(mapping: &[(PathBuf, PathBuf)], path: PathBuf) -> (PathBuf, bool) {
1152+
fn remap_path_prefix<'a>(
1153+
mapping: &'a [(PathBuf, PathBuf)],
1154+
path: Cow<'a, Path>,
1155+
) -> (Cow<'a, Path>, bool) {
11521156
// NOTE: We are iterating over the mapping entries from last to first
11531157
// because entries specified later on the command line should
11541158
// take precedence.
@@ -1163,9 +1167,9 @@ impl FilePathMapping {
11631167
// in remapped paths down the line.
11641168
// So, if we have an exact match, we just return that without a call
11651169
// to `Path::join()`.
1166-
to.clone()
1170+
to.into()
11671171
} else {
1168-
to.join(rest)
1172+
to.join(rest).into()
11691173
};
11701174
debug!("Match - remapped");
11711175

@@ -1183,11 +1187,11 @@ impl FilePathMapping {
11831187
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
11841188
match file {
11851189
FileName::Real(realfile) if let RealFileName::LocalPath(local_path) = realfile => {
1186-
let (mapped_path, mapped) = self.map_prefix(local_path.to_path_buf());
1190+
let (mapped_path, mapped) = self.map_prefix(local_path);
11871191
let realfile = if mapped {
11881192
RealFileName::Remapped {
11891193
local_path: Some(local_path.clone()),
1190-
virtual_name: mapped_path,
1194+
virtual_name: mapped_path.into_owned(),
11911195
}
11921196
} else {
11931197
realfile.clone()
@@ -1228,14 +1232,17 @@ impl FilePathMapping {
12281232
let (new_path, was_remapped) = self.map_prefix(unmapped_file_path);
12291233
if was_remapped {
12301234
// It was remapped, so don't modify further
1231-
return RealFileName::Remapped { local_path: None, virtual_name: new_path };
1235+
return RealFileName::Remapped {
1236+
local_path: None,
1237+
virtual_name: new_path.into_owned(),
1238+
};
12321239
}
12331240

12341241
if new_path.is_absolute() {
12351242
// No remapping has applied to this path and it is absolute,
12361243
// so the working directory cannot influence it either, so
12371244
// we are done.
1238-
return RealFileName::LocalPath(new_path);
1245+
return RealFileName::LocalPath(new_path.into_owned());
12391246
}
12401247

12411248
debug_assert!(new_path.is_relative());
@@ -1253,12 +1260,12 @@ impl FilePathMapping {
12531260
RealFileName::Remapped {
12541261
// Erase the actual path
12551262
local_path: None,
1256-
virtual_name: file_path_abs,
1263+
virtual_name: file_path_abs.into_owned(),
12571264
}
12581265
} else {
12591266
// No kind of remapping applied to this path, so
12601267
// we leave it as it is.
1261-
RealFileName::LocalPath(file_path_abs)
1268+
RealFileName::LocalPath(file_path_abs.into_owned())
12621269
}
12631270
}
12641271
RealFileName::Remapped {

Diff for: compiler/rustc_span/src/source_map/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ fn path_prefix_remapping_expand_to_absolute() {
387387
let working_directory = path("/foo");
388388
let working_directory = RealFileName::Remapped {
389389
local_path: Some(working_directory.clone()),
390-
virtual_name: mapping.map_prefix(working_directory).0,
390+
virtual_name: mapping.map_prefix(working_directory).0.into_owned(),
391391
};
392392

393393
assert_eq!(working_directory.remapped_path_if_available(), path("FOO"));

0 commit comments

Comments
 (0)