Skip to content

Commit 43247de

Browse files
authored
Rollup merge of rust-lang#129687 - Urgau:rfc3127-sysroot-2, r=jieyouxu
Implement RFC3137 trim-paths sysroot changes - take 2 This PR is a continuation of rust-lang#118149. Nothing really changed, except for rust-lang#129408 which I was able to trigger locally. Original description: > Implement parts of rust-lang#111540 > > Right now, backtraces into sysroot always shows /rustc/$hash in diagnostics, e.g. > > ``` > thread 'main' panicked at 'hello world', map-panic.rs:2:50 > stack backtrace: > 0: std::panicking::begin_panic > at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/std/src/panicking.rs:616:12 > 1: map_panic::main::{{closure}} > at ./map-panic.rs:2:50 > 2: core::option::Option<T>::map > at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/option.rs:929:29 > 3: map_panic::main > at ./map-panic.rs:2:30 > 4: core::ops::function::FnOnce::call_once > at /rustc/a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52/library/core/src/ops/function.rs:248:5 > note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace. > ``` > > [RFC 3127 said](https://rust-lang.github.io/rfcs/3127-trim-paths.html#changing-handling-of-sysroot-path-in-rustc) > > > We want to change this behaviour such that, when rust-src source files can be discovered, the virtual path is discarded and therefore the local path will be embedded, unless there is a --remap-path-prefix that causes this local path to be remapped in the usual way. > > This PR implements this behaviour. When `rust-src` is present at compile time, rustc replaces /rustc/$hash with a real path into local rust-src with best effort. To sanitise this, users must explicitly supply `--remap-path-prefix=<path to rust-src>=foo`. cc `@cbeuw` Fix rust-lang#105907 Fix rust-lang#85463 try-job: dist-x86_64-linux try-job: x86_64-msvc try-job: armhf-gnu
2 parents 0399709 + cac4a30 commit 43247de

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
@@ -1622,56 +1622,63 @@ impl<'a> CrateMetadataRef<'a> {
16221622
);
16231623

16241624
for virtual_dir in virtual_rust_source_base_dir.iter().flatten() {
1625-
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir {
1626-
if let rustc_span::FileName::Real(old_name) = name {
1627-
if let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1628-
old_name
1629-
{
1630-
if let Ok(rest) = virtual_name.strip_prefix(virtual_dir) {
1631-
let virtual_name = virtual_name.clone();
1632-
1633-
// The std library crates are in
1634-
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1635-
// may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1636-
// detect crates from the std libs and handle them specially.
1637-
const STD_LIBS: &[&str] = &[
1638-
"core",
1639-
"alloc",
1640-
"std",
1641-
"test",
1642-
"term",
1643-
"unwind",
1644-
"proc_macro",
1645-
"panic_abort",
1646-
"panic_unwind",
1647-
"profiler_builtins",
1648-
"rtstartup",
1649-
"rustc-std-workspace-core",
1650-
"rustc-std-workspace-alloc",
1651-
"rustc-std-workspace-std",
1652-
"backtrace",
1653-
];
1654-
let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l));
1655-
1656-
let new_path = if is_std_lib {
1657-
real_dir.join("library").join(rest)
1658-
} else {
1659-
real_dir.join(rest)
1660-
};
1661-
1662-
debug!(
1663-
"try_to_translate_virtual_to_real: `{}` -> `{}`",
1664-
virtual_name.display(),
1665-
new_path.display(),
1666-
);
1667-
let new_name = rustc_span::RealFileName::Remapped {
1668-
local_path: Some(new_path),
1669-
virtual_name,
1670-
};
1671-
*old_name = new_name;
1672-
}
1625+
if let Some(real_dir) = &sess.opts.real_rust_source_base_dir
1626+
&& let rustc_span::FileName::Real(old_name) = name
1627+
&& let rustc_span::RealFileName::Remapped { local_path: _, virtual_name } =
1628+
old_name
1629+
&& let Ok(rest) = virtual_name.strip_prefix(virtual_dir)
1630+
{
1631+
// The std library crates are in
1632+
// `$sysroot/lib/rustlib/src/rust/library`, whereas other crates
1633+
// may be in `$sysroot/lib/rustlib/src/rust/` directly. So we
1634+
// detect crates from the std libs and handle them specially.
1635+
const STD_LIBS: &[&str] = &[
1636+
"core",
1637+
"alloc",
1638+
"std",
1639+
"test",
1640+
"term",
1641+
"unwind",
1642+
"proc_macro",
1643+
"panic_abort",
1644+
"panic_unwind",
1645+
"profiler_builtins",
1646+
"rtstartup",
1647+
"rustc-std-workspace-core",
1648+
"rustc-std-workspace-alloc",
1649+
"rustc-std-workspace-std",
1650+
"backtrace",
1651+
];
1652+
let is_std_lib = STD_LIBS.iter().any(|l| rest.starts_with(l));
1653+
1654+
let new_path = if is_std_lib {
1655+
real_dir.join("library").join(rest)
1656+
} else {
1657+
real_dir.join(rest)
1658+
};
1659+
1660+
debug!(
1661+
"try_to_translate_virtual_to_real: `{}` -> `{}`",
1662+
virtual_name.display(),
1663+
new_path.display(),
1664+
);
1665+
1666+
// Check if the translated real path is affected by any user-requested
1667+
// remaps via --remap-path-prefix. Apply them if so.
1668+
// Note that this is a special case for imported rust-src paths specified by
1669+
// https://rust-lang.github.io/rfcs/3127-trim-paths.html#handling-sysroot-paths.
1670+
// Other imported paths are not currently remapped (see #66251).
1671+
let (user_remapped, applied) =
1672+
sess.source_map().path_mapping().map_prefix(&new_path);
1673+
let new_name = if applied {
1674+
rustc_span::RealFileName::Remapped {
1675+
local_path: Some(new_path.clone()),
1676+
virtual_name: user_remapped.to_path_buf(),
16731677
}
1674-
}
1678+
} else {
1679+
rustc_span::RealFileName::LocalPath(new_path)
1680+
};
1681+
*old_name = new_name;
16751682
}
16761683
}
16771684
};

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
@@ -2294,7 +2294,7 @@ impl<'test> TestCx<'test> {
22942294
}
22952295

22962296
let base_dir = Path::new("/rustc/FAKE_PREFIX");
2297-
// Paths into the libstd/libcore
2297+
// Fake paths into the libstd/libcore
22982298
normalize_path(&base_dir.join("library"), "$SRC_DIR");
22992299
// `ui-fulldeps` tests can show paths to the compiler source when testing macros from
23002300
// `rustc_macros`
@@ -2310,8 +2310,14 @@ impl<'test> TestCx<'test> {
23102310
// eg. /home/user/rust/build
23112311
normalize_path(parent_build_dir, "$BUILD_DIR");
23122312

2313-
// Paths into lib directory.
2314-
normalize_path(&parent_build_dir.parent().unwrap().join("lib"), "$LIB_DIR");
2313+
// Real paths into the libstd/libcore
2314+
let rust_src_dir = &self
2315+
.config
2316+
.sysroot_base
2317+
.join("lib/rustlib/src/rust")
2318+
.read_link()
2319+
.expect("lib/rustlib/src/rust in target is a symlink to checkout root");
2320+
normalize_path(&rust_src_dir.join("library"), "$SRC_DIR_REAL");
23152321

23162322
if json {
23172323
// 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)