rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
93.58k stars 12.05k forks source link

`const` code depending on `TypeId` throws mismatched type error #124755

Open cafce25 opened 1 week ago

cafce25 commented 1 week ago

I tried to create a function that statically computes if two type parameters are the same:

#![feature(const_type_id, const_trait_impl, effects)]
use std::any::TypeId;
const fn same_type<T1, T2>() -> bool {
    TypeId::of::<T1>() == TypeId::of::<T2>()
}

Depending on whether the type of T1 and T2 is the same the function should be able to produce true or false at compile time.

Instead, the compiler throws the following error:

error[E0308]: mismatched types
 --> src/lib.rs:4:5
  |
4 |     TypeId::of::<T1>() == TypeId::of::<T2>()
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `host`, found `true`
  |
  = note: expected constant `host`
             found constant `true`

Meta

rustc --version:

1.80.0-nightly (2024-05-04 e82c861d7e5ecd766cb0)
asquared31415 commented 1 week ago

@rustbot label +T-compiler +F-effects -needs-triage

bjorn3 commented 1 week ago

The PartialEq impl for TypeId is not marked as const and as such can't be used in const contexts. const_type_id only enables creating a TypeId in const contexts, not comparing it.