Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimally constify Add #133237

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions compiler/rustc_passes/src/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,16 +590,7 @@ impl<'tcx> MissingStabilityAnnotations<'tcx> {
}

fn check_missing_const_stability(&self, def_id: LocalDefId, span: Span) {
// if the const impl is derived using the `derive_const` attribute,
// then it would be "stable" at least for the impl.
// We gate usages of it using `feature(const_trait_impl)` anyways
// so there is no unstable leakage
if self.tcx.is_automatically_derived(def_id.to_def_id()) {
return;
}

let is_const = self.tcx.is_const_fn(def_id.to_def_id())
|| self.tcx.is_const_trait_impl(def_id.to_def_id());
let is_const = self.tcx.is_const_fn(def_id.to_def_id());

// Reachable const fn must have a stability attribute.
if is_const
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
#![feature(const_is_char_boundary)]
#![feature(const_precise_live_drops)]
#![feature(const_str_split_at)]
#![feature(const_trait_impl)]
#![feature(decl_macro)]
#![feature(deprecated_suggestion)]
#![feature(doc_cfg)]
Expand Down
13 changes: 13 additions & 0 deletions library/core/src/ops/arith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
append_const_msg
)]
#[doc(alias = "+")]
#[cfg_attr(not(bootstrap), const_trait)]
pub trait Add<Rhs = Self> {
/// The resulting type after applying the `+` operator.
#[stable(feature = "rust1", since = "1.0.0")]
Expand All @@ -94,6 +95,7 @@ pub trait Add<Rhs = Self> {
macro_rules! add_impl {
($($t:ty)*) => ($(
#[stable(feature = "rust1", since = "1.0.0")]
#[cfg(bootstrap)]
impl Add for $t {
type Output = $t;

Expand All @@ -103,6 +105,17 @@ macro_rules! add_impl {
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 }
)*)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/stability-attribute/missing-const-stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait Bar {
}
#[stable(feature = "stable", since = "1.0.0")]
impl const Bar for Foo {
//~^ ERROR implementation has missing const stability attribute
// ok because all users must enable `const_trait_impl`
fn fun() {}
}

Expand Down
11 changes: 1 addition & 10 deletions tests/ui/stability-attribute/missing-const-stability.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,6 @@ error: function has missing const stability attribute
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: implementation has missing const stability attribute
--> $DIR/missing-const-stability.rs:29:1
|
LL | / impl const Bar for Foo {
LL | |
LL | | fn fun() {}
LL | | }
| |_^

error: function has missing const stability attribute
--> $DIR/missing-const-stability.rs:36:1
|
Expand All @@ -25,5 +16,5 @@ error: associated function has missing const stability attribute
LL | pub const fn foo() {}
| ^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 4 previous errors
error: aborting due to 3 previous errors

19 changes: 1 addition & 18 deletions tests/ui/traits/const-traits/call-const-trait-method-pass.stderr
Original file line number Diff line number Diff line change
@@ -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]`
--> $DIR/call-const-trait-method-pass.rs:15:12
|
Expand All @@ -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: 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
--> $DIR/call-const-trait-method-pass.rs:20:15
|
Expand All @@ -32,6 +15,6 @@ LL | !self.eq(other)
|
= 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`.
20 changes: 1 addition & 19 deletions tests/ui/traits/const-traits/const-and-non-const-impl.stderr
Original file line number Diff line number Diff line change
@@ -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`
--> $DIR/const-and-non-const-impl.rs:23:1
|
Expand All @@ -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: 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.
For more information about an error, try `rustc --explain E0117`.
3 changes: 2 additions & 1 deletion tests/ui/traits/const-traits/generic-bound.rs
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this test also call twice from const context?

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

#![feature(const_trait_impl)]

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

fn main() {
const _: S<i32> = twice(S(PhantomData));
let _ = twice(S(PhantomData::<i32>));
}
20 changes: 0 additions & 20 deletions tests/ui/traits/const-traits/generic-bound.stderr

This file was deleted.

Loading