Skip to content

Commit d6d6d25

Browse files
Split const trait method test and impl ops::Add
1 parent 323ff19 commit d6d6d25

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method.rs renamed to src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Plus for u32 {
1919
}
2020

2121
pub const fn add_i32(a: i32, b: i32) -> i32 {
22-
a.plus(b)
22+
a.plus(b) // ok
2323
}
2424

2525
pub const fn add_u32(a: u32, b: u32) -> u32 {

src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method.stderr renamed to src/test/ui/rfc-2632-const-trait-impl/call-const-trait-method-fail.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
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
2+
--> $DIR/call-const-trait-method-fail.rs:26:5
33
|
44
LL | a.plus(b)
55
| ^^^^^^^^^
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// run-pass
2+
3+
#![allow(incomplete_features)]
4+
#![feature(const_trait_impl)]
5+
#![feature(const_fn)]
6+
7+
struct Int(i32);
8+
9+
impl const std::ops::Add for Int {
10+
type Output = Int;
11+
12+
fn add(self, rhs: Self) -> Self {
13+
Int(self.0.plus(rhs.0))
14+
}
15+
}
16+
17+
impl const PartialEq for Int {
18+
fn eq(&self, rhs: &Self) -> bool {
19+
self.0 == rhs.0
20+
}
21+
}
22+
23+
pub trait Plus {
24+
fn plus(self, rhs: Self) -> Self;
25+
}
26+
27+
impl const Plus for i32 {
28+
fn plus(self, rhs: Self) -> Self {
29+
self + rhs
30+
}
31+
}
32+
33+
pub const fn add_i32(a: i32, b: i32) -> i32 {
34+
a.plus(b)
35+
}
36+
37+
const ADD_INT: Int = Int(1i32) + Int(2i32);
38+
39+
fn main() {
40+
assert!(ADD_INT == Int(3i32));
41+
}

0 commit comments

Comments
 (0)