Skip to content

Commit dbae3b9

Browse files
committed
More tests
Add tests for coercing pattern types to other types
1 parent 73f6210 commit dbae3b9

File tree

4 files changed

+308
-0
lines changed

4 files changed

+308
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//! Check that pattern types can be cast where they can be coerced
2+
3+
#![feature(pattern_types)]
4+
#![feature(pattern_type_macro)]
5+
6+
use std::pat::pattern_type;
7+
8+
fn identity(x: pattern_type!(u32 is 1..)) -> pattern_type!(u32 is 1..) {
9+
x as pattern_type!(u32 is 1..)
10+
}
11+
12+
fn main() {
13+
let x: pattern_type!(u32 is 1..) = 3;
14+
let y = x as u32; //~ ERROR `(u32) is 1..` as `u32`
15+
let z = x as u64; //~ ERROR `(u32) is 1..` as `u64`
16+
}
17+
18+
fn bar() {
19+
let x = 4 as pattern_type!(u32 is 1..);
20+
}
21+
22+
fn foo() -> u32 {
23+
5 as pattern_type!(u32 is 1..)
24+
//~^ ERROR: mismatched types
25+
}
26+
27+
#[rustfmt::skip]
28+
fn arms(b: bool) -> u32 {
29+
if b {
30+
24
31+
} else {
32+
6 as pattern_type!(u32 is 1..)
33+
//~^ ERROR: mismatched types
34+
}
35+
}
36+
37+
#[rustfmt::skip]
38+
fn arms2(b: bool) -> u32 {
39+
if b {
40+
7 as pattern_type!(u32 is 1..)
41+
//~^ ERROR: mismatched types
42+
} else {
43+
24
44+
}
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
error[E0605]: non-primitive cast: `(u32) is 1..` as `u32`
2+
--> $DIR/casts.rs:14:13
3+
|
4+
LL | let y = x as u32;
5+
| ^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
6+
7+
error[E0605]: non-primitive cast: `(u32) is 1..` as `u64`
8+
--> $DIR/casts.rs:15:13
9+
|
10+
LL | let z = x as u64;
11+
| ^^^^^^^^ an `as` expression can only be used to convert between primitive types or to coerce to a specific trait object
12+
13+
error[E0308]: mismatched types
14+
--> $DIR/casts.rs:23:5
15+
|
16+
LL | fn foo() -> u32 {
17+
| --- expected `u32` because of return type
18+
LL | 5 as pattern_type!(u32 is 1..)
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `(u32) is 1..`
20+
|
21+
= note: expected type `u32`
22+
found pattern type `(u32) is 1..`
23+
24+
error[E0308]: mismatched types
25+
--> $DIR/casts.rs:32:9
26+
|
27+
LL | fn arms(b: bool) -> u32 {
28+
| --- expected `u32` because of return type
29+
...
30+
LL | 6 as pattern_type!(u32 is 1..)
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `(u32) is 1..`
32+
|
33+
= note: expected type `u32`
34+
found pattern type `(u32) is 1..`
35+
36+
error[E0308]: mismatched types
37+
--> $DIR/casts.rs:40:9
38+
|
39+
LL | fn arms2(b: bool) -> u32 {
40+
| --- expected `u32` because of return type
41+
LL | if b {
42+
LL | 7 as pattern_type!(u32 is 1..)
43+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `(u32) is 1..`
44+
|
45+
= note: expected type `u32`
46+
found pattern type `(u32) is 1..`
47+
48+
error: aborting due to 5 previous errors
49+
50+
Some errors have detailed explanations: E0308, E0605.
51+
For more information about an error, try `rustc --explain E0308`.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#![feature(pattern_types)]
2+
#![feature(pattern_type_macro)]
3+
4+
use std::pat::pattern_type;
5+
6+
fn drop_pattern(arg: pattern_type!(u32 is 1..)) -> u32 {
7+
arg //~ ERROR mismatched types
8+
}
9+
10+
fn drop_pattern_type_changing(arg: pattern_type!(u32 is 1..)) -> u64 {
11+
arg //~ ERROR mismatched types
12+
}
13+
14+
fn drop_pattern_nested(arg: Option<pattern_type!(u32 is 1..)>) -> Option<u32> {
15+
arg //~ ERROR mismatched types
16+
}
17+
18+
fn eq(a: pattern_type!(u32 is 1..), b: u32) -> bool {
19+
a == b
20+
//~^ `==` cannot be applied to type `(u32) is 1..`
21+
}
22+
23+
fn eq2(a: pattern_type!(u32 is 1..), b: pattern_type!(u32 is 1..)) -> bool {
24+
a == b
25+
//~^ `==` cannot be applied to type `(u32) is 1..`
26+
}
27+
28+
fn relax_pattern(arg: pattern_type!(u32 is 2..)) -> pattern_type!(u32 is 1..) {
29+
arg //~ ERROR mismatched types
30+
}
31+
32+
#[rustfmt::skip]
33+
fn arms(b: bool) -> u32 {
34+
if b {
35+
0
36+
} else {
37+
let x: pattern_type!(u32 is 1..) = 12;
38+
x //~ ERROR mismatched types
39+
}
40+
}
41+
42+
#[rustfmt::skip]
43+
fn arms2(b: bool) -> u32 {
44+
if b {
45+
let x: pattern_type!(u32 is 1..) = 12;
46+
x //~ ERROR mismatched types
47+
} else {
48+
0
49+
}
50+
}
51+
52+
#[rustfmt::skip]
53+
fn arms3(b: bool) -> pattern_type!(u32 is 1..) {
54+
if b {
55+
0 //~ ERROR mismatched types
56+
} else {
57+
let x: pattern_type!(u32 is 1..) = 12;
58+
x
59+
}
60+
}
61+
62+
#[rustfmt::skip]
63+
fn arms4(b: bool) -> pattern_type!(u32 is 1..) {
64+
if b {
65+
let x: pattern_type!(u32 is 1..) = 12;
66+
x
67+
} else {
68+
0 //~ ERROR mismatched types
69+
}
70+
}
71+
72+
trait Foo {
73+
fn foo(&self) {}
74+
}
75+
76+
impl Foo for u32 {}
77+
78+
fn foo() -> pattern_type!(u32 is 1..) {
79+
2
80+
}
81+
82+
fn bar() {
83+
foo().foo();
84+
//~^ ERROR: no method named `foo` found for pattern type `(u32) is 1..`
85+
}
86+
87+
fn main() {}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/coercion.rs:7:5
3+
|
4+
LL | fn drop_pattern(arg: pattern_type!(u32 is 1..)) -> u32 {
5+
| --- expected `u32` because of return type
6+
LL | arg
7+
| ^^^ expected `u32`, found `(u32) is 1..`
8+
|
9+
= note: expected type `u32`
10+
found pattern type `(u32) is 1..`
11+
12+
error[E0308]: mismatched types
13+
--> $DIR/coercion.rs:11:5
14+
|
15+
LL | fn drop_pattern_type_changing(arg: pattern_type!(u32 is 1..)) -> u64 {
16+
| --- expected `u64` because of return type
17+
LL | arg
18+
| ^^^ expected `u64`, found `(u32) is 1..`
19+
|
20+
= note: expected type `u64`
21+
found pattern type `(u32) is 1..`
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/coercion.rs:15:5
25+
|
26+
LL | fn drop_pattern_nested(arg: Option<pattern_type!(u32 is 1..)>) -> Option<u32> {
27+
| ----------- expected `Option<u32>` because of return type
28+
LL | arg
29+
| ^^^ expected `Option<u32>`, found `Option<(u32) is 1..>`
30+
|
31+
= note: expected enum `Option<u32>`
32+
found enum `Option<(u32) is 1..>`
33+
34+
error[E0369]: binary operation `==` cannot be applied to type `(u32) is 1..`
35+
--> $DIR/coercion.rs:19:7
36+
|
37+
LL | a == b
38+
| - ^^ - u32
39+
| |
40+
| (u32) is 1..
41+
42+
error[E0369]: binary operation `==` cannot be applied to type `(u32) is 1..`
43+
--> $DIR/coercion.rs:24:7
44+
|
45+
LL | a == b
46+
| - ^^ - (u32) is 1..
47+
| |
48+
| (u32) is 1..
49+
50+
error[E0308]: mismatched types
51+
--> $DIR/coercion.rs:29:5
52+
|
53+
LL | fn relax_pattern(arg: pattern_type!(u32 is 2..)) -> pattern_type!(u32 is 1..) {
54+
| ------------------------- expected `(u32) is 1..` because of return type
55+
LL | arg
56+
| ^^^ expected `1`, found `2`
57+
|
58+
= note: expected pattern type `(u32) is 1..`
59+
found pattern type `(u32) is 2..`
60+
61+
error[E0308]: mismatched types
62+
--> $DIR/coercion.rs:38:9
63+
|
64+
LL | fn arms(b: bool) -> u32 {
65+
| --- expected `u32` because of return type
66+
...
67+
LL | x
68+
| ^ expected `u32`, found `(u32) is 1..`
69+
|
70+
= note: expected type `u32`
71+
found pattern type `(u32) is 1..`
72+
73+
error[E0308]: mismatched types
74+
--> $DIR/coercion.rs:46:9
75+
|
76+
LL | fn arms2(b: bool) -> u32 {
77+
| --- expected `u32` because of return type
78+
...
79+
LL | x
80+
| ^ expected `u32`, found `(u32) is 1..`
81+
|
82+
= note: expected type `u32`
83+
found pattern type `(u32) is 1..`
84+
85+
error[E0308]: mismatched types
86+
--> $DIR/coercion.rs:55:9
87+
|
88+
LL | fn arms3(b: bool) -> pattern_type!(u32 is 1..) {
89+
| ------------------------- expected `(u32) is 1..` because of return type
90+
LL | if b {
91+
LL | 0
92+
| ^ expected `(u32) is 1..`, found integer
93+
|
94+
= note: expected pattern type `(u32) is 1..`
95+
found type `{integer}`
96+
97+
error[E0308]: mismatched types
98+
--> $DIR/coercion.rs:68:9
99+
|
100+
LL | fn arms4(b: bool) -> pattern_type!(u32 is 1..) {
101+
| ------------------------- expected `(u32) is 1..` because of return type
102+
...
103+
LL | 0
104+
| ^ expected `(u32) is 1..`, found integer
105+
|
106+
= note: expected pattern type `(u32) is 1..`
107+
found type `{integer}`
108+
109+
error[E0599]: no method named `foo` found for pattern type `(u32) is 1..` in the current scope
110+
--> $DIR/coercion.rs:83:11
111+
|
112+
LL | foo().foo();
113+
| ^^^ method not found in `(u32) is 1..`
114+
|
115+
= help: items from traits can only be used if the trait is implemented and in scope
116+
note: `Foo` defines an item `foo`, perhaps you need to implement it
117+
--> $DIR/coercion.rs:72:1
118+
|
119+
LL | trait Foo {
120+
| ^^^^^^^^^
121+
122+
error: aborting due to 11 previous errors
123+
124+
Some errors have detailed explanations: E0308, E0369, E0599.
125+
For more information about an error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)