Skip to content

Commit 75b8f43

Browse files
authored
Rollup merge of #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 92f5144 + 514ef18 commit 75b8f43

File tree

9 files changed

+21
-79
lines changed

9 files changed

+21
-79
lines changed

Diff for: compiler/rustc_passes/src/stability.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
590590
}
591591

592592
fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
593-
// if the const impl is derived using the `derive_const` attribute,
594-
// then it would be "stable" at least for the impl.
595-
// We gate usages of it using `feature(const_trait_impl)` anyways
596-
// so there is no unstable leakage
597-
if self.tcx.is_automatically_derived(def_id.to_def_id()) {
598-
return;
599-
}
600-
601-
let is_const = self.tcx.is_const_fn(def_id.to_def_id())
602-
|| self.tcx.is_const_trait_impl(def_id.to_def_id());
593+
let is_const = self.tcx.is_const_fn(def_id.to_def_id());
603594

604595
// Reachable const fn must have a stability attribute.
605596
if is_const

Diff for: library/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)]

Diff for: 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
}

Diff for: tests/ui/stability-attribute/missing-const-stability.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub trait Bar {
2727
}
2828
#[stable(feature = "stable", since = "1.0.0")]
2929
impl const Bar for Foo {
30-
//~^ ERROR implementation has missing const stability attribute
30+
// ok because all users must enable `const_trait_impl`
3131
fn fun() {}
3232
}
3333

Diff for: tests/ui/stability-attribute/missing-const-stability.stderr

+1-10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,6 @@ error: function has missing const stability attribute
44
LL | pub const fn foo() {}
55
| ^^^^^^^^^^^^^^^^^^^^^
66

7-
error: implementation has missing const stability attribute
8-
--> $DIR/missing-const-stability.rs:29:1
9-
|
10-
LL | / impl const Bar for Foo {
11-
LL | |
12-
LL | | fn fun() {}
13-
LL | | }
14-
| |_^
15-
167
error: function has missing const stability attribute
178
--> $DIR/missing-const-stability.rs:36:1
189
|
@@ -25,5 +16,5 @@ error: associated function has missing const stability attribute
2516
LL | pub const fn foo() {}
2617
| ^^^^^^^^^^^^^^^^^^^^^
2718

28-
error: aborting due to 4 previous errors
19+
error: aborting due to 3 previous errors
2920

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`.
+1-19
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`.

Diff for: tests/ui/traits/const-traits/generic-bound.rs

+2-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

@@ -26,5 +26,6 @@ const fn twice<T: std::ops::Add>(arg: S<T>) -> S<T> {
2626
}
2727

2828
fn main() {
29+
const _: S<i32> = twice(S(PhantomData));
2930
let _ = twice(S(PhantomData::<i32>));
3031
}

Diff for: tests/ui/traits/const-traits/generic-bound.stderr

-20
This file was deleted.

0 commit comments

Comments
 (0)