Skip to content

Commit 933e686

Browse files
authored
Rollup merge of #151137 - osiewicz:151090-checksum-freshness-binary-files, r=jdonszelmann
checksum-freshness: Fix invalid checksum calculation for binary files Admittedly this is not the cleanest way to achieve this, but SourceMap is quite intertwined with source files being represented as Strings. Tracking issue: rust-lang/cargo#14136 Closes: #151090
2 parents b4f8dc7 + 0df94dd commit 933e686

5 files changed

Lines changed: 30 additions & 19 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::{self as ast, CRATE_NODE_ID};
99
use rustc_attr_parsing::{AttributeParser, Early, 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};
@@ -584,7 +585,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
584585
let result: io::Result<()> = try {
585586
// Build a list of files used to compile the output and
586587
// write Makefile-compatible dependency rules
587-
let mut files: Vec<(String, u64, Option<SourceFileHash>)> = sess
588+
let mut files: IndexMap<String, (u64, Option<SourceFileHash>)> = sess
588589
.source_map()
589590
.files()
590591
.iter()
@@ -593,10 +594,12 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
593594
.map(|fmap| {
594595
(
595596
escape_dep_filename(&fmap.name.prefer_local_unconditionally().to_string()),
596-
// This needs to be unnormalized,
597-
// as external tools wouldn't know how rustc normalizes them
598-
fmap.unnormalized_source_len as u64,
599-
fmap.checksum_hash,
597+
(
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,
602+
),
600603
)
601604
})
602605
.collect();
@@ -614,7 +617,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
614617
fn hash_iter_files<P: AsRef<Path>>(
615618
it: impl Iterator<Item = P>,
616619
checksum_hash_algo: Option<SourceFileHashAlgorithm>,
617-
) -> impl Iterator<Item = (P, u64, Option<SourceFileHash>)> {
620+
) -> impl Iterator<Item = (P, (u64, Option<SourceFileHash>))> {
618621
it.map(move |path| {
619622
match checksum_hash_algo.and_then(|algo| {
620623
fs::File::open(path.as_ref())
@@ -630,8 +633,8 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
630633
})
631634
.ok()
632635
}) {
633-
Some((file_len, checksum)) => (path, file_len, Some(checksum)),
634-
None => (path, 0, None),
636+
Some((file_len, checksum)) => (path, (file_len, Some(checksum))),
637+
None => (path, (0, None)),
635638
}
636639
})
637640
}
@@ -705,18 +708,14 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
705708
file,
706709
"{}: {}\n",
707710
path.display(),
708-
files
709-
.iter()
710-
.map(|(path, _file_len, _checksum_hash_algo)| path.as_str())
711-
.intersperse(" ")
712-
.collect::<String>()
711+
files.keys().map(String::as_str).intersperse(" ").collect::<String>()
713712
)?;
714713
}
715714

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

@@ -745,7 +744,7 @@ fn write_out_deps(tcx: TyCtxt<'_>, outputs: &OutputFilenames, out_filenames: &[P
745744
if sess.opts.unstable_opts.checksum_hash_algorithm().is_some() {
746745
files
747746
.iter()
748-
.filter_map(|(path, file_len, hash_algo)| {
747+
.filter_map(|(path, (file_len, hash_algo))| {
749748
hash_algo.map(|hash_algo| (path, file_len, hash_algo))
750749
})
751750
.try_for_each(|(path, file_len, checksum_hash)| {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
binary�
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
lib.d: lib.rs foo.rs
1+
lib.d: lib.rs foo.rs binary_file
22

33
lib.rs:
44
foo.rs:
5-
# checksum:blake3=94af75ee4ed805434484c3de51c9025278e5c3ada2315e2592052e102168a503 file_len:120 lib.rs
5+
binary_file:
6+
# checksum:blake3=4ac56f3f877798fb762d714c7bcb72e70133f4cc585f80dbd99c07755ae2c7f6 file_len:222 lib.rs
67
# checksum:blake3=2720e17bfda4f3b2a5c96bb61b7e76ed8ebe3359b34128c0e5d8032c090a4f1a file_len:119 foo.rs
8+
# checksum:blake3=119a5db8711914922c5b1c1908be4958175c5afa95c08888de594725329b5439 file_len:7 binary_file
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// A basic library to be used in tests with no real purpose.
22

33
mod foo;
4-
4+
// Binary file with invalid UTF-8 sequence.
5+
static BINARY_FILE: &[u8] = include_bytes!("binary_file");
56
pub fn sum(a: i32, b: i32) -> i32 {
67
a + b
78
}

0 commit comments

Comments
 (0)