Skip to content

Commit b5da410

Browse files
committed
Remove contravariance; fix a bug in GLB; adjust tests
1 parent 20f421c commit b5da410

File tree

12 files changed

+31
-39
lines changed

12 files changed

+31
-39
lines changed

src/librustc/middle/infer/higher_ranked/README.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,14 +317,13 @@ in computing the replacements for the various variables. For each
317317
region `R` that appears in the type `F`, we again compute `Tainted(R)`
318318
and examine the results:
319319

320-
1. If `R` is not in `V`, it is not replaced.
321-
2. Else, if `Tainted(R)` contains only variables in `V`, and it
320+
1. If `Tainted(R)` contains only variables in `V`, and it
322321
contains exactly one variable from the LHS and one variable from
323322
the RHS, then `R` can be mapped to the bound version of the
324323
variable from the LHS.
325-
3. Else, if `Tainted(R)` contains no variable from the LHS and no
324+
2. Else, if `Tainted(R)` contains no variable from the LHS and no
326325
variable from the RHS, then `R` can be mapped to itself.
327-
4. Else, `R` is mapped to a fresh bound variable.
326+
3. Else, `R` is mapped to a fresh bound variable.
328327

329328
These rules are pretty complex. Let's look at some examples to see
330329
how they play out.

src/librustc/middle/infer/higher_ranked/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,11 +247,6 @@ impl<'a,'tcx> HigherRankedRelations<'a,'tcx> for CombineFields<'a,'tcx> {
247247
a_vars: &[ty::RegionVid],
248248
b_vars: &[ty::RegionVid],
249249
r0: ty::Region) -> ty::Region {
250-
if !is_var_in_set(new_vars, r0) {
251-
assert!(!r0.is_bound());
252-
return r0;
253-
}
254-
255250
let tainted = infcx.tainted_regions(snapshot, r0);
256251

257252
let mut a_r = None;

src/librustc/middle/ty_relate/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,9 @@ fn relate_arg_vecs<'a,'tcx:'a,R>(relation: &mut R,
290290
return Err(ty::terr_arg_count);
291291
}
292292

293-
a_args.iter().zip(b_args)
294-
.map(|(a, b)| relation.relate_with_variance(ty::Contravariant, a, b))
293+
a_args.iter()
294+
.zip(b_args)
295+
.map(|(a, b)| relation.relate_with_variance(ty::Invariant, a, b))
295296
.collect()
296297
}
297298

