Skip to content

Commit 70f7879

Browse files
Ensure const impl cannot coexist with non-const impl
1 parent d6d6d25 commit 70f7879

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![allow(incomplete_features)]
2+
#![feature(const_trait_impl)]
3+
4+
pub struct Int(i32);
5+
6+
impl const std::ops::Add for i32 {
7+
//~^ ERROR conflicting implementations of trait
8+
//~| ERROR only traits defined in the current crate can be implemented for arbitrary types
9+
type Output = Self;
10+
11+
fn add(self, rhs: Self) -> Self {
12+
self + rhs
13+
}
14+
}
15+
16+
impl std::ops::Add for Int {
17+
type Output = Self;
18+
19+
fn add(self, rhs: Self) -> Self {
20+
Int(self.0 + rhs.0)
21+
}
22+
}
23+
24+
impl const std::ops::Add for Int {
25+
//~^ ERROR conflicting implementations of trait
26+
type Output = Self;
27+
28+
fn add(self, rhs: Self) -> Self {
29+
Int(self.0 + rhs.0)
30+
}
31+
}
32+
33+
fn main() {}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
error[E0119]: conflicting implementations of trait `std::ops::Add` for type `i32`:
2+
--> $DIR/const-and-non-const-impl.rs:6:1
3+
|
4+
LL | impl const std::ops::Add for i32 {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: conflicting implementation in crate `core`:
8+
- impl std::ops::Add for i32;
9+
10+
error[E0119]: conflicting implementations of trait `std::ops::Add` for type `Int`:
11+
--> $DIR/const-and-non-const-impl.rs:24:1
12+
|
13+
LL | impl std::ops::Add for Int {
14+
| -------------------------- first implementation here
15+
...
16+
LL | impl const std::ops::Add for Int {
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `Int`
18+
19+
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
20+
--> $DIR/const-and-non-const-impl.rs:6:1
21+
|
22+
LL | impl const std::ops::Add for i32 {
23+
| ^^^^^^^^^^^-------------^^^^^---
24+
| | | |
25+
| | | `i32` is not defined in the current crate
26+
| | `i32` is not defined in the current crate
27+
| impl doesn't use only types from inside the current crate
28+
|
29+
= note: define and implement a trait or new type instead
30+
31+
error: aborting due to 3 previous errors
32+
33+
Some errors have detailed explanations: E0117, E0119.
34+
For more information about an error, try `rustc --explain E0117`.

0 commit comments

Comments
 (0)