about summary refs log tree commit diff
path: root/src/test
diff options
context:
space:
mode:
authorVadim Petrochenkov <vadim.petrochenkov@gmail.com>2015-12-18 20:00:19 +0300
committerVadim Petrochenkov <vadim.petrochenkov@gmail.com>2015-12-18 20:00:19 +0300
commit95fdaf237575e44ecf16f9fb13ab60058909f281 (patch)
treed5c2379977294b5bf5a0b51adb413f34851d958d /src/test
parente0ceef5a9e9224baf1f108ede1d1033e5e7fef08 (diff)
Require exact type equality + add tests
+ Rebase fixes
Diffstat (limited to 'src/test')
-rw-r--r--src/test/compile-fail/coerce-expect-unsized-ascribed.rs42
-rw-r--r--src/test/compile-fail/type-ascription-precedence.rs64
-rw-r--r--src/test/compile-fail/type-ascription-soundness.rs23
-rw-r--r--src/test/run-pass/coerce-expect-unsized-ascribed.rs40
-rw-r--r--src/test/run-pass/type-ascription.rs45
5 files changed, 174 insertions, 40 deletions
diff --git a/src/test/compile-fail/coerce-expect-unsized-ascribed.rs b/src/test/compile-fail/coerce-expect-unsized-ascribed.rs
new file mode 100644
index 00000000000..ef65927fc5d
--- /dev/null
+++ b/src/test/compile-fail/coerce-expect-unsized-ascribed.rs
@@ -0,0 +1,42 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// A version of coerce-expect-unsized that uses type ascription.
+// Doesn't work so far, but supposed to work eventually
+
+#![feature(box_syntax, type_ascription)]
+
+use std::fmt::Debug;
+
+pub fn main() {
+    let _ = box { [1, 2, 3] }: Box<[i32]>; //~ ERROR mismatched types
+    let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>; //~ ERROR mismatched types
+    let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
+    //~^ ERROR mismatched types
+    let _ = box { |x| (x as u8) }: Box<Fn(i32) -> _>; //~ ERROR mismatched types
+    let _ = box if true { false } else { true }: Box<Debug>; //~ ERROR mismatched types
+    let _ = box match true { true => 'a', false => 'b' }: Box<Debug>; //~ ERROR mismatched types
+
+    let _ = &{ [1, 2, 3] }: &[i32]; //~ ERROR mismatched types
+    let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32]; //~ ERROR mismatched types
+    let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
+    //~^ ERROR mismatched types
+    let _ = &{ |x| (x as u8) }: &Fn(i32) -> _; //~ ERROR mismatched types
+    let _ = &if true { false } else { true }: &Debug; //~ ERROR mismatched types
+    let _ = &match true { true => 'a', false => 'b' }: &Debug; //~ ERROR mismatched types
+
+    let _ = Box::new([1, 2, 3]): Box<[i32]>; //~ ERROR mismatched types
+    let _ = Box::new(|x| (x as u8)): Box<Fn(i32) -> _>; //~ ERROR mismatched types
+
+    let _ = vec![
+        Box::new(|x| (x as u8)),
+        box |x| (x as i16 as u8),
+    ]: Vec<Box<Fn(i32) -> _>>;
+}
diff --git a/src/test/compile-fail/type-ascription-precedence.rs b/src/test/compile-fail/type-ascription-precedence.rs
new file mode 100644
index 00000000000..bb7a8bc3ddf
--- /dev/null
+++ b/src/test/compile-fail/type-ascription-precedence.rs
@@ -0,0 +1,64 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Operator precedence of type ascription
+// Type ascription has very high precedence, the same as operator `as`
+
+#![feature(type_ascription)]
+
+use std::ops::*;
+
+struct S;
+struct Z;
+
+impl Add<Z> for S {
+    type Output = S;
+    fn add(self, _rhs: Z) -> S { panic!() }
+}
+impl Mul<Z> for S {
+    type Output = S;
+    fn mul(self, _rhs: Z) -> S { panic!() }
+}
+impl Neg for S {
+    type Output = Z;
+    fn neg(self) -> Z { panic!() }
+}
+impl Deref for S {
+    type Target = Z;
+    fn deref(&self) -> &Z { panic!() }
+}
+
+fn main() {
+    &S: &S; // OK
+    (&S): &S; // OK
+    &(S: &S); //~ ERROR mismatched types
+
+    *S: Z; // OK
+    (*S): Z; // OK
+    *(S: Z); //~ ERROR mismatched types
+    //~^ ERROR type `Z` cannot be dereferenced
+
+    -S: Z; // OK
+    (-S): Z; // OK
+    -(S: Z); //~ ERROR mismatched types
+    //~^ ERROR cannot apply unary operator `-` to type `Z`
+
+    S + Z: Z; // OK
+    S + (Z: Z); // OK
+    (S + Z): Z; //~ ERROR mismatched types
+
+    S * Z: Z; // OK
+    S * (Z: Z); // OK
+    (S * Z): Z; //~ ERROR mismatched types
+
+    S .. S: S; // OK
+    S .. (S: S); // OK
+    (S .. S): S; //~ ERROR mismatched types
+}
diff --git a/src/test/compile-fail/type-ascription-soundness.rs b/src/test/compile-fail/type-ascription-soundness.rs
new file mode 100644
index 00000000000..2d882e87ab8
--- /dev/null
+++ b/src/test/compile-fail/type-ascription-soundness.rs
@@ -0,0 +1,23 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Type ascription doesn't lead to unsoundness
+
+#![feature(type_ascription)]
+
+fn main() {
+    let arr = &[1u8, 2, 3];
+    let ref x = arr: &[u8]; //~ ERROR mismatched types
+    let ref mut x = arr: &[u8]; //~ ERROR mismatched types
+    match arr: &[u8] { //~ ERROR mismatched types
+        ref x => {}
+    }
+    let _len = (arr: &[u8]).len(); //~ ERROR mismatched types
+}
diff --git a/src/test/run-pass/coerce-expect-unsized-ascribed.rs b/src/test/run-pass/coerce-expect-unsized-ascribed.rs
deleted file mode 100644
index 33e9b424fea..00000000000
--- a/src/test/run-pass/coerce-expect-unsized-ascribed.rs
+++ /dev/null
@@ -1,40 +0,0 @@
-// 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 <LICENSE-APACHE or
-// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
-// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
-// option. This file may not be copied, modified, or distributed
-// except according to those terms.
-
-#![allow(unknown_features)]
-#![feature(box_syntax)]
-
-use std::fmt::Debug;
-
-// A version of coerce-expect-unsized that uses type ascription.
-
-pub fn main() {
-    let _ = box { [1, 2, 3] }: Box<[i32]>;
-    let _ = box if true { [1, 2, 3] } else { [1, 3, 4] }: Box<[i32]>;
-    let _ = box match true { true => [1, 2, 3], false => [1, 3, 4] }: Box<[i32]>;
-    let _ = box { |x| (x as u8) }: Box<Fn(i32) -> _>;
-    let _ = box if true { false } else { true }: Box<Debug>;
-    let _ = box match true { true => 'a', false => 'b' }: Box<Debug>;
-
-    let _ = &{ [1, 2, 3] }: &[i32];
-    let _ = &if true { [1, 2, 3] } else { [1, 3, 4] }: &[i32];
-    let _ = &match true { true => [1, 2, 3], false => [1, 3, 4] }: &[i32];
-    let _ = &{ |x| (x as u8) }: &Fn(i32) -> _;
-    let _ = &if true { false } else { true }: &Debug;
-    let _ = &match true { true => 'a', false => 'b' }: &Debug;
-
-    let _ = Box::new([1, 2, 3]): Box<[i32]>;
-    let _ = Box::new(|x| (x as u8)): Box<Fn(i32) -> _>;
-
-    let _ = vec![
-        Box::new(|x| (x as u8)),
-        box |x| (x as i16 as u8),
-    ]: Vec<Box<Fn(i32) -> _>>;
-}
diff --git a/src/test/run-pass/type-ascription.rs b/src/test/run-pass/type-ascription.rs
new file mode 100644
index 00000000000..bca384c6471
--- /dev/null
+++ b/src/test/run-pass/type-ascription.rs
@@ -0,0 +1,45 @@
+// 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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// Type ascription doesn't lead to unsoundness
+
+#![feature(type_ascription)]
+
+use std::mem;
+
+const C1: u8 = 10: u8;
+const C2: [u8; 1: usize] = [1];
+
+struct S {
+    a: u8
+}
+
+fn main() {
+    assert_eq!(C1.into(): i32, 10);
+    assert_eq!(C2[0], 1);
+
+    let s = S { a: 10: u8 };
+    let arr = &[1u8, 2, 3];
+
+    let mut v = arr.iter().cloned().collect(): Vec<_>;
+    v.push(4);
+    assert_eq!(v, [1, 2, 3, 4]);
+
+    let a = 1: u8;
+    let b = a.into(): u16;
+    assert_eq!(v[a.into(): usize], 2);
+    assert_eq!(mem::size_of_val(&a), 1);
+    assert_eq!(mem::size_of_val(&b), 2);
+    assert_eq!(b, 1: u16);
+
+    let mut v = Vec::new();
+    v: Vec<u8> = vec![1, 2, 3]; // Lvalue type ascription
+    assert_eq!(v, [1u8, 2, 3]);
+}