Skip to content

Commit a850f7c

Browse files
authoredNov 23, 2024
Rollup merge of rust-lang#133237 - fee1-dead-contrib:constadd, r=compiler-errors
Minimally constify `Add` * This PR removes the requirement for `impl const` to have a const stability attribute. cc ``@RalfJung`` I believe you mentioned that it would make much more sense to require `const_trait`s to have const stability instead. I agree with that sentiment but I don't think that is _required_ for a small scale experimentation like this PR. rust-lang/project-const-traits#16 should definitely be prioritized in the future, but removing the impl check should be good for now as all callers need `const_trait_impl` enabled for any const impl to work. * This PR is intentionally minimal as constifying other traits can become more complicated (`PartialEq`, for example, would run into requiring implementing it for `str` as that is used in matches, which runs into the implementation for slice equality which uses specialization) Per the reasons above, anyone who is interested in making traits `const` in the standard library are **strongly encouraged** to reach out to us on the [Zulip channel](https://rust-lang.zulipchat.com/#narrow/channel/419616-t-compiler.2Fproject-const-traits) before proceeding with the work. cc ``@rust-lang/project-const-traits`` I believe there is prior approval from libs that we can experiment, so r? project-const-traits
2 parents 6548ad8 + ec220b6 commit a850f7c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed
 

‎core/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
#![feature(const_is_char_boundary)]
175175
#![feature(const_precise_live_drops)]
176176
#![feature(const_str_split_at)]
177+
#![feature(const_trait_impl)]
177178
#![feature(decl_macro)]
178179
#![feature(deprecated_suggestion)]
179180
#![feature(doc_cfg)]

‎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
}

0 commit comments

Comments
 (0)