Skip to content

Commit 81d6f9c

Browse files
committed
Add tests
1 parent 3790f08 commit 81d6f9c

11 files changed

+278
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
5+
impl Foo for i32 { }
6+
7+
impl Foo for u32 { }
8+
9+
fn gimme<F: Foo>() { }
10+
11+
// Note: this also tests that `std::process::Termination` is implemented for `()`.
12+
fn main() {
13+
gimme::<i32>();
14+
gimme::<u32>();
15+
gimme::<f32>(); //~ERROR the trait bound `f32: Foo` is not satisfied
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
5+
impl<T> Foo for (T, u32) { }
6+
7+
fn gimme<F: Foo>() { }
8+
9+
fn foo<T>() {
10+
gimme::<(T, u32)>();
11+
gimme::<(Option<T>, u32)>();
12+
gimme::<(Option<T>, f32)>(); //~ ERROR
13+
}
14+
15+
fn main() {
16+
gimme::<(i32, u32)>();
17+
gimme::<(i32, f32)>(); //~ ERROR
18+
}
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo: Sized { }
4+
5+
trait Bar {
6+
type Item: Foo;
7+
}
8+
9+
impl Foo for i32 { }
10+
11+
impl Foo for str { }
12+
//~^ ERROR the size for values of type `str` cannot be known at compilation time
13+
14+
// Implicit `T: Sized` bound.
15+
impl<T> Foo for Option<T> { }
16+
17+
impl Bar for () {
18+
type Item = i32;
19+
}
20+
21+
impl<T> Bar for Option<T> {
22+
type Item = Option<T>;
23+
}
24+
25+
impl Bar for f32 {
26+
//~^ ERROR the trait bound `f32: Foo` is not satisfied
27+
type Item = f32;
28+
}
29+
30+
trait Baz<U: ?Sized> where U: Foo { }
31+
32+
impl Baz<i32> for i32 { }
33+
34+
impl Baz<f32> for f32 { }
35+
//~^ ERROR the trait bound `f32: Foo` is not satisfied
36+
37+
fn main() {
38+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
5+
struct S<T: Foo> {
6+
x: T,
7+
}
8+
9+
impl Foo for i32 { }
10+
impl<T> Foo for Option<T> { }
11+
12+
fn main() {
13+
let s = S {
14+
x: 5,
15+
};
16+
17+
let s = S { //~ ERROR the trait bound `{float}: Foo` is not satisfied
18+
x: 5.0,
19+
};
20+
21+
let s = S {
22+
x: Some(5.0),
23+
};
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
5+
impl Foo for i32 { }
6+
7+
struct S<T: Foo> {
8+
x: T,
9+
}
10+
11+
fn only_foo<T: Foo>(_x: &T) { }
12+
13+
impl<T> S<T> {
14+
// Test that we have the correct environment inside an inherent method.
15+
fn dummy_foo(&self) {
16+
only_foo(&self.x)
17+
}
18+
}
19+
20+
trait Bar { }
21+
impl Bar for u32 { }
22+
23+
fn only_bar<T: Bar>() { }
24+
25+
impl<T> S<T> {
26+
// Test that the environment of `dummy_bar` adds up with the environment
27+
// of the inherent impl.
28+
fn dummy_bar<U: Bar>(&self) {
29+
only_foo(&self.x);
30+
only_bar::<U>();
31+
}
32+
}
33+
34+
fn main() {
35+
let s = S {
36+
x: 5,
37+
};
38+
39+
s.dummy_foo();
40+
s.dummy_bar::<u32>();
41+
}
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
5+
trait Bar {
6+
type Item: Foo;
7+
}
8+
9+
impl Foo for i32 { }
10+
impl Bar for i32 {
11+
type Item = i32;
12+
}
13+
14+
fn only_foo<T: Foo>() { }
15+
16+
fn only_bar<T: Bar>() {
17+
// `T` implements `Bar` hence `<T as Bar>::Item` must also implement `Bar`
18+
only_foo::<T::Item>()
19+
}
20+
21+
fn main() {
22+
only_bar::<i32>();
23+
only_foo::<<i32 as Bar>::Item>();
24+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
trait Bar: Foo { }
5+
6+
impl Foo for i32 { }
7+
impl Bar for i32 { }
8+
9+
fn only_foo<T: Foo>() { }
10+
11+
fn only_bar<T: Bar>() {
12+
// `T` implements `Bar` hence `T` must also implement `Foo`
13+
only_foo::<T>()
14+
}
15+
16+
fn main() {
17+
only_bar::<i32>()
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
trait Bar<U> where U: Foo { }
5+
6+
impl Foo for i32 { }
7+
impl Bar<i32> for i32 { }
8+
9+
fn only_foo<T: Foo>() { }
10+
11+
fn only_bar<U, T: Bar<U>>() {
12+
only_foo::<U>()
13+
}
14+
15+
fn main() {
16+
only_bar::<i32, i32>()
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Eq { }
4+
trait Hash: Eq { }
5+
6+
impl Eq for i32 { }
7+
impl Hash for i32 { }
8+
9+
struct Set<T: Hash> {
10+
_x: T,
11+
}
12+
13+
fn only_eq<T: Eq>() { }
14+
15+
fn take_a_set<T>(_: &Set<T>) {
16+
// `Set<T>` is an input type of `take_a_set`, hence we know that
17+
// `T` must implement `Hash`, and we know in turn that `T` must
18+
// implement `Eq`.
19+
only_eq::<T>()
20+
}
21+
22+
fn main() {
23+
let set = Set {
24+
_x: 5,
25+
};
26+
27+
take_a_set(&set);
28+
}
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// compile-flags: -Z chalk
2+
3+
trait Foo { }
4+
impl Foo for i32 { }
5+
6+
trait Bar { }
7+
impl Bar for i32 { }
8+
impl Bar for u32 { }
9+
10+
fn only_foo<T: Foo>(_x: T) { }
11+
12+
fn only_bar<T: Bar>(_x: T) { }
13+
14+
fn main() {
15+
let x = 5.0;
16+
17+
// The only type which implements `Foo` is `i32`, so the chalk trait solver
18+
// is expecting a variable of type `i32`. This behavior differs from the
19+
// old-style trait solver. I guess this will change, that's why I'm
20+
// adding that test.
21+
only_foo(x); //~ ERROR mismatched types
22+
23+
// Here we have two solutions so we get back the behavior of the old-style
24+
// trait solver.
25+
only_bar(x); //~ ERROR the trait bound `{float}: Bar` is not satisfied
26+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/type_inference.rs:21:14
3+
|
4+
LL | only_foo(x); //~ ERROR mismatched types
5+
| ^ expected i32, found floating-point variable
6+
|
7+
= note: expected type `i32`
8+
found type `{float}`
9+
10+
error[E0277]: the trait bound `{float}: Bar` is not satisfied
11+
--> $DIR/type_inference.rs:25:5
12+
|
13+
LL | only_bar(x); //~ ERROR the trait bound `{float}: Bar` is not satisfied
14+
| ^^^^^^^^ the trait `Bar` is not implemented for `{float}`
15+
|
16+
= help: the following implementations were found:
17+
<i32 as Bar>
18+
<u32 as Bar>
19+
note: required by `only_bar`
20+
--> $DIR/type_inference.rs:12:1
21+
|
22+
LL | fn only_bar<T: Bar>(_x: T) { }
23+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 2 previous errors
26+
27+
Some errors occurred: E0277, E0308.
28+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)