From 9c9bb9ce1d51e2a9ca4963bd418e365b6e17fbfa Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 24 Mar 2015 15:55:29 -0400 Subject: Implement `Reflect` trait with a variant on the standard OIBIT semantics that tests the *interface* of trait objects, rather than what they close over. --- src/test/auxiliary/typeid-intrinsic.rs | 4 +-- src/test/auxiliary/typeid-intrinsic2.rs | 4 +-- src/test/compile-fail/reflect-assoc.rs | 35 ++++++++++++++++++ src/test/compile-fail/reflect-object-param.rs | 47 +++++++++++++++++++++++++ src/test/compile-fail/reflect.rs | 39 ++++++++++++++++++++ src/test/run-pass/object-one-type-two-traits.rs | 2 +- src/test/run-pass/type-id-higher-rank.rs | 4 +-- 7 files changed, 128 insertions(+), 7 deletions(-) create mode 100644 src/test/compile-fail/reflect-assoc.rs create mode 100644 src/test/compile-fail/reflect-object-param.rs create mode 100644 src/test/compile-fail/reflect.rs (limited to 'src/test') diff --git a/src/test/auxiliary/typeid-intrinsic.rs b/src/test/auxiliary/typeid-intrinsic.rs index 82d07a9df4e..bd47054f093 100644 --- a/src/test/auxiliary/typeid-intrinsic.rs +++ b/src/test/auxiliary/typeid-intrinsic.rs @@ -10,7 +10,7 @@ #![feature(core)] -use std::any::TypeId; +use std::any::{Any, TypeId}; pub struct A; pub struct B(Option); @@ -31,4 +31,4 @@ pub unsafe fn id_F() -> TypeId { TypeId::of::() } pub unsafe fn id_G() -> TypeId { TypeId::of::() } pub unsafe fn id_H() -> TypeId { TypeId::of::() } -pub unsafe fn foo() -> TypeId { TypeId::of::() } +pub unsafe fn foo() -> TypeId { TypeId::of::() } diff --git a/src/test/auxiliary/typeid-intrinsic2.rs b/src/test/auxiliary/typeid-intrinsic2.rs index 82d07a9df4e..5e81bf50ae4 100644 --- a/src/test/auxiliary/typeid-intrinsic2.rs +++ b/src/test/auxiliary/typeid-intrinsic2.rs @@ -10,7 +10,7 @@ #![feature(core)] -use std::any::TypeId; +use std::any::{Any, TypeId}; pub struct A; pub struct B(Option); @@ -31,4 +31,4 @@ pub unsafe fn id_F() -> TypeId { TypeId::of::() } pub unsafe fn id_G() -> TypeId { TypeId::of::() } pub unsafe fn id_H() -> TypeId { TypeId::of::() } -pub unsafe fn foo() -> TypeId { TypeId::of::() } +pub unsafe fn foo() -> TypeId { TypeId::of::() } diff --git a/src/test/compile-fail/reflect-assoc.rs b/src/test/compile-fail/reflect-assoc.rs new file mode 100644 index 00000000000..9cf0d252c2d --- /dev/null +++ b/src/test/compile-fail/reflect-assoc.rs @@ -0,0 +1,35 @@ +// Copyright 2015 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 types that appear in assoc bindings in an object +// type are subject to the reflect check. + +use std::marker::Reflect; +use std::io::Write; + +trait Get { + type Output; + fn get(self) -> Self::Output; +} + +struct Struct(T); + +fn is_reflect() { } + +fn a() { + is_reflect::>>(); //~ ERROR not implemented +} + +fn ok_a() { + is_reflect::>>(); // OK +} + +fn main() { +} diff --git a/src/test/compile-fail/reflect-object-param.rs b/src/test/compile-fail/reflect-object-param.rs new file mode 100644 index 00000000000..9f074667feb --- /dev/null +++ b/src/test/compile-fail/reflect-object-param.rs @@ -0,0 +1,47 @@ +// Copyright 2015 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 types that appear in input types in an object type are +// subject to the reflect check. + +use std::marker::Reflect; +use std::io::Write; + +trait Get { + fn get(self) -> T; +} + +struct Struct(T); + +fn is_reflect() { } + +fn a() { + is_reflect::(); //~ ERROR not implemented +} + +fn ok_a() { + is_reflect::(); // OK +} + +fn b() { + is_reflect::>>(); //~ ERROR not implemented +} + +fn ok_b() { + is_reflect::>>(); // OK +} + +fn c() { + is_reflect::>>>(); //~ ERROR not implemented +} + +fn main() { + is_reflect::>>>(); // OK +} diff --git a/src/test/compile-fail/reflect.rs b/src/test/compile-fail/reflect.rs new file mode 100644 index 00000000000..701aa5b40bc --- /dev/null +++ b/src/test/compile-fail/reflect.rs @@ -0,0 +1,39 @@ +// Copyright 2015 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 there is no way to get a generic type `T` to be +// considered as `Reflect` (or accessible via something that is +// considered `Reflect`) without a reflect bound, but that any +// concrete type works fine. Note that object types are tested +// separately. + +use std::marker::Reflect; +use std::io::Write; + +struct Struct(T); + +fn is_reflect() { } + +fn c() { + is_reflect::>(); //~ ERROR not implemented +} + +fn ok_c() { + is_reflect::>(); // OK +} + +fn d() { + is_reflect::<(i32, T)>(); //~ ERROR not implemented +} + +fn main() { + is_reflect::<&i32>(); // OK + is_reflect::>(); // OK +} diff --git a/src/test/run-pass/object-one-type-two-traits.rs b/src/test/run-pass/object-one-type-two-traits.rs index baf8c6e4c97..f4e056b3f21 100644 --- a/src/test/run-pass/object-one-type-two-traits.rs +++ b/src/test/run-pass/object-one-type-two-traits.rs @@ -30,7 +30,7 @@ impl Wrap for int { } } -fn is(x: &Any) -> bool { +fn is(x: &Any) -> bool { x.is::() } diff --git a/src/test/run-pass/type-id-higher-rank.rs b/src/test/run-pass/type-id-higher-rank.rs index 5670c45b68a..a40989d4e37 100644 --- a/src/test/run-pass/type-id-higher-rank.rs +++ b/src/test/run-pass/type-id-higher-rank.rs @@ -15,7 +15,7 @@ #![feature(unboxed_closures, core)] -use std::any::TypeId; +use std::any::{Any, TypeId}; fn main() { // Bare fns @@ -63,7 +63,7 @@ fn main() { assert!(a != b); } - fn id(_: T) -> TypeId { + fn id(_: T) -> TypeId { TypeId::of::() } } -- cgit 1.4.1-3-g733a5 From c59fe8bde2be55c46f627277e2cc37515fb7165e Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 25 Mar 2015 10:42:38 -0400 Subject: Drive-by fix for incorrect variance rule that I noticed. --- src/librustc_typeck/variance.rs | 24 +++++++++++++++++++++--- src/test/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 (limited to 'src/test') 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() { } -- cgit 1.4.1-3-g733a5