Skip to content

Commit 8228ccf

Browse files
committed
We can only link to monomorphizations from upstream crates if we know
they will be statically linked. In the particular case of #64872, we get burned by an attempt to link to a monomorphization in libcore.rlib that ends up not being exported by libstd.dylib. (For that scenario, the attempt to lookup the linkage returns `None`, which led us terribly astray.) (That scenario is encoded in a test in a follow-on commit.) Also added some `log::info` output for the `None` case, because I want easy access in all of our builds to inspect what goes on in this logic. In response to review feedback, I had tried to revise the fix to be more nuanced in handling of `None` (i.e., the case which I've previously asserted to be "unknown linkage"). Alex's take on matters is that we should use the output crate type to determine what format the dependency here will have. However, further testing showed that approach to be flawed. So I added debugflag: `-Z share-generics-for-unknown-linkage`, to make it easy to recover the earlier behavior (which I nonetheless assert to be buggy in general), and use that flag to keep one of our codegen tests working.
1 parent d3d28a4 commit 8228ccf

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

src/librustc/session/config.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,8 @@ options! {DebuggingOptions, DebuggingSetter, basic_debugging_options,
14331433
"tell the linker to strip debuginfo when building without debuginfo enabled."),
14341434
share_generics: Option<bool> = (None, parse_opt_bool, [TRACKED],
14351435
"make the current crate share its generic instantiations"),
1436+
share_generics_for_unknown_linkage: bool = (false, parse_bool, [TRACKED],
1437+
"optimistically assume upstream crates with unknown linkage can share generics"),
14361438
chalk: bool = (false, parse_bool, [TRACKED],
14371439
"enable the experimental Chalk-based trait solving engine"),
14381440
no_parallel_llvm: bool = (false, parse_bool, [UNTRACKED],

src/librustc_metadata/cstore_impl.rs

+39-2
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,45 @@ provide! { <'tcx> tcx, def_id, other, cdata,
243243
let formats = tcx.dependency_formats(LOCAL_CRATE);
244244
let remove_generics = formats.iter().any(|(_ty, list)| {
245245
match list.get(def_id.krate.as_usize() - 1) {
246-
Some(Linkage::IncludedFromDylib) | Some(Linkage::Dynamic) => true,
247-
_ => false,
246+
Some(l @ Linkage::NotLinked) |
247+
Some(l @ Linkage::IncludedFromDylib) |
248+
Some(l @ Linkage::Dynamic) => {
249+
log::debug!("compiling `{B}` def_id: {D:?} cannot reuse generics \
250+
from crate `{A}` because it has linkage {L:?}",
251+
A=tcx.crate_name(def_id.krate),
252+
B=tcx.crate_name(LOCAL_CRATE),
253+
D=def_id,
254+
L=l);
255+
true
256+
}
257+
258+
Some(Linkage::Static) => {
259+
log::debug!("compiling `{B}` def_id: {D:?} attempt to reuse generics \
260+
from statically-linked crate `{A}`",
261+
A=tcx.crate_name(def_id.krate),
262+
B=tcx.crate_name(LOCAL_CRATE),
263+
D=def_id);
264+
false
265+
}
266+
267+
None => {
268+
// rust-lang/rust#65890: A has unknown dependency format when compiling B.
269+
if tcx.sess.opts.debugging_opts.share_generics_for_unknown_linkage {
270+
log::info!("`{B}`, crate-type {O:?}: attempt to reuse generics \
271+
from crate `{A}` despite its unknown dependency format",
272+
A=tcx.crate_name(def_id.krate),
273+
B=tcx.crate_name(LOCAL_CRATE),
274+
O=_ty);
275+
false
276+
} else {
277+
log::info!("`{B}`, crate-type {O:?}: cannot reuse generics \
278+
from crate `{A}` because it has unknown dependency format",
279+
A=tcx.crate_name(def_id.krate),
280+
B=tcx.crate_name(LOCAL_CRATE),
281+
O=_ty);
282+
true
283+
}
284+
}
248285
}
249286
});
250287
if remove_generics {

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// ignore-tidy-linelength
22
// no-prefer-dynamic
3-
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe
3+
// FIXME: Use `-Z share-generics-for-unknown-linkage` to workaround rust-lang/rust#65890
4+
// compile-flags:-Zprint-mono-items=eager -Zshare-generics=yes -Zincremental=tmp/partitioning-tests/shared-generics-exe -Z share-generics-for-unknown-linkage
45

56
#![crate_type="rlib"]
67

0 commit comments

Comments
 (0)