Skip to content

Commit bc3b94a

Browse files
authored
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
2 parents e7176db + 0336dd1 commit bc3b94a

11 files changed

+115
-2
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

+23
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,26 @@ pub fn expand_deriving_copy(
2727

2828
trait_def.expand(cx, mitem, item, push);
2929
}
30+
31+
pub fn expand_deriving_const_param_ty(
32+
cx: &mut ExtCtxt<'_>,
33+
span: Span,
34+
mitem: &MetaItem,
35+
item: &Annotatable,
36+
push: &mut dyn FnMut(Annotatable),
37+
is_const: bool,
38+
) {
39+
let trait_def = TraitDef {
40+
span,
41+
path: path_std!(marker::ConstParamTy),
42+
skip_path_as_bound: false,
43+
needs_copy_as_bound_if_packed: false,
44+
additional_bounds: Vec::new(),
45+
supports_unions: false,
46+
methods: Vec::new(),
47+
associated_types: Vec::new(),
48+
is_const,
49+
};
50+
51+
trait_def.expand(cx, mitem, item, push);
52+
}

compiler/rustc_builtin_macros/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
115115
register_derive! {
116116
Clone: clone::expand_deriving_clone,
117117
Copy: bounds::expand_deriving_copy,
118+
ConstParamTy: bounds::expand_deriving_const_param_ty,
118119
Debug: debug::expand_deriving_debug,
119120
Default: default::expand_deriving_default,
120121
Eq: eq::expand_deriving_eq,

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ symbols! {
164164
Capture,
165165
Center,
166166
Clone,
167+
ConstParamTy,
167168
Context,
168169
Continue,
169170
Copy,

library/core/src/marker.rs

+8
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,14 @@ pub trait PointerLike {}
986986
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
987987
pub trait ConstParamTy: StructuralEq {}
988988

989+
/// Derive macro generating an impl of the trait `Copy`.
990+
#[rustc_builtin_macro]
991+
#[unstable(feature = "adt_const_params", issue = "95174")]
992+
#[cfg(not(bootstrap))]
993+
pub macro ConstParamTy($item:item) {
994+
/* compiler built-in */
995+
}
996+
989997
// FIXME(generic_const_parameter_types): handle `ty::FnDef`/`ty::Closure`
990998
// FIXME(generic_const_parameter_types): handle `ty::Tuple`
991999
marker_impls! {

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

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ struct S<T> {
1111

1212
impl<T: ConstParamTy> ConstParamTy for S<T> {}
1313

14+
#[derive(PartialEq, Eq, ConstParamTy)]
15+
struct D<T> {
16+
field: u8,
17+
gen: T,
18+
}
19+
20+
1421
fn check<T: ConstParamTy + ?Sized>() {}
1522

1623
fn main() {
@@ -39,5 +46,8 @@ fn main() {
3946
check::<S<u8>>();
4047
check::<S<[&[bool]; 8]>>();
4148

49+
check::<D<u8>>();
50+
check::<D<[&[bool]; 8]>>();
51+
4252
// FIXME: test tuples
4353
}

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

+4
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ struct CantParam(NotParam);
1010
impl std::marker::ConstParamTy for CantParam {}
1111
//~^ error: the trait `ConstParamTy` cannot be implemented for this type
1212

13+
#[derive(std::marker::ConstParamTy, Eq, PartialEq)]
14+
//~^ error: the trait `ConstParamTy` cannot be implemented for this type
15+
struct CantParamDerive(NotParam);
16+
1317
fn main() {}

tests/ui/const-generics/adt_const_params/const_param_ty_impl_bad_field.stderr

+12-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ LL |
77
LL | impl std::marker::ConstParamTy for CantParam {}
88
| ^^^^^^^^^
99

10-
error: aborting due to previous error
10+
error[E0204]: the trait `ConstParamTy` cannot be implemented for this type
11+
--> $DIR/const_param_ty_impl_bad_field.rs:13:10
12+
|
13+
LL | #[derive(std::marker::ConstParamTy, Eq, PartialEq)]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
15+
LL |
16+
LL | struct CantParamDerive(NotParam);
17+
| -------- this field does not implement `ConstParamTy`
18+
|
19+
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
20+
21+
error: aborting due to 2 previous errors
1122

1223
For more information about this error, try `rustc --explain E0204`.

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

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ struct CantParam(ImplementsConstParamTy);
1010
impl std::marker::ConstParamTy for CantParam {}
1111
//~^ error: the type `CantParam` does not `#[derive(Eq)]`
1212

13+
#[derive(std::marker::ConstParamTy)]
14+
//~^ error: the type `CantParamDerive` does not `#[derive(Eq)]`
15+
struct CantParamDerive(ImplementsConstParamTy);
16+
1317
fn check<T: std::marker::ConstParamTy>() {}
1418

1519
fn main() {

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ LL | impl std::marker::ConstParamTy for CantParam {}
77
note: required by a bound in `ConstParamTy`
88
--> $SRC_DIR/core/src/marker.rs:LL:COL
99

10-
error: aborting due to previous error
10+
error[E0277]: the type `CantParamDerive` does not `#[derive(Eq)]`
11+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:13:10
12+
|
13+
LL | #[derive(std::marker::ConstParamTy)]
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParamDerive`
15+
|
16+
note: required by a bound in `ConstParamTy`
17+
--> $SRC_DIR/core/src/marker.rs:LL:COL
18+
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
19+
20+
error: aborting due to 2 previous errors
1121

1222
For more information about this error, try `rustc --explain E0277`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![allow(incomplete_features)]
2+
#![feature(adt_const_params, structural_match)]
3+
4+
union Union {
5+
a: u8,
6+
}
7+
8+
impl PartialEq for Union {
9+
fn eq(&self, other: &Union) -> bool {
10+
true
11+
}
12+
}
13+
impl Eq for Union {}
14+
impl std::marker::StructuralEq for Union {}
15+
16+
impl std::marker::ConstParamTy for Union {}
17+
18+
#[derive(std::marker::ConstParamTy)]
19+
//~^ ERROR this trait cannot be derived for unions
20+
union UnionDerive {
21+
a: u8,
22+
}
23+
24+
impl PartialEq for UnionDerive {
25+
fn eq(&self, other: &UnionDerive) -> bool {
26+
true
27+
}
28+
}
29+
impl Eq for UnionDerive {}
30+
impl std::marker::StructuralEq for UnionDerive {}
31+
32+
33+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: this trait cannot be derived for unions
2+
--> $DIR/const_param_ty_impl_union.rs:18:10
3+
|
4+
LL | #[derive(std::marker::ConstParamTy)]
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)