Skip to content

Commit 1a5f8bc

Browse files
committed
Auto merge of #103291 - ink-feather-org:typeid_no_struct_match, r=dtolnay
Remove structural match from `TypeId` As per #99189 (comment). > Removing the structural equality might make sense, but is a breaking change that'd require a libs-api FCP. #99189 (comment) > Landing this PR now (well, mainly the "remove structural equality" part) would unblock `const fn` `TypeId::of`, since we only postponed that because we were guaranteeing too much. See also #99189, #101698
2 parents 917b0b6 + 6827a41 commit 1a5f8bc

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

Diff for: library/core/src/any.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -662,12 +662,20 @@ impl dyn Any + Send + Sync {
662662
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
663663
/// noting that the hashes and ordering will vary between Rust releases. Beware
664664
/// of relying on them inside of your code!
665-
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
665+
#[derive(Clone, Copy, Debug, Hash, Eq, PartialOrd, Ord)]
666666
#[stable(feature = "rust1", since = "1.0.0")]
667667
pub struct TypeId {
668668
t: u64,
669669
}
670670

671+
#[stable(feature = "rust1", since = "1.0.0")]
672+
impl PartialEq for TypeId {
673+
#[inline]
674+
fn eq(&self, other: &Self) -> bool {
675+
self.t == other.t
676+
}
677+
}
678+
671679
impl TypeId {
672680
/// Returns the `TypeId` of the type this generic function has been
673681
/// instantiated with.

Diff for: tests/ui/const-generics/generic_const_exprs/typeid-equality-by-subtyping.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// check-pass
1+
// known-bug: #110395
22
// known-bug: #97156
33

4-
#![feature(const_type_id, generic_const_exprs)]
4+
#![feature(const_type_id, const_trait_impl, generic_const_exprs)]
55
#![allow(incomplete_features)]
66

77
use std::any::TypeId;
@@ -26,7 +26,10 @@ impl<T: 'static> AssocCt for T {
2626
trait WithAssoc<U> {
2727
type Assoc;
2828
}
29-
impl<T: 'static> WithAssoc<()> for T where [(); <T as AssocCt>::ASSOC]: {
29+
impl<T: 'static> WithAssoc<()> for T
30+
where
31+
[(); <T as AssocCt>::ASSOC]:,
32+
{
3033
type Assoc = [u8; <T as AssocCt>::ASSOC];
3134
}
3235

@@ -38,7 +41,6 @@ where
3841
x
3942
}
4043

41-
4244
fn unsound<T>(x: <One as WithAssoc<T>>::Assoc) -> <Two as WithAssoc<T>>::Assoc
4345
where
4446
One: WithAssoc<T>,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: to use a constant of type `TypeId` in a pattern, `TypeId` must be annotated with `#[derive(PartialEq, Eq)]`
2+
--> $DIR/typeid-equality-by-subtyping.rs:18:9
3+
|
4+
LL | WHAT_A_TYPE => 0,
5+
| ^^^^^^^^^^^
6+
|
7+
= note: the traits must be derived, manual `impl`s are not sufficient
8+
= note: see https://doc.rust-lang.org/stable/std/marker/trait.StructuralEq.html for details
9+
10+
error: aborting due to previous error
11+

Diff for: tests/ui/consts/const_cmp_type_id.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
2020
note: impl defined here, but it is not `const`
2121
--> $SRC_DIR/core/src/any.rs:LL:COL
2222
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
23-
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
2423

2524
error[E0277]: can't compare `TypeId` with `TypeId` in const contexts
2625
--> $DIR/const_cmp_type_id.rs:9:13
@@ -44,7 +43,6 @@ LL | assert!(TypeId::of::<()>() != TypeId::of::<u8>());
4443
note: impl defined here, but it is not `const`
4544
--> $SRC_DIR/core/src/any.rs:LL:COL
4645
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
47-
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
4846

4947
error[E0277]: can't compare `TypeId` with `TypeId` in const contexts
5048
--> $DIR/const_cmp_type_id.rs:10:22

Diff for: tests/ui/consts/issue-73976-monomorphic.stderr

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ LL | GetTypeId::<T>::VALUE == GetTypeId::<usize>::VALUE
2020
note: impl defined here, but it is not `const`
2121
--> $SRC_DIR/core/src/any.rs:LL:COL
2222
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
23-
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)
2423

2524
error: aborting due to 2 previous errors
2625

0 commit comments

Comments
 (0)