From 7bd19112ee30925b636ebbe26de9e043b47cb67f Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 12 Jan 2015 10:20:14 -0500 Subject: [PATCH] Patch variance bug: appearing in a binding is an invariant position (at least right now). --- src/librustc_typeck/variance.rs | 6 +++++ .../compile-fail/variance-object-types.rs | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 src/test/compile-fail/variance-object-types.rs diff --git a/src/librustc_typeck/variance.rs b/src/librustc_typeck/variance.rs index ed8a50110e5a..63ad47ff31f6 100644 --- a/src/librustc_typeck/variance.rs +++ b/src/librustc_typeck/variance.rs @@ -818,6 +818,12 @@ impl<'a, 'tcx> ConstraintContext<'a, 'tcx> { trait_def.generics.regions.get_slice(subst::TypeSpace), trait_ref.substs(), variance); + + let projections = data.projection_bounds_with_self_ty(self.tcx(), + self.tcx().types.err); + for projection in projections.iter() { + self.add_constraints_from_ty(generics, projection.0.ty, self.invariant); + } } ty::ty_param(ref data) => { diff --git a/src/test/compile-fail/variance-object-types.rs b/src/test/compile-fail/variance-object-types.rs new file mode 100644 index 000000000000..972ec96f5f27 --- /dev/null +++ b/src/test/compile-fail/variance-object-types.rs @@ -0,0 +1,24 @@ +// Copyright 2012 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. + +// Test that Cell is considered invariant with respect to its +// type. + +use std::cell::Cell; + +// For better or worse, associated types are invariant, and hence we +// get an invariant result for `'a`. +#[rustc_variance] +struct Foo<'a> { //~ ERROR regions=[[o];[];[]] + x: Box &'a i32 + 'static> +} + +fn main() { +}