From 919b3913150e8cce662e03891efeafdde038b383 Mon Sep 17 00:00:00 2001 From: jyn Date: Sat, 22 Apr 2023 11:56:41 -0500 Subject: [PATCH 1/2] Decrease the indentation in `imported_source_file` --- compiler/rustc_metadata/src/rmeta/decoder.rs | 23 +++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index c9ab07f5f270f..5884b052eabab 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1467,19 +1467,16 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // `try_to_translate_virtual_to_real` don't have to worry about how the // compiler is bootstrapped. if let Some(virtual_dir) = &sess.opts.unstable_opts.simulate_remapped_rust_src_base - { - if let Some(real_dir) = &sess.opts.real_rust_source_base_dir { - for subdir in ["library", "compiler"] { - if let rustc_span::FileName::Real(ref mut old_name) = name { - if let rustc_span::RealFileName::LocalPath(local) = old_name { - if let Ok(rest) = local.strip_prefix(real_dir.join(subdir)) { - *old_name = rustc_span::RealFileName::Remapped { - local_path: None, - virtual_name: virtual_dir.join(subdir).join(rest), - }; - } - } - } + && let Some(real_dir) = &sess.opts.real_rust_source_base_dir + && let rustc_span::FileName::Real(ref mut old_name) = name + && let rustc_span::RealFileName::LocalPath(local) = old_name { + for subdir in ["library", "compiler"] { + if let Ok(rest) = local.strip_prefix(real_dir.join(subdir)) { + *old_name = rustc_span::RealFileName::Remapped { + local_path: None, + virtual_name: virtual_dir.join(subdir).join(rest), + }; + break; } } } From 8785615c894b9a315c2a114881c77be25a32253f Mon Sep 17 00:00:00 2001 From: jyn Date: Sat, 22 Apr 2023 14:24:32 -0500 Subject: [PATCH 2/2] Apply `simulate-remapped-rust-src-base` even `remap-debuginfo` is set in config.toml This is really a mess. Here is the situation before this change: - UI tests depend on not having `rust-src` available. In particular, is depending on the `note` being a single line and not showing the source code. - When `download-rustc` is disabled, we pass `-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX` `-Ztranslate-remapped-path-to-local-path=no`, which changes the diagnostic to something like ` --> /rustc/FAKE_PREFIX/library/alloc/src/collections/vec_deque/mod.rs:1657:12` - When `download-rustc` is enabled, we still pass those flags, but they no longer have an effect. Instead rustc emits diagnostic paths like this: ` --> /rustc/39c6804b92aa202369e402525cee329556bc1db0/library/alloc/src/collections/vec_deque/mod.rs:1657:12`. Notice how there's a real commit and not `FAKE_PREFIX`. This happens because we set `CFG_VIRTUAL_RUST_SOURCE_BASE_DIR` during bootstrapping for CI artifacts, and rustc previously didn't allow for `simulate-remapped` to affect paths that had already been remapped. - Pietro noticed this and decided the right thing was to normalize `/rustc/` to `$SRC_DIR` in compiletest: https://github.com/rust-lang/rust/pull/103969/commits/470423c3d2cde3a62d5d4ac23840d734e8145366 - After my change to `x test core`, which rebuilds stage 2 std from source so `build/stage2-std` and `build/stage2` use the same `.rlib` metadata, the compiler suddenly notices it has sources for `std` available and prints those in the diagnostic, causing the test to fail. This changes `simulate-remapped-rust-src-base` to support remapping paths that have already been remapped, unblocking download-rustc. Unfortunately, although this fixes the specific problem for download-rustc, it doesn't seem to affect all the compiler's diagnostics. In particular, various `mir-opt` tests are failing to respect `simulate-remapped-path-prefix` (I looked into fixing this but it seems non-trivial). As a result, we can't remove the normalization in compiletest that maps `/rustc/` to `$SRC_DIR`, so this change is currently untested anywhere except locally. --- compiler/rustc_metadata/src/rmeta/decoder.rs | 19 ++++++++++++------- tests/ui/track-diagnostics/track6.rs | 3 +++ tests/ui/track-diagnostics/track6.stderr | 4 ++-- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/compiler/rustc_metadata/src/rmeta/decoder.rs b/compiler/rustc_metadata/src/rmeta/decoder.rs index 5884b052eabab..c6f8d3aec05b4 100644 --- a/compiler/rustc_metadata/src/rmeta/decoder.rs +++ b/compiler/rustc_metadata/src/rmeta/decoder.rs @@ -1458,9 +1458,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { .. } = source_file_to_import; - // If this file is under $sysroot/lib/rustlib/src/ but has not been remapped - // during rust bootstrapping by `remap-debuginfo = true`, and the user - // wish to simulate that behaviour by -Z simulate-remapped-rust-src-base, + // If this file is under $sysroot/lib/rustlib/src/ + // and the user wish to simulate remapping with -Z simulate-remapped-rust-src-base, // then we change `name` to a similar state as if the rust was bootstrapped // with `remap-debuginfo = true`. // This is useful for testing so that tests about the effects of @@ -1468,12 +1467,18 @@ impl<'a, 'tcx> CrateMetadataRef<'a> { // compiler is bootstrapped. if let Some(virtual_dir) = &sess.opts.unstable_opts.simulate_remapped_rust_src_base && let Some(real_dir) = &sess.opts.real_rust_source_base_dir - && let rustc_span::FileName::Real(ref mut old_name) = name - && let rustc_span::RealFileName::LocalPath(local) = old_name { + && let rustc_span::FileName::Real(ref mut old_name) = name { + let relative_path = match old_name { + rustc_span::RealFileName::LocalPath(local) => local.strip_prefix(real_dir).ok(), + rustc_span::RealFileName::Remapped { virtual_name, .. } => { + option_env!("CFG_VIRTUAL_RUST_SOURCE_BASE_DIR").and_then(|virtual_dir| virtual_name.strip_prefix(virtual_dir).ok()) + } + }; + debug!(?relative_path, ?virtual_dir, "simulate_remapped_rust_src_base"); for subdir in ["library", "compiler"] { - if let Ok(rest) = local.strip_prefix(real_dir.join(subdir)) { + if let Some(rest) = relative_path.and_then(|p| p.strip_prefix(subdir).ok()) { *old_name = rustc_span::RealFileName::Remapped { - local_path: None, + local_path: None, // FIXME: maybe we should preserve this? virtual_name: virtual_dir.join(subdir).join(rest), }; break; diff --git a/tests/ui/track-diagnostics/track6.rs b/tests/ui/track-diagnostics/track6.rs index 307e3101849a9..fc6f5f23d92fa 100644 --- a/tests/ui/track-diagnostics/track6.rs +++ b/tests/ui/track-diagnostics/track6.rs @@ -1,6 +1,9 @@ // compile-flags: -Z track-diagnostics // error-pattern: created at +// Normalize the emitted location so this doesn't need +// updating everytime someone adds or removes a line. +// normalize-stderr-test ".rs:\d+:\d+" -> ".rs:LL:CC" pub trait Foo { diff --git a/tests/ui/track-diagnostics/track6.stderr b/tests/ui/track-diagnostics/track6.stderr index 1c7537633ff23..89438aea9ad62 100644 --- a/tests/ui/track-diagnostics/track6.stderr +++ b/tests/ui/track-diagnostics/track6.stderr @@ -1,9 +1,9 @@ error[E0658]: specialization is unstable - --> $DIR/track6.rs:11:5 + --> $DIR/track6.rs:LL:CC | LL | default fn bar() {} | ^^^^^^^^^^^^^^^^^^^ --Ztrack-diagnostics: created at $COMPILER_DIR/rustc_session/src/parse.rs:93:5 +-Ztrack-diagnostics: created at $COMPILER_DIR/rustc_session/src/parse.rs:LL:CC | = note: see issue #31844 for more information = help: add `#![feature(specialization)]` to the crate attributes to enable