Skip to content

Commit a9fcb52

Browse files
Impl ConstParamTy for tuples, make PartialStructuralEq a supertrait too
1 parent 8ab10ba commit a9fcb52

File tree

7 files changed

+68
-7
lines changed

7 files changed

+68
-7
lines changed

compiler/rustc_trait_selection/src/traits/misc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ pub fn type_allowed_to_implement_const_param_ty<'tcx>(
100100
| ty::Str
101101
| ty::Array(..)
102102
| ty::Slice(_)
103-
| ty::Ref(.., hir::Mutability::Not) => return Ok(()),
103+
| ty::Ref(.., hir::Mutability::Not)
104+
| ty::Tuple(_) => return Ok(()),
104105

105106
&ty::Adt(adt, substs) => (adt, substs),
106107

library/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",

library/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")]

tests/ui/const-generics/adt_const_params/const_param_ty_good.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,7 @@ fn main() {
4949
check::<D<u8>>();
5050
check::<D<[&[bool]; 8]>>();
5151

52-
// FIXME: test tuples
52+
check::<()>();
53+
check::<(i32,)>();
54+
check::<(D<u8>, D<i32>)>();
5355
}

tests/ui/const-generics/adt_const_params/const_param_ty_impl_no_structural_eq.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ struct CantParam(ImplementsConstParamTy);
99

1010
impl std::marker::ConstParamTy for CantParam {}
1111
//~^ error: the type `CantParam` does not `#[derive(Eq)]`
12+
//~| error: the type `CantParam` does not `#[derive(PartialEq)]`
1213

1314
#[derive(std::marker::ConstParamTy)]
1415
//~^ error: the type `CantParamDerive` does not `#[derive(Eq)]`
16+
//~| error: the type `CantParamDerive` does not `#[derive(PartialEq)]`
1517
struct CantParamDerive(ImplementsConstParamTy);
1618

1719
fn check<T: std::marker::ConstParamTy>() {}

tests/ui/const-generics/adt_const_params/const_param_ty_impl_union.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl Eq for Union {}
1414
impl std::marker::StructuralEq for Union {}
1515

1616
impl std::marker::ConstParamTy for Union {}
17+
//~^ ERROR the type `Union` does not `#[derive(PartialEq)]`
1718

1819
#[derive(std::marker::ConstParamTy)]
1920
//~^ ERROR this trait cannot be derived for unions
Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
error: this trait cannot be derived for unions
2-
--> $DIR/const_param_ty_impl_union.rs:18:10
2+
--> $DIR/const_param_ty_impl_union.rs:19:10
33
|
44
LL | #[derive(std::marker::ConstParamTy)]
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error: aborting due to previous error
7+
error[E0277]: the type `Union` does not `#[derive(PartialEq)]`
8+
--> $DIR/const_param_ty_impl_union.rs:16:36
9+
|
10+
LL | impl std::marker::ConstParamTy for Union {}
11+
| ^^^^^ the trait `StructuralPartialEq` is not implemented for `Union`
12+
|
13+
note: required by a bound in `ConstParamTy`
14+
--> $SRC_DIR/core/src/marker.rs:LL:COL
15+
16+
error: aborting due to 2 previous errors
817

18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)