constify Add

This commit is contained in:
Deadbeef 2024-11-20 17:04:14 +08:00
parent 030ddeecab
commit 514ef180fd
6 changed files with 18 additions and 58 deletions

View file

@ -175,6 +175,7 @@
#![feature(const_is_char_boundary)] #![feature(const_is_char_boundary)]
#![feature(const_precise_live_drops)] #![feature(const_precise_live_drops)]
#![feature(const_str_split_at)] #![feature(const_str_split_at)]
#![feature(const_trait_impl)]
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(deprecated_suggestion)] #![feature(deprecated_suggestion)]
#![feature(doc_cfg)] #![feature(doc_cfg)]

View file

@ -73,6 +73,7 @@
append_const_msg append_const_msg
)] )]
#[doc(alias = "+")] #[doc(alias = "+")]
#[cfg_attr(not(bootstrap), const_trait)]
pub trait Add<Rhs = Self> { pub trait Add<Rhs = Self> {
/// The resulting type after applying the `+` operator. /// The resulting type after applying the `+` operator.
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
@ -94,6 +95,7 @@ pub trait Add<Rhs = Self> {
macro_rules! add_impl { macro_rules! add_impl {
($($t:ty)*) => ($( ($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
#[cfg(bootstrap)]
impl Add for $t { impl Add for $t {
type Output = $t; type Output = $t;
@ -103,6 +105,17 @@ macro_rules! add_impl {
fn add(self, other: $t) -> $t { self + other } fn add(self, other: $t) -> $t { self + other }
} }
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(not(bootstrap))]
impl const Add for $t {
type Output = $t;
#[inline]
#[track_caller]
#[rustc_inherit_overflow_checks]
fn add(self, other: $t) -> $t { self + other }
}
forward_ref_binop! { impl Add, add for $t, $t } forward_ref_binop! { impl Add, add for $t, $t }
)*) )*)
} }

View file

@ -1,12 +1,3 @@
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
--> $DIR/call-const-trait-method-pass.rs:7:12
|
LL | impl const std::ops::Add for Int {
| ^^^^^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error: const `impl` for trait `PartialEq` which is not marked with `#[const_trait]` error: const `impl` for trait `PartialEq` which is not marked with `#[const_trait]`
--> $DIR/call-const-trait-method-pass.rs:15:12 --> $DIR/call-const-trait-method-pass.rs:15:12
| |
@ -16,14 +7,6 @@ LL | impl const PartialEq for Int {
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const` = note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change = note: adding a non-const method body in the future would be a breaking change
error[E0015]: cannot call non-const operator in constants
--> $DIR/call-const-trait-method-pass.rs:39:22
|
LL | const ADD_INT: Int = Int(1i32) + Int(2i32);
| ^^^^^^^^^^^^^^^^^^^^^
|
= note: calls in constants are limited to constant functions, tuple structs and tuple variants
error[E0015]: cannot call non-const fn `<Int as PartialEq>::eq` in constant functions error[E0015]: cannot call non-const fn `<Int as PartialEq>::eq` in constant functions
--> $DIR/call-const-trait-method-pass.rs:20:15 --> $DIR/call-const-trait-method-pass.rs:20:15
| |
@ -32,6 +15,6 @@ LL | !self.eq(other)
| |
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 4 previous errors error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0015`. For more information about this error, try `rustc --explain E0015`.

View file

@ -1,21 +1,3 @@
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
--> $DIR/const-and-non-const-impl.rs:7:12
|
LL | impl const std::ops::Add for i32 {
| ^^^^^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
--> $DIR/const-and-non-const-impl.rs:23:12
|
LL | impl const std::ops::Add for Int {
| ^^^^^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0119]: conflicting implementations of trait `Add` for type `Int` error[E0119]: conflicting implementations of trait `Add` for type `Int`
--> $DIR/const-and-non-const-impl.rs:23:1 --> $DIR/const-and-non-const-impl.rs:23:1
| |
@ -38,7 +20,7 @@ LL | impl const std::ops::Add for i32 {
= note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules = note: for more information see https://doc.rust-lang.org/reference/items/implementations.html#orphan-rules
= note: define and implement a trait or new type instead = note: define and implement a trait or new type instead
error: aborting due to 4 previous errors error: aborting due to 2 previous errors
Some errors have detailed explanations: E0117, E0119. Some errors have detailed explanations: E0117, E0119.
For more information about an error, try `rustc --explain E0117`. For more information about an error, try `rustc --explain E0117`.

View file

@ -1,4 +1,4 @@
//@ known-bug: #110395 //@ check-pass
#![feature(const_trait_impl)] #![feature(const_trait_impl)]
@ -26,5 +26,6 @@ const fn twice<T: std::ops::Add>(arg: S<T>) -> S<T> {
} }
fn main() { fn main() {
const _: S<i32> = twice(S(PhantomData));
let _ = twice(S(PhantomData::<i32>)); let _ = twice(S(PhantomData::<i32>));
} }

View file

@ -1,20 +0,0 @@
error: const `impl` for trait `Add` which is not marked with `#[const_trait]`
--> $DIR/generic-bound.rs:16:15
|
LL | impl<T> const std::ops::Add for S<T> {
| ^^^^^^^^^^^^^
|
= note: marking a trait with `#[const_trait]` ensures all default method bodies are `const`
= note: adding a non-const method body in the future would be a breaking change
error[E0015]: cannot call non-const operator in constant functions
--> $DIR/generic-bound.rs:25:5
|
LL | arg + arg
| ^^^^^^^^^
|
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0015`.