Skip to content

Commit 09faa26

Browse files
authored
Rollup merge of rust-lang#101698 - raldone01:feat/const_cmp_typeid, r=scottmcm
Constify `TypeId` ordering impls Tracking issue: rust-lang#101871 Adding const ordering to `TypeId` allows rtti crates to optimize some casting scenarios (without transmuting to `u64`). This would also prevent these crates from breaking if the underlying type is changed from `u64` to something different. Feature gate: `#![feature(const_cmp_type_id)]`
2 parents f34cc65 + 7355ab3 commit 09faa26

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,8 @@ 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, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
665+
#[derive(Clone, Copy, Debug, Hash, Eq)]
666+
#[derive_const(PartialEq, PartialOrd, Ord)]
666667
#[stable(feature = "rust1", since = "1.0.0")]
667668
pub struct TypeId {
668669
t: u64,

Diff for: tests/ui/const-generics/issues/issue-90318.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ impl True for If<true> {}
1212
fn consume<T: 'static>(_val: T)
1313
where
1414
If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
15-
//~^ ERROR: can't compare
15+
//~^ overly complex generic constant
1616
{
1717
}
1818

1919
fn test<T: 'static>()
2020
where
2121
If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
22-
//~^ ERROR: can't compare
22+
//~^ overly complex generic constant
2323
{
2424
}
2525

Diff for: tests/ui/const-generics/issues/issue-90318.stderr

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,24 @@
1-
error[E0277]: can't compare `TypeId` with `_` in const contexts
2-
--> $DIR/issue-90318.rs:14:28
1+
error: overly complex generic constant
2+
--> $DIR/issue-90318.rs:14:8
33
|
44
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
5-
| ^^ no implementation for `TypeId == _`
5+
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| borrowing is not supported in generic constants
68
|
7-
= help: the trait `~const PartialEq<_>` is not implemented for `TypeId`
8-
note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementation is not `const`
9-
--> $DIR/issue-90318.rs:14:28
10-
|
11-
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
12-
| ^^
9+
= help: consider moving this anonymous constant into a `const` function
10+
= note: this operation may be supported in the future
1311

14-
error[E0277]: can't compare `TypeId` with `_` in const contexts
15-
--> $DIR/issue-90318.rs:21:28
12+
error: overly complex generic constant
13+
--> $DIR/issue-90318.rs:21:8
1614
|
1715
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
18-
| ^^ no implementation for `TypeId == _`
16+
| ^^-----------------^^^^^^^^^^^^^^^^^^^^^^^^
17+
| |
18+
| borrowing is not supported in generic constants
1919
|
20-
= help: the trait `~const PartialEq<_>` is not implemented for `TypeId`
21-
note: the trait `PartialEq<_>` is implemented for `TypeId`, but that implementation is not `const`
22-
--> $DIR/issue-90318.rs:21:28
23-
|
24-
LL | If<{ TypeId::of::<T>() != TypeId::of::<()>() }>: True,
25-
| ^^
20+
= help: consider moving this anonymous constant into a `const` function
21+
= note: this operation may be supported in the future
2622

2723
error: aborting due to 2 previous errors
2824

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

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

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// run-pass
2+
#![feature(const_type_id)]
3+
#![feature(const_trait_impl)]
4+
5+
use std::any::TypeId;
6+
7+
const fn main() {
8+
assert!(TypeId::of::<u8>() == TypeId::of::<u8>());
9+
assert!(TypeId::of::<()>() != TypeId::of::<u8>());
10+
const _A: bool = TypeId::of::<u8>() < TypeId::of::<u16>();
11+
// can't assert `_A` because it is not deterministic
12+
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#![feature(const_type_id)]
99
#![feature(const_type_name)]
10+
#![feature(const_trait_impl)]
1011

1112
use std::any::{self, TypeId};
1213

@@ -17,7 +18,7 @@ impl<T: 'static> GetTypeId<T> {
1718
}
1819

1920
const fn check_type_id<T: 'static>() -> bool {
20-
matches!(GetTypeId::<T>::VALUE, GetTypeId::<usize>::VALUE)
21+
GetTypeId::<T>::VALUE == GetTypeId::<usize>::VALUE
2122
}
2223

2324
pub struct GetTypeNameLen<T>(T);

0 commit comments

Comments
 (0)