|
| 1 | +use std::borrow::Cow; |
1 | 2 | use std::fmt;
|
2 | 3 | use std::hash::Hash;
|
3 | 4 |
|
@@ -468,6 +469,29 @@ impl<'tcx> CodegenUnit<'tcx> {
|
468 | 469 | hash.as_u128().to_base_fixed_len(CASE_INSENSITIVE)
|
469 | 470 | }
|
470 | 471 |
|
| 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 | + const TRUNCATED_NAME_PREFIX: &str = "-trunc-"; |
| 476 | + if human_readable_name.len() > MAX_CGU_NAME_LENGTH { |
| 477 | + let mangled_name = Self::mangle_name(human_readable_name); |
| 478 | + // Determine a safe byte offset to truncate the name to |
| 479 | + let truncate_to = human_readable_name.floor_char_boundary( |
| 480 | + MAX_CGU_NAME_LENGTH - TRUNCATED_NAME_PREFIX.len() - mangled_name.len(), |
| 481 | + ); |
| 482 | + format!( |
| 483 | + "{}{}{}", |
| 484 | + &human_readable_name[..truncate_to], |
| 485 | + TRUNCATED_NAME_PREFIX, |
| 486 | + mangled_name |
| 487 | + ) |
| 488 | + .into() |
| 489 | + } else { |
| 490 | + // If the name is short enough, we can just return it as is. |
| 491 | + human_readable_name.into() |
| 492 | + } |
| 493 | + } |
| 494 | + |
471 | 495 | pub fn compute_size_estimate(&mut self) {
|
472 | 496 | // The size of a codegen unit as the sum of the sizes of the items
|
473 | 497 | // within it.
|
@@ -604,7 +628,7 @@ impl<'tcx> CodegenUnitNameBuilder<'tcx> {
|
604 | 628 | let cgu_name = self.build_cgu_name_no_mangle(cnum, components, special_suffix);
|
605 | 629 |
|
606 | 630 | if self.tcx.sess.opts.unstable_opts.human_readable_cgu_names {
|
607 |
| - cgu_name |
| 631 | + Symbol::intern(&CodegenUnit::shorten_name(cgu_name.as_str())) |
608 | 632 | } else {
|
609 | 633 | Symbol::intern(&CodegenUnit::mangle_name(cgu_name.as_str()))
|
610 | 634 | }
|
|
0 commit comments