Skip to content

Commit 27b4eb9

Browse files
committed
Auto merge of rust-lang#116125 - RalfJung:const-param-ty-eq, r=compiler-errors
ConstParamTy: require Eq as supertrait As discussed with `@BoxyUwu` [on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/260443-project-const-generics/topic/.60ConstParamTy.60.20and.20.60Eq.60). We want to say that valtree equality on const generic params agrees with `==`, but that only makes sense if `==` actually exists, hence we should have an appropriate bound. Valtree equality is an equivalence relation, so such a type can always be `Eq` and not just `PartialEq`.
2 parents a6dce3b + ad50963 commit 27b4eb9

File tree

5 files changed

+44
-6
lines changed

5 files changed

+44
-6
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub fn expand_deriving_const_param_ty(
4141
path: path_std!(marker::ConstParamTy),
4242
skip_path_as_bound: false,
4343
needs_copy_as_bound_if_packed: false,
44-
additional_bounds: Vec::new(),
44+
additional_bounds: vec![ty::Ty::Path(path_std!(cmp::Eq))],
4545
supports_unions: false,
4646
methods: Vec::new(),
4747
associated_types: Vec::new(),

compiler/rustc_resolve/src/diagnostics.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -2596,7 +2596,9 @@ fn show_candidates(
25962596
);
25972597
if let [first, .., last] = &path[..] {
25982598
let sp = first.ident.span.until(last.ident.span);
2599-
if sp.can_be_used_for_suggestions() {
2599+
// Our suggestion is empty, so make sure the span is not empty (or we'd ICE).
2600+
// Can happen for derive-generated spans.
2601+
if sp.can_be_used_for_suggestions() && !sp.is_empty() {
26002602
err.span_suggestion_verbose(
26012603
sp,
26022604
format!("if you import `{}`, refer to it directly", last.ident),

library/core/src/marker.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -986,11 +986,16 @@ pub trait Tuple {}
986986
pub trait PointerLike {}
987987

988988
/// A marker for types which can be used as types of `const` generic parameters.
989+
///
990+
/// These types must have a proper equivalence relation (`Eq`) and it must be automatically
991+
/// derived (`StructuralPartialEq`). There's a hard-coded check in the compiler ensuring
992+
/// that all fields are also `ConstParamTy`, which implies that recursively, all fields
993+
/// are `StructuralPartialEq`.
989994
#[lang = "const_param_ty"]
990995
#[unstable(feature = "adt_const_params", issue = "95174")]
991996
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
992997
#[allow(multiple_supertrait_upcastable)]
993-
pub trait ConstParamTy: StructuralEq + StructuralPartialEq {}
998+
pub trait ConstParamTy: StructuralEq + StructuralPartialEq + Eq {}
994999

9951000
/// Derive macro generating an impl of the trait `ConstParamTy`.
9961001
#[rustc_builtin_macro]

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

+2
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ struct CantParam(ImplementsConstParamTy);
1010
impl std::marker::ConstParamTy for CantParam {}
1111
//~^ error: the type `CantParam` does not `#[derive(Eq)]`
1212
//~| error: the type `CantParam` does not `#[derive(PartialEq)]`
13+
//~| the trait bound `CantParam: Eq` is not satisfied
1314

1415
#[derive(std::marker::ConstParamTy)]
1516
//~^ error: the type `CantParamDerive` does not `#[derive(Eq)]`
1617
//~| error: the type `CantParamDerive` does not `#[derive(PartialEq)]`
18+
//~| the trait bound `CantParamDerive: Eq` is not satisfied
1719
struct CantParamDerive(ImplementsConstParamTy);
1820

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

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

+32-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
error[E0277]: the trait bound `CantParam: Eq` is not satisfied
2+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:10:36
3+
|
4+
LL | impl std::marker::ConstParamTy for CantParam {}
5+
| ^^^^^^^^^ the trait `Eq` is not implemented for `CantParam`
6+
|
7+
note: required by a bound in `ConstParamTy`
8+
--> $SRC_DIR/core/src/marker.rs:LL:COL
9+
help: consider annotating `CantParam` with `#[derive(Eq)]`
10+
|
11+
LL + #[derive(Eq)]
12+
LL | struct CantParam(ImplementsConstParamTy);
13+
|
14+
115
error[E0277]: the type `CantParam` does not `#[derive(PartialEq)]`
216
--> $DIR/const_param_ty_impl_no_structural_eq.rs:10:36
317
|
@@ -16,8 +30,23 @@ LL | impl std::marker::ConstParamTy for CantParam {}
1630
note: required by a bound in `ConstParamTy`
1731
--> $SRC_DIR/core/src/marker.rs:LL:COL
1832

33+
error[E0277]: the trait bound `CantParamDerive: Eq` is not satisfied
34+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
35+
|
36+
LL | #[derive(std::marker::ConstParamTy)]
37+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `CantParamDerive`
38+
|
39+
note: required by a bound in `ConstParamTy`
40+
--> $SRC_DIR/core/src/marker.rs:LL:COL
41+
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
42+
help: consider annotating `CantParamDerive` with `#[derive(Eq)]`
43+
|
44+
LL + #[derive(Eq)]
45+
LL | struct CantParamDerive(ImplementsConstParamTy);
46+
|
47+
1948
error[E0277]: the type `CantParamDerive` does not `#[derive(PartialEq)]`
20-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10
49+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
2150
|
2251
LL | #[derive(std::marker::ConstParamTy)]
2352
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralPartialEq` is not implemented for `CantParamDerive`
@@ -27,7 +56,7 @@ note: required by a bound in `ConstParamTy`
2756
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
2857

2958
error[E0277]: the type `CantParamDerive` does not `#[derive(Eq)]`
30-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10
59+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
3160
|
3261
LL | #[derive(std::marker::ConstParamTy)]
3362
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParamDerive`
@@ -36,6 +65,6 @@ note: required by a bound in `ConstParamTy`
3665
--> $SRC_DIR/core/src/marker.rs:LL:COL
3766
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
3867

39-
error: aborting due to 4 previous errors
68+
error: aborting due to 6 previous errors
4069

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

0 commit comments

Comments
 (0)