Skip to content

Commit

Permalink
Rollup merge of rust-lang#103291 - ink-feather-org:typeid_no_struct_m…
Browse files Browse the repository at this point in the history
…atch, r=dtolnay

Remove structural match from `TypeId`

As per rust-lang#99189 (comment).

> Removing the structural equality might make sense, but is a breaking change that'd require a libs-api FCP.

rust-lang#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 rust-lang#99189, rust-lang#101698
  • Loading branch information
Dylan-DPC authored May 26, 2023
2 parents 1221e43 + 6827a41 commit c05413a
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
10 changes: 9 additions & 1 deletion library/core/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,20 @@ impl dyn Any + Send + Sync {
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
/// noting that the hashes and ordering will vary between Rust releases. Beware
/// of relying on them inside of your code!
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Clone, Copy, Debug, Hash, Eq, PartialOrd, Ord)]
#[stable(feature = "rust1", since = "1.0.0")]
pub struct TypeId {
t: u64,
}

#[stable(feature = "rust1", since = "1.0.0")]
impl PartialEq for TypeId {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.t == other.t
}
}

impl TypeId {
/// Returns the `TypeId` of the type this generic function has been
/// instantiated with.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// check-pass
// known-bug: #110395
// known-bug: #97156

#![feature(const_type_id, generic_const_exprs)]
#![feature(const_type_id, const_trait_impl, generic_const_exprs)]
#![allow(incomplete_features)]

use std::any::TypeId;
Expand All @@ -26,7 +26,10 @@ impl<T: 'static> AssocCt for T {
trait WithAssoc<U> {
type Assoc;
}
impl<T: 'static> WithAssoc<()> for T where [(); <T as AssocCt>::ASSOC]: {
impl<T: 'static> WithAssoc<()> for T
where
[(); <T as AssocCt>::ASSOC]:,
{
type Assoc = [u8; <T as AssocCt>::ASSOC];
}

Expand All @@ -38,7 +41,6 @@ where
x
}


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

error: aborting due to previous error

2 changes: 0 additions & 2 deletions tests/ui/consts/const_cmp_type_id.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LL | assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

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

error[E0277]: can't compare `TypeId` with `TypeId` in const contexts
--> $DIR/const_cmp_type_id.rs:10:22
Expand Down
1 change: 0 additions & 1 deletion tests/ui/consts/issue-73976-monomorphic.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ LL | GetTypeId::<T>::VALUE == GetTypeId::<usize>::VALUE
note: impl defined here, but it is not `const`
--> $SRC_DIR/core/src/any.rs:LL:COL
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
= note: this error originates in the derive macro `PartialEq` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

Expand Down

0 comments on commit c05413a

Please sign in to comment.