Skip to content

Commit f11f3e7

Browse files
committed
auto merge of #20572 : nikomatsakis/rust/assoc-supertrait-stuff, r=brson
The first few commits in the PR are just general refactoring. I was intending them for some other code I didn't get around to writing yet, but might as well land them now. cc @japaric Fixes #19541
2 parents 03268bb + 928bb2b commit f11f3e7

9 files changed

+504
-235
lines changed

src/librustc_typeck/astconv.rs

+184-134
Large diffs are not rendered by default.

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4939,7 +4939,7 @@ pub fn check_enum_variants(ccx: &CrateCtxt,
49394939
Some(i) => {
49404940
span_err!(ccx.tcx.sess, v.span, E0081,
49414941
"discriminant value `{}` already exists", disr_vals[i]);
4942-
span_note!(ccx.tcx.sess, ccx.tcx().map.span(variants[i].id.node),
4942+
span_note!(ccx.tcx.sess, ccx.tcx.map.span(variants[i].id.node),
49434943
"conflicting discriminant here")
49444944
}
49454945
None => {}

src/librustc_typeck/collect.rs

+107-98
Large diffs are not rendered by default.

src/librustc_typeck/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ struct TypeAndSubsts<'tcx> {
119119
struct CrateCtxt<'a, 'tcx: 'a> {
120120
// A mapping from method call sites to traits that have that method.
121121
trait_map: ty::TraitMap,
122-
tcx: &'a ty::ctxt<'tcx>
122+
tcx: &'a ty::ctxt<'tcx>,
123123
}
124124

125125
// Functions that write types into the node type table
@@ -320,7 +320,7 @@ pub fn check_crate(tcx: &ty::ctxt, trait_map: ty::TraitMap) {
320320
};
321321

322322
time(time_passes, "type collecting", (), |_|
323-
collect::collect_item_types(&ccx));
323+
collect::collect_item_types(tcx));
324324

325325
// this ensures that later parts of type checking can assume that items
326326
// have valid types and not error
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
fn paint<C:BoxCar>(c: C, d: C::Color) {
42+
//~^ ERROR ambiguous associated type `Color` in bounds of `C`
43+
//~| NOTE could derive from `Vehicle`
44+
//~| NOTE could derive from `Box`
45+
}
46+
47+
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(mut t: Box<DoubleEndedIterator<Item=int>>) -> 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(box v.into_iter());
26+
assert_eq!(r, 9);
27+
}
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)