From c59fe8bde2be55c46f627277e2cc37515fb7165e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 25 Mar 2015 10:42:38 -0400 Subject: [PATCH] Drive-by fix for incorrect variance rule that I noticed. --- src/librustc_typeck/variance.rs | 24 +++++++++++++++--- .../compile-fail/variance-region-bounds.rs | 25 +++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/variance-region-bounds.rs diff --git a/src/librustc_typeck/variance.rs b/src/librustc_typeck/variance.rs index ac1ff29e7f5..3a716b28e73 100644 --- a/src/librustc_typeck/variance.rs +++ b/src/librustc_typeck/variance.rs @@ -1059,14 +1059,29 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { } ty::Predicate::Equate(ty::Binder(ref data)) => { - self.add_constraints_from_ty(generics, data.0, variance); - self.add_constraints_from_ty(generics, data.1, variance); + // A == B is only true if A and B are the same + // types, not subtypes of one another, so this is + // an invariant position: + self.add_constraints_from_ty(generics, data.0, self.invariant); + self.add_constraints_from_ty(generics, data.1, self.invariant); } ty::Predicate::TypeOutlives(ty::Binder(ref data)) => { - self.add_constraints_from_ty(generics, data.0, variance); + // Why contravariant on both? Let's consider: + // + // Under what conditions is `(T:'t) <: (U:'u)`, + // meaning that `(T:'t) => (U:'u)`. The answer is + // if `U <: T` or `'u <= 't`. Let's see some examples: + // + // (T: 'big) => (T: 'small) + // where 'small <= 'big + // + // (&'small Foo: 't) => (&'big Foo: 't) + // where 'small <= 'big + // note that &'big Foo <: &'small Foo let variance_r = self.xform(variance, self.contravariant); + self.add_constraints_from_ty(generics, data.0, variance_r); self.add_constraints_from_region(generics, data.1, variance_r); } @@ -1084,6 +1099,9 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { &*data.projection_ty.trait_ref, variance); + // as the equality predicate above, a binder is a + // type equality relation, not a subtyping + // relation self.add_constraints_from_ty(generics, data.ty, self.invariant); } } diff --git a/src/test/compile-fail/variance-region-bounds.rs b/src/test/compile-fail/variance-region-bounds.rs new file mode 100644 index 00000000000..96ae201f6ae --- /dev/null +++ b/src/test/compile-fail/variance-region-bounds.rs @@ -0,0 +1,25 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Check that `T:'a` is contravariant in T. + +#![feature(rustc_attrs)] + +#[rustc_variance] +trait Foo: 'static { //~ ERROR types=[[];[-];[]] +} + +#[rustc_variance] +trait Bar { //~ ERROR types=[[+];[-];[]] + fn do_it(&self) + where T: 'static; +} + +fn main() { }