Skip to content

Commit d194926

Browse files
Rollup merge of #111670 - compiler-errors:const-param-ty, r=BoxyUwU
Require that const param tys implement `ConstParamTy` 1. Require that const param tys implement `ConstParamTy` instead of using `search_for_adt_const_param_violation` 2. Add `StructuralPartialEq` as a supertrait for `ConstParamTy`, since we need to make sure that we derive *both* `PartialEq` and `Eq` 3. Implement `ConstParamTy` for tuples up to 12 (or whatever the default for tuples is) 4. Add some custom diagnostics to `ConstParamTy` errors, to avoid regressions from (1.). It's still not as great as it could be -- will point out inline in comments. r? `@BoxyUwU`
2 parents e344e84 + 047d3ed commit d194926

File tree

3 files changed

+53
-3
lines changed

3 files changed

+53
-3
lines changed

core/src/marker.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,20 @@ pub trait StructuralPartialEq {
205205
// Empty.
206206
}
207207

208+
marker_impls! {
209+
#[unstable(feature = "structural_match", issue = "31434")]
210+
StructuralPartialEq for
211+
usize, u8, u16, u32, u64, u128,
212+
isize, i8, i16, i32, i64, i128,
213+
bool,
214+
char,
215+
str /* Technically requires `[u8]: StructuralEq` */,
216+
(),
217+
{T, const N: usize} [T; N],
218+
{T} [T],
219+
{T: ?Sized} &T,
220+
}
221+
208222
/// Required trait for constants used in pattern matches.
209223
///
210224
/// Any type that derives `Eq` automatically implements this trait, *regardless*
@@ -267,6 +281,7 @@ marker_impls! {
267281
bool,
268282
char,
269283
str /* Technically requires `[u8]: StructuralEq` */,
284+
(),
270285
{T, const N: usize} [T; N],
271286
{T} [T],
272287
{T: ?Sized} &T,
@@ -974,7 +989,8 @@ pub trait PointerLike {}
974989
#[lang = "const_param_ty"]
975990
#[unstable(feature = "adt_const_params", issue = "95174")]
976991
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
977-
pub trait ConstParamTy: StructuralEq {}
992+
#[allow(multiple_supertrait_upcastable)]
993+
pub trait ConstParamTy: StructuralEq + StructuralPartialEq {}
978994

979995
/// Derive macro generating an impl of the trait `ConstParamTy`.
980996
#[rustc_builtin_macro]
@@ -983,8 +999,7 @@ pub macro ConstParamTy($item:item) {
983999
/* compiler built-in */
9841000
}
9851001

986-
// FIXME(generic_const_parameter_types): handle `ty::FnDef`/`ty::Closure`
987-
// FIXME(generic_const_parameter_types): handle `ty::Tuple`
1002+
// FIXME(adt_const_params): handle `ty::FnDef`/`ty::Closure`
9881003
marker_impls! {
9891004
#[unstable(feature = "adt_const_params", issue = "95174")]
9901005
ConstParamTy for
@@ -998,6 +1013,11 @@ marker_impls! {
9981013
{T: ?Sized + ConstParamTy} &T,
9991014
}
10001015

1016+
// FIXME(adt_const_params): Add to marker_impls call above once not in bootstrap
1017+
#[unstable(feature = "adt_const_params", issue = "95174")]
1018+
#[cfg(not(bootstrap))]
1019+
impl ConstParamTy for () {}
1020+
10011021
/// A common trait implemented by all function pointers.
10021022
#[unstable(
10031023
feature = "fn_ptr_trait",

core/src/mem/transmutability.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use crate::marker::ConstParamTy;
2+
13
/// Are values of a type transmutable into values of another type?
24
///
35
/// This trait is implemented on-the-fly by the compiler for types `Src` and `Self` when the bits of
@@ -33,6 +35,9 @@ pub struct Assume {
3335
pub validity: bool,
3436
}
3537

38+
#[unstable(feature = "transmutability", issue = "99571")]
39+
impl ConstParamTy for Assume {}
40+
3641
impl Assume {
3742
/// Do not assume that *you* have ensured any safety properties are met.
3843
#[unstable(feature = "transmutability", issue = "99571")]

core/src/tuple.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
// See src/libstd/primitive_docs.rs for documentation.
22

33
use crate::cmp::Ordering::{self, *};
4+
#[cfg(not(bootstrap))]
5+
use crate::marker::ConstParamTy;
6+
use crate::marker::{StructuralEq, StructuralPartialEq};
47

58
// Recursive macro for implementing n-ary tuple functions and operations
69
//
@@ -45,6 +48,28 @@ macro_rules! tuple_impls {
4548
{}
4649
}
4750

51+
maybe_tuple_doc! {
52+
$($T)+ @
53+
#[unstable(feature = "structural_match", issue = "31434")]
54+
#[cfg(not(bootstrap))]
55+
impl<$($T: ConstParamTy),+> ConstParamTy for ($($T,)+)
56+
{}
57+
}
58+
59+
maybe_tuple_doc! {
60+
$($T)+ @
61+
#[unstable(feature = "structural_match", issue = "31434")]
62+
impl<$($T),+> StructuralPartialEq for ($($T,)+)
63+
{}
64+
}
65+
66+
maybe_tuple_doc! {
67+
$($T)+ @
68+
#[unstable(feature = "structural_match", issue = "31434")]
69+
impl<$($T),+> StructuralEq for ($($T,)+)
70+
{}
71+
}
72+
4873
maybe_tuple_doc! {
4974
$($T)+ @
5075
#[stable(feature = "rust1", since = "1.0.0")]

0 commit comments

Comments
 (0)