Skip to content

Commit 2aa3eea

Browse files
authored
Rollup merge of #109109 - compiler-errors:polymorphize-foreign, r=Nilstrieb
Use `unused_generic_params` from crate metadata Due to the way that `separate_provide_extern` interacted with the implementation of `<ty::InstanceDef<'tcx> as Key>::query_crate_is_local`, we actually never hit the foreign provider for `unused_generic_params`. Additionally, since the *local* provider of `unused_generic_params` calls `should_polymorphize`, which always returns false if the def-id is foreign, this means that we never actually polymorphize monomorphic instances originating from foreign crates. We don't actually encode `unused_generic_params` for items where all generics are used, so I had to tweak the foreign provider to fall back to `ty::UnusedGenericParams::new_all_used()` to avoid more ICEs when the above bugs were fixed.
2 parents c11399b + ee2d428 commit 2aa3eea

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,15 @@ provide! { tcx, def_id, other, cdata,
226226
lookup_default_body_stability => { table }
227227
lookup_deprecation_entry => { table }
228228
params_in_repr => { table }
229-
unused_generic_params => { table }
229+
// FIXME: Could be defaulted, but `LazyValue<UnusedGenericParams>` is not `FixedSizeEncoding`..
230+
unused_generic_params => {
231+
cdata
232+
.root
233+
.tables
234+
.unused_generic_params
235+
.get(cdata, def_id.index)
236+
.map_or_else(|| ty::UnusedGenericParams::new_all_used(), |lazy| lazy.decode((cdata, tcx)))
237+
}
230238
opt_def_kind => { table_direct }
231239
impl_parent => { table }
232240
impl_polarity => { table_direct }

compiler/rustc_middle/src/query/keys.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ impl<'tcx> Key for ty::InstanceDef<'tcx> {
6363

6464
#[inline(always)]
6565
fn query_crate_is_local(&self) -> bool {
66-
true
66+
self.def_id().is_local()
6767
}
6868

6969
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
@@ -76,7 +76,7 @@ impl<'tcx> Key for ty::Instance<'tcx> {
7676

7777
#[inline(always)]
7878
fn query_crate_is_local(&self) -> bool {
79-
true
79+
self.def_id().is_local()
8080
}
8181

8282
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {

compiler/rustc_monomorphize/src/polymorphize.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ fn unused_generic_params<'tcx>(
3636
tcx: TyCtxt<'tcx>,
3737
instance: ty::InstanceDef<'tcx>,
3838
) -> UnusedGenericParams {
39+
assert!(instance.def_id().is_local());
40+
3941
if !tcx.sess.opts.unstable_opts.polymorphize {
4042
// If polymorphization disabled, then all parameters are used.
4143
return UnusedGenericParams::new_all_used();
@@ -100,13 +102,6 @@ fn should_polymorphize<'tcx>(
100102
return false;
101103
}
102104

103-
// Polymorphization results are stored in cross-crate metadata only when there are unused
104-
// parameters, so assume that non-local items must have only used parameters (else this query
105-
// would not be invoked, and the cross-crate metadata used instead).
106-
if !def_id.is_local() {
107-
return false;
108-
}
109-
110105
// Foreign items have no bodies to analyze.
111106
if tcx.is_foreign_item(def_id) {
112107
return false;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// compile-flags: -Zpolymorphize=on
2+
3+
#[inline(never)]
4+
pub fn foo<T>() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// aux-build:poly-dep.rs
2+
// compile-flags: --crate-type=lib -Zprint-mono-items=eager -Zpolymorphize=on
3+
4+
extern crate poly_dep;
5+
6+
pub static FN1: fn() = poly_dep::foo::<i32>;
7+
pub static FN2: fn() = poly_dep::foo::<u32>;
8+
9+
//~ MONO_ITEM static FN1
10+
//~ MONO_ITEM static FN2
11+
//~ MONO_ITEM fn poly_dep::foo::<T>

0 commit comments

Comments
 (0)