Skip to content

Commit a9f5190

Browse files
committed
remove StructuralEq trait
1 parent 27b4eb9 commit a9f5190

File tree

71 files changed

+192
-583
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+192
-583
lines changed

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

-13
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,6 @@ pub fn expand_deriving_eq(
1919
) {
2020
let span = cx.with_def_site_ctxt(span);
2121

22-
let structural_trait_def = TraitDef {
23-
span,
24-
path: path_std!(marker::StructuralEq),
25-
skip_path_as_bound: true, // crucial!
26-
needs_copy_as_bound_if_packed: false,
27-
additional_bounds: Vec::new(),
28-
supports_unions: true,
29-
methods: Vec::new(),
30-
associated_types: Vec::new(),
31-
is_const: false,
32-
};
33-
structural_trait_def.expand(cx, mitem, item, push);
34-
3522
let trait_def = TraitDef {
3623
span,
3724
path: path_std!(cmp::Eq),

compiler/rustc_codegen_cranelift/example/mini_core.rs

-3
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,6 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
104104
#[lang = "structural_peq"]
105105
pub trait StructuralPartialEq {}
106106

107-
#[lang = "structural_teq"]
108-
pub trait StructuralEq {}
109-
110107
#[lang = "not"]
111108
pub trait Not {
112109
type Output;

compiler/rustc_codegen_gcc/example/mini_core.rs

-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,6 @@ unsafe impl<T: ?Sized> Freeze for &mut T {}
100100
#[lang = "structural_peq"]
101101
pub trait StructuralPartialEq {}
102102

103-
#[lang = "structural_teq"]
104-
pub trait StructuralEq {}
105-
106103
#[lang = "not"]
107104
pub trait Not {
108105
type Output;

compiler/rustc_codegen_gcc/tests/run/static.rs

-3
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ mod libc {
6060
#[lang = "structural_peq"]
6161
pub trait StructuralPartialEq {}
6262

63-
#[lang = "structural_teq"]
64-
pub trait StructuralEq {}
65-
6663
#[lang = "drop_in_place"]
6764
#[allow(unconditional_recursion)]
6865
pub unsafe fn drop_in_place<T: ?Sized>(to_drop: *mut T) {

compiler/rustc_const_eval/src/const_eval/valtrees.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub(crate) fn const_to_valtree_inner<'tcx>(
117117
}
118118
// Trait objects are not allowed in type level constants, as we have no concept for
119119
// resolving their backing type, even if we can do that at const eval time. We may
120-
// hypothetically be able to allow `dyn StructuralEq` trait objects in the future,
120+
// hypothetically be able to allow `dyn StructuralPartialEq` trait objects in the future,
121121
// but it is unclear if this is useful.
122122
ty::Dynamic(..) => Err(ValTreeCreationError::NonSupportedType),
123123

compiler/rustc_hir/src/lang_items.rs

-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,6 @@ language_item_table! {
143143
Unsize, sym::unsize, unsize_trait, Target::Trait, GenericRequirement::Minimum(1);
144144
/// Trait injected by `#[derive(PartialEq)]`, (i.e. "Partial EQ").
145145
StructuralPeq, sym::structural_peq, structural_peq_trait, Target::Trait, GenericRequirement::None;
146-
/// Trait injected by `#[derive(Eq)]`, (i.e. "Total EQ"; no, I will not apologize).
147-
StructuralTeq, sym::structural_teq, structural_teq_trait, Target::Trait, GenericRequirement::None;
148146
Copy, sym::copy, copy_trait, Target::Trait, GenericRequirement::Exact(0);
149147
Clone, sym::clone, clone_trait, Target::Trait, GenericRequirement::None;
150148
Sync, sym::sync, sync_trait, Target::Trait, GenericRequirement::Exact(0);

compiler/rustc_middle/src/query/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1359,9 +1359,9 @@ rustc_queries! {
13591359
///
13601360
/// This is only correct for ADTs. Call `is_structural_eq_shallow` to handle all types
13611361
/// correctly.
1362-
query has_structural_eq_impls(ty: Ty<'tcx>) -> bool {
1362+
query has_structural_eq_impl(ty: Ty<'tcx>) -> bool {
13631363
desc {
1364-
"computing whether `{}` implements `PartialStructuralEq` and `StructuralEq`",
1364+
"computing whether `{}` implements `PartialStructuralEq`",
13651365
ty
13661366
}
13671367
}

compiler/rustc_middle/src/ty/util.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -1150,19 +1150,18 @@ impl<'tcx> Ty<'tcx> {
11501150
/// Primitive types (`u32`, `str`) have structural equality by definition. For composite data
11511151
/// types, equality for the type as a whole is structural when it is the same as equality
11521152
/// between all components (fields, array elements, etc.) of that type. For ADTs, structural
1153-
/// equality is indicated by an implementation of `PartialStructuralEq` and `StructuralEq` for
1154-
/// that type.
1153+
/// equality is indicated by an implementation of `PartialStructuralEq` for that type.
11551154
///
11561155
/// This function is "shallow" because it may return `true` for a composite type whose fields
1157-
/// are not `StructuralEq`. For example, `[T; 4]` has structural equality regardless of `T`
1156+
/// are not `StructuralPartialEq`. For example, `[T; 4]` has structural equality regardless of `T`
11581157
/// because equality for arrays is determined by the equality of each array element. If you
11591158
/// want to know whether a given call to `PartialEq::eq` will proceed structurally all the way
11601159
/// down, you will need to use a type visitor.
11611160
#[inline]
11621161
pub fn is_structural_eq_shallow(self, tcx: TyCtxt<'tcx>) -> bool {
11631162
match self.kind() {
1164-
// Look for an impl of both `PartialStructuralEq` and `StructuralEq`.
1165-
ty::Adt(..) => tcx.has_structural_eq_impls(self),
1163+
// Look for an impl of `PartialStructuralEq`.
1164+
ty::Adt(..) => tcx.has_structural_eq_impl(self),
11661165

11671166
// Primitive types that satisfy `Eq`.
11681167
ty::Bool | ty::Char | ty::Int(_) | ty::Uint(_) | ty::Str | ty::Never => true,

compiler/rustc_mir_build/messages.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ mir_build_extern_static_requires_unsafe_unsafe_op_in_unsafe_fn_allowed =
9090
mir_build_float_pattern = floating-point types cannot be used in patterns
9191
9292
mir_build_indirect_structural_match =
93-
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
93+
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
9494
9595
mir_build_inform_irrefutable = `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
9696
@@ -230,7 +230,7 @@ mir_build_non_exhaustive_patterns_type_not_empty = non-exhaustive patterns: type
230230
.help = ensure that all possible cases are being handled by adding a match arm with a wildcard pattern
231231
232232
mir_build_nontrivial_structural_match =
233-
to use a constant of type `{$non_sm_ty}` in a pattern, the constant's initializer must be trivial or `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
233+
to use a constant of type `{$non_sm_ty}` in a pattern, the constant's initializer must be trivial or `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
234234
235235
mir_build_overlapping_range_endpoints = multiple patterns overlap on their endpoints
236236
.range = ... with this range
@@ -277,9 +277,9 @@ mir_build_trailing_irrefutable_let_patterns = trailing irrefutable {$count ->
277277
} into the body
278278
279279
mir_build_type_not_structural =
280-
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq, Eq)]`
280+
to use a constant of type `{$non_sm_ty}` in a pattern, `{$non_sm_ty}` must be annotated with `#[derive(PartialEq)]`
281281
282-
mir_build_type_not_structural_more_info = see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
282+
mir_build_type_not_structural_more_info = see https://doc.rust-lang.org/stable/std/marker/trait.StructuralPartialEq.html for details
283283
284284
mir_build_type_not_structural_tip = the traits must be derived, manual `impl`s are not sufficient
285285

compiler/rustc_span/src/symbol.rs

-2
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ symbols! {
286286
SliceIndex,
287287
Some,
288288
String,
289-
StructuralEq,
290289
StructuralPartialEq,
291290
SubdiagnosticMessage,
292291
Sync,
@@ -1523,7 +1522,6 @@ symbols! {
15231522
struct_variant,
15241523
structural_match,
15251524
structural_peq,
1526-
structural_teq,
15271525
sty,
15281526
sub,
15291527
sub_assign,

compiler/rustc_trait_selection/src/traits/structural_match.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ pub fn search_for_structural_match_violation<'tcx>(
3939

4040
/// This implements the traversal over the structure of a given type to try to
4141
/// find instances of ADTs (specifically structs or enums) that do not implement
42-
/// the structural-match traits (`StructuralPartialEq` and `StructuralEq`).
42+
/// `StructuralPartialEq`.
4343
struct Search<'tcx> {
4444
span: Span,
4545

compiler/rustc_ty_utils/src/structural_match.rs

+5-11
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@ use rustc_infer::infer::TyCtxtInferExt;
66
use rustc_trait_selection::traits::{ObligationCause, ObligationCtxt};
77

88
/// This method returns true if and only if `adt_ty` itself has been marked as
9-
/// eligible for structural-match: namely, if it implements both
10-
/// `StructuralPartialEq` and `StructuralEq` (which are respectively injected by
11-
/// `#[derive(PartialEq)]` and `#[derive(Eq)]`).
9+
/// eligible for structural-match: namely, if it implements
10+
/// `StructuralPartialEq` (which is injected by `#[derive(PartialEq)]`).
1211
///
1312
/// Note that this does *not* recursively check if the substructure of `adt_ty`
14-
/// implements the traits.
15-
fn has_structural_eq_impls<'tcx>(tcx: TyCtxt<'tcx>, adt_ty: Ty<'tcx>) -> bool {
13+
/// implements the trait.
14+
fn has_structural_eq_impl<'tcx>(tcx: TyCtxt<'tcx>, adt_ty: Ty<'tcx>) -> bool {
1615
let infcx = &tcx.infer_ctxt().build();
1716
let cause = ObligationCause::dummy();
1817

@@ -21,11 +20,6 @@ fn has_structural_eq_impls<'tcx>(tcx: TyCtxt<'tcx>, adt_ty: Ty<'tcx>) -> bool {
2120
let structural_peq_def_id =
2221
infcx.tcx.require_lang_item(LangItem::StructuralPeq, Some(cause.span));
2322
ocx.register_bound(cause.clone(), ty::ParamEnv::empty(), adt_ty, structural_peq_def_id);
24-
// for now, require `#[derive(Eq)]`. (Doing so is a hack to work around
25-
// the type `for<'a> fn(&'a ())` failing to implement `Eq` itself.)
26-
let structural_teq_def_id =
27-
infcx.tcx.require_lang_item(LangItem::StructuralTeq, Some(cause.span));
28-
ocx.register_bound(cause, ty::ParamEnv::empty(), adt_ty, structural_teq_def_id);
2923

3024
// We deliberately skip *reporting* fulfillment errors (via
3125
// `report_fulfillment_errors`), for two reasons:
@@ -40,5 +34,5 @@ fn has_structural_eq_impls<'tcx>(tcx: TyCtxt<'tcx>, adt_ty: Ty<'tcx>) -> bool {
4034
}
4135

4236
pub fn provide(providers: &mut Providers) {
43-
providers.has_structural_eq_impls = has_structural_eq_impls;
37+
providers.has_structural_eq_impl = has_structural_eq_impl;
4438
}

library/core/src/marker.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ pub trait Unsize<T: ?Sized> {
181181
/// Required trait for constants used in pattern matches.
182182
///
183183
/// Any type that derives `PartialEq` automatically implements this trait,
184-
/// *regardless* of whether its type-parameters implement `Eq`.
184+
/// *regardless* of whether its type-parameters implement `PartialEq`.
185185
///
186186
/// If a `const` item contains some type that does not implement this trait,
187187
/// then that type either (1.) does not implement `PartialEq` (which means the
@@ -194,7 +194,7 @@ pub trait Unsize<T: ?Sized> {
194194
/// a pattern match.
195195
///
196196
/// See also the [structural match RFC][RFC1445], and [issue 63438] which
197-
/// motivated migrating from attribute-based design to this trait.
197+
/// motivated migrating from an attribute-based design to this trait.
198198
///
199199
/// [RFC1445]: https://github.com/rust-lang/rfcs/blob/master/text/1445-restrict-constants-in-patterns.md
200200
/// [issue 63438]: https://github.com/rust-lang/rust/issues/63438
@@ -212,7 +212,7 @@ marker_impls! {
212212
isize, i8, i16, i32, i64, i128,
213213
bool,
214214
char,
215-
str /* Technically requires `[u8]: StructuralEq` */,
215+
str /* Technically requires `[u8]: StructuralPartialEq` */,
216216
(),
217217
{T, const N: usize} [T; N],
218218
{T} [T],
@@ -268,13 +268,15 @@ marker_impls! {
268268
#[unstable(feature = "structural_match", issue = "31434")]
269269
#[rustc_on_unimplemented(message = "the type `{Self}` does not `#[derive(Eq)]`")]
270270
#[lang = "structural_teq"]
271+
#[cfg(bootstrap)]
271272
pub trait StructuralEq {
272273
// Empty.
273274
}
274275

275276
// FIXME: Remove special cases of these types from the compiler pattern checking code and always check `T: StructuralEq` instead
276277
marker_impls! {
277278
#[unstable(feature = "structural_match", issue = "31434")]
279+
#[cfg(bootstrap)]
278280
StructuralEq for
279281
usize, u8, u16, u32, u64, u128,
280282
isize, i8, i16, i32, i64, i128,
@@ -839,6 +841,7 @@ impl<T: ?Sized> Default for PhantomData<T> {
839841
impl<T: ?Sized> StructuralPartialEq for PhantomData<T> {}
840842

841843
#[unstable(feature = "structural_match", issue = "31434")]
844+
#[cfg(bootstrap)]
842845
impl<T: ?Sized> StructuralEq for PhantomData<T> {}
843846

844847
/// Compiler-internal trait used to indicate the type of enum discriminants.
@@ -995,6 +998,20 @@ pub trait PointerLike {}
995998
#[unstable(feature = "adt_const_params", issue = "95174")]
996999
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
9971000
#[allow(multiple_supertrait_upcastable)]
1001+
#[cfg(not(bootstrap))]
1002+
pub trait ConstParamTy: StructuralPartialEq + Eq {}
1003+
1004+
/// A marker for types which can be used as types of `const` generic parameters.
1005+
///
1006+
/// These types must have a proper equivalence relation (`Eq`) and it must be automatically
1007+
/// derived (`StructuralPartialEq`). There's a hard-coded check in the compiler ensuring
1008+
/// that all fields are also `ConstParamTy`, which implies that recursively, all fields
1009+
/// are `StructuralPartialEq`.
1010+
#[lang = "const_param_ty"]
1011+
#[unstable(feature = "adt_const_params", issue = "95174")]
1012+
#[rustc_on_unimplemented(message = "`{Self}` can't be used as a const parameter type")]
1013+
#[allow(multiple_supertrait_upcastable)]
1014+
#[cfg(bootstrap)]
9981015
pub trait ConstParamTy: StructuralEq + StructuralPartialEq + Eq {}
9991016

10001017
/// Derive macro generating an impl of the trait `ConstParamTy`.

library/core/src/tuple.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use crate::cmp::Ordering::{self, *};
44
use crate::marker::ConstParamTy;
5-
use crate::marker::{StructuralEq, StructuralPartialEq};
5+
use crate::marker::StructuralPartialEq;
66

77
// Recursive macro for implementing n-ary tuple functions and operations
88
//
@@ -64,7 +64,8 @@ macro_rules! tuple_impls {
6464
maybe_tuple_doc! {
6565
$($T)+ @
6666
#[unstable(feature = "structural_match", issue = "31434")]
67-
impl<$($T),+> StructuralEq for ($($T,)+)
67+
#[cfg(bootstrap)]
68+
impl<$($T),+> crate::marker::StructuralEq for ($($T,)+)
6869
{}
6970
}
7071

src/tools/clippy/tests/ui/crashes/ice-6254.rs

-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ fn main() {
1111
// This used to cause an ICE (https://github.com/rust-lang/rust/issues/78071)
1212
match FOO_REF_REF {
1313
FOO_REF_REF => {},
14-
//~^ ERROR: to use a constant of type `Foo` in a pattern, `Foo` must be annotated
15-
//~| NOTE: for more information, see issue #62411 <https://github.com/rust-lang/ru
1614
Foo(_) => {},
1715
}
1816
}

src/tools/clippy/tests/ui/crashes/ice-6254.stderr

-15
This file was deleted.

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

+2-4
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,11 @@ impl std::marker::ConstParamTy for ImplementsConstParamTy {}
88
struct CantParam(ImplementsConstParamTy);
99

1010
impl std::marker::ConstParamTy for CantParam {}
11-
//~^ error: the type `CantParam` does not `#[derive(Eq)]`
12-
//~| error: the type `CantParam` does not `#[derive(PartialEq)]`
11+
//~^ error: the type `CantParam` does not `#[derive(PartialEq)]`
1312
//~| the trait bound `CantParam: Eq` is not satisfied
1413

1514
#[derive(std::marker::ConstParamTy)]
16-
//~^ error: the type `CantParamDerive` does not `#[derive(Eq)]`
17-
//~| error: the type `CantParamDerive` does not `#[derive(PartialEq)]`
15+
//~^ error: the type `CantParamDerive` does not `#[derive(PartialEq)]`
1816
//~| the trait bound `CantParamDerive: Eq` is not satisfied
1917
struct CantParamDerive(ImplementsConstParamTy);
2018

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

+3-22
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,8 @@ LL | impl std::marker::ConstParamTy for CantParam {}
2121
note: required by a bound in `ConstParamTy`
2222
--> $SRC_DIR/core/src/marker.rs:LL:COL
2323

24-
error[E0277]: the type `CantParam` does not `#[derive(Eq)]`
25-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:10:36
26-
|
27-
LL | impl std::marker::ConstParamTy for CantParam {}
28-
| ^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParam`
29-
|
30-
note: required by a bound in `ConstParamTy`
31-
--> $SRC_DIR/core/src/marker.rs:LL:COL
32-
3324
error[E0277]: the trait bound `CantParamDerive: Eq` is not satisfied
34-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
25+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10
3526
|
3627
LL | #[derive(std::marker::ConstParamTy)]
3728
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Eq` is not implemented for `CantParamDerive`
@@ -46,7 +37,7 @@ LL | struct CantParamDerive(ImplementsConstParamTy);
4637
|
4738

4839
error[E0277]: the type `CantParamDerive` does not `#[derive(PartialEq)]`
49-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
40+
--> $DIR/const_param_ty_impl_no_structural_eq.rs:14:10
5041
|
5142
LL | #[derive(std::marker::ConstParamTy)]
5243
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralPartialEq` is not implemented for `CantParamDerive`
@@ -55,16 +46,6 @@ note: required by a bound in `ConstParamTy`
5546
--> $SRC_DIR/core/src/marker.rs:LL:COL
5647
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
5748

58-
error[E0277]: the type `CantParamDerive` does not `#[derive(Eq)]`
59-
--> $DIR/const_param_ty_impl_no_structural_eq.rs:15:10
60-
|
61-
LL | #[derive(std::marker::ConstParamTy)]
62-
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `StructuralEq` is not implemented for `CantParamDerive`
63-
|
64-
note: required by a bound in `ConstParamTy`
65-
--> $SRC_DIR/core/src/marker.rs:LL:COL
66-
= note: this error originates in the derive macro `std::marker::ConstParamTy` (in Nightly builds, run with -Z macro-backtrace for more info)
67-
68-
error: aborting due to 6 previous errors
49+
error: aborting due to 4 previous errors
6950

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

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(incomplete_features)]
2-
#![feature(adt_const_params, structural_match)]
2+
#![feature(adt_const_params)]
33

44
union Union {
55
a: u8,
@@ -11,7 +11,6 @@ impl PartialEq for Union {
1111
}
1212
}
1313
impl Eq for Union {}
14-
impl std::marker::StructuralEq for Union {}
1514

1615
impl std::marker::ConstParamTy for Union {}
1716
//~^ ERROR the type `Union` does not `#[derive(PartialEq)]`
@@ -28,7 +27,6 @@ impl PartialEq for UnionDerive {
2827
}
2928
}
3029
impl Eq for UnionDerive {}
31-
impl std::marker::StructuralEq for UnionDerive {}
3230

3331

3432
fn main() {}

0 commit comments

Comments
 (0)