Skip to content

Commit

Permalink
Implement RFC 3127 sysroot handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeuw committed Nov 21, 2023
1 parent a013915 commit 4780f7d
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 50 deletions.
93 changes: 43 additions & 50 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Expand Up @@ -1605,56 +1605,49 @@ 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;
}
}
};
Expand Down
12 changes: 12 additions & 0 deletions 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::<String>::with_capacity(!0);
}
36 changes: 36 additions & 0 deletions 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 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::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<T,A>::allocate_in::he48752b99f53b509
at remapped/library/alloc/src/raw_vec.rs:177:27
13: 0x10498adac - alloc::raw_vec::RawVec<T,A>::with_capacity_in::h0f23d7d992348ac1
at remapped/library/alloc/src/raw_vec.rs:130:9
14: 0x10498adac - alloc::vec::Vec<T,A>::with_capacity_in::h8aa83d89b81dd67c
at remapped/library/alloc/src/vec/mod.rs:670:20
15: 0x10498adac - alloc::vec::Vec<T>::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
36 changes: 36 additions & 0 deletions 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 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::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<T,A>::allocate_in::he48752b99f53b509
at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL
13: 0x102fcadac - alloc::raw_vec::RawVec<T,A>::with_capacity_in::h0f23d7d992348ac1
at $SRC_DIR_REAL/alloc/src/raw_vec.rs:LL:COL
14: 0x102fcadac - alloc::vec::Vec<T,A>::with_capacity_in::h8aa83d89b81dd67c
at $SRC_DIR_REAL/alloc/src/vec/mod.rs:LL:COL
15: 0x102fcadac - alloc::vec::Vec<T>::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

0 comments on commit 4780f7d

Please sign in to comment.