Skip to content

Commit 9f1ead8

Browse files
committed
auto merge of #20655 : nikomatsakis/rust/carl-ice, r=aturon
Remember to check the name of the associated type being projected when searching the environment. Fixes #20651.
2 parents 2a8cb67 + ea441e1 commit 9f1ead8

File tree

3 files changed

+69
-1
lines changed

3 files changed

+69
-1
lines changed

src/librustc/middle/traits/project.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,9 @@ fn assemble_candidates_from_predicates<'cx,'tcx>(
452452
for predicate in elaborate_predicates(selcx.tcx(), env_predicates) {
453453
match predicate {
454454
ty::Predicate::Projection(ref data) => {
455-
let is_match = infcx.probe(|_| {
455+
let same_name = data.item_name() == obligation.predicate.item_name;
456+
457+
let is_match = same_name && infcx.probe(|_| {
456458
let origin = infer::Misc(obligation.cause.span);
457459
let obligation_poly_trait_ref =
458460
obligation_trait_ref.to_poly_trait_ref();
@@ -465,6 +467,9 @@ fn assemble_candidates_from_predicates<'cx,'tcx>(
465467
});
466468

467469
if is_match {
470+
debug!("assemble_candidates_from_predicates: candidate {}",
471+
data.repr(selcx.tcx()));
472+
468473
candidate_set.vec.push(
469474
ProjectionTyCandidate::ParamEnv(data.clone()));
470475
}
@@ -527,6 +532,9 @@ fn assemble_candidates_from_impls<'cx,'tcx>(
527532

528533
match vtable {
529534
super::VtableImpl(data) => {
535+
debug!("assemble_candidates_from_impls: impl candidate {}",
536+
data.repr(selcx.tcx()));
537+
530538
candidate_set.vec.push(
531539
ProjectionTyCandidate::Impl(data));
532540
}

src/librustc/middle/ty.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,10 @@ pub struct ProjectionPredicate<'tcx> {
18921892
pub type PolyProjectionPredicate<'tcx> = Binder<ProjectionPredicate<'tcx>>;
18931893

18941894
impl<'tcx> PolyProjectionPredicate<'tcx> {
1895+
pub fn item_name(&self) -> ast::Name {
1896+
self.0.projection_ty.item_name // safe to skip the binder to access a name
1897+
}
1898+
18951899
pub fn sort_key(&self) -> (ast::DefId, ast::Name) {
18961900
self.0.projection_ty.sort_key()
18971901
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
trait Foo {
12+
type X;
13+
type Y;
14+
}
15+
16+
fn have_x_want_x<T:Foo<X=u32>>(t: &T)
17+
{
18+
want_x(t);
19+
}
20+
21+
fn have_x_want_y<T:Foo<X=u32>>(t: &T)
22+
{
23+
want_y(t); //~ ERROR type mismatch
24+
}
25+
26+
fn have_y_want_x<T:Foo<Y=i32>>(t: &T)
27+
{
28+
want_x(t); //~ ERROR type mismatch
29+
}
30+
31+
fn have_y_want_y<T:Foo<Y=i32>>(t: &T)
32+
{
33+
want_y(t);
34+
}
35+
36+
fn have_xy_want_x<T:Foo<X=u32,Y=i32>>(t: &T)
37+
{
38+
want_x(t);
39+
}
40+
41+
fn have_xy_want_y<T:Foo<X=u32,Y=i32>>(t: &T)
42+
{
43+
want_y(t);
44+
}
45+
46+
fn have_xy_want_xy<T:Foo<X=u32,Y=i32>>(t: &T)
47+
{
48+
want_x(t);
49+
want_y(t);
50+
}
51+
52+
fn want_x<T:Foo<X=u32>>(t: &T) { }
53+
54+
fn want_y<T:Foo<Y=i32>>(t: &T) { }
55+
56+
fn main() { }

0 commit comments

Comments
 (0)