Skip to content

Commit d3fdf1d

Browse files
committed
Remap paths from other crates
Previously, `remap-path-prefix` only applied to files loaded *in the current session*. File names loaded in a previous invocation were used unchanged. This is unfortunate for UI testing when some of the crates involved are in the standard library (we don't want to recompile the standard library for arbitrary tests). Change it to apply to existing files as well. I believe this will eventually allow removing the special-casing for `-Z simulate-remapped-rust-src-base`.
1 parent 4b85902 commit d3fdf1d

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1726,6 +1726,12 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
17261726
// on `try_to_translate_virtual_to_real`).
17271727
try_to_translate_virtual_to_real(&mut name);
17281728

1729+
// We may have had different remap-path-prefix flags at the time this file was loaded.
1730+
// Apply the remapping for the current session.
1731+
// NOTE: this does not "undo and redo" the mapping - any existing remapping from the old
1732+
// crate is retained unmodified. Only files which were never remapped are considered.
1733+
name = sess.source_map().path_mapping().map_filename_prefix(&name).0;
1734+
17291735
let local_version = sess.source_map().new_imported_source_file(
17301736
name,
17311737
src_hash,

compiler/rustc_span/src/source_map.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -1147,9 +1147,12 @@ impl FilePathMapping {
11471147
}
11481148
}
11491149

1150-
fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
1150+
/// Given a `file`, map it using the `remap-path-prefix` options for the current [`Session`].
1151+
///
1152+
/// Public for use in rustc_metadata::decoder
1153+
pub fn map_filename_prefix(&self, file: &FileName) -> (FileName, bool) {
11511154
match file {
1152-
FileName::Real(realfile) if let RealFileName::LocalPath(local_path) = realfile => {
1155+
FileName::Real(realfile @ RealFileName::LocalPath(local_path)) => {
11531156
let (mapped_path, mapped) = self.map_prefix(local_path);
11541157
let realfile = if mapped {
11551158
RealFileName::Remapped {
@@ -1161,7 +1164,13 @@ impl FilePathMapping {
11611164
};
11621165
(FileName::Real(realfile), mapped)
11631166
}
1164-
FileName::Real(_) => unreachable!("attempted to remap an already remapped filename"),
1167+
existing @ FileName::Real(RealFileName::Remapped { .. }) => {
1168+
// This can happen if we are remapping the name of file that was loaded in a
1169+
// different Session, and inconsistent `remap-path-prefix` flags were passed between
1170+
// sessions. It's unclear what to do here. For now, respect the original flag, not
1171+
// the flag from the current session.
1172+
(existing.clone(), false)
1173+
}
11651174
other => (other.clone(), false),
11661175
}
11671176
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
include ../tools.mk
3+
4+
all: inconsistent-mapping last-crate-only
5+
6+
last-crate-only:
7+
$(RUSTC) is_true.rs --crate-type lib
8+
$(RUSTC) main.rs --remap-path-prefix=$(CURDIR)=/TEST_DIR 2>$(TMPDIR)/last-crate.txt --extern is_true=$(TMPDIR)/libis_true.rlib || true
9+
$(RUSTC_TEST_OP) $(TMPDIR)/last-crate.txt expected-last-crate.txt
10+
$(CGREP) "/TEST_DIR" < $(TMPDIR)/last-crate.txt
11+
12+
inconsistent-mapping:
13+
$(RUSTC) is_true.rs --remap-path-prefix=$(CURDIR)=/TEST_DIR1 --crate-type lib
14+
$(RUSTC) main.rs --remap-path-prefix=$(CURDIR)=/TEST_DIR2 2>$(TMPDIR)/inconsistent.txt --extern is_true=$(TMPDIR)/libis_true.rlib || true
15+
$(RUSTC_TEST_OP) $(TMPDIR)/inconsistent.txt expected-inconsistent.txt
16+
$(CGREP) "/TEST_DIR1" < $(TMPDIR)/inconsistent.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0061]: this function takes 1 argument but 0 arguments were supplied
2+
--> main.rs:2:5
3+
|
4+
2 | is_true::query();
5+
| ^^^^^^^^^^^^^^-- an argument of type `bool` is missing
6+
|
7+
note: function defined here
8+
--> /TEST_DIR1/is_true.rs:1:8
9+
help: provide the argument
10+
|
11+
2 | is_true::query(/* bool */);
12+
| ~~~~~~~~~~~~
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0061`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0061]: this function takes 1 argument but 0 arguments were supplied
2+
--> main.rs:2:5
3+
|
4+
2 | is_true::query();
5+
| ^^^^^^^^^^^^^^-- an argument of type `bool` is missing
6+
|
7+
note: function defined here
8+
--> /TEST_DIR/is_true.rs:1:8
9+
|
10+
1 | pub fn query(_: bool) {}
11+
| ^^^^^
12+
help: provide the argument
13+
|
14+
2 | is_true::query(/* bool */);
15+
| ~~~~~~~~~~~~
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0061`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub fn query(_: bool) {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
is_true::query();
3+
}

0 commit comments

Comments
 (0)