Curiosity: Why `TypeId::of` has a `cfg(bootstrap)`?

source

#[must_use]
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
pub const fn of<T: ?Sized + 'static>() -> TypeId {
    #[cfg(bootstrap)]
    let t = intrinsics::type_id::<T>() as u128;
    #[cfg(not(bootstrap))]
    let t: u128 = intrinsics::type_id::<T>();
    TypeId { t }
}

In some stage TypeId is not a u128 internally?

btw: Why it's marked by a #[must_use]? Does not using it lead to anything bad?

Since it's an intrinsic, it depends on the compiler, and it used to be returned as u64. The cfg(bootstrap) deals with the difference between the former and current behavior.

#[must_use] is advisory -- nothing bad will happen, but there's no point in calling this if you don't use the value, so the attribute raises a warning that you might have done this by mistake.

Look at beta or nightly and the cfg is gone. This is simply because the current stable, 1.72, is exactly when the change to 128 bit representation landed. The standard library is used to build rustc itself, too, and rustc 1.72 is first built using a rustc 1.71 binary (but already the 1.72 standard library) during bootstrap, then re-built using the rustc 1.72 from the first step to make 1.72 "build itself". The first step (aka first "stage" of bootstrap) is thus using a typeid intrinsic that is returning 64 bit values with rustc 1.71, the next step uses the proper new 128 bit returning one from rust 1.72 itself. See the "Milestone 1.72" tag on the relevant PR, indicating in what stable version it first landed: https://github.com/rust-lang/rust/pull/109953

See also:

5 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.