From cd6ba986398eb4a65b7968aa71063645ab9c1156 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 20 Nov 2023 23:24:44 +0000 Subject: [PATCH 1/9] Output-normalise real path to /library --- src/tools/compiletest/src/runtest.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 055671afd14d3..4c1c08617e714 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -4205,7 +4205,7 @@ impl<'test> TestCx<'test> { } let base_dir = Path::new("/rustc/FAKE_PREFIX"); - // Paths into the libstd/libcore + // Fake paths into the libstd/libcore normalize_path(&base_dir.join("library"), "$SRC_DIR"); // `ui-fulldeps` tests can show paths to the compiler source when testing macros from // `rustc_macros` @@ -4221,8 +4221,8 @@ impl<'test> TestCx<'test> { // eg. /home/user/rust/build normalize_path(parent_build_dir, "$BUILD_DIR"); - // Paths into lib directory. - normalize_path(&parent_build_dir.parent().unwrap().join("lib"), "$LIB_DIR"); + // Real paths into the libstd/libcore + normalize_path(&parent_build_dir.parent().unwrap().join("library"), "$SRC_DIR_REAL"); if json { // escaped newlines in json strings should be readable From e480904031064ee1c53e70cc38597d1dcf7a937b Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 20 Nov 2023 23:25:23 +0000 Subject: [PATCH 2/9] Allow {{rust-src-base}} in test headers --- src/tools/compiletest/src/header.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index f85f9e674ab12..5672db5c69194 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -1,5 +1,6 @@ use std::collections::HashSet; use std::env; +use std::fs::canonicalize; use std::fs::File; use std::io::prelude::*; use std::io::BufReader; @@ -795,6 +796,7 @@ fn expand_variables(mut value: String, config: &Config) -> String { const CWD: &str = "{{cwd}}"; const SRC_BASE: &str = "{{src-base}}"; const BUILD_BASE: &str = "{{build-base}}"; + const RUST_SRC_BASE: &str = "{{rust-src-base}}"; if value.contains(CWD) { let cwd = env::current_dir().unwrap(); @@ -809,6 +811,12 @@ fn expand_variables(mut value: String, config: &Config) -> String { value = value.replace(BUILD_BASE, &config.build_base.to_string_lossy()); } + if value.contains(RUST_SRC_BASE) { + let src = config.sysroot_base.join("lib/rustlib/src/rust"); + let canonical = canonicalize(&src).unwrap(); + value = value.replace(RUST_SRC_BASE, &canonical.to_string_lossy()); + } + value } From 090843aa03af466e4f92d971916d3626443c9850 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Tue, 21 Nov 2023 19:42:11 +0000 Subject: [PATCH 3/9] Implement RFC 3127 sysroot handling --- compiler/rustc_metadata/src/rmeta/decoder.rs | 94 +++++++++---------- tests/ui/errors/remap-path-prefix-sysroot.rs | 12 +++ ...-path-prefix-sysroot.with-remap.run.stderr | 36 +++++++ ...th-prefix-sysroot.without-remap.run.stderr | 36 +++++++ 4 files changed, 128 insertions(+), 50 deletions(-) create mode 100644 tests/ui/errors/remap-path-prefix-sysroot.rs create mode 100644 tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr create mode 100644 tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 24ab4f94d5c5b..8e5f6d8005926 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1597,56 +1597,50 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { ); for virtual_dir in virtual_rust_source_base_dir.iter().flatten() { - if let Some(real_dir) = &sess.opts.real_rust_source_base_dir { - if let rustc_span::FileName::Real(old_name) = name { - if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } = - old_name - { - if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) { - let virtual_name = virtual_name.clone(); - - // The std library crates are in - // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates - // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we - // detect crates from the std libs and handle them specially. - const STD_LIBS: &[&str] = &[ - "core", - "alloc", - "std", - "test", - "term", - "unwind", - "proc_macro", - "panic_abort", - "panic_unwind", - "profiler_builtins", - "rtstartup", - "rustc-std-workspace-core", - "rustc-std-workspace-alloc", - "rustc-std-workspace-std", - "backtrace", - ]; - let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l)); - - let new_path = if is_std_lib { - real_dir.join("library").join(rest) - } else { - real_dir.join(rest) - }; - - debug!( - "try_to_translate_virtual_to_real: `{}` -> `{}`", - virtual_name.display(), - new_path.display(), - ); - let new_name = rustc_span::RealFileName::Remapped { - local_path: Some(new_path), - virtual_name, - }; - *old_name = new_name; - } - } - } + if let Some(real_dir) = &sess.opts.real_rust_source_base_dir + && let rustc_span::FileName::Real(old_name) = name + && let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } = + old_name + && let Ok(rest) = virtual_name.strip_prefix(virtual_dir) + { + let virtual_name = virtual_name.clone(); + + // The std library crates are in + // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates + // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we + // detect crates from the std libs and handle them specially. + const STD_LIBS: &[&str] = &[ + "core", + "alloc", + "std", + "test", + "term", + "unwind", + "proc_macro", + "panic_abort", + "panic_unwind", + "profiler_builtins", + "rtstartup", + "rustc-std-workspace-core", + "rustc-std-workspace-alloc", + "rustc-std-workspace-std", + "backtrace", + ]; + let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l)); + + let new_path = if is_std_lib { + real_dir.join("library").join(rest) + } else { + real_dir.join(rest) + }; + + debug!( + "try_to_translate_virtual_to_real: `{}` -> `{}`", + virtual_name.display(), + new_path.display(), + ); + let new_name = rustc_span::RealFileName::LocalPath(new_path); + *old_name = new_name; } } }; diff --git a/tests/ui/errors/remap-path-prefix-sysroot.rs b/tests/ui/errors/remap-path-prefix-sysroot.rs new file mode 100644 index 0000000000000..1b8a9d1f0e6f1 --- /dev/null +++ b/tests/ui/errors/remap-path-prefix-sysroot.rs @@ -0,0 +1,12 @@ +// run-fail +// check-run-results + +// exec-env:RUST_BACKTRACE=full +// revisions: with-remap without-remap +// compile-flags: -g -Ztranslate-remapped-path-to-local-path=yes +// [with-remap]compile-flags: --remap-path-prefix={{rust-src-base}}=remapped +// [without-remap]compile-flags: + +fn main() { + Vec::::with_capacity(!0); +} diff --git a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr new file mode 100644 index 0000000000000..b54529fd0f5d6 --- /dev/null +++ b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr @@ -0,0 +1,36 @@ +thread 'main' panicked at library/alloc/src/raw_vec.rs:545:5: +capacity overflow +stack backtrace: + 0: 0x1056a1fb0 - ::fmt::h34ddcf7e122b89ac + 1: 0x1056f9714 - core::fmt::write::hd1aeea4ff96546ca + 2: 0x1056a59e4 - std::io::Write::write_fmt::h17b1090c1278c55b + 3: 0x1056a1e20 - std::sys_common::backtrace::print::hfafdd06e00730994 + 4: 0x1056c218c - std::panicking::default_hook::{{closure}}::h97a3d80e798f2ed6 + 5: 0x1056c1f6c - std::panicking::default_hook::hda0f04e04c8eb266 + 6: 0x1056c25d0 - std::panicking::rust_panic_with_hook::hf66d5a2176bf0f70 + 7: 0x1056a27f4 - std::panicking::begin_panic_handler::{{closure}}::haacaa3525d299c3a + 8: 0x1056a21e0 - std::sys_common::backtrace::__rust_end_short_backtrace::h87e6f3e754a75849 + 9: 0x1056c2334 - _rust_begin_unwind + 10: 0x1057167f8 - core::panicking::panic_fmt::h02ef70cf9926e63a + 11: 0x1056f45c4 - alloc::raw_vec::capacity_overflow::h4447458ef5d0325b + 12: 0x10498b250 - alloc::raw_vec::RawVec::allocate_in::he48752b99f53b509 + at remapped/library/alloc/src/raw_vec.rs:177:27 + 13: 0x10498adac - alloc::raw_vec::RawVec::with_capacity_in::h0f23d7d992348ac1 + at remapped/library/alloc/src/raw_vec.rs:130:9 + 14: 0x10498adac - alloc::vec::Vec::with_capacity_in::h8aa83d89b81dd67c + at remapped/library/alloc/src/vec/mod.rs:670:20 + 15: 0x10498adac - alloc::vec::Vec::with_capacity::h9bcc7d8009345416 + at remapped/library/alloc/src/vec/mod.rs:479:9 + 16: 0x10498b7ac - remap_path_prefix_sysroot::main::hc6dc31a7bd7adcd2 + at remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:11:5 + 17: 0x10498aa28 - core::ops::function::FnOnce::call_once::hc6cd7e6e6c0a39ec + at remapped/library/core/src/ops/function.rs:250:5 + 18: 0x10498a938 - std::sys_common::backtrace::__rust_begin_short_backtrace::h00cfadb0500687d9 + at remapped/library/std/src/sys_common/backtrace.rs:155:18 + 19: 0x10498a9cc - std::rt::lang_start::{{closure}}::hdaed3a6f285f5688 + at remapped/library/std/src/rt.rs:167:18 + 20: 0x1056c227c - std::panicking::try::hfb03667ecc24ab29 + 21: 0x1056ae9e4 - std::rt::lang_start_internal::he5e74d4e4d2cb01e + 22: 0x10498a998 - std::rt::lang_start::hdf7d4c5cfb24235f + at remapped/library/std/src/rt.rs:166:17 + 23: 0x10498b7e4 - _main diff --git a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr new file mode 100644 index 0000000000000..6670bb72da75a --- /dev/null +++ b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr @@ -0,0 +1,36 @@ +thread 'main' panicked at library/alloc/src/raw_vec.rs:545:5: +capacity overflow +stack backtrace: + 0: 0x103ce1fb0 - ::fmt::h34ddcf7e122b89ac + 1: 0x103d39714 - core::fmt::write::hd1aeea4ff96546ca + 2: 0x103ce59e4 - std::io::Write::write_fmt::h17b1090c1278c55b + 3: 0x103ce1e20 - std::sys_common::backtrace::print::hfafdd06e00730994 + 4: 0x103d0218c - std::panicking::default_hook::{{closure}}::h97a3d80e798f2ed6 + 5: 0x103d01f6c - std::panicking::default_hook::hda0f04e04c8eb266 + 6: 0x103d025d0 - std::panicking::rust_panic_with_hook::hf66d5a2176bf0f70 + 7: 0x103ce27f4 - std::panicking::begin_panic_handler::{{closure}}::haacaa3525d299c3a + 8: 0x103ce21e0 - std::sys_common::backtrace::__rust_end_short_backtrace::h87e6f3e754a75849 + 9: 0x103d02334 - _rust_begin_unwind + 10: 0x103d567f8 - core::panicking::panic_fmt::h02ef70cf9926e63a + 11: 0x103d345c4 - alloc::raw_vec::capacity_overflow::h4447458ef5d0325b + 12: 0x102fcb250 - alloc::raw_vec::RawVec::allocate_in::he48752b99f53b509 + at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL + 13: 0x102fcadac - alloc::raw_vec::RawVec::with_capacity_in::h0f23d7d992348ac1 + at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL + 14: 0x102fcadac - alloc::vec::Vec::with_capacity_in::h8aa83d89b81dd67c + at $SRC_DIR_REAL/alloc/src/vec/mod.rs:LL:COL + 15: 0x102fcadac - alloc::vec::Vec::with_capacity::h9bcc7d8009345416 + at $SRC_DIR_REAL/alloc/src/vec/mod.rs:LL:COL + 16: 0x102fcb7ac - remap_path_prefix_sysroot::main::hc6dc31a7bd7adcd2 + at $DIR/remap-path-prefix-sysroot.rs:11:5 + 17: 0x102fcaa28 - core::ops::function::FnOnce::call_once::hc6cd7e6e6c0a39ec + at $SRC_DIR_REAL/core/src/ops/function.rs:LL:COL + 18: 0x102fca938 - std::sys_common::backtrace::__rust_begin_short_backtrace::h00cfadb0500687d9 + at $SRC_DIR_REAL/std/src/sys_common/backtrace.rs:LL:COL + 19: 0x102fca9cc - std::rt::lang_start::{{closure}}::hdaed3a6f285f5688 + at $SRC_DIR_REAL/std/src/rt.rs:LL:COL + 20: 0x103d0227c - std::panicking::try::hfb03667ecc24ab29 + 21: 0x103cee9e4 - std::rt::lang_start_internal::he5e74d4e4d2cb01e + 22: 0x102fca998 - std::rt::lang_start::hdf7d4c5cfb24235f + at $SRC_DIR_REAL/std/src/rt.rs:LL:COL + 23: 0x102fcb7e4 - _main From bd4114e8d1a2bc747bf7d93f2903ee2bf7e5bbd9 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 11 Dec 2023 22:35:41 +0000 Subject: [PATCH 4/9] Remove unnecessary clone --- compiler/rustc_metadata/src/rmeta/decoder.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 8e5f6d8005926..48f1df54829c8 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1603,8 +1603,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { old_name && let Ok(rest) = virtual_name.strip_prefix(virtual_dir) { - let virtual_name = virtual_name.clone(); - // The std library crates are in // `$sysroot/lib/rustlib/src/rust/library`, whereas other crates // may be in `$sysroot/lib/rustlib/src/rust/` directly. So we From fde286a7928d8092361c3fda65740054865d2c4a Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Mon, 11 Dec 2023 22:45:24 +0000 Subject: [PATCH 5/9] Update test --- tests/ui/errors/remap-path-prefix-sysroot.rs | 18 ++++++---- ...-path-prefix-sysroot.with-remap.run.stderr | 36 ------------------- ...emap-path-prefix-sysroot.with-remap.stderr | 17 +++++++++ ...th-prefix-sysroot.without-remap.run.stderr | 36 ------------------- ...p-path-prefix-sysroot.without-remap.stderr | 17 +++++++++ 5 files changed, 46 insertions(+), 78 deletions(-) delete mode 100644 tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr create mode 100644 tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr delete mode 100644 tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr create mode 100644 tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr diff --git a/tests/ui/errors/remap-path-prefix-sysroot.rs b/tests/ui/errors/remap-path-prefix-sysroot.rs index 1b8a9d1f0e6f1..d8ff666110226 100644 --- a/tests/ui/errors/remap-path-prefix-sysroot.rs +++ b/tests/ui/errors/remap-path-prefix-sysroot.rs @@ -1,12 +1,18 @@ -// run-fail -// check-run-results - -// exec-env:RUST_BACKTRACE=full // revisions: with-remap without-remap // compile-flags: -g -Ztranslate-remapped-path-to-local-path=yes // [with-remap]compile-flags: --remap-path-prefix={{rust-src-base}}=remapped // [without-remap]compile-flags: +// error-pattern: E0507 + +use std::thread; +struct Worker { + thread: thread::JoinHandle<()>, +} -fn main() { - Vec::::with_capacity(!0); +impl Drop for Worker { + fn drop(&mut self) { + self.thread.join().unwrap(); + } } + +pub fn main(){} diff --git a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr deleted file mode 100644 index b54529fd0f5d6..0000000000000 --- a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.run.stderr +++ /dev/null @@ -1,36 +0,0 @@ -thread 'main' panicked at library/alloc/src/raw_vec.rs:545:5: -capacity overflow -stack backtrace: - 0: 0x1056a1fb0 - ::fmt::h34ddcf7e122b89ac - 1: 0x1056f9714 - core::fmt::write::hd1aeea4ff96546ca - 2: 0x1056a59e4 - std::io::Write::write_fmt::h17b1090c1278c55b - 3: 0x1056a1e20 - std::sys_common::backtrace::print::hfafdd06e00730994 - 4: 0x1056c218c - std::panicking::default_hook::{{closure}}::h97a3d80e798f2ed6 - 5: 0x1056c1f6c - std::panicking::default_hook::hda0f04e04c8eb266 - 6: 0x1056c25d0 - std::panicking::rust_panic_with_hook::hf66d5a2176bf0f70 - 7: 0x1056a27f4 - std::panicking::begin_panic_handler::{{closure}}::haacaa3525d299c3a - 8: 0x1056a21e0 - std::sys_common::backtrace::__rust_end_short_backtrace::h87e6f3e754a75849 - 9: 0x1056c2334 - _rust_begin_unwind - 10: 0x1057167f8 - core::panicking::panic_fmt::h02ef70cf9926e63a - 11: 0x1056f45c4 - alloc::raw_vec::capacity_overflow::h4447458ef5d0325b - 12: 0x10498b250 - alloc::raw_vec::RawVec::allocate_in::he48752b99f53b509 - at remapped/library/alloc/src/raw_vec.rs:177:27 - 13: 0x10498adac - alloc::raw_vec::RawVec::with_capacity_in::h0f23d7d992348ac1 - at remapped/library/alloc/src/raw_vec.rs:130:9 - 14: 0x10498adac - alloc::vec::Vec::with_capacity_in::h8aa83d89b81dd67c - at remapped/library/alloc/src/vec/mod.rs:670:20 - 15: 0x10498adac - alloc::vec::Vec::with_capacity::h9bcc7d8009345416 - at remapped/library/alloc/src/vec/mod.rs:479:9 - 16: 0x10498b7ac - remap_path_prefix_sysroot::main::hc6dc31a7bd7adcd2 - at remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:11:5 - 17: 0x10498aa28 - core::ops::function::FnOnce::call_once::hc6cd7e6e6c0a39ec - at remapped/library/core/src/ops/function.rs:250:5 - 18: 0x10498a938 - std::sys_common::backtrace::__rust_begin_short_backtrace::h00cfadb0500687d9 - at remapped/library/std/src/sys_common/backtrace.rs:155:18 - 19: 0x10498a9cc - std::rt::lang_start::{{closure}}::hdaed3a6f285f5688 - at remapped/library/std/src/rt.rs:167:18 - 20: 0x1056c227c - std::panicking::try::hfb03667ecc24ab29 - 21: 0x1056ae9e4 - std::rt::lang_start_internal::he5e74d4e4d2cb01e - 22: 0x10498a998 - std::rt::lang_start::hdf7d4c5cfb24235f - at remapped/library/std/src/rt.rs:166:17 - 23: 0x10498b7e4 - _main diff --git a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr new file mode 100644 index 0000000000000..59bbfffedaf9f --- /dev/null +++ b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr @@ -0,0 +1,17 @@ +error[E0507]: cannot move out of `self.thread` which is behind a mutable reference + --> remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:14:9 + | +LL | self.thread.join().unwrap(); + | ^^^^^^^^^^^ ------ `self.thread` moved due to this method call + | | + | move occurs because `self.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait + | +note: `JoinHandle::::join` takes ownership of the receiver `self`, which moves `self.thread` + --> remapped/library/std/src/thread/mod.rs:LL:COL + | +LL | pub fn join(self) -> Result { + | ^^^^ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0507`. diff --git a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr deleted file mode 100644 index 6670bb72da75a..0000000000000 --- a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.run.stderr +++ /dev/null @@ -1,36 +0,0 @@ -thread 'main' panicked at library/alloc/src/raw_vec.rs:545:5: -capacity overflow -stack backtrace: - 0: 0x103ce1fb0 - ::fmt::h34ddcf7e122b89ac - 1: 0x103d39714 - core::fmt::write::hd1aeea4ff96546ca - 2: 0x103ce59e4 - std::io::Write::write_fmt::h17b1090c1278c55b - 3: 0x103ce1e20 - std::sys_common::backtrace::print::hfafdd06e00730994 - 4: 0x103d0218c - std::panicking::default_hook::{{closure}}::h97a3d80e798f2ed6 - 5: 0x103d01f6c - std::panicking::default_hook::hda0f04e04c8eb266 - 6: 0x103d025d0 - std::panicking::rust_panic_with_hook::hf66d5a2176bf0f70 - 7: 0x103ce27f4 - std::panicking::begin_panic_handler::{{closure}}::haacaa3525d299c3a - 8: 0x103ce21e0 - std::sys_common::backtrace::__rust_end_short_backtrace::h87e6f3e754a75849 - 9: 0x103d02334 - _rust_begin_unwind - 10: 0x103d567f8 - core::panicking::panic_fmt::h02ef70cf9926e63a - 11: 0x103d345c4 - alloc::raw_vec::capacity_overflow::h4447458ef5d0325b - 12: 0x102fcb250 - alloc::raw_vec::RawVec::allocate_in::he48752b99f53b509 - at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL - 13: 0x102fcadac - alloc::raw_vec::RawVec::with_capacity_in::h0f23d7d992348ac1 - at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL - 14: 0x102fcadac - alloc::vec::Vec::with_capacity_in::h8aa83d89b81dd67c - at $SRC_DIR_REAL/alloc/src/vec/mod.rs:LL:COL - 15: 0x102fcadac - alloc::vec::Vec::with_capacity::h9bcc7d8009345416 - at $SRC_DIR_REAL/alloc/src/vec/mod.rs:LL:COL - 16: 0x102fcb7ac - remap_path_prefix_sysroot::main::hc6dc31a7bd7adcd2 - at $DIR/remap-path-prefix-sysroot.rs:11:5 - 17: 0x102fcaa28 - core::ops::function::FnOnce::call_once::hc6cd7e6e6c0a39ec - at $SRC_DIR_REAL/core/src/ops/function.rs:LL:COL - 18: 0x102fca938 - std::sys_common::backtrace::__rust_begin_short_backtrace::h00cfadb0500687d9 - at $SRC_DIR_REAL/std/src/sys_common/backtrace.rs:LL:COL - 19: 0x102fca9cc - std::rt::lang_start::{{closure}}::hdaed3a6f285f5688 - at $SRC_DIR_REAL/std/src/rt.rs:LL:COL - 20: 0x103d0227c - std::panicking::try::hfb03667ecc24ab29 - 21: 0x103cee9e4 - std::rt::lang_start_internal::he5e74d4e4d2cb01e - 22: 0x102fca998 - std::rt::lang_start::hdf7d4c5cfb24235f - at $SRC_DIR_REAL/std/src/rt.rs:LL:COL - 23: 0x102fcb7e4 - _main diff --git a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr new file mode 100644 index 0000000000000..ae674ee2990e6 --- /dev/null +++ b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr @@ -0,0 +1,17 @@ +error[E0507]: cannot move out of `self.thread` which is behind a mutable reference + --> $DIR/remap-path-prefix-sysroot.rs:14:9 + | +LL | self.thread.join().unwrap(); + | ^^^^^^^^^^^ ------ `self.thread` moved due to this method call + | | + | move occurs because `self.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait + | +note: `JoinHandle::::join` takes ownership of the receiver `self`, which moves `self.thread` + --> $SRC_DIR_REAL/std/src/thread/mod.rs:LL:COL + | +LL | pub fn join(self) -> Result { + | ^^^^ + +error: aborting due to 1 previous error + +For more information about this error, try `rustc --explain E0507`. From 5b31e0018be58fc67e2e614d737c40eb1ed42dcf Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Tue, 12 Dec 2023 23:31:54 +0000 Subject: [PATCH 6/9] Explicitly remap translated real path --- compiler/rustc_metadata/src/rmeta/decoder.rs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 48f1df54829c8..8e90487367329 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1637,7 +1637,22 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { virtual_name.display(), new_path.display(), ); - let new_name = rustc_span::RealFileName::LocalPath(new_path); + + // Check if the translated real path is affected by any user-requested + // remaps via --remap-path-prefix. Apply them if so. + // Note that this is a special case for imported rust-src paths specified by + // https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths. + // Other imported paths are not currently remapped (see #66251). + let (user_remapped, applied) = + sess.source_map().path_mapping().map_prefix(&new_path); + let new_name = if applied { + rustc_span::RealFileName::Remapped { + local_path: Some(new_path.clone()), + virtual_name: user_remapped.to_path_buf(), + } + } else { + rustc_span::RealFileName::LocalPath(new_path) + }; *old_name = new_name; } } From ff5934f85cfb98e16e7dea2cb3ca7f10620b2955 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Tue, 12 Dec 2023 23:34:08 +0000 Subject: [PATCH 7/9] Update tests --- tests/ui/errors/remap-path-prefix-sysroot.rs | 5 +++++ tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr | 2 +- .../ui/errors/remap-path-prefix-sysroot.without-remap.stderr | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/ui/errors/remap-path-prefix-sysroot.rs b/tests/ui/errors/remap-path-prefix-sysroot.rs index d8ff666110226..a6a1360de86b6 100644 --- a/tests/ui/errors/remap-path-prefix-sysroot.rs +++ b/tests/ui/errors/remap-path-prefix-sysroot.rs @@ -4,6 +4,11 @@ // [without-remap]compile-flags: // error-pattern: E0507 +// The $SRC_DIR*.rs:LL:COL normalisation doesn't kick in automatically +// as the remapped revision will not begin with $SRC_DIR_REAL, +// so we have to do it ourselves. +// normalize-stderr-test: ".rs:\d+:\d+" -> ".rs:LL:COL" + use std::thread; struct Worker { thread: thread::JoinHandle<()>, diff --git a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr index 59bbfffedaf9f..bdb882e2df5a4 100644 --- a/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr +++ b/tests/ui/errors/remap-path-prefix-sysroot.with-remap.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of `self.thread` which is behind a mutable reference - --> remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:14:9 + --> remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:LL:COL | LL | self.thread.join().unwrap(); | ^^^^^^^^^^^ ------ `self.thread` moved due to this method call diff --git a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr index ae674ee2990e6..9b337699c3295 100644 --- a/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr +++ b/tests/ui/errors/remap-path-prefix-sysroot.without-remap.stderr @@ -1,5 +1,5 @@ error[E0507]: cannot move out of `self.thread` which is behind a mutable reference - --> $DIR/remap-path-prefix-sysroot.rs:14:9 + --> $DIR/remap-path-prefix-sysroot.rs:LL:COL | LL | self.thread.join().unwrap(); | ^^^^^^^^^^^ ------ `self.thread` moved due to this method call From b7eeb3c72abb3170f299f707436e0cc832fc8c1e Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Wed, 13 Dec 2023 00:08:15 +0000 Subject: [PATCH 8/9] Make $SRC_DIR_REAL normalisation more robust --- src/tools/compiletest/src/runtest.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 4c1c08617e714..78aef3c64e359 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -4222,7 +4222,9 @@ impl<'test> TestCx<'test> { normalize_path(parent_build_dir, "$BUILD_DIR"); // Real paths into the libstd/libcore - normalize_path(&parent_build_dir.parent().unwrap().join("library"), "$SRC_DIR_REAL"); + let rust_src_dir = + &self.config.sysroot_base.join("lib/rustlib/src/rust").canonicalize().unwrap(); + normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL"); if json { // escaped newlines in json strings should be readable From 0cba7c8a079568ee77baf74d7aa57d81c48e46f7 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Sat, 16 Dec 2023 14:49:13 +0000 Subject: [PATCH 9/9] Resolve link once instead of canonicalize --- src/tools/compiletest/src/header.rs | 10 ++++++---- src/tools/compiletest/src/runtest.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/tools/compiletest/src/header.rs b/src/tools/compiletest/src/header.rs index 5672db5c69194..9cdce9c2cbf24 100644 --- a/src/tools/compiletest/src/header.rs +++ b/src/tools/compiletest/src/header.rs @@ -1,6 +1,5 @@ use std::collections::HashSet; use std::env; -use std::fs::canonicalize; use std::fs::File; use std::io::prelude::*; use std::io::BufReader; @@ -812,9 +811,12 @@ fn expand_variables(mut value: String, config: &Config) -> String { } if value.contains(RUST_SRC_BASE) { - let src = config.sysroot_base.join("lib/rustlib/src/rust"); - let canonical = canonicalize(&src).unwrap(); - value = value.replace(RUST_SRC_BASE, &canonical.to_string_lossy()); + let src_base = config + .sysroot_base + .join("lib/rustlib/src/rust") + .read_link() + .expect("lib/rustlib/src/rust in target is a symlink to checkout root"); + value = value.replace(RUST_SRC_BASE, &src_base.to_string_lossy()); } value diff --git a/src/tools/compiletest/src/runtest.rs b/src/tools/compiletest/src/runtest.rs index 78aef3c64e359..d8e2c2e5c587f 100644 --- a/src/tools/compiletest/src/runtest.rs +++ b/src/tools/compiletest/src/runtest.rs @@ -4222,8 +4222,12 @@ impl<'test> TestCx<'test> { normalize_path(parent_build_dir, "$BUILD_DIR"); // Real paths into the libstd/libcore - let rust_src_dir = - &self.config.sysroot_base.join("lib/rustlib/src/rust").canonicalize().unwrap(); + let rust_src_dir = &self + .config + .sysroot_base + .join("lib/rustlib/src/rust") + .read_link() + .expect("lib/rustlib/src/rust in target is a symlink to checkout root"); normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL"); if json {