Skip to content

Commit 898f978

Browse files
committed
add back variance testing mechanism
make it work for traits etc uniformly
1 parent 4355169 commit 898f978

File tree

7 files changed

+51
-19
lines changed

7 files changed

+51
-19
lines changed

src/librustc_typeck/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>)
318318
coherence::check_coherence(tcx));
319319
})?;
320320

321+
tcx.sess.track_errors(|| {
322+
time(time_passes, "variance testing", ||
323+
variance::test::test_variance(tcx));
324+
})?;
325+
321326
time(time_passes, "wf checking", || check::check_wf_new(tcx))?;
322327

323328
time(time_passes, "item-types checking", || check::check_item_types(tcx))?;

src/librustc_typeck/variance/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ mod constraints;
2929
/// Code to solve constraints and write out the results.
3030
mod solve;
3131

32+
/// Code to write unit tests of variance.
33+
pub mod test;
34+
3235
/// Code for transforming variances.
3336
mod xform;
3437

@@ -89,3 +92,4 @@ fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
8992
}
9093
}
9194
}
95+

src/librustc_typeck/variance/solve.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,6 @@ impl<'a, 'tcx> SolveContext<'a, 'tcx> {
128128

129129
let item_def_id = tcx.hir.local_def_id(item_id);
130130

131-
// For unit testing: check for a special "rustc_variance"
132-
// attribute and report an error with various results if found.
133-
if tcx.has_attr(item_def_id, "rustc_variance") {
134-
span_err!(tcx.sess,
135-
tcx.hir.span(item_id),
136-
E0208,
137-
"{:?}",
138-
item_variances);
139-
}
140-
141131
map.insert(item_def_id, Rc::new(item_variances));
142132
}
143133

src/librustc_typeck/variance/test.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2013 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+
use rustc::hir;
12+
use rustc::hir::itemlikevisit::ItemLikeVisitor;
13+
use rustc::ty::TyCtxt;
14+
15+
pub fn test_variance<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
16+
tcx.hir.krate().visit_all_item_likes(&mut VarianceTest { tcx });
17+
}
18+
19+
struct VarianceTest<'a, 'tcx: 'a> {
20+
tcx: TyCtxt<'a, 'tcx, 'tcx>
21+
}
22+
23+
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for VarianceTest<'a, 'tcx> {
24+
fn visit_item(&mut self, item: &'tcx hir::Item) {
25+
let item_def_id = self.tcx.hir.local_def_id(item.id);
26+
27+
// For unit testing: check for a special "rustc_variance"
28+
// attribute and report an error with various results if found.
29+
if self.tcx.has_attr(item_def_id, "rustc_variance") {
30+
let item_variances = self.tcx.variances_of(item_def_id);
31+
span_err!(self.tcx.sess,
32+
item.span,
33+
E0208,
34+
"{:?}",
35+
item_variances);
36+
}
37+
}
38+
39+
fn visit_trait_item(&mut self, _: &'tcx hir::TraitItem) { }
40+
fn visit_impl_item(&mut self, _: &'tcx hir::ImplItem) { }
41+
}

src/test/compile-fail/variance-regions-direct.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ struct Test6<'a, 'b:'a> { //~ ERROR [-, o]
6060

6161
#[rustc_variance]
6262
struct Test7<'a> { //~ ERROR [*]
63-
//~^ ERROR parameter `'a` is never used
6463
x: isize
6564
}
6665

src/test/compile-fail/variance-regions-indirect.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,23 @@
1616

1717
#[rustc_variance]
1818
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR [+, -, o, *]
19-
//~^ ERROR parameter `'d` is never used
2019
Test8A(extern "Rust" fn(&'a isize)),
2120
Test8B(&'b [isize]),
2221
Test8C(&'b mut &'c str),
2322
}
2423

2524
#[rustc_variance]
2625
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR [*, o, -, +]
27-
//~^ ERROR parameter `'w` is never used
2826
f: Base<'z, 'y, 'x, 'w>
2927
}
3028

3129
#[rustc_variance] // Combine - and + to yield o
3230
struct Derived2<'a, 'b:'a, 'c> { //~ ERROR [o, o, *]
33-
//~^ ERROR parameter `'c` is never used
3431
f: Base<'a, 'a, 'b, 'c>
3532
}
3633

3734
#[rustc_variance] // Combine + and o to yield o (just pay attention to 'a here)
3835
struct Derived3<'a:'b, 'b, 'c> { //~ ERROR [o, -, *]
39-
//~^ ERROR parameter `'c` is never used
4036
f: Base<'a, 'b, 'a, 'c>
4137
}
4238

src/test/compile-fail/variance-trait-bounds.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ struct TestStruct<U,T:Setter<U>> { //~ ERROR [+, +]
3030
}
3131

3232
#[rustc_variance]
33-
enum TestEnum<U,T:Setter<U>> {//~ ERROR [*, +]
34-
//~^ ERROR parameter `U` is never used
33+
enum TestEnum<U,T:Setter<U>> { //~ ERROR [*, +]
3534
Foo(T)
3635
}
3736

@@ -51,13 +50,11 @@ trait TestTrait3<U> { //~ ERROR [o, o]
5150

5251
#[rustc_variance]
5352
struct TestContraStruct<U,T:Setter<U>> { //~ ERROR [*, +]
54-
//~^ ERROR parameter `U` is never used
5553
t: T
5654
}
5755

5856
#[rustc_variance]
5957
struct TestBox<U,T:Getter<U>+Setter<U>> { //~ ERROR [*, +]
60-
//~^ ERROR parameter `U` is never used
6158
t: T
6259
}
6360

0 commit comments

Comments
 (0)