Skip to content

Commit 727812e

Browse files
committed
Make TypeId a lang item, and return it directly from intrinsics::type_id.
1 parent 4bb685e commit 727812e

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

compiler/rustc_const_eval/src/interpret/intrinsics.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use std::convert::TryFrom;
66

77
use rustc_hir::def_id::DefId;
8+
use rustc_hir::lang_items::LangItem;
89
use rustc_middle::mir::{
910
self,
1011
interpret::{ConstValue, GlobalId, InterpResult, Scalar},
@@ -166,7 +167,9 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
166167
let ty = match intrinsic_name {
167168
sym::pref_align_of | sym::variant_count => self.tcx.types.usize,
168169
sym::needs_drop => self.tcx.types.bool,
169-
sym::type_id => self.tcx.types.u64,
170+
sym::type_id => self
171+
.tcx
172+
.type_of(self.tcx.require_lang_item(LangItem::TypeId, Some(self.tcx.span))),
170173
sym::type_name => self.tcx.mk_static_str(),
171174
_ => bug!("already checked for nullary intrinsics"),
172175
};

compiler/rustc_hir/src/lang_items.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ language_item_table! {
217217
IndexMut(Op), sym::index_mut, index_mut_trait, Target::Trait, GenericRequirement::Exact(1);
218218

219219
UnsafeCell, sym::unsafe_cell, unsafe_cell_type, Target::Struct, GenericRequirement::None;
220-
VaList, sym::va_list, va_list, Target::Struct, GenericRequirement::None;
220+
VaList, sym::va_list, va_list_type, Target::Struct, GenericRequirement::None;
221221

222222
Deref, sym::deref, deref_trait, Target::Trait, GenericRequirement::Exact(0);
223223
DerefMut, sym::deref_mut, deref_mut_trait, Target::Trait, GenericRequirement::Exact(0);
@@ -326,6 +326,8 @@ language_item_table! {
326326
Range, sym::Range, range_struct, Target::Struct, GenericRequirement::None;
327327
RangeToInclusive, sym::RangeToInclusive, range_to_inclusive_struct, Target::Struct, GenericRequirement::None;
328328
RangeTo, sym::RangeTo, range_to_struct, Target::Struct, GenericRequirement::None;
329+
330+
TypeId, sym::TypeId, type_id_struct, Target::Struct, GenericRequirement::Exact(0);
329331
}
330332

331333
pub enum GenericRequirement {

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ symbols! {
266266
Ty,
267267
TyCtxt,
268268
TyKind,
269+
TypeId,
269270
Unknown,
270271
UnsafeArg,
271272
Vec,

compiler/rustc_typeck/src/check/intrinsic.rs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::require_same_types;
99

1010
use rustc_errors::struct_span_err;
1111
use rustc_hir as hir;
12+
use rustc_hir::lang_items::LangItem;
1213
use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
1314
use rustc_middle::ty::subst::Subst;
1415
use rustc_middle::ty::{self, TyCtxt};
@@ -120,18 +121,17 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
120121
.copied(),
121122
);
122123
let mk_va_list_ty = |mutbl| {
123-
tcx.lang_items().va_list().map(|did| {
124-
let region = tcx.mk_region(ty::ReLateBound(
125-
ty::INNERMOST,
126-
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0) },
127-
));
128-
let env_region = tcx.mk_region(ty::ReLateBound(
129-
ty::INNERMOST,
130-
ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrEnv },
131-
));
132-
let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
133-
(tcx.mk_ref(env_region, ty::TypeAndMut { ty: va_list_ty, mutbl }), va_list_ty)
134-
})
124+
let did = tcx.require_lang_item(LangItem::VaList, Some(it.span));
125+
let region = tcx.mk_region(ty::ReLateBound(
126+
ty::INNERMOST,
127+
ty::BoundRegion { var: ty::BoundVar::from_u32(0), kind: ty::BrAnon(0) },
128+
));
129+
let env_region = tcx.mk_region(ty::ReLateBound(
130+
ty::INNERMOST,
131+
ty::BoundRegion { var: ty::BoundVar::from_u32(1), kind: ty::BrEnv },
132+
));
133+
let va_list_ty = tcx.type_of(did).subst(tcx, &[region.into()]);
134+
(tcx.mk_ref(env_region, ty::TypeAndMut { ty: va_list_ty, mutbl }), va_list_ty)
135135
};
136136

