Skip to content

Commit 9989288

Browse files
committed
Permit bindings of (and references to) associated types defined in supertraits.
1 parent 2ccab19 commit 9989288

5 files changed

+224
-13
lines changed

src/librustc_typeck/astconv.rs

+47-13
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ use middle::def;
5353
use middle::resolve_lifetime as rl;
5454
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs};
5555
use middle::subst::{VecPerParamSpace};
56-
use middle::ty::{self, RegionEscape, Ty};
56+
use middle::traits;
57+
use middle::ty::{self, RegionEscape, ToPolyTraitRef, Ty};
5758
use rscope::{self, UnelidableRscope, RegionScope, SpecificRscope,
5859
ShiftedRscope, BindingRscope};
5960
use TypeAndSubsts;
@@ -637,7 +638,7 @@ fn ast_path_to_trait_ref<'a,'tcx>(
637638
trait_ref
638639
}
639640

640-
pub fn ast_type_binding_to_projection_predicate<'tcx>(
641+
fn ast_type_binding_to_projection_predicate<'tcx>(
641642
this: &AstConv<'tcx>,
642643
trait_ref: Rc<ty::TraitRef<'tcx>>,
643644
binding: &ConvertedBinding<'tcx>)
@@ -659,20 +660,56 @@ pub fn ast_type_binding_to_projection_predicate<'tcx>(
659660
//
660661
// We want to produce `<B as SuperTrait<int>>::T == foo`.
661662

662-
// FIXME(#19541): supertrait upcasting not actually impl'd :)
663+
// Simple case: X is defined in the current trait.
664+
if trait_defines_associated_type_named(this, trait_ref.def_id, binding.item_name) {
665+
return Ok(ty::ProjectionPredicate {
666+
projection_ty: ty::ProjectionTy {
667+
trait_ref: trait_ref,
668+
item_name: binding.item_name,
669+
},
670+
ty: binding.ty,
671+
});
672+
}
673+
674+
// Otherwise, we have to walk through the supertraits to find those that do.
675+
let mut candidates: Vec<_> =
676+
traits::supertraits(this.tcx(), trait_ref.to_poly_trait_ref())
677+
.filter(|r| trait_defines_associated_type_named(this, r.def_id(), binding.item_name))
678+
.collect();
679+
680+
if candidates.len() > 1 {
681+
this.tcx().sess.span_err(
682+
binding.span,
683+
format!("ambiguous associated type: `{}` defined in multiple supertraits `{}`",
684+
token::get_name(binding.item_name),
685+
candidates.user_string(this.tcx())).as_slice());
686+
return Err(ErrorReported);
687+
}
688+
689+
let candidate = match candidates.pop() {
690+
Some(c) => c,
691+
None => {
692+
this.tcx().sess.span_err(
693+
binding.span,
694+
format!("no associated type `{}` defined in `{}`",
695+
token::get_name(binding.item_name),
696+
trait_ref.user_string(this.tcx())).as_slice());
697+
return Err(ErrorReported);
698+
}
699+
};
663700

664-
if !trait_defines_associated_type_named(this, trait_ref.def_id, binding.item_name) {
701+
if ty::binds_late_bound_regions(this.tcx(), &candidate) {
665702
this.tcx().sess.span_err(
666703
binding.span,
667-
format!("no associated type `{}` defined in `{}`",
704+
format!("associated type `{}` defined in higher-ranked supertrait `{}`",
668705
token::get_name(binding.item_name),
669-
trait_ref.user_string(this.tcx())).as_slice());
706+
candidate.user_string(this.tcx())).as_slice());
670707
return Err(ErrorReported);
671708
}
672709

