Skip to content

Commit bf87bed

Browse files
committed
Auto merge of #100982 - fee1-dead-contrib:const-impl-requires-const-trait, r=oli-obk
Require `#[const_trait]` on `Trait` for `impl const Trait` r? `@oli-obk`
2 parents 542d5d3 + 53e3ce7 commit bf87bed

File tree

16 files changed

+62
-61
lines changed

16 files changed

+62
-61
lines changed

alloc/src/boxed.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,8 +2037,7 @@ impl<T: ?Sized, A: Allocator> AsMut<T> for Box<T, A> {
20372037
* could have a method to project a Pin<T> from it.
20382038
*/
20392039
#[stable(feature = "pin", since = "1.33.0")]
2040-
#[rustc_const_unstable(feature = "const_box", issue = "92521")]
2041-
impl<T: ?Sized, A: Allocator> const Unpin for Box<T, A> where A: 'static {}
2040+
impl<T: ?Sized, A: Allocator> Unpin for Box<T, A> where A: 'static {}
20422041

20432042
#[unstable(feature = "generator_trait", issue = "43122")]
20442043
impl<G: ?Sized + Generator<R> + Unpin, R, A: Allocator> Generator<R> for Box<G, A>

core/src/alloc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ impl fmt::Display for AllocError {
107107
///
108108
/// [*currently allocated*]: #currently-allocated-memory
109109
#[unstable(feature = "allocator_api", issue = "32838")]
110+
#[const_trait]
110111
pub unsafe trait Allocator {
111112
/// Attempts to allocate a block of memory.
112113
///

core/src/borrow.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@
154154
/// [`String`]: ../../std/string/struct.String.html
155155
#[stable(feature = "rust1", since = "1.0.0")]
156156
#[rustc_diagnostic_item = "Borrow"]
157+
#[const_trait]
157158
pub trait Borrow<Borrowed: ?Sized> {
158159
/// Immutably borrows from an owned value.
159160
///
@@ -184,6 +185,7 @@ pub trait Borrow<Borrowed: ?Sized> {
184185
/// an underlying type by providing a mutable reference. See [`Borrow<T>`]
185186
/// for more information on borrowing as another type.
186187
#[stable(feature = "rust1", since = "1.0.0")]
188+
#[const_trait]
187189
pub trait BorrowMut<Borrowed: ?Sized>: Borrow<Borrowed> {
188190
/// Mutably borrows from an owned value.
189191
///

core/src/cmp.rs

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,10 @@ use self::Ordering::*;
204204
#[stable(feature = "rust1", since = "1.0.0")]
205205
#[doc(alias = "==")]
206206
#[doc(alias = "!=")]
207-
#[cfg_attr(
208-
bootstrap,
209-
rustc_on_unimplemented(
210-
message = "can't compare `{Self}` with `{Rhs}`",
211-
label = "no implementation for `{Self} == {Rhs}`"
212-
)
213-
)]
214-
#[cfg_attr(
215-
not(bootstrap),
216-
rustc_on_unimplemented(
217-
message = "can't compare `{Self}` with `{Rhs}`",
218-
label = "no implementation for `{Self} == {Rhs}`",
219-
append_const_msg,
220-
)
207+
#[rustc_on_unimplemented(
208+
message = "can't compare `{Self}` with `{Rhs}`",
209+
label = "no implementation for `{Self} == {Rhs}`",
210+
append_const_msg
221211
)]
222212
#[const_trait]
223213
#[rustc_diagnostic_item = "PartialEq"]
@@ -1076,20 +1066,10 @@ impl const PartialOrd for Ordering {
10761066
#[doc(alias = "<")]
10771067
#[doc(alias = "<=")]
10781068
#[doc(alias = ">=")]
1079-
#[cfg_attr(
1080-
bootstrap,
1081-
rustc_on_unimplemented(
1082-
message = "can't compare `{Self}` with `{Rhs}`",
1083-
label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1084-
)
1085-
)]
1086-
#[cfg_attr(
1087-
not(bootstrap),
1088-
rustc_on_unimplemented(
1089-
message = "can't compare `{Self}` with `{Rhs}`",
1090-
label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1091-
append_const_msg,
1092-
)
1069+
#[rustc_on_unimplemented(
1070+
message = "can't compare `{Self}` with `{Rhs}`",
1071+
label = "no implementation for `{Self} < {Rhs}` and `{Self} > {Rhs}`",
1072+
append_const_msg
10931073
)]
10941074
#[const_trait]
10951075
#[rustc_diagnostic_item = "PartialOrd"]

