Skip to content

Commit

Permalink
Rollup merge of rust-lang#75333 - davidtwco:polymorphization-75260-fi…
Browse files Browse the repository at this point in the history
…xes, r=lcnr

polymorphize: constrain unevaluated const handling

This PR constrains the support added for handling unevaluated consts in polymorphization (introduced in rust-lang#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 rust-lang#75260 and rust-lang#75255, some tests started failing when polymorphization was enabled that I didn't notice until after landing - this PR fixes the regressions from rust-lang#75260.

r? @lcnr
  • Loading branch information
JohnTitor authored Aug 11, 2020
2 parents c27779f + 20f4e16 commit 06f296a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/librustc_mir/monomorphize/polymorphize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,21 @@ impl<'a, 'tcx> TypeVisitor<'tcx> for UsedGenericParametersVisitor<'a, 'tcx> {
self.unused_parameters.clear(param.index);
false
}
ty::ConstKind::Unevaluated(_, _, Some(p)) => {
ty::ConstKind::Unevaluated(def, _, Some(p))
// Avoid considering `T` unused when constants are of the form:
// `<Self as Foo<T>>::foo::promoted[p]`
if self.def_id == def.did && !self.tcx.generics_of(def.did).has_self =>
{
// If there is a promoted, don't look at the substs - since it will always contain
// the generic parameters, instead, traverse the promoted MIR.
let promoted = self.tcx.promoted_mir(self.def_id);
let promoted = self.tcx.promoted_mir(def.did);
self.visit_body(&promoted[p]);
false
}
ty::ConstKind::Unevaluated(def_id, unevaluated_substs, None) => {
self.visit_child_body(def_id.did, unevaluated_substs);
ty::ConstKind::Unevaluated(def, unevaluated_substs, None)
if self.tcx.def_kind(def.did) == DefKind::AnonConst =>
{
self.visit_child_body(def.did, unevaluated_substs);
false
}
_ => c.super_visit_with(self),
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/polymorphization/promoted-function-3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// run-pass
// compile-flags: -Zpolymorphize=on -Zmir-opt-level=3

fn caller<T, U>() -> &'static usize {
callee::<U>()
}

fn callee<T>() -> &'static usize {
&std::mem::size_of::<T>()
}

fn main() {
assert_eq!(caller::<(), ()>(), &0);
}

0 comments on commit 06f296a

Please sign in to comment.