Skip to content

Commit 12d4fd7

Browse files
committed
Limit the size of cgu names when using the -Zhuman-readable-cgu-names option
Prior to this change, cgu names could be generated which would result in filenames longer than the limit imposed by the OS.
1 parent 88b3b52 commit 12d4fd7

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

compiler/rustc_middle/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
#![feature(negative_impls)]
5252
#![feature(never_type)]
5353
#![feature(ptr_alignment_type)]
54+
#![feature(round_char_boundary)]
5455
#![feature(rustc_attrs)]
5556
#![feature(rustdoc_internals)]
5657
#![feature(trusted_len)]

compiler/rustc_middle/src/mir/mono.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::borrow::Cow;
12
use std::fmt;
23
use std::hash::Hash;
34

@@ -468,6 +469,20 @@ impl<'tcx> CodegenUnit<'tcx> {
468469
hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
469470
}
470471

472+
pub fn shorten_name(human_readable_name: &str) -> Cow<'_, str> {
473+
// Set a limit a somewhat below the common platform limits for file names.
474+
const MAX_CGU_NAME_LENGTH: usize = 200;
475+
if human_readable_name.len() > MAX_CGU_NAME_LENGTH {
476+
let mangled_name = Self::mangle_name(human_readable_name);
477+
let truncate_to = human_readable_name
478+
.floor_char_boundary(MAX_CGU_NAME_LENGTH - mangled_name.len() - 1);
479+
format!("{}-{}", &human_readable_name[..truncate_to], mangled_name).into()
480+
} else {
481+
// If the name is short enough, we can just return it as is.
482+
human_readable_name.into()
483+
}
484+
}
485+
471486
pub fn compute_size_estimate(&mut self) {
472487
// The size of a codegen unit as the sum of the sizes of the items
473488
// within it.
@@ -604,7 +619,7 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
604619
let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);
605620

606621
if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
607-
cgu_name
622+
Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str()))
608623
} else {
609624
Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
610625
}

compiler/rustc_monomorphize/src/partitioning.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -461,15 +461,15 @@ fn merge_codegen_units<'tcx>(
461461

462462
for cgu in codegen_units.iter_mut() {
463463
if let Some(new_cgu_name) = new_cgu_names.get(&cgu.name()) {
464-
if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
465-
cgu.set_name(Symbol::intern(new_cgu_name));
464+
let new_cgu_name = if cx.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
465+
Symbol::intern(&CodegenUnit::shorten_name(new_cgu_name))
466466
} else {
467467
// If we don't require CGU names to be human-readable,
468468
// we use a fixed length hash of the composite CGU name
469469
// instead.
470-
let new_cgu_name = CodegenUnit::mangle_name(new_cgu_name);
471-
cgu.set_name(Symbol::intern(&new_cgu_name));
472-
}
470+
Symbol::intern(&CodegenUnit::mangle_name(new_cgu_name))
471+
};
472+
cgu.set_name(new_cgu_name);
473473
}
474474
}
475475

0 commit comments

Comments
 (0)