Skip to content

Commit a600fe8

Browse files
committed
Auto merge of #129687 - Urgau:rfc3127-sysroot-2, r=<try>
Implement RFC3137 trim-paths sysroot changes - take 2 Continuation of #118149 *Draft for the time being, to figure out if the previous issues have been fixed.* r? ghost try-job: dist-x86_64-linux try-job: x86_64-msvc try-job: armhf-gnu
2 parents 748c548 + cac4a30 commit a600fe8

File tree

6 files changed

+132
-52
lines changed

6 files changed

+132
-52
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+56-49
Original file line numberDiff line numberDiff line change
@@ -1650,56 +1650,63 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
16501650
);
16511651

16521652
for virtual_dir in virtual_rust_source_base_dir.iter().flatten() {
1653-
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
1654-
if let rustc_span::FileName::Real(old_name) = name {
1655-
if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1656-
old_name
1657-
{
1658-
if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) {
1659-
let virtual_name = virtual_name.clone();
1660-
1661-
// The std library crates are in
1662-
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1663-
// may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1664-
// detect crates from the std libs and handle them specially.
1665-
const STD_LIBS: &[&str] = &[
1666-
"core",
1667-
"alloc",
1668-
"std",
1669-
"test",
1670-
"term",
1671-
"unwind",
1672-
"proc_macro",
1673-
"panic_abort",
1674-
"panic_unwind",
1675-
"profiler_builtins",
1676-
"rtstartup",
1677-
"rustc-std-workspace-core",
1678-
"rustc-std-workspace-alloc",
1679-
"rustc-std-workspace-std",
1680-
"backtrace",
1681-
];
1682-
let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l));
1683-
1684-
let new_path = if is_std_lib {
1685-
real_dir.join("library").join(rest)
1686-
} else {
1687-
real_dir.join(rest)
1688-
};
1689-
1690-
debug!(
1691-
"try_to_translate_virtual_to_real: `{}` -> `{}`",
1692-
virtual_name.display(),
1693-
new_path.display(),
1694-
);
1695-
let new_name = rustc_span::RealFileName::Remapped {
1696-
local_path: Some(new_path),
1697-
virtual_name,
1698-
};
1699-
*old_name = new_name;
1700-
}
1653+
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir
1654+
&& let rustc_span::FileName::Real(old_name) = name
1655+
&& let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1656+
old_name
1657+
&& let Ok(rest) = virtual_name.strip_prefix(virtual_dir)
1658+
{
1659+
// The std library crates are in
1660+
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1661+
// may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1662+
// detect crates from the std libs and handle them specially.
1663+
const STD_LIBS: &[&str] = &[
1664+
"core",
1665+
"alloc",
1666+
"std",
1667+
"test",
1668+
"term",
1669+
"unwind",
1670+
"proc_macro",
1671+
"panic_abort",
1672+
"panic_unwind",
1673+
"profiler_builtins",
1674+
"rtstartup",
1675+
"rustc-std-workspace-core",
1676+
"rustc-std-workspace-alloc",
1677+
"rustc-std-workspace-std",
1678+
"backtrace",
1679+
];
1680+
let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l));
1681+
1682+
let new_path = if is_std_lib {
1683+
real_dir.join("library").join(rest)
1684+
} else {
1685+
real_dir.join(rest)
1686+
};
1687+
1688+
debug!(
1689+
"try_to_translate_virtual_to_real: `{}` -> `{}`",
1690+
virtual_name.display(),
1691+
new_path.display(),
1692+
);
1693+
1694+
// Check if the translated real path is affected by any user-requested
1695+
// remaps via --remap-path-prefix. Apply them if so.
1696+
// Note that this is a special case for imported rust-src paths specified by
1697+
// https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths.
1698+
// Other imported paths are not currently remapped (see #66251).
1699+
let (user_remapped, applied) =
1700+
sess.source_map().path_mapping().map_prefix(&new_path);
1701+
let new_name = if applied {
1702+
rustc_span::RealFileName::Remapped {
1703+
local_path: Some(new_path.clone()),
1704+
virtual_name: user_remapped.to_path_buf(),
17011705
}
1702-
}
1706+
} else {
1707+
rustc_span::RealFileName::LocalPath(new_path)
1708+
};
1709+
*old_name = new_name;
17031710
}
17041711
}
17051712
};

src/tools/compiletest/src/header.rs

