@@ -80,10 +80,6 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
8080 let filenames_val = cx. const_bytes ( & filenames_buffer) ;
8181 let filenames_ref = llvm_cov:: hash_bytes ( & filenames_buffer) ;
8282
83- // Generate the coverage map header, which contains the filenames used by
84- // this CGU's coverage mappings, and store it in a well-known global.
85- generate_covmap_record ( cx, covmap_version, filenames_size, filenames_val) ;
86-
8783 let mut unused_function_names = Vec :: new ( ) ;
8884
8985 let covfun_records = function_coverage_map
@@ -93,6 +89,15 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
9389 } )
9490 . collect :: < Vec < _ > > ( ) ;
9591
92+ // If there are no covfun records for this CGU, don't generate a covmap record.
93+ // Emitting a covmap record without any covfun records causes `llvm-cov` to
94+ // fail when generating coverage reports, and if there are no covfun records
95+ // then the covmap record isn't useful anyway.
96+ // This should prevent a repeat of <https://github.com/rust-lang/rust/issues/133606>.
97+ if covfun_records. is_empty ( ) {
98+ return ;
99+ }
100+
96101 for covfun in & covfun_records {
97102 unused_function_names. extend ( covfun. mangled_function_name_if_unused ( ) ) ;
98103
@@ -117,6 +122,11 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
117122 llvm:: set_linkage ( array, llvm:: Linkage :: InternalLinkage ) ;
118123 llvm:: set_initializer ( array, initializer) ;
119124 }
125+
126+ // Generate the coverage map header, which contains the filenames used by
127+ // this CGU's coverage mappings, and store it in a well-known global.
128+ // (This is skipped if we returned early due to having no covfun records.)
129+ generate_covmap_record ( cx, covmap_version, filenames_size, filenames_val) ;
120130}
121131
122132/// Maps "global" (per-CGU) file ID numbers to their underlying filenames.
0 commit comments