Skip to content

Add derive for core::marker::ConstParamTy #111649

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

Merged
merged 1 commit into from
May 17, 2023
Merged
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
23 changes: 23 additions & 0 deletions compiler/rustc_builtin_macros/src/deriving/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ symbols! {
Capture,
Center,
Clone,
ConstParamTy,
Context,
Continue,
Copy,
Expand Down
8 changes: 8 additions & 0 deletions library/core/src/marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -991,6 +991,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! {
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/const-generics/adt_const_params/const_param_ty_good.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -39,5 +46,8 @@ fn main() {
check::<S<u8>>();
check::<S<[&[bool]; 8]>>();

check::<D<u8>>();
check::<D<[&[bool]; 8]>>();

// FIXME: test tuples
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Original file line number Diff line number Diff line change
@@ -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 {}
Copy link
Member

Choose a reason for hiding this comment

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

xd this should error oh well not this PR's fault

Copy link
Member Author

Choose a reason for hiding this comment

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

well, i'm glad i added a test at least 😆


#[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() {}
Original file line number Diff line number Diff line change
@@ -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