+10
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,7 @@ fn expand_variables(mut value: String, config: &Config) -> String {
11151115
const CWD: &str = "{{cwd}}";
11161116
const SRC_BASE: &str = "{{src-base}}";
11171117
const BUILD_BASE: &str = "{{build-base}}";
1118+
const RUST_SRC_BASE: &str = "{{rust-src-base}}";
11181119
const SYSROOT_BASE: &str = "{{sysroot-base}}";
11191120
const TARGET_LINKER: &str = "{{target-linker}}";
11201121
const TARGET: &str = "{{target}}";
@@ -1144,6 +1145,15 @@ fn expand_variables(mut value: String, config: &Config) -> String {
11441145
value = value.replace(TARGET, &config.target);
11451146
}
11461147

1148+
if value.contains(RUST_SRC_BASE) {
1149+
let src_base = config
1150+
.sysroot_base
1151+
.join("lib/rustlib/src/rust")
1152+
.read_link()
1153+
.expect("lib/rustlib/src/rust in target is a symlink to checkout root");
1154+
value = value.replace(RUST_SRC_BASE, &src_base.to_string_lossy());
1155+
}
1156+
11471157
value
11481158
}
11491159

src/tools/compiletest/src/runtest.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -4301,7 +4301,7 @@ impl<'test> TestCx<'test> {
43014301
}
43024302

43034303
let base_dir = Path::new("/rustc/FAKE_PREFIX");
4304-
// Paths into the libstd/libcore
4304+
// Fake paths into the libstd/libcore
43054305
normalize_path(&base_dir.join("library"), "$SRC_DIR");
43064306
// `ui-fulldeps` tests can show paths to the compiler source when testing macros from
43074307
// `rustc_macros`
@@ -4317,8 +4317,14 @@ impl<'test> TestCx<'test> {
43174317
// eg. /home/user/rust/build
43184318
normalize_path(parent_build_dir, "$BUILD_DIR");
43194319

4320-
// Paths into lib directory.
4321-
normalize_path(&parent_build_dir.parent().unwrap().join("lib"), "$LIB_DIR");
4320+
// Real paths into the libstd/libcore
4321+
let rust_src_dir = &self
4322+
.config
4323+
.sysroot_base
4324+
.join("lib/rustlib/src/rust")
4325+
.read_link()
4326+
.expect("lib/rustlib/src/rust in target is a symlink to checkout root");
4327+
normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
43224328

43234329
if json {
43244330
// escaped newlines in json strings should be readable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ revisions: with-remap without-remap
2+
//@ compile-flags: -g -Ztranslate-remapped-path-to-local-path=yes
3+
//@ [with-remap]compile-flags: --remap-path-prefix={{rust-src-base}}=remapped
4+
//@ [without-remap]compile-flags:
5+
//@ error-pattern: E0507
6+
7+
// The $SRC_DIR*.rs:LL:COL normalisation doesn't kick in automatically
8+
// as the remapped revision will not begin with $SRC_DIR_REAL,
9+
// so we have to do it ourselves.
10+
//@ normalize-stderr-test: ".rs:\d+:\d+" -> ".rs:LL:COL"
11+
12+
use std::thread;
13+
struct Worker {
14+
thread: thread::JoinHandle<()>,
15+
}
16+
17+
impl Drop for Worker {
18+
fn drop(&mut self) {
19+
self.thread.join().unwrap();
20+
}
21+
}
22+
23+
pub fn main(){}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0507]: cannot move out of `self.thread` which is behind a mutable reference
2+
--> remapped/tests/ui/errors/remap-path-prefix-sysroot.rs:LL:COL
3+
|
4+
LL | self.thread.join().unwrap();
5+
| ^^^^^^^^^^^ ------ `self.thread` moved due to this method call
6+
| |
7+
| move occurs because `self.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
8+
|
9+
note: `JoinHandle::<T>::join` takes ownership of the receiver `self`, which moves `self.thread`
10+
--> remapped/library/std/src/thread/mod.rs:LL:COL
11+
|
12+
LL | pub fn join(self) -> Result<T> {
13+
| ^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0507`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0507]: cannot move out of `self.thread` which is behind a mutable reference
2+
--> $DIR/remap-path-prefix-sysroot.rs:LL:COL
3+
|
4+
LL | self.thread.join().unwrap();
5+
| ^^^^^^^^^^^ ------ `self.thread` moved due to this method call
6+
| |
7+
| move occurs because `self.thread` has type `JoinHandle<()>`, which does not implement the `Copy` trait
8+
|
9+
note: `JoinHandle::<T>::join` takes ownership of the receiver `self`, which moves `self.thread`
10+
--> $SRC_DIR_REAL/std/src/thread/mod.rs:LL:COL
11+
|
12+
LL | pub fn join(self) -> Result<T> {
13+
| ^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)