Skip to content

Commit 9623203

Browse files
committed
Demonstrate that const Trait bounds allow trait methods to be called in comptime fn
1 parent 8216948 commit 9623203

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

tests/ui/comptime/trait_bounds.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(rustc_attrs, const_trait_impl)]
2+
3+
const trait Trait {
4+
fn method() {}
5+
}
6+
7+
#[rustc_comptime]
8+
fn always_const<T: const Trait>() {
9+
T::method()
10+
}
11+
12+
#[rustc_comptime]
13+
fn conditionally_const<T: [const] Trait>() {
14+
//~^ ERROR: `[const]` is not allowed here
15+
T::method()
16+
//~^ ERROR: `T: const Trait` is not satisfied
17+
}
18+
19+
#[rustc_comptime]
20+
fn non_const<T: Trait>() {
21+
T::method()
22+
//~^ ERROR: `T: const Trait` is not satisfied
23+
}
24+
25+
fn main() {}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
error: `[const]` is not allowed here
2+
--> $DIR/trait_bounds.rs:13:27
3+
|
4+
LL | fn conditionally_const<T: [const] Trait>() {
5+
| ^^^^^^^
6+
|
7+
note: this function is not `const`, so it cannot have `[const]` trait bounds
8+
--> $DIR/trait_bounds.rs:13:4
9+
|
10+
LL | fn conditionally_const<T: [const] Trait>() {
11+
| ^^^^^^^^^^^^^^^^^^^
12+
13+
error[E0277]: the trait bound `T: const Trait` is not satisfied
14+
--> $DIR/trait_bounds.rs:15:5
15+
|
16+
LL | T::method()
17+
| ^
18+
19+
error[E0277]: the trait bound `T: const Trait` is not satisfied
20+
--> $DIR/trait_bounds.rs:21:5
21+
|
22+
LL | T::method()
23+
| ^
24+
25+
error: aborting due to 3 previous errors
26+
27+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)