]> git.lizzy.rs Git - rust.git/commitdiff
Add tests
authorscalexm <alexandre@scalexm.fr>
Fri, 30 Nov 2018 14:07:38 +0000 (15:07 +0100)
committerscalexm <alexandre@scalexm.fr>
Thu, 27 Dec 2018 18:21:16 +0000 (19:21 +0100)
src/test/compile-fail/chalkify/chalk_initial_program.rs [new file with mode: 0644]
src/test/compile-fail/chalkify/generic_impls.rs [new file with mode: 0644]
src/test/compile-fail/chalkify/impl_wf.rs [new file with mode: 0644]
src/test/compile-fail/chalkify/type_wf.rs [new file with mode: 0644]
src/test/run-pass/chalkify/inherent_impl.rs [new file with mode: 0644]
src/test/run-pass/chalkify/projection.rs [new file with mode: 0644]
src/test/run-pass/chalkify/super_trait.rs [new file with mode: 0644]
src/test/run-pass/chalkify/trait_implied_bound.rs [new file with mode: 0644]
src/test/run-pass/chalkify/type_implied_bound.rs [new file with mode: 0644]
src/test/ui/chalkify/type_inference.rs [new file with mode: 0644]
src/test/ui/chalkify/type_inference.stderr [new file with mode: 0644]

