Skip to content

Commit 323ff19

Browse files
Add tests for calling trait methods on concrete types
1 parent 7a019b1 commit 323ff19

9 files changed

+78
-38
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![allow(incomplete_features)]
2+
#![feature(const_trait_impl)]
3+
#![feature(const_fn)]
4+
5+
pub trait Plus {
6+
fn plus(self, rhs: Self) -> Self;
7+
}
8+
9+
impl const Plus for i32 {
10+
fn plus(self, rhs: Self) -> Self {
11+
self + rhs
12+
}
13+
}
14+
15+
impl Plus for u32 {
16+
fn plus(self, rhs: Self) -> Self {
17+
self + rhs
18+
}
19+
}
20+
21+
pub const fn add_i32(a: i32, b: i32) -> i32 {
22+
a.plus(b)
23+
}
24+
25+
pub const fn add_u32(a: u32, b: u32) -> u32 {
26+
a.plus(b)
27+
//~^ ERROR calls in constant functions are limited to constant functions
28+
}
29+
30+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
2+
--> $DIR/call-const-trait-method.rs:26:5
3+
|
4+
LL | a.plus(b)
5+
| ^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0015`.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![allow(incomplete_features)]
2+
#![feature(const_trait_impl)]
3+
4+
struct S;
5+
trait T {
6+
fn foo();
7+
}
8+
9+
fn non_const() {}
10+
11+
impl const T for S {
12+
fn foo() { non_const() }
13+
//~^ ERROR
14+
}
15+
16+
fn main() {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0723]: can only call other `const fn` within a `const fn`, but `const non_const` is not stable as `const fn`
2+
--> $DIR/const-check-fns-in-const-impl.rs:12:16
3+
|
4+
LL | fn foo() { non_const() }
5+
| ^^^^^^^^^^^
6+
|
7+
= note: for more information, see issue https://github.com/rust-lang/rust/issues/57563
8+
= help: add `#![feature(const_fn)]` to the crate attributes to enable
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0723`.
Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
error: const trait impls are not yet implemented
2-
--> $DIR/feature-gate.rs:9:1
1+
error: fatal error triggered by #[rustc_error]
2+
--> $DIR/feature-gate.rs:14:1
33
|
4-
LL | impl const T for S {}
5-
| ^^^^^-----^^^^^^^^^^^
6-
| |
7-
| const because of this
4+
LL | fn main() {}
5+
| ^^^^^^^^^^^^
86

97
error: aborting due to previous error
108

src/test/ui/rfc-2632-const-trait-impl/feature-gate.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33

44
#![cfg_attr(gated, feature(const_trait_impl))]
55
#![allow(incomplete_features)]
6+
#![feature(rustc_attrs)]
67

78
struct S;
89
trait T {}
910
impl const T for S {}
1011
//[stock]~^ ERROR const trait impls are experimental
11-
//[stock,gated]~^^ ERROR const trait impls are not yet implemented
1212

13-
fn main() {}
13+
#[rustc_error]
14+
fn main() {} //[gated]~ ERROR fatal error triggered by #[rustc_error]
Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
error[E0658]: const trait impls are experimental
2-
--> $DIR/feature-gate.rs:9:6
2+
--> $DIR/feature-gate.rs:10:6
33
|
44
LL | impl const T for S {}
55
| ^^^^^
66
|
77
= note: see issue #67792 <https://github.com/rust-lang/rust/issues/67792> for more information
88
= help: add `#![feature(const_trait_impl)]` to the crate attributes to enable
99

10-
error: const trait impls are not yet implemented
11-
--> $DIR/feature-gate.rs:9:1
12-
|
13-
LL | impl const T for S {}
14-
| ^^^^^-----^^^^^^^^^^^
15-
| |
16-
| const because of this
17-
18-
error: aborting due to 2 previous errors
10+
error: aborting due to previous error
1911

2012
For more information about this error, try `rustc --explain E0658`.

src/test/ui/rfc-2632-const-trait-impl/inherent-impl.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@ trait T {}
88

99
impl const S {}
1010
//~^ ERROR inherent impls cannot be `const`
11-
//~| ERROR const trait impls are not yet implemented
1211

1312
impl const T {}
1413
//~^ ERROR inherent impls cannot be `const`
15-
//~| ERROR const trait impls are not yet implemented
1614

1715
fn main() {}

src/test/ui/rfc-2632-const-trait-impl/inherent-impl.stderr

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | impl const S {}
99
= note: only trait implementations may be annotated with `const`
1010

1111
error: inherent impls cannot be `const`
12-
--> $DIR/inherent-impl.rs:13:1
12+
--> $DIR/inherent-impl.rs:12:1
1313
|
1414
LL | impl const T {}
1515
| ^^^^^-----^^^^^
@@ -18,21 +18,5 @@ LL | impl const T {}
1818
|
1919
= note: only trait implementations may be annotated with `const`
2020

21-
error: const trait impls are not yet implemented
22-
--> $DIR/inherent-impl.rs:9:1
23-
|
24-
LL | impl const S {}
25-
| ^^^^^-----^^^^^
26-
| |
27-
| const because of this
28-
29-
error: const trait impls are not yet implemented
30-
--> $DIR/inherent-impl.rs:13:1
31-
|
32-
LL | impl const T {}
33-
| ^^^^^-----^^^^^
34-
| |
35-
| const because of this
36-
37-
error: aborting due to 4 previous errors
21+
error: aborting due to 2 previous errors
3822

0 commit comments

Comments
 (0)