137137
let (n_tps, n_lts, inputs, output, unsafety) = if name_str.starts_with("atomic_") {
@@ -191,7 +191,9 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
191191
sym::needs_drop => (1, Vec::new(), tcx.types.bool),
192192

193193
sym::type_name => (1, Vec::new(), tcx.mk_static_str()),
194-
sym::type_id => (1, Vec::new(), tcx.types.u64),
194+
sym::type_id => {
195+
(1, Vec::new(), tcx.type_of(tcx.require_lang_item(LangItem::TypeId, Some(it.span))))
196+
}
195197
sym::offset | sym::arith_offset => (
196198
1,
197199
vec![
@@ -366,23 +368,21 @@ pub fn check_intrinsic_type(tcx: TyCtxt<'_>, it: &hir::ForeignItem<'_>) {
366368
)
367369
}
368370

369-
sym::va_start | sym::va_end => match mk_va_list_ty(hir::Mutability::Mut) {
370-
Some((va_list_ref_ty, _)) => (0, vec![va_list_ref_ty], tcx.mk_unit()),
371-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
372-
},
371+
sym::va_start | sym::va_end => {
372+
let (va_list_ref_ty, _) = mk_va_list_ty(hir::Mutability::Mut);
373+
(0, vec![va_list_ref_ty], tcx.mk_unit())
374+
}
373375

374-
sym::va_copy => match mk_va_list_ty(hir::Mutability::Not) {
375-
Some((va_list_ref_ty, va_list_ty)) => {
376-
let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
377-
(0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
378-
}
379-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
380-
},
376+
sym::va_copy => {
377+
let (va_list_ref_ty, va_list_ty) = mk_va_list_ty(hir::Mutability::Not);
378+
let va_list_ptr_ty = tcx.mk_mut_ptr(va_list_ty);
379+
(0, vec![va_list_ptr_ty, va_list_ref_ty], tcx.mk_unit())
380+
}
381381

382-
sym::va_arg => match mk_va_list_ty(hir::Mutability::Mut) {
383-
Some((va_list_ref_ty, _)) => (1, vec![va_list_ref_ty], param(0)),
384-
None => bug!("`va_list` language item needed for C-variadic intrinsics"),
385-
},
382+
sym::va_arg => {
383+
let (va_list_ref_ty, _) = mk_va_list_ty(hir::Mutability::Mut);
384+
(1, vec![va_list_ref_ty], param(0))
385+
}
386386

387387
sym::nontemporal_store => (1, vec![tcx.mk_mut_ptr(param(0)), param(0)], tcx.mk_unit()),
388388

library/core/src/any.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ impl dyn Any + Send + Sync {
594594
/// While `TypeId` implements `Hash`, `PartialOrd`, and `Ord`, it is worth
595595
/// noting that the hashes and ordering will vary between Rust releases. Beware
596596
/// of relying on them inside of your code!
597+
#[cfg_attr(not(bootstrap), lang = "TypeId")]
597598
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
598599
#[stable(feature = "rust1", since = "1.0.0")]
599600
pub struct TypeId {
@@ -620,7 +621,14 @@ impl TypeId {
620621
#[stable(feature = "rust1", since = "1.0.0")]
621622
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
622623
pub const fn of<T: ?Sized + 'static>() -> TypeId {
623-
TypeId { t: intrinsics::type_id::<T>() }
624+
#[cfg(bootstrap)]
625+
{
626+
TypeId { t: intrinsics::type_id::<T>() }
627+
}
628+
#[cfg(not(bootstrap))]
629+
{
630+
intrinsics::type_id::<T>()
631+
}
624632
}
625633
}
626634

library/core/src/intrinsics.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,6 +848,11 @@ extern "rust-intrinsic" {
848848
///
849849
/// The stabilized version of this intrinsic is [`core::any::TypeId::of`].
850850
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
851+
#[cfg(not(bootstrap))]
852+
pub fn type_id<T: ?Sized + 'static>() -> crate::any::TypeId;
853+
854+
#[rustc_const_unstable(feature = "const_type_id", issue = "77125")]
855+
#[cfg(bootstrap)]
851856
pub fn type_id<T: ?Sized + 'static>() -> u64;
852857

853858
/// A guard for unsafe functions that cannot ever be executed if `T` is uninhabited:

0 commit comments

Comments
 (0)