Skip to content

Commit d34486f

Browse files
authored
Unrolled build for rust-lang#115869
Rollup merge of rust-lang#115869 - ferrocene:pa-fix-tests-cargo-remap, r=compiler-errors Avoid blessing cargo deps's source code in ui tests Before this PR, the source code of dependencies was included in UI test error messages whenever possible. Unfortunately, "whenever possible" means in some cases the source code wouldn't be injected, resulting in a test failure. One such case is when `$CARGO_HOME` is remapped to something that is not present on disk [^1]. As the remapped path doesn't exist on disk, the source code wouldn't be showed in `tests/ui/issues/issue-21763.rs`: ```diff = note: required for `hashbrown::raw::RawTable<(Rc<()>, Rc<()>)>` to implement `Send` note: required because it appears within the type `HashMap<Rc<()>, Rc<()>, RandomState>` --> $HASHBROWN_SRC_LOCATION - | -LL | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> { - | ^^^^^^^ note: required because it appears within the type `HashMap<Rc<()>, Rc<()>>` --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL note: required by a bound in `foo` ``` This PR fixes the problem by always hiding dependencies source code in the error messages generated during UI tests. This is implemented with a new internal flag, `-Z ignore-directory-in-diagnostics-source-blocks=$path`, which compiletest passes during UI tests. Once this is merged, remapping the Cargo home will be supported. This PR is best reviewed commit-by-commit. [^1]: After being puzzled for a bit, I discovered why this never impacted `rust-lang/rust`: we don't remap `$CARGO_HOME` :sweat_smile:. Instead, we set `$CARGO_HOME` to `/cargo` in CI, which sort-of-but-not-really achieves the same effect.
2 parents 65ea825 + c230637 commit d34486f

File tree

9 files changed

+69
-10
lines changed

9 files changed

+69
-10
lines changed

Cargo.lock

