Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b671ec9

Browse files
committedNov 20, 2024
constify Add
1 parent 030ddee commit b671ec9

File tree

6 files changed

+17
-58
lines changed

6 files changed

+17
-58
lines changed
 

‎library/core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@
175175
#![feature(const_is_char_boundary)]
176176
#![feature(const_precise_live_drops)]
177177
#![feature(const_str_split_at)]
178+
#![feature(const_trait_impl)]
178179
#![feature(decl_macro)]
179180
#![feature(deprecated_suggestion)]
180181
#![feature(doc_cfg)]

‎library/core/src/ops/arith.rs

+13
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
append_const_msg
7474
)]
7575
#[doc(alias = "+")]
76+
#[cfg_attr(not(bootstrap), const_trait)]
7677
pub trait Add<Rhs = Self> {
7778
/// The resulting type after applying the `+` operator.
7879
#[stable(feature = "rust1", since = "1.0.0")]
@@ -94,6 +95,7 @@ pub trait Add<Rhs = Self> {
9495
macro_rules! add_impl {
9596
($($t:ty)*) => ($(
9697
#[stable(feature = "rust1", since = "1.0.0")]
98+
#[cfg(bootstrap)]
9799
impl Add for $t {
98100
type Output = $t;
99101

@@ -103,6 +105,17 @@ macro_rules! add_impl {
103105
fn add(self, other: $t) -> $t { self + other }
104106
}
105107

108+
#[stable(feature = "rust1", since = "1.0.0")]
109+
#[cfg(not(bootstrap))]
110+
impl const Add for $t {
111+
type Output = $t;
112+
113+
#[inline]
114+
#[track_caller]
115+
#[rustc_inherit_overflow_checks]
116+
fn add(self, other: $t) -> $t { self + other }
117+
}
118+
106119
forward_ref_binop! { impl Add, add for $t, $t }
107120
)*)
108121
}
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
2-
--> $DIR/call-const-trait-method-pass.rs:7:12
3-
|
4-
LL | impl const std::ops::Add for Int {
5-
| ^^^^^^^^^^^^^
6-
|
7-
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
8-
= note: adding a non-const method body in the future would be a breaking change
9-
101
error: const `impl` for trait `PartialEq` which is not marked with `#[const_trait]`
112
--> $DIR/call-const-trait-method-pass.rs:15:12
123
|
@@ -16,14 +7,6 @@ LL | impl const PartialEq for Int {
167
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
178
= note: adding a non-const method body in the future would be a breaking change
189

19-
error[E0015]: cannot call non-const operator in constants
20-
--> $DIR/call-const-trait-method-pass.rs:39:22
21-
|
22-
LL | const ADD_INT: Int = Int(1i32) + Int(2i32);
23-
| ^^^^^^^^^^^^^^^^^^^^^
24-
|
25-
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
26-
2710
error[E0015]: cannot call non-const fn `<Int as PartialEq>::eq` in constant functions
2811
--> $DIR/call-const-trait-method-pass.rs:20:15
2912
|
@@ -32,6 +15,6 @@ LL | !self.eq(other)
3215
|
3316
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
3417

35-
error: aborting due to 4 previous errors
18+
error: aborting due to 2 previous errors
3619

3720
For more information about this error, try `rustc --explain E0015`.
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,3 @@
1-
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
2-
--> $DIR/const-and-non-const-impl.rs:7:12
3-
|
4-
LL | impl const std::ops::Add for i32 {
5-
| ^^^^^^^^^^^^^
6-
|
7-
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
8-
= note: adding a non-const method body in the future would be a breaking change
9-
10-
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
11-
--> $DIR/const-and-non-const-impl.rs:23:12
12-
|
13-
LL | impl const std::ops::Add for Int {
14-
| ^^^^^^^^^^^^^
15-
|
16-
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
17-
= note: adding a non-const method body in the future would be a breaking change
18-
191
error[E0119]: conflicting implementations of trait `Add` for type `Int`
202
--> $DIR/const-and-non-const-impl.rs:23:1
213
|
@@ -38,7 +20,7 @@ LL | impl const std::ops::Add for i32 {
3820
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
3921
= note: define and implement a trait or new type instead
4022

41-
error: aborting due to 4 previous errors
23+
error: aborting due to 2 previous errors
4224

4325
Some errors have detailed explanations: E0117, E0119.
4426
For more information about an error, try `rustc --explain E0117`.

‎tests/ui/traits/const-traits/generic-bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ known-bug: #110395
1+
//@ check-pass
22

33
#![feature(const_trait_impl)]
44

‎tests/ui/traits/const-traits/generic-bound.stderr

-20
This file was deleted.

0 commit comments

Comments
 (0)