Skip to content

Commit 8fa5f09

Browse files
committed
distinguish projections from the env/obj-types vs those from
trait definitions, and give prefence to the former. This is consistent with what we do for selection. It also works around a limitation that was leading to #28871.
1 parent 83cf3ce commit 8fa5f09

File tree

2 files changed

+93
-12
lines changed

2 files changed

+93
-12
lines changed

src/librustc/middle/traits/project.rs

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,19 @@ pub struct MismatchedProjectionTypes<'tcx> {
5454

5555
#[derive(PartialEq, Eq, Debug)]
5656
enum ProjectionTyCandidate<'tcx> {
57+
// from a where-clause in the env or object type
5758
ParamEnv(ty::PolyProjectionPredicate<'tcx>),
59+
60+
// from the definition of `Trait` when you have something like <<A as Trait>::B as Trait2>::C
61+
TraitDef(ty::PolyProjectionPredicate<'tcx>),
62+
63+
// defined in an impl
5864
Impl(VtableImplData<'tcx, PredicateObligation<'tcx>>),
65+
66+
// closure return type
5967
Closure(VtableClosureData<'tcx, PredicateObligation<'tcx>>),
68+
69+
// fn pointer return type
6070
FnPointer(Ty<'tcx>),
6171
}
6272

@@ -491,7 +501,11 @@ fn project_type<'cx,'tcx>(
491501
candidates.vec.len(),
492502
candidates.ambiguous);
493503

494-
// We probably need some winnowing logic similar to select here.
504+
// Inherent ambiguity that prevents us from even enumerating the
505+
// candidates.
506+
if candidates.ambiguous {
507+
return Err(ProjectionTyError::TooManyCandidates);
508+
}
495509

496510
// Drop duplicates.
497511
//
@@ -512,10 +526,30 @@ fn project_type<'cx,'tcx>(
512526
}
513527
}
514528

515-
if candidates.ambiguous || candidates.vec.len() > 1 {
516-
return Err(ProjectionTyError::TooManyCandidates);
529+
// Prefer where-clauses. As in select, if there are multiple
530+
// candidates, we prefer where-clause candidates over impls. This
531+
// may seem a bit surprising, since impls are the source of
532+
// "truth" in some sense, but in fact some of the impls that SEEM
533+
// applicable are not, because of nested obligations. Where
534+
// clauses are the safer choice. See the comment on
535+
// `select::SelectionCandidate` and #21974 for more details.
536+
if candidates.vec.len() > 1 {
537+
debug!("retaining param-env candidates only from {:?}", candidates.vec);
538+
candidates.vec.retain(|c| match *c {
539+
ProjectionTyCandidate::ParamEnv(..) => true,
540+
ProjectionTyCandidate::Impl(..) |
541+
ProjectionTyCandidate::Closure(..) |
542+
ProjectionTyCandidate::TraitDef(..) |
543+
ProjectionTyCandidate::FnPointer(..) => false,
544+
});
545+
debug!("resulting candidate set: {:?}", candidates.vec);
546+
if candidates.vec.len() != 1 {
547+
return Err(ProjectionTyError::TooManyCandidates);
548+
}
517549
}
518550

551+
assert!(candidates.vec.len() <= 1);
552+
519553
match candidates.vec.pop() {
520554
Some(candidate) => {
521555
let (ty, obligations) = confirm_candidate(selcx, obligation, candidate);
@@ -538,9 +572,14 @@ fn assemble_candidates_from_param_env<'cx,'tcx>(
538572
obligation_trait_ref: &ty::TraitRef<'tcx>,
539573
candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
540574
{
575+
debug!("assemble_candidates_from_param_env(..)");
541576
let env_predicates = selcx.param_env().caller_bounds.iter().cloned();
542-
assemble_candidates_from_predicates(selcx, obligation, obligation_trait_ref,
543-
candidate_set, env_predicates);
577+
assemble_candidates_from_predicates(selcx,
578+
obligation,
579+
obligation_trait_ref,
580+
candidate_set,
581+
ProjectionTyCandidate::ParamEnv,
582+
env_predicates);
544583
}
545584

546585
/// In the case of a nested projection like <<A as Foo>::FooT as Bar>::BarT, we may find
@@ -559,6 +598,8 @@ fn assemble_candidates_from_trait_def<'cx,'tcx>(
559598
obligation_trait_ref: &ty::TraitRef<'tcx>,
560599
candidate_set: &mut ProjectionTyCandidateSet<'tcx>)
561600
{
601+
debug!("assemble_candidates_from_trait_def(..)");
602+
562603
// Check whether the self-type is itself a projection.
563604
let trait_ref = match obligation_trait_ref.self_ty().sty {
564605
ty::TyProjection(ref data) => data.trait_ref.clone(),
@@ -575,15 +616,20 @@ fn assemble_candidates_from_trait_def<'cx,'tcx>(
575616
let trait_predicates = selcx.tcx().lookup_predicates(trait_ref.def_id);
576617
let bounds = trait_predicates.instantiate(selcx.tcx(), trait_ref.substs);
577618
let bounds = elaborate_predicates(selcx.tcx(), bounds.predicates.into_vec());
578-
assemble_candidates_from_predicates(selcx, obligation, obligation_trait_ref,
579-
candidate_set, bounds)
619+
assemble_candidates_from_predicates(selcx,
620+
obligation,
621+
obligation_trait_ref,
622+
candidate_set,
623+
ProjectionTyCandidate::TraitDef,
624+
bounds)
580625
}
581626

582627
fn assemble_candidates_from_predicates<'cx,'tcx,I>(
583628
selcx: &mut SelectionContext<'cx,'tcx>,
584629
obligation: &ProjectionTyObligation<'tcx>,
585630
obligation_trait_ref: &ty::TraitRef<'tcx>,
586631
candidate_set: &mut ProjectionTyCandidateSet<'tcx>,
632+
ctor: fn(ty::PolyProjectionPredicate<'tcx>) -> ProjectionTyCandidate<'tcx>,
587633
env_predicates: I)
588634
where I: Iterator<Item=ty::Predicate<'tcx>>
589635
{
@@ -614,8 +660,7 @@ fn assemble_candidates_from_predicates<'cx,'tcx,I>(
614660
data, is_match, same_name);
615661

616662
if is_match {
617-
candidate_set.vec.push(
618-
ProjectionTyCandidate::ParamEnv(data.clone()));
663+
candidate_set.vec.push(ctor(data.clone()));
619664
}
620665
}
621666
_ => { }
@@ -647,8 +692,12 @@ fn assemble_candidates_from_object_type<'cx,'tcx>(
647692
.map(|p| p.to_predicate())
648693
.collect();
649694
let env_predicates = elaborate_predicates(selcx.tcx(), env_predicates);
650-
assemble_candidates_from_predicates(selcx, obligation, obligation_trait_ref,
651-
candidate_set, env_predicates)
695+
assemble_candidates_from_predicates(selcx,
696+
obligation,
697+
obligation_trait_ref,
698+
candidate_set,
699+
ProjectionTyCandidate::ParamEnv,
700+
env_predicates)
652701
}
653702

654703
fn assemble_candidates_from_impls<'cx,'tcx>(
@@ -746,7 +795,8 @@ fn confirm_candidate<'cx,'tcx>(
746795
obligation);
747796

748797
match candidate {
749-
ProjectionTyCandidate::ParamEnv(poly_projection) => {
798+
ProjectionTyCandidate::ParamEnv(poly_projection) |
799+
ProjectionTyCandidate::TraitDef(poly_projection) => {
750800
confirm_param_env_candidate(selcx, obligation, poly_projection)
751801
}
752802

src/test/run-pass/issue-28871.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #28871. The problem is that rustc encountered
12+
// two ways to project, one from a where clause and one from the where
13+
// clauses on the trait definition. (In fact, in this case, the where
14+
// clauses originated from the trait definition as well.) The true
15+
// cause of the error is that the trait definition where clauses are
16+
// not being normalized, and hence the two sources are considered in
17+
// conflict, and not a duplicate. Hacky solution is to prefer where
18+
// clauses over the data found in the trait definition.
19+
20+
trait T {
21+
type T;
22+
}
23+
24+
struct S;
25+
impl T for S {
26+
type T = S;
27+
}
28+
29+
trait T2 {
30+
type T: Iterator<Item=<S as T>::T>;
31+
}

0 commit comments

Comments
 (0)