src/librustc_driver/test.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,8 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
415415
panic!("unexpected error computing LUB: {:?}", e)
416416
}
417417
Ok(t) => {
418+
debug!("glb yielded: y={}", t);
419+
418420
self.assert_eq(t, t_glb);
419421

420422
// sanity check for good measure:

src/librustc_typeck/variance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,9 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> {
10271027
generics: &ty::Generics<'tcx>,
10281028
sig: &ty::PolyFnSig<'tcx>,
10291029
variance: VarianceTermPtr<'a>) {
1030-
let contra = self.contravariant(variance);
1030+
let invariant = self.invariant(variance);
10311031
for &input in &sig.0.inputs {
1032-
self.add_constraints_from_ty(generics, input, contra);
1032+
self.add_constraints_from_ty(generics, input, invariant);
10331033
}
10341034
if let ty::FnConverging(result_type) = sig.0.output {
10351035
self.add_constraints_from_ty(generics, result_type, variance);

src/test/compile-fail/regions-assoc-type-outlives-container-hrtb.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test that structs with higher-ranked where clauses don't generate
12-
// "outlives" requirements. Issue #22246.
11+
// Test structs with higher-ranked where clauses and "outlives"
12+
// requirements. Issue #22246.
1313

1414
#![allow(dead_code)]
1515
#![feature(rustc_attrs)]
@@ -37,10 +37,9 @@ pub struct WithHrAssoc<T>
3737
}
3838

3939
fn with_assoc<'a,'b>() {
40-
// We get no error here because the where clause has a higher-ranked assoc type,
41-
// which could not be projected from.
42-
40+
// We now get an error here because `&'a` can reach the `'b`:
4341
let _: &'a WithHrAssoc<TheType<'b>> = loop { };
42+
//~^ ERROR cannot infer
4443
}
4544

4645
///////////////////////////////////////////////////////////////////////////
@@ -61,8 +60,7 @@ fn with_assoc_sub<'a,'b>() {
6160
// extends a trait in a HR way.
6261

6362
let _: &'a WithHrAssocSub<TheType<'b>> = loop { };
63+
//~^ ERROR cannot infer
6464
}
6565

66-
#[rustc_error]
67-
fn main() { //~ ERROR compilation successful
68-
}
66+
fn main() {}

src/test/compile-fail/regions-assoc-type-outlives-container.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ fn without_assoc<'a,'b>() {
6464
// that `'b:'a` holds.
6565

6666
let _: &'a WithoutAssoc<TheType<'b>> = loop { };
67+
//~^ ERROR cannot infer
6768
}
6869

6970
fn call_with_assoc<'a,'b>() {
@@ -73,12 +74,14 @@ fn call_with_assoc<'a,'b>() {
7374

7475
call::<&'a WithAssoc<TheType<'b>>>();
7576
//~^ ERROR cannot infer
77+
//~| ERROR reference has a longer lifetime
7678
}
7779

7880
fn call_without_assoc<'a,'b>() {
7981
// As `without_assoc`, but in a distinct scenario.
8082

8183
call::<&'a WithoutAssoc<TheType<'b>>>();
84+
//~^ ERROR reference has a longer lifetime
8285
}
8386

8487
fn call<T>() { }

src/test/run-pass/regions-variance-covariant-use-covariant.rs renamed to src/test/compile-fail/regions-variance-covariant-use-covariant.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ struct Covariant<'a> {
2424
}
2525

2626
fn use_<'a>(c: Covariant<'a>) {
27-
// OK Because Covariant<'a> <: Covariant<'static> iff 'a <= 'static
27+
// fn arguments are invariant now
2828
let _: Covariant<'static> = c;
29+
//~^ ERROR mismatched types
2930
}
3031

3132
pub fn main() {}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct Test2<'a, 'b, 'c> { //~ ERROR regions=[[-, -, -];[];[]]
2525
// Those same annotations in function arguments become covariant:
2626

2727
#[rustc_variance]
28-
struct Test3<'a, 'b, 'c> { //~ ERROR regions=[[+, +, +];[];[]]
28+
struct Test3<'a, 'b, 'c> { //~ ERROR regions=[[o, o, o];[];[]]
2929
x: extern "Rust" fn(&'a isize),
3030
y: extern "Rust" fn(&'b [isize]),
3131
c: extern "Rust" fn(&'c str),
@@ -42,7 +42,7 @@ struct Test4<'a, 'b:'a> { //~ ERROR regions=[[-, o];[];[]]
4242
// contravariant context:
4343

4444
#[rustc_variance]
45-
struct Test5<'a, 'b> { //~ ERROR regions=[[+, o];[];[]]
45+
struct Test5<'a, 'b> { //~ ERROR regions=[[o, o];[];[]]
4646
x: extern "Rust" fn(&'a mut &'b isize),
4747
}
4848

@@ -67,7 +67,7 @@ struct Test7<'a> { //~ ERROR regions=[[*];[];[]]
6767
// Try enums too.
6868

6969
#[rustc_variance]
70-
enum Test8<'a, 'b, 'c:'b> { //~ ERROR regions=[[+, -, o];[];[]]
70+
enum Test8<'a, 'b, 'c:'b> { //~ ERROR regions=[[o, -, o];[];[]]
7171
Test8A(extern "Rust" fn(&'a isize)),
7272
Test8B(&'b [isize]),
7373
Test8C(&'b mut &'c str),

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
#![feature(rustc_attrs)]
1616

1717
#[rustc_variance]
18-
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR regions=[[+, -, o, *];[];[]]
18+
enum Base<'a, 'b, 'c:'b, 'd> { //~ ERROR regions=[[o, -, o, *];[];[]]
1919
//~^ ERROR parameter `'d` is never used
2020
Test8A(extern "Rust" fn(&'a isize)),
2121
Test8B(&'b [isize]),
2222
Test8C(&'b mut &'c str),
2323
}
2424

2525
#[rustc_variance]
26-
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR regions=[[*, o, -, +];[];[]]
26+
struct Derived1<'w, 'x:'y, 'y, 'z> { //~ ERROR regions=[[*, o, -, o];[];[]]
2727
//~^ ERROR parameter `'w` is never used
2828
f: Base<'z, 'y, 'x, 'w>
2929
}
@@ -40,9 +40,4 @@ struct Derived3<'a:'b, 'b, 'c> { //~ ERROR regions=[[o, -, *];[];[]]
4040
f: Base<'a, 'b, 'a, 'c>
4141
}
4242

43-
#[rustc_variance] // Combine + and * to yield + (just pay attention to 'a here)
44-
struct Derived4<'a, 'b, 'c:'b> { //~ ERROR regions=[[+, -, o];[];[]]
45-
f: Base<'a, 'b, 'c, 'a>
46-
}
47-
4843
fn main() {}

0 commit comments

Comments
 (0)