Skip to content

Commit 0df94dd

Browse files
committed
checksum-freshness: Fix incorrect hash/file length values of binary
dependency files
1 parent a1a9448 commit 0df94dd

2 files changed

Lines changed: 23 additions & 16 deletions

File tree

compiler/rustc_builtin_macros/src/source_util.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,15 @@ fn load_binary_file(
275275
}
276276
};
277277
match cx.source_map().load_binary_file(&resolved_path) {
278-
Ok(data) => Ok(data),
278+
Ok(data) => {
279+
cx.sess
280+
.psess
281+
.file_depinfo
282+
.borrow_mut()
283+
.insert(Symbol::intern(&resolved_path.to_string_lossy()));
284+
285+
Ok(data)
286+
}
279287
Err(io_err) => {
280288
let mut err = cx.dcx().struct_span_err(
281289
macro_span,

compiler/rustc_interface/src/passes.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast as ast;
99
use rustc_attr_parsing::{AttributeParser, ShouldEmit};
1010
use rustc_codegen_ssa::traits::CodegenBackend;
1111
use rustc_codegen_ssa::{CodegenResults, CrateInfo};
12+
use rustc_data_structures::indexmap::IndexMap;
1213
use rustc_data_structures::jobserver::Proxy;
1314
use rustc_data_structures::steal::Steal;
1415
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, WorkerLocal};
@@ -586,7 +587,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
586587
let result: io::Result<()> = try {
587588
// Build a list of files used to compile the output and
588589
// write Makefile-compatible dependency rules
589-
let mut files: Vec<(String, u64, Option<SourceFileHash>)> = sess
590+
let mut files: IndexMap<String, (u64, Option<SourceFileHash>)> = sess
590591
.source_map()
591592
.files()
592593
.iter()
@@ -595,10 +596,12 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
595596
.map(|fmap| {
596597
(
597598
escape_dep_filename(&fmap.name.prefer_local_unconditionally().to_string()),
598-
// This needs to be unnormalized,
599-
// as external tools wouldn't know how rustc normalizes them
600-
fmap.unnormalized_source_len as u64,
601-
fmap.checksum_hash,
599+
(
600+
// This needs to be unnormalized,
601+
// as external tools wouldn't know how rustc normalizes them
602+
fmap.unnormalized_source_len as u64,
603+
fmap.checksum_hash,
604+
),
602605
)
603606
})
604607
.collect();
@@ -616,7 +619,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
616619
fn hash_iter_files<P: AsRef<Path>>(
617620
it: impl Iterator<Item = P>,
618621
checksum_hash_algo: Option<SourceFileHashAlgorithm>,
619-
) -> impl Iterator<Item = (P, u64, Option<SourceFileHash>)> {
622+
) -> impl Iterator<Item = (P, (u64, Option<SourceFileHash>))> {
620623
it.map(move |path| {
621624
match checksum_hash_algo.and_then(|algo| {
622625
fs::File::open(path.as_ref())
@@ -632,8 +635,8 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
632635
})
633636
.ok()
634637
}) {
635-
Some((file_len, checksum)) => (path, file_len, Some(checksum)),
636-
None => (path, 0, None),
638+
Some((file_len, checksum)) => (path, (file_len, Some(checksum))),
639+
None => (path, (0, None)),
637640
}
638641
})
639642
}
@@ -707,18 +710,14 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
707710
file,
708711
"{}: {}\n",
709712
path.display(),
710-
files
711-
.iter()
712-
.map(|(path, _file_len, _checksum_hash_algo)| path.as_str())
713-
.intersperse(" ")
714-
.collect::<String>()
713+
files.keys().map(String::as_str).intersperse(" ").collect::<String>()
715714
)?;
716715
}
717716

718717
// Emit a fake target for each input file to the compilation. This
719718
// prevents `make` from spitting out an error if a file is later
720719
// deleted. For more info see #28735
721-
for (path, _file_len, _checksum_hash_algo) in &files {
720+
for path in files.keys() {
722721
writeln!(file, "{path}:")?;
723722
}
724723

@@ -747,7 +746,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
747746
if sess.opts.unstable_opts.checksum_hash_algorithm().is_some() {
748747
files
749748
.iter()
750-
.filter_map(|(path, file_len, hash_algo)| {
749+
.filter_map(|(path, (file_len, hash_algo))| {
751750
hash_algo.map(|hash_algo| (path, file_len, hash_algo))
752751
})
753752
.try_for_each(|(path, file_len, checksum_hash)| {

0 commit comments

Comments
 (0)