core/src/convert/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ pub const fn identity<T>(x: T) -> T {
155155
/// ```
156156
#[stable(feature = "rust1", since = "1.0.0")]
157157
#[cfg_attr(not(test), rustc_diagnostic_item = "AsRef")]
158+
#[const_trait]
158159
pub trait AsRef<T: ?Sized> {
159160
/// Converts this type into a shared reference of the (usually inferred) input type.
160161
#[stable(feature = "rust1", since = "1.0.0")]
@@ -197,6 +198,7 @@ pub trait AsRef<T: ?Sized> {
197198
/// [`Box<T>`]: ../../std/boxed/struct.Box.html
198199
#[stable(feature = "rust1", since = "1.0.0")]
199200
#[cfg_attr(not(test), rustc_diagnostic_item = "AsMut")]
201+
#[const_trait]
200202
pub trait AsMut<T: ?Sized> {
201203
/// Converts this type into a mutable reference of the (usually inferred) input type.
202204
#[stable(feature = "rust1", since = "1.0.0")]
@@ -273,6 +275,7 @@ pub trait AsMut<T: ?Sized> {
273275
/// [`Vec`]: ../../std/vec/struct.Vec.html
274276
#[rustc_diagnostic_item = "Into"]
275277
#[stable(feature = "rust1", since = "1.0.0")]
278+
#[const_trait]
276279
pub trait Into<T>: Sized {
277280
/// Converts this type into the (usually inferred) input type.
278281
#[must_use]
@@ -368,6 +371,7 @@ pub trait Into<T>: Sized {
368371
all(_Self = "&str", T = "std::string::String"),
369372
note = "to coerce a `{T}` into a `{Self}`, use `&*` as a prefix",
370373
))]
374+
#[const_trait]
371375
pub trait From<T>: Sized {
372376
/// Converts to this type from the input type.
373377
#[lang = "from"]
@@ -392,6 +396,7 @@ pub trait From<T>: Sized {
392396
/// [`Into`], see there for details.
393397
#[rustc_diagnostic_item = "TryInto"]
394398
#[stable(feature = "try_from", since = "1.34.0")]
399+
#[const_trait]
395400
pub trait TryInto<T>: Sized {
396401
/// The type returned in the event of a conversion error.
397402
#[stable(feature = "try_from", since = "1.34.0")]
@@ -468,6 +473,7 @@ pub trait TryInto<T>: Sized {
468473
/// [`try_from`]: TryFrom::try_from
469474
#[rustc_diagnostic_item = "TryFrom"]
470475
#[stable(feature = "try_from", since = "1.34.0")]
476+
#[const_trait]
471477
pub trait TryFrom<T>: Sized {
472478
/// The type returned in the event of a conversion error.
473479
#[stable(feature = "try_from", since = "1.34.0")]

core/src/default.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
/// ```
100100
#[cfg_attr(not(test), rustc_diagnostic_item = "Default")]
101101
#[stable(feature = "rust1", since = "1.0.0")]
102+
#[cfg_attr(not(bootstrap), const_trait)]
102103
pub trait Default: Sized {
103104
/// Returns the "default value" for a type.
104105
///

core/src/iter/traits/collect.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ pub trait FromIterator<A>: Sized {
228228
#[rustc_diagnostic_item = "IntoIterator"]
229229
#[rustc_skip_array_during_method_dispatch]
230230
#[stable(feature = "rust1", since = "1.0.0")]
231+
#[const_trait]
231232
pub trait IntoIterator {
232233
/// The type of the elements being iterated over.
233234
#[stable(feature = "rust1", since = "1.0.0")]

core/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
#![feature(const_str_from_utf8_unchecked_mut)]
142142
#![feature(const_swap)]
143143
#![feature(const_trait_impl)]
144+
#![feature(const_try)]
144145
#![feature(const_type_id)]
145146
#![feature(const_type_name)]
146147
#![feature(const_default_impls)]

core/src/ops/arith.rs

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,38 +65,15 @@
6565
/// ```
6666
#[lang = "add"]
6767
#[stable(feature = "rust1", since = "1.0.0")]
68-
#[cfg_attr(
69-
bootstrap,
70-
rustc_on_unimplemented(
71-
on(
72-
all(_Self = "{integer}", Rhs = "{float}"),
73-
message = "cannot add a float to an integer",
74-
),
75-
on(
76-
all(_Self = "{float}", Rhs = "{integer}"),
77-
message = "cannot add an integer to a float",
78-
),
79-
message = "cannot add `{Rhs}` to `{Self}`",
80-
label = "no implementation for `{Self} + {Rhs}`"
81-
)
82-
)]
83-
#[cfg_attr(
84-
not(bootstrap),
85-
rustc_on_unimplemented(
86-
on(
87-
all(_Self = "{integer}", Rhs = "{float}"),
88-
message = "cannot add a float to an integer",
89-
),
90-
on(
91-
all(_Self = "{float}", Rhs = "{integer}"),
92-
message = "cannot add an integer to a float",
93-
),
94-
message = "cannot add `{Rhs}` to `{Self}`",
95-
label = "no implementation for `{Self} + {Rhs}`",
96-
append_const_msg,
97-
)
68+
#[rustc_on_unimplemented(
69+
on(all(_Self = "{integer}", Rhs = "{float}"), message = "cannot add a float to an integer",),
70+
on(all(_Self = "{float}", Rhs = "{integer}"), message = "cannot add an integer to a float",),
71+
message = "cannot add `{Rhs}` to `{Self}`",
72+
label = "no implementation for `{Self} + {Rhs}`",
73+
append_const_msg
9874
)]
9975
#[doc(alias = "+")]
76+
#[const_trait]
10077
pub trait Add<Rhs = Self> {
10178
/// The resulting type after applying the `+` operator.
10279
#[stable(feature = "rust1", since = "1.0.0")]
@@ -201,9 +178,11 @@ add_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
201178
#[stable(feature = "rust1", since = "1.0.0")]
202179
#[rustc_on_unimplemented(
203180
message = "cannot subtract `{Rhs}` from `{Self}`",
204-
label = "no implementation for `{Self} - {Rhs}`"
181+
label = "no implementation for `{Self} - {Rhs}`",
182+
append_const_msg
205183
)]
206184
#[doc(alias = "-")]
185+
#[const_trait]
207186
pub trait Sub<Rhs = Self> {
208187
/// The resulting type after applying the `-` operator.
209188
#[stable(feature = "rust1", since = "1.0.0")]
@@ -333,6 +312,7 @@ sub_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
333312
label = "no implementation for `{Self} * {Rhs}`"
334313
)]
335314
#[doc(alias = "*")]
315+
#[const_trait]
336316
pub trait Mul<Rhs = Self> {
337317
/// The resulting type after applying the `*` operator.
338318
#[stable(feature = "rust1", since = "1.0.0")]
@@ -466,6 +446,7 @@ mul_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
466446
label = "no implementation for `{Self} / {Rhs}`"
467447
)]
468448
#[doc(alias = "/")]
449+
#[const_trait]
469450
pub trait Div<Rhs = Self> {
470451
/// The resulting type after applying the `/` operator.
471452
#[stable(feature = "rust1", since = "1.0.0")]
@@ -568,6 +549,7 @@ div_impl_float! { f32 f64 }
568549
label = "no implementation for `{Self} % {Rhs}`"
569550
)]
570551
#[doc(alias = "%")]
552+
#[const_trait]
571553
pub trait Rem<Rhs = Self> {
572554
/// The resulting type after applying the `%` operator.
573555
#[stable(feature = "rust1", since = "1.0.0")]
@@ -682,6 +664,7 @@ rem_impl_float! { f32 f64 }
682664
#[lang = "neg"]
683665
#[stable(feature = "rust1", since = "1.0.0")]
684666
#[doc(alias = "-")]
667+
#[const_trait]
685668
pub trait Neg {
686669
/// The resulting type after applying the `-` operator.
687670
#[stable(feature = "rust1", since = "1.0.0")]
@@ -755,6 +738,7 @@ neg_impl! { isize i8 i16 i32 i64 i128 f32 f64 }
755738
)]
756739
#[doc(alias = "+")]
757740
#[doc(alias = "+=")]
741+
#[const_trait]
758742
pub trait AddAssign<Rhs = Self> {
759743
/// Performs the `+=` operation.
760744
///
@@ -822,6 +806,7 @@ add_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
822806
)]
823807
#[doc(alias = "-")]
824808
#[doc(alias = "-=")]
809+
#[const_trait]
825810
pub trait SubAssign<Rhs = Self> {
826811
/// Performs the `-=` operation.
827812
///
@@ -880,6 +865,7 @@ sub_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
880865
)]
881866
#[doc(alias = "*")]
882867
#[doc(alias = "*=")]
868+
#[const_trait]
883869
pub trait MulAssign<Rhs = Self> {
884870
/// Performs the `*=` operation.
885871
///
@@ -938,6 +924,7 @@ mul_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
938924
)]
939925
#[doc(alias = "/")]
940926
#[doc(alias = "/=")]
927+
#[const_trait]
941928
pub trait DivAssign<Rhs = Self> {
942929
/// Performs the `/=` operation.
943930
///
@@ -999,6 +986,7 @@ div_assign_impl! { usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 f32 f64 }
999986
)]
1000987
#[doc(alias = "%")]
1001988
#[doc(alias = "%=")]
989+
#[const_trait]
1002990
pub trait RemAssign<Rhs = Self> {
1003991
/// Performs the `%=` operation.
1004992
///

core/src/ops/bit.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#[lang = "not"]
3232
#[stable(feature = "rust1", since = "1.0.0")]
3333
#[doc(alias = "!")]
34+
#[const_trait]
3435
pub trait Not {
3536
/// The resulting type after applying the `!` operator.
3637
#[stable(feature = "rust1", since = "1.0.0")]
@@ -143,6 +144,7 @@ impl const Not for ! {
143144
message = "no implementation for `{Self} & {Rhs}`",
144145
label = "no implementation for `{Self} & {Rhs}`"
145146
)]
147+
#[const_trait]
146148
pub trait BitAnd<Rhs = Self> {
147149
/// The resulting type after applying the `&` operator.
148150
#[stable(feature = "rust1", since = "1.0.0")]
@@ -244,6 +246,7 @@ bitand_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
244246
message = "no implementation for `{Self} | {Rhs}`",
245247
label = "no implementation for `{Self} | {Rhs}`"
246248
)]
249+
#[const_trait]
247250
pub trait BitOr<Rhs = Self> {
248251
/// The resulting type after applying the `|` operator.
249252
#[stable(feature = "rust1", since = "1.0.0")]
@@ -345,6 +348,7 @@ bitor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
345348
message = "no implementation for `{Self} ^ {Rhs}`",
346349
label = "no implementation for `{Self} ^ {Rhs}`"
347350
)]
351+
#[const_trait]
348352
pub trait BitXor<Rhs = Self> {
349353
/// The resulting type after applying the `^` operator.
350354
#[stable(feature = "rust1", since = "1.0.0")]
@@ -445,6 +449,7 @@ bitxor_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
445449
message = "no implementation for `{Self} << {Rhs}`",
446450
label = "no implementation for `{Self} << {Rhs}`"
447451
)]
452+
#[const_trait]
448453
pub trait Shl<Rhs = Self> {
449454
/// The resulting type after applying the `<<` operator.
450455
#[stable(feature = "rust1", since = "1.0.0")]
@@ -564,6 +569,7 @@ shl_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 isize i128 }
564569
message = "no implementation for `{Self} >> {Rhs}`",
565570
label = "no implementation for `{Self} >> {Rhs}`"
566571
)]
572+
#[const_trait]
567573
pub trait Shr<Rhs = Self> {
568574
/// The resulting type after applying the `>>` operator.
569575
#[stable(feature = "rust1", since = "1.0.0")]
@@ -692,6 +698,7 @@ shr_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
692698
message = "no implementation for `{Self} &= {Rhs}`",
693699
label = "no implementation for `{Self} &= {Rhs}`"
694700
)]
701+
#[const_trait]
695702
pub trait BitAndAssign<Rhs = Self> {
696703
/// Performs the `&=` operation.
697704
///
@@ -764,6 +771,7 @@ bitand_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
764771
message = "no implementation for `{Self} |= {Rhs}`",
765772
label = "no implementation for `{Self} |= {Rhs}`"
766773
)]
774+
#[const_trait]
767775
pub trait BitOrAssign<Rhs = Self> {
768776
/// Performs the `|=` operation.
769777
///
@@ -836,6 +844,7 @@ bitor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
836844
message = "no implementation for `{Self} ^= {Rhs}`",
837845
label = "no implementation for `{Self} ^= {Rhs}`"
838846
)]
847+
#[const_trait]
839848
pub trait BitXorAssign<Rhs = Self> {
840849
/// Performs the `^=` operation.
841850
///
@@ -906,6 +915,7 @@ bitxor_assign_impl! { bool usize u8 u16 u32 u64 u128 isize i8 i16 i32 i64 i128 }
906915
message = "no implementation for `{Self} <<= {Rhs}`",
907916
label = "no implementation for `{Self} <<= {Rhs}`"
908917
)]
918+
#[const_trait]
909919
pub trait ShlAssign<Rhs = Self> {
910920
/// Performs the `<<=` operation.
911921
///
@@ -989,6 +999,7 @@ shl_assign_impl_all! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize }
989999
message = "no implementation for `{Self} >>= {Rhs}`",
9901000
label = "no implementation for `{Self} >>= {Rhs}`"
9911001
)]
1002+
#[const_trait]
9921003
pub trait ShrAssign<Rhs = Self> {
9931004
/// Performs the `>>=` operation.
9941005
///

0 commit comments

Comments
 (0)