Skip to content

Commit 97f0d7f

Browse files
jackh726Mark-Simulacrum
authored andcommitted
Revert rust-lang#92191 Prefer projection candidates instead of param_env candidates for Sized predicates
1 parent 169374f commit 97f0d7f

File tree

5 files changed

+64
-17
lines changed

5 files changed

+64
-17
lines changed

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

-4
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
175175

176176
let needs_infer = stack.obligation.predicate.has_infer_types_or_consts();
177177

178-
let sized_predicate = self.tcx().lang_items().sized_trait()
179-
== Some(stack.obligation.predicate.skip_binder().def_id());
180-
181178
// If there are STILL multiple candidates, we can further
182179
// reduce the list by dropping duplicates -- including
183180
// resolving specializations.
@@ -186,7 +183,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
186183
while i < candidates.len() {
187184
let is_dup = (0..candidates.len()).filter(|&j| i != j).any(|j| {
188185
self.candidate_should_be_dropped_in_favor_of(
189-
sized_predicate,
190186
&candidates[i],
191187
&candidates[j],
192188
needs_infer,

compiler/rustc_trait_selection/src/traits/select/mod.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -1553,7 +1553,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
15531553
/// See the comment for "SelectionCandidate" for more details.
15541554
fn candidate_should_be_dropped_in_favor_of(
15551555
&mut self,
1556-
sized_predicate: bool,
15571556
victim: &EvaluatedCandidate<'tcx>,
15581557
other: &EvaluatedCandidate<'tcx>,
15591558
needs_infer: bool,
@@ -1625,16 +1624,6 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16251624
// Drop otherwise equivalent non-const fn pointer candidates
16261625
(FnPointerCandidate { .. }, FnPointerCandidate { is_const: false }) => true,
16271626

1628-
// If obligation is a sized predicate or the where-clause bound is
1629-
// global, prefer the projection or object candidate. See issue
1630-
// #50825 and #89352.
1631-
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
1632-
sized_predicate || is_global(cand)
1633-
}
1634-
(ParamCandidate(ref cand), ObjectCandidate(_) | ProjectionCandidate(_)) => {
1635-
!(sized_predicate || is_global(cand))
1636-
}
1637-
16381627
// Global bounds from the where clause should be ignored
16391628
// here (see issue #50825). Otherwise, we have a where
16401629
// clause so don't go around looking for impls.
@@ -1650,8 +1639,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
16501639
| BuiltinUnsizeCandidate
16511640
| TraitUpcastingUnsizeCandidate(_)
16521641
| BuiltinCandidate { .. }
1653-
| TraitAliasCandidate(..),
1642+
| TraitAliasCandidate(..)
1643+
| ObjectCandidate(_)
1644+
| ProjectionCandidate(_),
16541645
) => !is_global(cand),
1646+
(ObjectCandidate(_) | ProjectionCandidate(_), ParamCandidate(ref cand)) => {
1647+
// Prefer these to a global where-clause bound
1648+
// (see issue #50825).
1649+
is_global(cand)
1650+
}
16551651
(
16561652
ImplCandidate(_)
16571653
| ClosureCandidate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-89352.rs:36:13
3+
|
4+
LL | let a = A::reborrow::<'ai, 's>(self.a.clone());
5+
| ^ lifetime mismatch
6+
|
7+
= note: expected type `<<A as GenAssoc<T>>::Iter<'s> as Sized>`
8+
found type `<<A as GenAssoc<T>>::Iter<'ai> as Sized>`
9+
note: the lifetime `'s` as defined here...
10+
--> $DIR/issue-89352.rs:35:13
11+
|
12+
LL | fn iter<'s>(&'s self) -> Self::Iter<'s> {
13+
| ^^
14+
note: ...does not necessarily outlive the lifetime `'ai` as defined here
15+
--> $DIR/issue-89352.rs:30:6
16+
|
17+
LL | impl<'ai, T: 'ai, A: GenAssoc<T>> GenAssoc<T> for Wrapper<'ai, T, A>
18+
| ^^^
19+
20+
error: aborting due to previous error
21+
22+
For more information about this error, try `rustc --explain E0308`.

src/test/ui/generic-associated-types/issue-89352.rs src/test/ui/generic-associated-types/bugs/issue-89352.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
// check-pass
1+
// revisions: base nll
2+
// ignore-compare-mode-nll
3+
//[nll] compile-flags: -Z borrowck=mir
4+
5+
//[base] check-fail
6+
//[nll] check-pass
7+
// known-bug
8+
9+
// This should pass, but we end up with `A::Iter<'ai>: Sized` for some specific
10+
// `'ai`. We also know that `for<'at> A::Iter<'at>: Sized` from the definition,
11+
// but we prefer param env candidates. We changed this to preference in #92191,
12+
// but this led to unintended consequences (#93262). Suprisingly, this passes
13+
// under NLL. So only a bug in migrate mode.
214

315
#![feature(generic_associated_types)]
416

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// check-pass
2+
3+
#![feature(generic_associated_types)]
4+
5+
pub trait Trait {
6+
type Assoc<'a> where Self: 'a;
7+
}
8+
9+
pub trait Foo<T: Trait>
10+
where
11+
for<'a> T::Assoc<'a>: Clone
12+
{}
13+
14+
pub struct Type;
15+
16+
impl<T: Trait> Foo<T> for Type
17+
where
18+
for<'a> T::Assoc<'a>: Clone
19+
{}
20+
21+
fn main() {}

0 commit comments

Comments
 (0)