Skip to content

Commit 06d2448

Browse files
committed
Auto merge of #54152 - michaelwoerister:cgu-name-fix, r=alexcrichton
Really make CGU names unique across crates. This will hopefully fix issue #53794. r? @alexcrichton
2 parents 6810f52 + 3beb762 commit 06d2448

File tree

6 files changed

+44
-19
lines changed

6 files changed

+44
-19
lines changed

src/librustc/mir/mono.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use hir::def_id::{DefId, CrateNum};
11+
use hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
1212
use syntax::ast::NodeId;
1313
use syntax::symbol::{Symbol, InternedString};
1414
use ty::{Instance, TyCtxt};
@@ -266,7 +266,8 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
266266
/// This function will build CGU names of the form:
267267
///
268268
/// ```
269-
/// <crate-name>.<crate-disambiguator>(-<component>)*[.<special-suffix>]
269+
/// <crate-name>.<crate-disambiguator>[-in-<local-crate-id>](-<component>)*[.<special-suffix>]
270+
/// <local-crate-id> = <local-crate-name>.<local-crate-disambiguator>
270271
/// ```
271272
///
272273
/// The '.' before `<special-suffix>` makes sure that names with a special
@@ -311,9 +312,25 @@ impl<'a, 'gcx: 'tcx, 'tcx: 'a> CodegenUnitNameBuilder<'a, 'gcx, 'tcx> {
311312
// Start out with the crate name and disambiguator
312313
let tcx = self.tcx;
313314
let crate_prefix = self.cache.entry(cnum).or_insert_with(|| {
315+
// Whenever the cnum is not LOCAL_CRATE we also mix in the
316+
// local crate's ID. Otherwise there can be collisions between CGUs
317+
// instantiating stuff for upstream crates.
318+
let local_crate_id = if cnum != LOCAL_CRATE {
319+
let local_crate_disambiguator =
320+
format!("{}", tcx.crate_disambiguator(LOCAL_CRATE));
321+
format!("-in-{}.{}",
322+
tcx.crate_name(LOCAL_CRATE),
323+
&local_crate_disambiguator[0 .. 8])
324+
} else {
325+
String::new()
326+
};
327+
314328
let crate_disambiguator = format!("{}", tcx.crate_disambiguator(cnum));
315329
// Using a shortened disambiguator of about 40 bits
316-
format!("{}.{}", tcx.crate_name(cnum), &crate_disambiguator[0 .. 8])
330+
format!("{}.{}{}",
331+
tcx.crate_name(cnum),
332+
&crate_disambiguator[0 .. 8],
333+
local_crate_id)
317334
});
318335

319336
write!(cgu_name, "{}", crate_prefix).unwrap();

src/test/codegen-units/partitioning/extern-generic.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,5 @@ mod mod3 {
5858

5959
// Make sure the two generic functions from the extern crate get instantiated
6060
// once for the current crate
61-
//~ MONO_ITEM fn cgu_generic_function::foo[0]<&str> @@ cgu_generic_function.volatile[External]
62-
//~ MONO_ITEM fn cgu_generic_function::bar[0]<&str> @@ cgu_generic_function.volatile[External]
61+
//~ MONO_ITEM fn cgu_generic_function::foo[0]<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]
62+
//~ MONO_ITEM fn cgu_generic_function::bar[0]<&str> @@ cgu_generic_function-in-extern_generic.volatile[External]

src/test/codegen-units/partitioning/shared-generics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ extern crate shared_generics_aux;
1919
//~ MONO_ITEM fn shared_generics::foo[0]
2020
pub fn foo() {
2121

22-
//~ MONO_ITEM fn shared_generics_aux::generic_fn[0]<u16> @@ shared_generics_aux.volatile[External]
22+
//~ MONO_ITEM fn shared_generics_aux::generic_fn[0]<u16> @@ shared_generics_aux-in-shared_generics.volatile[External]
2323
let _ = shared_generics_aux::generic_fn(0u16, 1u16);
2424

2525
// This should not generate a monomorphization because it's already

src/tools/compiletest/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ serde = "1.0"
1414
serde_json = "1.0"
1515
serde_derive = "1.0"
1616
rustfix = "0.4.1"
17+
lazy_static = "1.0"
1718

1819
[target.'cfg(unix)'.dependencies]
1920
libc = "0.2"
2021

2122
[target.'cfg(windows)'.dependencies]
22-
lazy_static = "1.0"
2323
miow = "0.3"
2424
winapi = { version = "0.3", features = ["winerror"] }

src/tools/compiletest/src/main.rs

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ extern crate libc;
2222
extern crate log;
2323
extern crate regex;
2424
#[macro_use]
25-
#[cfg(windows)]
2625
extern crate lazy_static;
2726
#[macro_use]
2827
extern crate serde_derive;

src/tools/compiletest/src/runtest.rs

+20-11
Original file line numberDiff line numberDiff line change
@@ -2355,21 +2355,30 @@ impl<'test> TestCx<'test> {
23552355
string
23562356
}
23572357

2358+
// Given a cgu-name-prefix of the form <crate-name>.<crate-disambiguator> or
2359+
// the form <crate-name1>.<crate-disambiguator1>-in-<crate-name2>.<crate-disambiguator2>,
2360+
// remove all crate-disambiguators.
23582361
fn remove_crate_disambiguator_from_cgu(cgu: &str) -> String {
2359-
// The first '.' is the start of the crate disambiguator
2360-
let disambiguator_start = cgu.find('.')
2361-
.expect("Could not find start of crate disambiguator in CGU spec");
2362+
lazy_static! {
2363+
static ref RE: Regex = Regex::new(
2364+
r"^[^\.]+(?P<d1>\.[[:alnum:]]+)(-in-[^\.]+(?P<d2>\.[[:alnum:]]+))?"
2365+
).unwrap();
2366+
}
2367+
2368+
let captures = RE.captures(cgu).unwrap_or_else(|| {
2369+
panic!("invalid cgu name encountered: {}", cgu)
2370+
});
23622371

2363-
// The first non-alphanumeric character is the end of the disambiguator
2364-
let disambiguator_end = cgu[disambiguator_start + 1 ..]
2365-
.find(|c| !char::is_alphanumeric(c))
2366-
.expect("Could not find end of crate disambiguator in CGU spec")
2367-
+ disambiguator_start + 1;
2372+
let mut new_name = cgu.to_owned();
2373+
2374+
if let Some(d2) = captures.name("d2") {
2375+
new_name.replace_range(d2.start() .. d2.end(), "");
2376+
}
23682377

2369-
let mut result = cgu[0 .. disambiguator_start].to_string();
2370-
result.push_str(&cgu[disambiguator_end ..]);
2378+
let d1 = captures.name("d1").unwrap();
2379+
new_name.replace_range(d1.start() .. d1.end(), "");
23712380

2372-
result
2381+
new_name
23732382
}
23742383
}
23752384

0 commit comments

Comments
 (0)