Rollup merge of #111649 - Nilstrieb:derive-const-param-ty, r=BoxyUwU

Add derive for `core::marker::ConstParamTy`

This makes it easier to implement it for a type, just like `Copy`.

`@BoxyUwU` half asked me to add it
This commit is contained in:
Dylan DPC 2023-05-17 11:13:57 +05:30 committed by GitHub
commit bc3b94a486
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 115 additions and 2 deletions

View File

@ -27,3 +27,26 @@ pub fn expand_deriving_copy(
trait_def.expand(cx, mitem, item, push);
}
pub fn expand_deriving_const_param_ty(
cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable),
is_const: bool,
) {
let trait_def = TraitDef {
span,
path: path_std!(marker::ConstParamTy),
skip_path_as_bound: false,
needs_copy_as_bound_if_packed: false,
additional_bounds: Vec::new(),
supports_unions: false,
methods: Vec::new(),
associated_types: Vec::new(),
is_const,
};
trait_def.expand(cx, mitem, item, push);
}

View File

@ -115,6 +115,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
register_derive! {
Clone: clone::expand_deriving_clone,
Copy: bounds::expand_deriving_copy,
ConstParamTy: bounds::expand_deriving_const_param_ty,
Debug: debug::expand_deriving_debug,
Default: default::expand_deriving_default,
Eq: eq::expand_deriving_eq,

View File

@ -164,6 +164,7 @@ symbols! {
Capture,
Center,
Clone,
ConstParamTy,
Context,
Continue,
Copy,

View File

@ -986,6 +986,14 @@ pub trait PointerLike {}
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
pub trait ConstParamTy: StructuralEq {}
/// Derive macro generating an impl of the trait `Copy`.
#[rustc_builtin_macro]
#[unstable(feature = "adt_const_params", issue = "95174")]
#[cfg(not(bootstrap))]
pub macro ConstParamTy($item:item) {
/* compiler built-in */
}
// FIXME(generic_const_parameter_types): handle `ty::FnDef`/`ty::Closure`
// FIXME(generic_const_parameter_types): handle `ty::Tuple`
marker_impls! {

View File

@ -11,6 +11,13 @@ struct S<T> {
impl<T: ConstParamTy> ConstParamTy for S<T> {}
#[derive(PartialEq, Eq, ConstParamTy)]
struct D<T> {
field: u8,
gen: T,
}
fn check<T: ConstParamTy + ?Sized>() {}
fn main() {
@ -39,5 +46,8 @@ fn main() {
check::<S<u8>>();
check::<S<[&[bool]; 8]>>();
check::<D<u8>>();
check::<D<[&[bool]; 8]>>();
// FIXME: test tuples
}

View File

@ -10,4 +10,8 @@ struct CantParam(NotParam);
impl std::marker::ConstParamTy for CantParam {}
//~^ error: the trait `ConstParamTy` cannot be implemented for this type
#[derive(std::marker::ConstParamTy, Eq, PartialEq)]
//~^ error: the trait `ConstParamTy` cannot be implemented for this type
struct CantParamDerive(NotParam);
fn main() {}

View File

@ -7,6 +7,17 @@ LL |
LL | impl std::marker::ConstParamTy for CantParam {}
| ^^^^^^^^^
error: aborting due to previous error
error[E0204]: the trait `ConstParamTy` cannot be implemented for this type
--> $DIR/const_param_ty_impl_bad_field.rs:13:10
|
LL | #[derive(std::marker::ConstParamTy, Eq, PartialEq)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
LL |
LL | struct CantParamDerive(NotParam);
| -------- this field does not implement `ConstParamTy`
|
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0204`.

View File

@ -10,6 +10,10 @@ struct CantParam(ImplementsConstParamTy);
impl std::marker::ConstParamTy for CantParam {}
//~^ error: the type `CantParam` does not `#[derive(Eq)]`
#[derive(std::marker::ConstParamTy)]
//~^ error: the type `CantParamDerive` does not `#[derive(Eq)]`
struct CantParamDerive(ImplementsConstParamTy);
fn check<T: std::marker::ConstParamTy>() {}
fn main() {

View File

@ -7,6 +7,16 @@ LL | impl std::marker::ConstParamTy for CantParam {}
note: required by a bound in `ConstParamTy`
--> $SRC_DIR/core/src/marker.rs:LL:COL
error: aborting due to previous error
error[E0277]: the type `CantParamDerive` does not `#[derive(Eq)]`
--> $DIR/const_param_ty_impl_no_structural_eq.rs:13:10
|
LL | #[derive(std::marker::ConstParamTy)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParamDerive`
|
note: required by a bound in `ConstParamTy`
--> $SRC_DIR/core/src/marker.rs:LL:COL
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.

View File

@ -0,0 +1,33 @@
#![allow(incomplete_features)]
#![feature(adt_const_params, structural_match)]
union Union {
a: u8,
}
impl PartialEq for Union {
fn eq(&self, other: &Union) -> bool {
true
}
}
impl Eq for Union {}
impl std::marker::StructuralEq for Union {}
impl std::marker::ConstParamTy for Union {}
#[derive(std::marker::ConstParamTy)]
//~^ ERROR this trait cannot be derived for unions
union UnionDerive {
a: u8,
}
impl PartialEq for UnionDerive {
fn eq(&self, other: &UnionDerive) -> bool {
true
}
}
impl Eq for UnionDerive {}
impl std::marker::StructuralEq for UnionDerive {}
fn main() {}

View File

@ -0,0 +1,8 @@
error: this trait cannot be derived for unions
--> $DIR/const_param_ty_impl_union.rs:18:10
|
LL | #[derive(std::marker::ConstParamTy)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error