Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't consider predicates with HRTBs to be global during winnowing #95755

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions compiler/rustc_trait_selection/src/traits/select/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1562,13 +1562,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
return true;
}

// Check if a bound would previously have been removed when normalizing
// the param_env so that it can be given the lowest priority. See
// #50825 for the motivation for this.
let is_global = |cand: &ty::PolyTraitPredicate<'tcx>| {
cand.is_global() && !cand.has_late_bound_regions()
};

// (*) Prefer `BuiltinCandidate { has_nested: false }`, `PointeeCandidate`,
// `DiscriminantKindCandidate`, and `ConstDestructCandidate` to anything else.
//
Expand Down Expand Up @@ -1629,10 +1622,10 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// global, prefer the projection or object candidate. See issue
// #50825 and #89352.
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
sized_predicate || is_global(cand)
sized_predicate || cand.is_global()
}
(ParamCandidate(ref cand), ObjectCandidate(_) | ProjectionCandidate(_)) => {
!(sized_predicate || is_global(cand))
!(sized_predicate || cand.is_global())
}

// Global bounds from the where clause should be ignored
Expand All @@ -1651,7 +1644,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
| TraitUpcastingUnsizeCandidate(_)
| BuiltinCandidate { .. }
| TraitAliasCandidate(..),
) => !is_global(cand),
) => !cand.is_global(),
(
ImplCandidate(_)
| ClosureCandidate
Expand All @@ -1666,7 +1659,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
) => {
// Prefer these to a global where-clause bound
// (see issue #50825).
is_global(cand) && other.evaluation.must_apply_modulo_regions()
cand.is_global() && other.evaluation.must_apply_modulo_regions()
}

(ProjectionCandidate(i), ProjectionCandidate(j))
Expand Down
31 changes: 31 additions & 0 deletions src/test/ui/mir/issue-95640.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// build-pass
// compile-flags:-Zmir-opt-level=3

struct D;

trait Tr {
type It;
fn foo(self) -> Option<Self::It>;
}

impl<'a> Tr for &'a D {
type It = ();
fn foo(self) -> Option<()> {
None
}
}

fn run<F>(f: F)
where
for<'a> &'a D: Tr,
F: Fn(<&D as Tr>::It),
{
let d = &D;
while let Some(i) = d.foo() {
f(i);
}
}

fn main() {
run(|_| {});
}