673710
Ok(ty::ProjectionPredicate {
674711
projection_ty: ty::ProjectionTy {
675-
trait_ref: trait_ref,
712+
trait_ref: candidate.0,
676713
item_name: binding.item_name,
677714
},
678715
ty: binding.ty,
@@ -899,20 +936,17 @@ fn associated_path_def_to_ty<'tcx>(this: &AstConv<'tcx>,
899936
{
900937
let tcx = this.tcx();
901938
let ty_param_def_id = provenance.def_id();
939+
902940
let mut suitable_bounds: Vec<_>;
903941
let ty_param_name: ast::Name;
904942
{ // contain scope of refcell:
905943
let ty_param_defs = tcx.ty_param_defs.borrow();
906944
let ty_param_def = &ty_param_defs[ty_param_def_id.node];
907945
ty_param_name = ty_param_def.name;
908946

909-
// FIXME(#19541): we should consider associated types in
910-
// super-traits. Probably by elaborating the bounds.
911-
947+
// FIXME(#20300) -- search where clauses, not bounds
912948
suitable_bounds =
913-
ty_param_def.bounds.trait_bounds // FIXME(#20300) -- search where clauses, not bounds
914-
.iter()
915-
.cloned()
949+
traits::transitive_bounds(tcx, ty_param_def.bounds.trait_bounds.as_slice())
916950
.filter(|b| trait_defines_associated_type_named(this, b.def_id(), assoc_name))
917951
.collect();
918952
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2014 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+
// Test equality constraints in a where clause where the type being
12+
// equated appears in a supertrait.
13+
14+
#![feature(associated_types)]
15+
16+
pub trait Vehicle {
17+
type Color;
18+
19+
fn go(&self) { }
20+
}
21+
22+
pub trait Box {
23+
type Color;
24+
25+
fn mail(&self) { }
26+
}
27+
28+
pub trait BoxCar : Box + Vehicle {
29+
}
30+
31+
fn dent<C:BoxCar>(c: C, color: C::Color) {
32+
//~^ ERROR ambiguous associated type `Color` in bounds of `C`
33+
//~| NOTE could derive from `Vehicle`
34+
//~| NOTE could derive from `Box`
35+
}
36+
37+
fn dent_object<COLOR>(c: BoxCar<Color=COLOR>) {
38+
//~^ ERROR ambiguous associated type
39+
}
40+
41+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2014 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+
// Test equality constraints in a where clause where the type being
12+
// equated appears in a supertrait.
13+
14+
#![feature(associated_types)]
15+
16+
pub trait Vehicle {
17+
type Color;
18+
19+
fn go(&self) { }
20+
}
21+
22+
pub trait Car : Vehicle {
23+
fn honk(&self) { }
24+
fn chip_paint(&self, c: Self::Color) { }
25+
}
26+
27+
///////////////////////////////////////////////////////////////////////////
28+
29+
struct Black;
30+
struct ModelT;
31+
impl Vehicle for ModelT { type Color = Black; }
32+
impl Car for ModelT { }
33+
34+
///////////////////////////////////////////////////////////////////////////
35+
36+
struct Blue;
37+
struct ModelU;
38+
impl Vehicle for ModelU { type Color = Blue; }
39+
impl Car for ModelU { }
40+
41+
///////////////////////////////////////////////////////////////////////////
42+
43+
fn dent<C:Car>(c: C, color: C::Color) { c.chip_paint(color) }
44+
fn a() { dent(ModelT, Black); }
45+
fn b() { dent(ModelT, Blue); } //~ ERROR type mismatch
46+
fn c() { dent(ModelU, Black); } //~ ERROR type mismatch
47+
fn d() { dent(ModelU, Blue); }
48+
49+
///////////////////////////////////////////////////////////////////////////
50+
51+
fn e() { ModelT.chip_paint(Black); }
52+
fn f() { ModelT.chip_paint(Blue); } //~ ERROR mismatched types
53+
fn g() { ModelU.chip_paint(Black); } //~ ERROR mismatched types
54+
fn h() { ModelU.chip_paint(Blue); }
55+
56+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2014 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+
// Test equality constraints in a where clause where the type being
12+
// equated appears in a supertrait.
13+
14+
#![feature(associated_types)]
15+
16+
pub trait Vehicle {
17+
type Color;
18+
19+
fn go(&self) { }
20+
}
21+
22+
pub trait Car : Vehicle {
23+
fn honk(&self) { }
24+
}
25+
26+
///////////////////////////////////////////////////////////////////////////
27+
28+
struct Black;
29+
struct ModelT;
30+
impl Vehicle for ModelT { type Color = Black; }
31+
impl Car for ModelT { }
32+
33+
///////////////////////////////////////////////////////////////////////////
34+
35+
struct Blue;
36+
struct ModelU;
37+
impl Vehicle for ModelU { type Color = Blue; }
38+
impl Car for ModelU { }
39+
40+
///////////////////////////////////////////////////////////////////////////
41+
42+
fn black_car<C:Car<Color=Black>>(c: C) {
43+
}
44+
45+
fn blue_car<C:Car<Color=Blue>>(c: C) {
46+
}
47+
48+
fn a() { black_car(ModelT); }
49+
fn b() { blue_car(ModelT); } //~ ERROR type mismatch
50+
fn c() { black_car(ModelU); } //~ ERROR type mismatch
51+
fn d() { blue_car(ModelU); }
52+
53+
pub fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2014 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+
fn pairwise_sub<T:DoubleEndedIterator<Item=int>>(mut t: T) -> int {
12+
let mut result = 0;
13+
loop {
14+
let front = t.next();
15+
let back = t.next_back();
16+
match (front, back) {
17+
(Some(f), Some(b)) => { result += b - f; }
18+
_ => { return result; }
19+
}
20+
}
21+
}
22+
23+
fn main() {
24+
let v = vec!(1, 2, 3, 4, 5, 6);
25+
let r =pairwise_sub(v.into_iter());
26+
assert_eq!(r, 9);
27+
}

0 commit comments

Comments
 (0)