Skip to content

Commit 06f296a

Browse files
authored
Rollup merge of #75333 - davidtwco:polymorphization-75260-fixes, r=lcnr
polymorphize: constrain unevaluated const handling This PR constrains the support added for handling unevaluated consts in polymorphization (introduced in #75260) by: - Skipping associated constants as this causes cycle errors. - Skipping promoted constants when they contain `Self` as this ensures `T` is used in constants of the form `<Self as Foo<T>>`. Due to an oversight on my part, when landing #75260 and #75255, some tests started failing when polymorphization was enabled that I didn't notice until after landing - this PR fixes the regressions from #75260. r? @lcnr
2 parents c27779f + 20f4e16 commit 06f296a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

src/librustc_mir/monomorphize/polymorphize.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -269,15 +269,21 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
269269
self.unused_parameters.clear(param.index);
270270
false
271271
}
272-
ty::ConstKind::Unevaluated(_, _, Some(p)) => {
272+
ty::ConstKind::Unevaluated(def, _, Some(p))
273+
// Avoid considering `T` unused when constants are of the form:
274+
// `<Self as Foo<T>>::foo::promoted[p]`
275+
if self.def_id == def.did && !self.tcx.generics_of(def.did).has_self =>
276+
{
273277
// If there is a promoted, don't look at the substs - since it will always contain
274278
// the generic parameters, instead, traverse the promoted MIR.
275-
let promoted = self.tcx.promoted_mir(self.def_id);
279+
let promoted = self.tcx.promoted_mir(def.did);
276280
self.visit_body(&promoted[p]);
277281
false
278282
}
279-
ty::ConstKind::Unevaluated(def_id, unevaluated_substs, None) => {
280-
self.visit_child_body(def_id.did, unevaluated_substs);
283+
ty::ConstKind::Unevaluated(def, unevaluated_substs, None)
284+
if self.tcx.def_kind(def.did) == DefKind::AnonConst =>
285+
{
286+
self.visit_child_body(def.did, unevaluated_substs);
281287
false
282288
}
283289
_ => c.super_visit_with(self),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// run-pass
2+
// compile-flags: -Zpolymorphize=on -Zmir-opt-level=3
3+
4+
fn caller<T, U>() -> &'static usize {
5+
callee::<U>()
6+
}
7+
8+
fn callee<T>() -> &'static usize {
9+
&std::mem::size_of::<T>()
10+
}
11+
12+
fn main() {
13+
assert_eq!(caller::<(), ()>(), &0);
14+
}

0 commit comments

Comments
 (0)