diff --git a/src/test/compile-fail/chalkify/chalk_initial_program.rs b/src/test/compile-fail/chalkify/chalk_initial_program.rs
new file mode 100644 (file)
index 0000000..df25bad
--- /dev/null
@@ -0,0 +1,16 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+
+impl Foo for i32 { }
+
+impl Foo for u32 { }
+
+fn gimme<F: Foo>() { }
+
+// Note: this also tests that `std::process::Termination` is implemented for `()`.
+fn main() {
+    gimme::<i32>();
+    gimme::<u32>();
+    gimme::<f32>(); //~ERROR the trait bound `f32: Foo` is not satisfied
+}
diff --git a/src/test/compile-fail/chalkify/generic_impls.rs b/src/test/compile-fail/chalkify/generic_impls.rs
new file mode 100644 (file)
index 0000000..d70c6f8
--- /dev/null
@@ -0,0 +1,18 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+
+impl<T> Foo for (T, u32) { }
+
+fn gimme<F: Foo>() { }
+
+fn foo<T>() {
+    gimme::<(T, u32)>();
+    gimme::<(Option<T>, u32)>();
+    gimme::<(Option<T>, f32)>(); //~ ERROR
+}
+
+fn main() {
+    gimme::<(i32, u32)>();
+    gimme::<(i32, f32)>(); //~ ERROR
+}
diff --git a/src/test/compile-fail/chalkify/impl_wf.rs b/src/test/compile-fail/chalkify/impl_wf.rs
new file mode 100644 (file)
index 0000000..96b1b25
--- /dev/null
@@ -0,0 +1,38 @@
+// compile-flags: -Z chalk
+
+trait Foo: Sized { }
+
+trait Bar {
+    type Item: Foo;
+}
+
+impl Foo for i32 { }
+
+impl Foo for str { }
+//~^ ERROR the size for values of type `str` cannot be known at compilation time
+
+// Implicit `T: Sized` bound.
+impl<T> Foo for Option<T> { }
+
+impl Bar for () {
+    type Item = i32;
+}
+
+impl<T> Bar for Option<T> {
+    type Item = Option<T>;
+}
+
+impl Bar for f32 {
+//~^ ERROR the trait bound `f32: Foo` is not satisfied
+    type Item = f32;
+}
+
+trait Baz<U: ?Sized> where U: Foo { }
+
+impl Baz<i32> for i32 { }
+
+impl Baz<f32> for f32 { }
+//~^ ERROR the trait bound `f32: Foo` is not satisfied
+
+fn main() {
+}
diff --git a/src/test/compile-fail/chalkify/type_wf.rs b/src/test/compile-fail/chalkify/type_wf.rs
new file mode 100644 (file)
index 0000000..d1aa975
--- /dev/null
@@ -0,0 +1,24 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+
+struct S<T: Foo> {
+    x: T,
+}
+
+impl Foo for i32 { }
+impl<T> Foo for Option<T> { }
+
+fn main() {
+    let s = S {
+       x: 5,
+    };
+
+    let s = S { //~ ERROR the trait bound `{float}: Foo` is not satisfied
+        x: 5.0,
+    };
+
+    let s = S {
+        x: Some(5.0),
+    };
+}
diff --git a/src/test/run-pass/chalkify/inherent_impl.rs b/src/test/run-pass/chalkify/inherent_impl.rs
new file mode 100644 (file)
index 0000000..fbe30f1
--- /dev/null
@@ -0,0 +1,41 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+
+impl Foo for i32 { }
+
+struct S<T: Foo> {
+    x: T,
+}
+
+fn only_foo<T: Foo>(_x: &T) { }
+
+impl<T> S<T> {
+    // Test that we have the correct environment inside an inherent method.
+    fn dummy_foo(&self) {
+        only_foo(&self.x)
+    }
+}
+
+trait Bar { }
+impl Bar for u32 { }
+
+fn only_bar<T: Bar>() { }
+
+impl<T> S<T> {
+    // Test that the environment of `dummy_bar` adds up with the environment
+    // of the inherent impl.
+    fn dummy_bar<U: Bar>(&self) {
+        only_foo(&self.x);
+        only_bar::<U>();
+    }
+}
+
+fn main() {
+    let s = S {
+        x: 5,
+    };
+
+    s.dummy_foo();
+    s.dummy_bar::<u32>();
+}
diff --git a/src/test/run-pass/chalkify/projection.rs b/src/test/run-pass/chalkify/projection.rs
new file mode 100644 (file)
index 0000000..a598f68
--- /dev/null
@@ -0,0 +1,24 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+
+trait Bar {
+    type Item: Foo;
+}
+
+impl Foo for i32 { }
+impl Bar for i32 {
+    type Item = i32;
+}
+
+fn only_foo<T: Foo>() { }
+
+fn only_bar<T: Bar>() {
+    // `T` implements `Bar` hence `<T as Bar>::Item` must also implement `Bar`
+    only_foo::<T::Item>()
+}
+
+fn main() {
+    only_bar::<i32>();
+    only_foo::<<i32 as Bar>::Item>();
+}
diff --git a/src/test/run-pass/chalkify/super_trait.rs b/src/test/run-pass/chalkify/super_trait.rs
new file mode 100644 (file)
index 0000000..441d61e
--- /dev/null
@@ -0,0 +1,18 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+trait Bar: Foo { }
+
+impl Foo for i32 { }
+impl Bar for i32 { }
+
+fn only_foo<T: Foo>() { }
+
+fn only_bar<T: Bar>() {
+    // `T` implements `Bar` hence `T` must also implement `Foo`
+    only_foo::<T>()
+}
+
+fn main() {
+    only_bar::<i32>()
+}
diff --git a/src/test/run-pass/chalkify/trait_implied_bound.rs b/src/test/run-pass/chalkify/trait_implied_bound.rs
new file mode 100644 (file)
index 0000000..f824537
--- /dev/null
@@ -0,0 +1,17 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+trait Bar<U> where U: Foo { }
+
+impl Foo for i32 { }
+impl Bar<i32> for i32 { }
+
+fn only_foo<T: Foo>() { }
+
+fn only_bar<U, T: Bar<U>>() {
+    only_foo::<U>()
+}
+
+fn main() {
+    only_bar::<i32, i32>()
+}
diff --git a/src/test/run-pass/chalkify/type_implied_bound.rs b/src/test/run-pass/chalkify/type_implied_bound.rs
new file mode 100644 (file)
index 0000000..94d976d
--- /dev/null
@@ -0,0 +1,28 @@
+// compile-flags: -Z chalk
+
+trait Eq { }
+trait Hash: Eq { }
+
+impl Eq for i32 { }
+impl Hash for i32 { }
+
+struct Set<T: Hash> {
+    _x: T,
+}
+
+fn only_eq<T: Eq>() { }
+
+fn take_a_set<T>(_: &Set<T>) {
+    // `Set<T>` is an input type of `take_a_set`, hence we know that
+    // `T` must implement `Hash`, and we know in turn that `T` must
+    // implement `Eq`.
+    only_eq::<T>()
+}
+
+fn main() {
+    let set = Set {
+        _x: 5,
+    };
+
+    take_a_set(&set);
+}
diff --git a/src/test/ui/chalkify/type_inference.rs b/src/test/ui/chalkify/type_inference.rs
new file mode 100644 (file)
index 0000000..62a53ec
--- /dev/null
@@ -0,0 +1,26 @@
+// compile-flags: -Z chalk
+
+trait Foo { }
+impl Foo for i32 { }
+
+trait Bar { }
+impl Bar for i32 { }
+impl Bar for u32 { }
+
+fn only_foo<T: Foo>(_x: T) { }
+
+fn only_bar<T: Bar>(_x: T) { }
+
+fn main() {
+    let x = 5.0;
+
+    // The only type which implements `Foo` is `i32`, so the chalk trait solver
+    // is expecting a variable of type `i32`. This behavior differs from the
+    // old-style trait solver. I guess this will change, that's why I'm
+    // adding that test.
+    only_foo(x); //~ ERROR mismatched types
+
+    // Here we have two solutions so we get back the behavior of the old-style
+    // trait solver.
+    only_bar(x); //~ ERROR the trait bound `{float}: Bar` is not satisfied
+}
diff --git a/src/test/ui/chalkify/type_inference.stderr b/src/test/ui/chalkify/type_inference.stderr
new file mode 100644 (file)
index 0000000..49ed97d
--- /dev/null
@@ -0,0 +1,28 @@
+error[E0308]: mismatched types
+  --> $DIR/type_inference.rs:21:14
+   |
+LL |     only_foo(x); //~ ERROR mismatched types
+   |              ^ expected i32, found floating-point variable
+   |
+   = note: expected type `i32`
+              found type `{float}`
+
+error[E0277]: the trait bound `{float}: Bar` is not satisfied
+  --> $DIR/type_inference.rs:25:5
+   |
+LL |     only_bar(x); //~ ERROR the trait bound `{float}: Bar` is not satisfied
+   |     ^^^^^^^^ the trait `Bar` is not implemented for `{float}`
+   |
+   = help: the following implementations were found:
+             <i32 as Bar>
+             <u32 as Bar>
+note: required by `only_bar`
+  --> $DIR/type_inference.rs:12:1
+   |
+LL | fn only_bar<T: Bar>(_x: T) { }
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+error: aborting due to 2 previous errors
+
+Some errors occurred: E0277, E0308.
+For more information about an error, try `rustc --explain E0277`.