Skip to content

Commit dc57601

Browse files
committed
Add traits with consts tests
1 parent 0de138c commit dc57601

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

tests/consts.rs

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
use formality::test_program_ok;
2+
use formality_macros::test;
3+
4+
#[test]
5+
fn test_ok() {
6+
expect_test::expect![[r#"
7+
Ok(
8+
(),
9+
)
10+
"#]]
11+
.assert_debug_eq(&test_program_ok(
12+
"[
13+
crate Foo {
14+
trait Foo<const C> where [type_of_const C is bool] {}
15+
trait Bar<const C> where [type_of_const C is u32] {}
16+
17+
impl<const C> Foo<const C> for u32 where [type_of_const C is bool] {}
18+
}
19+
]",
20+
));
21+
}
22+
23+
#[test]
24+
fn test_holds() {
25+
expect_test::expect![[r#"
26+
Ok(
27+
(),
28+
)
29+
"#]]
30+
.assert_debug_eq(&test_program_ok(
31+
"[
32+
crate Foo {
33+
trait Foo<const C> where [type_of_const C is bool] {}
34+
35+
impl<> Foo<const true> for u32 where [] {}
36+
}
37+
]",
38+
));
39+
}
40+
41+
#[test]
42+
fn test_mismatch() {
43+
expect_test::expect![[r#"
44+
Err(
45+
Error {
46+
context: "check_trait_impl(impl <> Foo < const 42_(rigid (scalar u32)) > for (rigid (scalar u32)) where [] { })",
47+
source: "failed to prove {Foo((rigid (scalar u32)), const 42_(rigid (scalar u32)))} given {}, got {}",
48+
},
49+
)
50+
"#]]
51+
.assert_debug_eq(&test_program_ok(
52+
"[
53+
crate Foo {
54+
trait Foo<const C> where [type_of_const C is bool] {}
55+
56+
impl<> Foo<const 42_u32> for u32 where [] {}
57+
}
58+
]",
59+
));
60+
}
61+
62+
#[test]
63+
fn test_generic_mismatch() {
64+
expect_test::expect![[r#"
65+
Err(
66+
Error {
67+
context: "check_trait_impl(impl <const> Foo < const ^const0_0 > for (rigid (scalar u32)) where [type_of_const ^const0_0 is (rigid (scalar u32))] { })",
68+
source: "failed to prove {Foo((rigid (scalar u32)), const !const_1)} given {@ ConstHasType(!const_1 , (rigid (scalar u32)))}, got {}",
69+
},
70+
)
71+
"#]]
72+
.assert_debug_eq(&test_program_ok(
73+
"[
74+
crate Foo {
75+
trait Foo<const C> where [type_of_const C is bool] {}
76+
77+
impl<const C> Foo<const C> for u32 where [type_of_const C is u32] {}
78+
}
79+
]",
80+
));
81+
}
82+
83+
#[test]
84+
fn test_trait_with_const() {
85+
expect_test::expect![[r#"
86+
Ok(
87+
(),
88+
)
89+
"#]]
90+
.assert_debug_eq(&test_program_ok(
91+
"[
92+
crate Foo {
93+
trait Foo<const C> where [type_of_const C is bool] {}
94+
95+
trait Bar<ty T> where [T: Foo<const true>] {}
96+
97+
impl<> Foo<const true> for u32 where [] {}
98+
impl<> Bar<u32> for u32 where [] {}
99+
}
100+
]",
101+
));
102+
}
103+
104+
#[test]
105+
fn test_trait_with_generic_const() {
106+
expect_test::expect![[r#"
107+
Ok(
108+
(),
109+
)
110+
"#]]
111+
.assert_debug_eq(&test_program_ok(
112+
"[
113+
crate Foo {
114+
trait Foo<const C> where [type_of_const C is bool] {}
115+
116+
trait Bar<ty T, const C> where [T: Foo<const C>, type_of_const C is bool] {}
117+
118+
impl<const C> Foo<const C> for u32 where [type_of_const C is bool] {}
119+
impl<const C> Bar<u32, const C> for u32 where [type_of_const C is bool] {}
120+
}
121+
]",
122+
));
123+
}
124+
125+
#[test]
126+
/// This test is the short version of `test_holds`, skipping
127+
/// substituting and directly going to a rigid constant.
128+
fn test_rigid_const_bound() {
129+
expect_test::expect![[r#"
130+
Ok(
131+
(),
132+
)
133+
"#]]
134+
.assert_debug_eq(&test_program_ok(
135+
"[
136+
crate Foo {
137+
trait Foo<> where [type_of_const true is bool] {}
138+
}
139+
]",
140+
));
141+
}
142+
143+
#[test]
144+
/// This test is the short version of `test_generic_mismatch`, skipping
145+
/// substituting and directly going to a wrong constant.
146+
fn test_nonsense_rigid_const_bound() {
147+
expect_test::expect![[r#"
148+
Err(
149+
Error {
150+
context: "check_trait(Foo)",
151+
source: Error {
152+
context: "prove_where_clause_well_formed(type_of_const 0_(rigid (scalar bool)) is (rigid (scalar u32)))",
153+
source: "failed to prove {(rigid (scalar u32)) = (rigid (scalar bool))} given {@ ConstHasType(0_(rigid (scalar bool)) , (rigid (scalar u32)))}, got {}",
154+
},
155+
},
156+
)
157+
"#]]
158+
.assert_debug_eq(&test_program_ok(
159+
"[
160+
crate Foo {
161+
trait Foo<> where [type_of_const true is u32] {}
162+
}
163+
]",
164+
));
165+
}
166+
167+
#[test]
168+
/// This test is wrong, but it's also not something rustc ever generates.
169+
/// Types on const generics only get exactly one `type_of_const` bound.
170+
fn test_multiple_type_of_const() {
171+
expect_test::expect![[r#"
172+
Ok(
173+
(),
174+
)
175+
"#]]
176+
.assert_debug_eq(&test_program_ok(
177+
"[
178+
crate Foo {
179+
trait Foo<const C> where [type_of_const C is bool, type_of_const C is u32] {}
180+
}
181+
]",
182+
));
183+
}

0 commit comments

Comments
 (0)