+10
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,7 @@ dependencies = [
662662
"diff",
663663
"getopts",
664664
"glob",
665+
"home",
665666
"lazycell",
666667
"libc",
667668
"miow",
@@ -1663,6 +1664,15 @@ version = "0.4.3"
16631664
source = "registry+https://github.com/rust-lang/crates.io-index"
16641665
checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
16651666

1667+
[[package]]
1668+
name = "home"
1669+
version = "0.5.5"
1670+
source = "registry+https://github.com/rust-lang/crates.io-index"
1671+
checksum = "5444c27eef6923071f7ebcc33e3444508466a76f7a2b93da00ed6e19f30c1ddb"
1672+
dependencies = [
1673+
"windows-sys 0.48.0",
1674+
]
1675+
16661676
[[package]]
16671677
name = "html-checker"
16681678
version = "0.1.0"

compiler/rustc_errors/src/annotate_snippet_emitter_writer.rs

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ impl AnnotateSnippetEmitterWriter {
169169
.map(|line| {
170170
// Ensure the source file is present before we try
171171
// to load a string from it.
172+
// FIXME(#115869): support -Z ignore-directory-in-diagnostics-source-blocks
172173
source_map.ensure_source_file_source_present(&file);
173174
(
174175
format!("{}", source_map.filename_for_diagnostics(&file.name)),

compiler/rustc_errors/src/emitter.rs

+24-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//! The output types are defined in `rustc_session::config::ErrorOutputType`.
99
1010
use rustc_span::source_map::SourceMap;
11-
use rustc_span::{FileLines, SourceFile, Span};
11+
use rustc_span::{FileLines, FileName, SourceFile, Span};
1212

1313
use crate::snippet::{
1414
Annotation, AnnotationColumn, AnnotationType, Line, MultilineAnnotation, Style, StyledString,
@@ -635,6 +635,7 @@ pub struct EmitterWriter {
635635
short_message: bool,
636636
teach: bool,
637637
ui_testing: bool,
638+
ignored_directories_in_source_blocks: Vec<String>,
638639
diagnostic_width: Option<usize>,
639640

640641
macro_backtrace: bool,
@@ -664,6 +665,7 @@ impl EmitterWriter {
664665
short_message: false,
665666
teach: false,
666667
ui_testing: false,
668+
ignored_directories_in_source_blocks: Vec::new(),
667669
diagnostic_width: None,
668670
macro_backtrace: false,
669671
track_diagnostics: false,
@@ -1193,7 +1195,7 @@ impl EmitterWriter {
11931195
let will_be_emitted = |span: Span| {
11941196
!span.is_dummy() && {
11951197
let file = sm.lookup_source_file(span.hi());
1196-
sm.ensure_source_file_source_present(&file)
1198+
should_show_source_code(&self.ignored_directories_in_source_blocks, sm, &file)
11971199
}
11981200
};
11991201

@@ -1388,7 +1390,11 @@ impl EmitterWriter {
13881390
// Print out the annotate source lines that correspond with the error
13891391
for annotated_file in annotated_files {
13901392
// we can't annotate anything if the source is unavailable.
1391-
if !sm.ensure_source_file_source_present(&annotated_file.file) {
1393+
if !should_show_source_code(
1394+
&self.ignored_directories_in_source_blocks,
1395+
sm,
1396+
&annotated_file.file,
1397+
) {
13921398
if !self.short_message {
13931399
// We'll just print an unannotated message.
13941400
for (annotation_id, line) in annotated_file.lines.iter().enumerate() {
@@ -2737,3 +2743,18 @@ pub fn is_case_difference(sm: &SourceMap, suggested: &str, sp: Span) -> bool {
27372743
// bug, but be defensive against that here.
27382744
&& found != suggested
27392745
}
2746+
2747+
pub(crate) fn should_show_source_code(
2748+
ignored_directories: &[String],
2749+
sm: &SourceMap,
2750+
file: &SourceFile,
2751+
) -> bool {
2752+
if !sm.ensure_source_file_source_present(file) {
2753+
return false;
2754+
}
2755+
2756+
let FileName::Real(name) = &file.name else { return true };
2757+
name.local_path()
2758+
.map(|path| ignored_directories.iter().all(|dir| !path.starts_with(dir)))
2759+
.unwrap_or(true)
2760+
}

compiler/rustc_errors/src/json.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use rustc_span::source_map::{FilePathMapping, SourceMap};
1313
use termcolor::{ColorSpec, WriteColor};
1414

15-
use crate::emitter::{Emitter, HumanReadableErrorType};
15+
use crate::emitter::{should_show_source_code, Emitter, HumanReadableErrorType};
1616
use crate::registry::Registry;
1717
use crate::translation::{to_fluent_args, Translate};
1818
use crate::DiagnosticId;
@@ -45,6 +45,7 @@ pub struct JsonEmitter {
4545
fallback_bundle: LazyFallbackBundle,
4646
pretty: bool,
4747
ui_testing: bool,
48+
ignored_directories_in_source_blocks: Vec<String>,
4849
json_rendered: HumanReadableErrorType,
4950
diagnostic_width: Option<usize>,
5051
macro_backtrace: bool,
@@ -73,6 +74,7 @@ impl JsonEmitter {
7374
fallback_bundle,
7475
pretty,
7576
ui_testing: false,
77+
ignored_directories_in_source_blocks: Vec::new(),
7678
json_rendered,
7779
diagnostic_width,
7880
macro_backtrace,
@@ -127,6 +129,7 @@ impl JsonEmitter {
127129
fallback_bundle,
128130
pretty,
129131
ui_testing: false,
132+
ignored_directories_in_source_blocks: Vec::new(),
130133
json_rendered,
131134
diagnostic_width,
132135
macro_backtrace,
@@ -138,6 +141,10 @@ impl JsonEmitter {
138141
pub fn ui_testing(self, ui_testing: bool) -> Self {
139142
Self { ui_testing, ..self }
140143
}
144+
145+
pub fn ignored_directories_in_source_blocks(self, value: Vec<String>) -> Self {
146+
Self { ignored_directories_in_source_blocks: value, ..self }
147+
}
141148
}
142149

143150
impl Translate for JsonEmitter {
@@ -381,6 +388,7 @@ impl Diagnostic {
381388
.track_diagnostics(je.track_diagnostics)
382389
.terminal_url(je.terminal_url)
383390
.ui_testing(je.ui_testing)
391+
.ignored_directories_in_source_blocks(je.ignored_directories_in_source_blocks.clone())
384392
.emit_diagnostic(diag);
385393
let output = Arc::try_unwrap(output.0).unwrap().into_inner().unwrap();
386394
let output = String::from_utf8(output).unwrap();
@@ -558,7 +566,11 @@ impl DiagnosticSpanLine {
558566
.span_to_lines(span)
559567
.map(|lines| {
560568
// We can't get any lines if the source is unavailable.
561-
if !je.sm.ensure_source_file_source_present(&lines.file) {
569+
if !should_show_source_code(
570+
&je.ignored_directories_in_source_blocks,
571+
&je.sm,
572+
&lines.file,
573+
) {
562574
return vec![];
563575
}
564576

compiler/rustc_session/src/options.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1536,6 +1536,8 @@ options! {
15361536
"generate human-readable, predictable names for codegen units (default: no)"),
15371537
identify_regions: bool = (false, parse_bool, [UNTRACKED],
15381538
"display unnamed regions as `'<id>`, using a non-ident unique id (default: no)"),
1539+
ignore_directory_in_diagnostics_source_blocks: Vec<String> = (Vec::new(), parse_string_push, [UNTRACKED],
1540+
"do not display the source code block in diagnostics for files in the directory"),
15391541
incremental_ignore_spans: bool = (false, parse_bool, [TRACKED],
15401542
"ignore spans during ICH computation -- used for testing (default: no)"),
15411543
incremental_info: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_session/src/session.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,10 @@ fn default_emitter(
12951295
.diagnostic_width(sopts.diagnostic_width)
12961296
.macro_backtrace(macro_backtrace)
12971297
.track_diagnostics(track_diagnostics)
1298-
.terminal_url(terminal_url);
1298+
.terminal_url(terminal_url)
1299+
.ignored_directories_in_source_blocks(
1300+
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
1301+
);
12991302
Box::new(emitter.ui_testing(sopts.unstable_opts.ui_testing))
13001303
}
13011304
}
@@ -1312,7 +1315,10 @@ fn default_emitter(
13121315
track_diagnostics,
13131316
terminal_url,
13141317
)
1315-
.ui_testing(sopts.unstable_opts.ui_testing),
1318+
.ui_testing(sopts.unstable_opts.ui_testing)
1319+
.ignored_directories_in_source_blocks(
1320+
sopts.unstable_opts.ignore_directory_in_diagnostics_source_blocks.clone(),
1321+
),
13161322
),
13171323
}
13181324
}

src/tools/compiletest/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ walkdir = "2"
2424
glob = "0.3.0"
2525
lazycell = "1.3.0"
2626
anyhow = "1"
27+
home = "0.5.5"
2728

2829
[target.'cfg(unix)'.dependencies]
2930
libc = "0.2"

src/tools/compiletest/src/runtest.rs

+9
Original file line numberDiff line numberDiff line change
@@ -2335,6 +2335,15 @@ impl<'test> TestCx<'test> {
23352335
rustc.arg("-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX");
23362336
rustc.arg("-Ztranslate-remapped-path-to-local-path=no");
23372337

2338+
// Hide Cargo dependency sources from ui tests to make sure the error message doesn't
2339+
// change depending on whether $CARGO_HOME is remapped or not. If this is not present,
2340+
// when $CARGO_HOME is remapped the source won't be shown, and when it's not remapped the
2341+
// source will be shown, causing a blessing hell.
2342+
rustc.arg("-Z").arg(format!(
2343+
"ignore-directory-in-diagnostics-source-blocks={}",
2344+
home::cargo_home().expect("failed to find cargo home").to_str().unwrap()
2345+
));
2346+
23382347
// Optionally prevent default --sysroot if specified in test compile-flags.
23392348
if !self.props.compile_flags.iter().any(|flag| flag.starts_with("--sysroot"))
23402349
&& !self.config.host_rustcflags.iter().any(|flag| flag == "--sysroot")

tests/ui/issues/issue-21763.stderr

-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,6 @@ LL | foo::<HashMap<Rc<()>, Rc<()>>>();
99
= note: required for `hashbrown::raw::RawTable<(Rc<()>, Rc<()>)>` to implement `Send`
1010
note: required because it appears within the type `HashMap<Rc<()>, Rc<()>, RandomState>`
1111
--> $HASHBROWN_SRC_LOCATION
12-
|
13-
LL | pub struct HashMap<K, V, S = DefaultHashBuilder, A: Allocator + Clone = Global> {
14-
| ^^^^^^^
1512
note: required because it appears within the type `HashMap<Rc<()>, Rc<()>>`
1613
--> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
1714
note: required by a bound in `foo`

0 commit comments

Comments
 (0)