1#![allow(rustc::usage_of_ty_tykind)]
4
5use std::assert_matches::debug_assert_matches;
6use std::borrow::Cow;
7use std::ops::{ControlFlow, Range};
8
9use hir::def::{CtorKind, DefKind};
10use rustc_abi::{FIRST_VARIANT, FieldIdx, ScalableElt, VariantIdx};
11use rustc_errors::{ErrorGuaranteed, MultiSpan};
12use rustc_hir as hir;
13use rustc_hir::LangItem;
14use rustc_hir::def_id::DefId;
15use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, extension};
16use rustc_span::{DUMMY_SP, Span, Symbol, sym};
17use rustc_type_ir::TyKind::*;
18use rustc_type_ir::solve::SizedTraitKind;
19use rustc_type_ir::walk::TypeWalker;
20use rustc_type_ir::{self as ir, BoundVar, CollectAndApply, TypeVisitableExt, elaborate};
21use tracing::instrument;
22use ty::util::IntTypeExt;
23
24use super::GenericParamDefKind;
25use crate::infer::canonical::Canonical;
26use crate::traits::ObligationCause;
27use crate::ty::InferTy::*;
28use crate::ty::{
29 self, AdtDef, BoundRegionKind, Discr, GenericArg, GenericArgs, GenericArgsRef, List, ParamEnv,
30 Region, Ty, TyCtxt, TypeFlags, TypeSuperVisitable, TypeVisitable, TypeVisitor, UintTy,
31};
32
33#[rustc_diagnostic_item = "TyKind"]
35pub type TyKind<'tcx> = ir::TyKind<TyCtxt<'tcx>>;
36pub type TypeAndMut<'tcx> = ir::TypeAndMut<TyCtxt<'tcx>>;
37pub type AliasTy<'tcx> = ir::AliasTy<TyCtxt<'tcx>>;
38pub type FnSig<'tcx> = ir::FnSig<TyCtxt<'tcx>>;
39pub type Binder<'tcx, T> = ir::Binder<TyCtxt<'tcx>, T>;
40pub type EarlyBinder<'tcx, T> = ir::EarlyBinder<TyCtxt<'tcx>, T>;
41pub type TypingMode<'tcx> = ir::TypingMode<TyCtxt<'tcx>>;
42pub type Placeholder<'tcx, T> = ir::Placeholder<TyCtxt<'tcx>, T>;
43
44pub trait Article {
45 fn article(&self) -> &'static str;
46}
47
48impl<'tcx> Article for TyKind<'tcx> {
49 fn article(&self) -> &'static str {
51 match self {
52 Int(_) | Float(_) | Array(_, _) => "an",
53 Adt(def, _) if def.is_enum() => "an",
54 Error(_) => "a",
57 _ => "a",
58 }
59 }
60}
61
62#[extension(pub trait CoroutineArgsExt<'tcx>)]
63impl<'tcx> ty::CoroutineArgs<TyCtxt<'tcx>> {
64 const UNRESUMED: usize = 0;
66 const RETURNED: usize = 1;
68 const POISONED: usize = 2;
70 const RESERVED_VARIANTS: usize = 3;
74
75 const UNRESUMED_NAME: &'static str = "Unresumed";
76 const RETURNED_NAME: &'static str = "Returned";
77 const POISONED_NAME: &'static str = "Panicked";
78
79 #[inline]
81 fn variant_range(&self, def_id: DefId, tcx: TyCtxt<'tcx>) -> Range<VariantIdx> {
82 FIRST_VARIANT..tcx.coroutine_layout(def_id, self.args).unwrap().variant_fields.next_index()
84 }
85
86 #[inline]
89 fn discriminant_for_variant(
90 &self,
91 def_id: DefId,
92 tcx: TyCtxt<'tcx>,
93 variant_index: VariantIdx,
94 ) -> Discr<'tcx> {
95 assert!(self.variant_range(def_id, tcx).contains(&variant_index));
98 Discr { val: variant_index.as_usize() as u128, ty: self.discr_ty(tcx) }
99 }
100
101 #[inline]
104 fn discriminants(
105 self,
106 def_id: DefId,
107 tcx: TyCtxt<'tcx>,
108 ) -> impl Iterator<Item = (VariantIdx, Discr<'tcx>)> {
109 self.variant_range(def_id, tcx).map(move |index| {
110 (index, Discr { val: index.as_usize() as u128, ty: self.discr_ty(tcx) })
111 })
112 }
113
114 fn variant_name(v: VariantIdx) -> Cow<'static, str> {
117 match v.as_usize() {
118 Self::UNRESUMED => Cow::from(Self::UNRESUMED_NAME),
119 Self::RETURNED => Cow::from(Self::RETURNED_NAME),
120 Self::POISONED => Cow::from(Self::POISONED_NAME),
121 _ => Cow::from(format!("Suspend{}", v.as_usize() - Self::RESERVED_VARIANTS)),
122 }
123 }
124
125 #[inline]
127 fn discr_ty(&self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
128 tcx.types.u32
129 }
130
131 #[inline]
138 fn state_tys(
139 self,
140 def_id: DefId,
141 tcx: TyCtxt<'tcx>,
142 ) -> impl Iterator<Item: Iterator<Item = Ty<'tcx>>> {
143 let layout = tcx.coroutine_layout(def_id, self.args).unwrap();
144 layout.variant_fields.iter().map(move |variant| {
145 variant.iter().map(move |field| {
146 if tcx.is_async_drop_in_place_coroutine(def_id) {
147 layout.field_tys[*field].ty
148 } else {
149 ty::EarlyBinder::bind(layout.field_tys[*field].ty).instantiate(tcx, self.args)
150 }
151 })
152 })
153 }
154
155 #[inline]
158 fn prefix_tys(self) -> &'tcx List<Ty<'tcx>> {
159 self.upvar_tys()
160 }
161}
162
163#[derive(Debug, Copy, Clone, HashStable, TypeFoldable, TypeVisitable)]
164pub enum UpvarArgs<'tcx> {
165 Closure(GenericArgsRef<'tcx>),
166 Coroutine(GenericArgsRef<'tcx>),
167 CoroutineClosure(GenericArgsRef<'tcx>),
168}
169
170impl<'tcx> UpvarArgs<'tcx> {
171 #[inline]
175 pub fn upvar_tys(self) -> &'tcx List<Ty<'tcx>> {
176 let tupled_tys = match self {
177 UpvarArgs::Closure(args) => args.as_closure().tupled_upvars_ty(),
178 UpvarArgs::Coroutine(args) => args.as_coroutine().tupled_upvars_ty(),
179 UpvarArgs::CoroutineClosure(args) => args.as_coroutine_closure().tupled_upvars_ty(),
180 };
181
182 match tupled_tys.kind() {
183 TyKind::Error(_) => ty::List::empty(),
184 TyKind::Tuple(..) => self.tupled_upvars_ty().tuple_fields(),
185 TyKind::Infer(_) => bug!("upvar_tys called before capture types are inferred"),
186 ty => bug!("Unexpected representation of upvar types tuple {:?}", ty),
187 }
188 }
189
190 #[inline]
191 pub fn tupled_upvars_ty(self) -> Ty<'tcx> {
192 match self {
193 UpvarArgs::Closure(args) => args.as_closure().tupled_upvars_ty(),
194 UpvarArgs::Coroutine(args) => args.as_coroutine().tupled_upvars_ty(),
195 UpvarArgs::CoroutineClosure(args) => args.as_coroutine_closure().tupled_upvars_ty(),
196 }
197 }
198}
199
200#[derive(Copy, Clone, Debug)]
215pub struct InlineConstArgs<'tcx> {
216 pub args: GenericArgsRef<'tcx>,
219}
220
221pub struct InlineConstArgsParts<'tcx, T> {
223 pub parent_args: &'tcx [GenericArg<'tcx>],
224 pub ty: T,
225}
226
227impl<'tcx> InlineConstArgs<'tcx> {
228 pub fn new(
230 tcx: TyCtxt<'tcx>,
231 parts: InlineConstArgsParts<'tcx, Ty<'tcx>>,
232 ) -> InlineConstArgs<'tcx> {
233 InlineConstArgs {
234 args: tcx.mk_args_from_iter(
235 parts.parent_args.iter().copied().chain(std::iter::once(parts.ty.into())),
236 ),
237 }
238 }
239
240 fn split(self) -> InlineConstArgsParts<'tcx, GenericArg<'tcx>> {
243 match self.args[..] {
244 [ref parent_args @ .., ty] => InlineConstArgsParts { parent_args, ty },
245 _ => bug!("inline const args missing synthetics"),
246 }
247 }
248
249 pub fn parent_args(self) -> &'tcx [GenericArg<'tcx>] {
251 self.split().parent_args
252 }
253
254 pub fn ty(self) -> Ty<'tcx> {
256 self.split().ty.expect_ty()
257 }
258}
259
260#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
261#[derive(HashStable)]
262pub enum BoundVariableKind {
263 Ty(BoundTyKind),
264 Region(BoundRegionKind),
265 Const,
266}
267
268impl BoundVariableKind {
269 pub fn expect_region(self) -> BoundRegionKind {
270 match self {
271 BoundVariableKind::Region(lt) => lt,
272 _ => bug!("expected a region, but found another kind"),
273 }
274 }
275
276 pub fn expect_ty(self) -> BoundTyKind {
277 match self {
278 BoundVariableKind::Ty(ty) => ty,
279 _ => bug!("expected a type, but found another kind"),
280 }
281 }
282
283 pub fn expect_const(self) {
284 match self {
285 BoundVariableKind::Const => (),
286 _ => bug!("expected a const, but found another kind"),
287 }
288 }
289}
290
291pub type PolyFnSig<'tcx> = Binder<'tcx, FnSig<'tcx>>;
292pub type CanonicalPolyFnSig<'tcx> = Canonical<'tcx, Binder<'tcx, FnSig<'tcx>>>;
293
294#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, TyEncodable, TyDecodable)]
295#[derive(HashStable)]
296pub struct ParamTy {
297 pub index: u32,
298 pub name: Symbol,
299}
300
301impl rustc_type_ir::inherent::ParamLike for ParamTy {
302 fn index(self) -> u32 {
303 self.index
304 }
305}
306
307impl<'tcx> ParamTy {
308 pub fn new(index: u32, name: Symbol) -> ParamTy {
309 ParamTy { index, name }
310 }
311
312 pub fn for_def(def: &ty::GenericParamDef) -> ParamTy {
313 ParamTy::new(def.index, def.name)
314 }
315
316 #[inline]
317 pub fn to_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
318 Ty::new_param(tcx, self.index, self.name)
319 }
320
321 pub fn span_from_generics(self, tcx: TyCtxt<'tcx>, item_with_generics: DefId) -> Span {
322 let generics = tcx.generics_of(item_with_generics);
323 let type_param = generics.type_param(self, tcx);
324 tcx.def_span(type_param.def_id)
325 }
326}
327
328#[derive(Copy, Clone, Hash, TyEncodable, TyDecodable, Eq, PartialEq, Ord, PartialOrd)]
329#[derive(HashStable)]
330pub struct ParamConst {
331 pub index: u32,
332 pub name: Symbol,
333}
334
335impl rustc_type_ir::inherent::ParamLike for ParamConst {
336 fn index(self) -> u32 {
337 self.index
338 }
339}
340
341impl ParamConst {
342 pub fn new(index: u32, name: Symbol) -> ParamConst {
343 ParamConst { index, name }
344 }
345
346 pub fn for_def(def: &ty::GenericParamDef) -> ParamConst {
347 ParamConst::new(def.index, def.name)
348 }
349
350 #[instrument(level = "debug")]
351 pub fn find_const_ty_from_env<'tcx>(self, env: ParamEnv<'tcx>) -> Ty<'tcx> {
352 let mut candidates = env.caller_bounds().iter().filter_map(|clause| {
353 match clause.kind().skip_binder() {
355 ty::ClauseKind::ConstArgHasType(param_ct, ty) => {
356 assert!(!(param_ct, ty).has_escaping_bound_vars());
357
358 match param_ct.kind() {
359 ty::ConstKind::Param(param_ct) if param_ct.index == self.index => Some(ty),
360 _ => None,
361 }
362 }
363 _ => None,
364 }
365 });
366
367 let ty = candidates.next().unwrap_or_else(|| {
374 bug!("cannot find `{self:?}` in param-env: {env:#?}");
375 });
376 assert!(
377 candidates.next().is_none(),
378 "did not expect duplicate `ConstParamHasTy` for `{self:?}` in param-env: {env:#?}"
379 );
380 ty
381 }
382}
383
384#[derive(Clone, Copy, PartialEq, Eq, Hash, TyEncodable, TyDecodable)]
385#[derive(HashStable)]
386pub struct BoundTy {
387 pub var: BoundVar,
388 pub kind: BoundTyKind,
389}
390
391impl<'tcx> rustc_type_ir::inherent::BoundVarLike<TyCtxt<'tcx>> for BoundTy {
392 fn var(self) -> BoundVar {
393 self.var
394 }
395
396 fn assert_eq(self, var: ty::BoundVariableKind) {
397 assert_eq!(self.kind, var.expect_ty())
398 }
399}
400
401#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)]
402#[derive(HashStable)]
403pub enum BoundTyKind {
404 Anon,
405 Param(DefId),
406}
407
408impl<'tcx> Ty<'tcx> {
410 #[allow(rustc::usage_of_ty_tykind)]
413 #[inline]
414 fn new(tcx: TyCtxt<'tcx>, st: TyKind<'tcx>) -> Ty<'tcx> {
415 tcx.mk_ty_from_kind(st)
416 }
417
418 #[inline]
419 pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferTy) -> Ty<'tcx> {
420 Ty::new(tcx, TyKind::Infer(infer))
421 }
422
423 #[inline]
424 pub fn new_var(tcx: TyCtxt<'tcx>, v: ty::TyVid) -> Ty<'tcx> {
425 tcx.types
427 .ty_vars
428 .get(v.as_usize())
429 .copied()
430 .unwrap_or_else(|| Ty::new(tcx, Infer(TyVar(v))))
431 }
432
433 #[inline]
434 pub fn new_int_var(tcx: TyCtxt<'tcx>, v: ty::IntVid) -> Ty<'tcx> {
435 Ty::new_infer(tcx, IntVar(v))
436 }
437
438 #[inline]
439 pub fn new_float_var(tcx: TyCtxt<'tcx>, v: ty::FloatVid) -> Ty<'tcx> {
440 Ty::new_infer(tcx, FloatVar(v))
441 }
442
443 #[inline]
444 pub fn new_fresh(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
445 tcx.types
447 .fresh_tys
448 .get(n as usize)
449 .copied()
450 .unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshTy(n)))
451 }
452
453 #[inline]
454 pub fn new_fresh_int(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
455 tcx.types
457 .fresh_int_tys
458 .get(n as usize)
459 .copied()
460 .unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshIntTy(n)))
461 }
462
463 #[inline]
464 pub fn new_fresh_float(tcx: TyCtxt<'tcx>, n: u32) -> Ty<'tcx> {
465 tcx.types
467 .fresh_float_tys
468 .get(n as usize)
469 .copied()
470 .unwrap_or_else(|| Ty::new_infer(tcx, ty::FreshFloatTy(n)))
471 }
472
473 #[inline]
474 pub fn new_param(tcx: TyCtxt<'tcx>, index: u32, name: Symbol) -> Ty<'tcx> {
475 Ty::new(tcx, Param(ParamTy { index, name }))
476 }
477
478 #[inline]
479 pub fn new_bound(
480 tcx: TyCtxt<'tcx>,
481 index: ty::DebruijnIndex,
482 bound_ty: ty::BoundTy,
483 ) -> Ty<'tcx> {
484 if let ty::BoundTy { var, kind: ty::BoundTyKind::Anon } = bound_ty
486 && let Some(inner) = tcx.types.anon_bound_tys.get(index.as_usize())
487 && let Some(ty) = inner.get(var.as_usize()).copied()
488 {
489 ty
490 } else {
491 Ty::new(tcx, Bound(ty::BoundVarIndexKind::Bound(index), bound_ty))
492 }
493 }
494
495 #[inline]
496 pub fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: BoundVar) -> Ty<'tcx> {
497 if let Some(ty) = tcx.types.anon_canonical_bound_tys.get(var.as_usize()).copied() {
499 ty
500 } else {
501 Ty::new(
502 tcx,
503 Bound(
504 ty::BoundVarIndexKind::Canonical,
505 ty::BoundTy { var, kind: ty::BoundTyKind::Anon },
506 ),
507 )
508 }
509 }
510
511 #[inline]
512 pub fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType<'tcx>) -> Ty<'tcx> {
513 Ty::new(tcx, Placeholder(placeholder))
514 }
515
516 #[inline]
517 pub fn new_alias(
518 tcx: TyCtxt<'tcx>,
519 kind: ty::AliasTyKind,
520 alias_ty: ty::AliasTy<'tcx>,
521 ) -> Ty<'tcx> {
522 debug_assert_matches!(
523 (kind, tcx.def_kind(alias_ty.def_id)),
524 (ty::Opaque, DefKind::OpaqueTy)
525 | (ty::Projection | ty::Inherent, DefKind::AssocTy)
526 | (ty::Free, DefKind::TyAlias)
527 );
528 Ty::new(tcx, Alias(kind, alias_ty))
529 }
530
531 #[inline]
532 pub fn new_pat(tcx: TyCtxt<'tcx>, base: Ty<'tcx>, pat: ty::Pattern<'tcx>) -> Ty<'tcx> {
533 Ty::new(tcx, Pat(base, pat))
534 }
535
536 #[inline]
537 #[instrument(level = "debug", skip(tcx))]
538 pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
539 Ty::new_alias(tcx, ty::Opaque, AliasTy::new_from_args(tcx, def_id, args))
540 }
541
542 pub fn new_error(tcx: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Ty<'tcx> {
544 Ty::new(tcx, Error(guar))
545 }
546
547 #[track_caller]
549 pub fn new_misc_error(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
550 Ty::new_error_with_message(tcx, DUMMY_SP, "TyKind::Error constructed but no error reported")
551 }
552
553 #[track_caller]
556 pub fn new_error_with_message<S: Into<MultiSpan>>(
557 tcx: TyCtxt<'tcx>,
558 span: S,
559 msg: impl Into<Cow<'static, str>>,
560 ) -> Ty<'tcx> {
561 let reported = tcx.dcx().span_delayed_bug(span, msg);
562 Ty::new(tcx, Error(reported))
563 }
564
565 #[inline]
566 pub fn new_int(tcx: TyCtxt<'tcx>, i: ty::IntTy) -> Ty<'tcx> {
567 use ty::IntTy::*;
568 match i {
569 Isize => tcx.types.isize,
570 I8 => tcx.types.i8,
571 I16 => tcx.types.i16,
572 I32 => tcx.types.i32,
573 I64 => tcx.types.i64,
574 I128 => tcx.types.i128,
575 }
576 }
577
578 #[inline]
579 pub fn new_uint(tcx: TyCtxt<'tcx>, ui: ty::UintTy) -> Ty<'tcx> {
580 use ty::UintTy::*;
581 match ui {
582 Usize => tcx.types.usize,
583 U8 => tcx.types.u8,
584 U16 => tcx.types.u16,
585 U32 => tcx.types.u32,
586 U64 => tcx.types.u64,
587 U128 => tcx.types.u128,
588 }
589 }
590
591 #[inline]
592 pub fn new_float(tcx: TyCtxt<'tcx>, f: ty::FloatTy) -> Ty<'tcx> {
593 use ty::FloatTy::*;
594 match f {
595 F16 => tcx.types.f16,
596 F32 => tcx.types.f32,
597 F64 => tcx.types.f64,
598 F128 => tcx.types.f128,
599 }
600 }
601
602 #[inline]
603 pub fn new_ref(
604 tcx: TyCtxt<'tcx>,
605 r: Region<'tcx>,
606 ty: Ty<'tcx>,
607 mutbl: ty::Mutability,
608 ) -> Ty<'tcx> {
609 Ty::new(tcx, Ref(r, ty, mutbl))
610 }
611
612 #[inline]
613 pub fn new_mut_ref(tcx: TyCtxt<'tcx>, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
614 Ty::new_ref(tcx, r, ty, hir::Mutability::Mut)
615 }
616
617 #[inline]
618 pub fn new_imm_ref(tcx: TyCtxt<'tcx>, r: Region<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
619 Ty::new_ref(tcx, r, ty, hir::Mutability::Not)
620 }
621
622 pub fn new_pinned_ref(
623 tcx: TyCtxt<'tcx>,
624 r: Region<'tcx>,
625 ty: Ty<'tcx>,
626 mutbl: ty::Mutability,
627 ) -> Ty<'tcx> {
628 let pin = tcx.adt_def(tcx.require_lang_item(LangItem::Pin, DUMMY_SP));
629 Ty::new_adt(tcx, pin, tcx.mk_args(&[Ty::new_ref(tcx, r, ty, mutbl).into()]))
630 }
631
632 #[inline]
633 pub fn new_ptr(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, mutbl: ty::Mutability) -> Ty<'tcx> {
634 Ty::new(tcx, ty::RawPtr(ty, mutbl))
635 }
636
637 #[inline]
638 pub fn new_mut_ptr(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
639 Ty::new_ptr(tcx, ty, hir::Mutability::Mut)
640 }
641
642 #[inline]
643 pub fn new_imm_ptr(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
644 Ty::new_ptr(tcx, ty, hir::Mutability::Not)
645 }
646
647 #[inline]
648 pub fn new_adt(tcx: TyCtxt<'tcx>, def: AdtDef<'tcx>, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
649 tcx.debug_assert_args_compatible(def.did(), args);
650 if cfg!(debug_assertions) {
651 match tcx.def_kind(def.did()) {
652 DefKind::Struct | DefKind::Union | DefKind::Enum => {}
653 DefKind::Mod
654 | DefKind::Variant
655 | DefKind::Trait
656 | DefKind::TyAlias
657 | DefKind::ForeignTy
658 | DefKind::TraitAlias
659 | DefKind::AssocTy
660 | DefKind::TyParam
661 | DefKind::Fn
662 | DefKind::Const
663 | DefKind::ConstParam
664 | DefKind::Static { .. }
665 | DefKind::Ctor(..)
666 | DefKind::AssocFn
667 | DefKind::AssocConst
668 | DefKind::Macro(..)
669 | DefKind::ExternCrate
670 | DefKind::Use
671 | DefKind::ForeignMod
672 | DefKind::AnonConst
673 | DefKind::InlineConst
674 | DefKind::OpaqueTy
675 | DefKind::Field
676 | DefKind::LifetimeParam
677 | DefKind::GlobalAsm
678 | DefKind::Impl { .. }
679 | DefKind::Closure
680 | DefKind::SyntheticCoroutineBody => {
681 bug!("not an adt: {def:?} ({:?})", tcx.def_kind(def.did()))
682 }
683 }
684 }
685 Ty::new(tcx, Adt(def, args))
686 }
687
688 #[inline]
689 pub fn new_foreign(tcx: TyCtxt<'tcx>, def_id: DefId) -> Ty<'tcx> {
690 Ty::new(tcx, Foreign(def_id))
691 }
692
693 #[inline]
694 pub fn new_array(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, n: u64) -> Ty<'tcx> {
695 Ty::new(tcx, Array(ty, ty::Const::from_target_usize(tcx, n)))
696 }
697
698 #[inline]
699 pub fn new_array_with_const_len(
700 tcx: TyCtxt<'tcx>,
701 ty: Ty<'tcx>,
702 ct: ty::Const<'tcx>,
703 ) -> Ty<'tcx> {
704 Ty::new(tcx, Array(ty, ct))
705 }
706
707 #[inline]
708 pub fn new_slice(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
709 Ty::new(tcx, Slice(ty))
710 }
711
712 #[inline]
713 pub fn new_tup(tcx: TyCtxt<'tcx>, ts: &[Ty<'tcx>]) -> Ty<'tcx> {
714 if ts.is_empty() { tcx.types.unit } else { Ty::new(tcx, Tuple(tcx.mk_type_list(ts))) }
715 }
716
717 pub fn new_tup_from_iter<I, T>(tcx: TyCtxt<'tcx>, iter: I) -> T::Output
718 where
719 I: Iterator<Item = T>,
720 T: CollectAndApply<Ty<'tcx>, Ty<'tcx>>,
721 {
722 T::collect_and_apply(iter, |ts| Ty::new_tup(tcx, ts))
723 }
724
725 #[inline]
726 pub fn new_fn_def(
727 tcx: TyCtxt<'tcx>,
728 def_id: DefId,
729 args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
730 ) -> Ty<'tcx> {
731 debug_assert_matches!(
732 tcx.def_kind(def_id),
733 DefKind::AssocFn | DefKind::Fn | DefKind::Ctor(_, CtorKind::Fn)
734 );
735 let args = tcx.check_and_mk_args(def_id, args);
736 Ty::new(tcx, FnDef(def_id, args))
737 }
738
739 #[inline]
740 pub fn new_fn_ptr(tcx: TyCtxt<'tcx>, fty: PolyFnSig<'tcx>) -> Ty<'tcx> {
741 let (sig_tys, hdr) = fty.split();
742 Ty::new(tcx, FnPtr(sig_tys, hdr))
743 }
744
745 #[inline]
746 pub fn new_unsafe_binder(tcx: TyCtxt<'tcx>, b: Binder<'tcx, Ty<'tcx>>) -> Ty<'tcx> {
747 Ty::new(tcx, UnsafeBinder(b.into()))
748 }
749
750 #[inline]
751 pub fn new_dynamic(
752 tcx: TyCtxt<'tcx>,
753 obj: &'tcx List<ty::PolyExistentialPredicate<'tcx>>,
754 reg: ty::Region<'tcx>,
755 ) -> Ty<'tcx> {
756 if cfg!(debug_assertions) {
757 let projection_count = obj
758 .projection_bounds()
759 .filter(|item| !tcx.generics_require_sized_self(item.item_def_id()))
760 .count();
761 let expected_count: usize = obj
762 .principal_def_id()
763 .into_iter()
764 .flat_map(|principal_def_id| {
765 elaborate::supertraits(
768 tcx,
769 ty::Binder::dummy(ty::TraitRef::identity(tcx, principal_def_id)),
770 )
771 .map(|principal| {
772 tcx.associated_items(principal.def_id())
773 .in_definition_order()
774 .filter(|item| item.is_type())
775 .filter(|item| !item.is_impl_trait_in_trait())
776 .filter(|item| !tcx.generics_require_sized_self(item.def_id))
777 .count()
778 })
779 })
780 .sum();
781 assert_eq!(
782 projection_count, expected_count,
783 "expected {obj:?} to have {expected_count} projections, \
784 but it has {projection_count}"
785 );
786 }
787 Ty::new(tcx, Dynamic(obj, reg))
788 }
789
790 #[inline]
791 pub fn new_projection_from_args(
792 tcx: TyCtxt<'tcx>,
793 item_def_id: DefId,
794 args: ty::GenericArgsRef<'tcx>,
795 ) -> Ty<'tcx> {
796 Ty::new_alias(tcx, ty::Projection, AliasTy::new_from_args(tcx, item_def_id, args))
797 }
798
799 #[inline]
800 pub fn new_projection(
801 tcx: TyCtxt<'tcx>,
802 item_def_id: DefId,
803 args: impl IntoIterator<Item: Into<GenericArg<'tcx>>>,
804 ) -> Ty<'tcx> {
805 Ty::new_alias(tcx, ty::Projection, AliasTy::new(tcx, item_def_id, args))
806 }
807
808 #[inline]
809 pub fn new_closure(
810 tcx: TyCtxt<'tcx>,
811 def_id: DefId,
812 closure_args: GenericArgsRef<'tcx>,
813 ) -> Ty<'tcx> {
814 tcx.debug_assert_args_compatible(def_id, closure_args);
815 Ty::new(tcx, Closure(def_id, closure_args))
816 }
817
818 #[inline]
819 pub fn new_coroutine_closure(
820 tcx: TyCtxt<'tcx>,
821 def_id: DefId,
822 closure_args: GenericArgsRef<'tcx>,
823 ) -> Ty<'tcx> {
824 tcx.debug_assert_args_compatible(def_id, closure_args);
825 Ty::new(tcx, CoroutineClosure(def_id, closure_args))
826 }
827
828 #[inline]
829 pub fn new_coroutine(
830 tcx: TyCtxt<'tcx>,
831 def_id: DefId,
832 coroutine_args: GenericArgsRef<'tcx>,
833 ) -> Ty<'tcx> {
834 tcx.debug_assert_args_compatible(def_id, coroutine_args);
835 Ty::new(tcx, Coroutine(def_id, coroutine_args))
836 }
837
838 #[inline]
839 pub fn new_coroutine_witness(
840 tcx: TyCtxt<'tcx>,
841 def_id: DefId,
842 args: GenericArgsRef<'tcx>,
843 ) -> Ty<'tcx> {
844 if cfg!(debug_assertions) {
845 tcx.debug_assert_args_compatible(tcx.typeck_root_def_id(def_id), args);
846 }
847 Ty::new(tcx, CoroutineWitness(def_id, args))
848 }
849
850 pub fn new_coroutine_witness_for_coroutine(
851 tcx: TyCtxt<'tcx>,
852 def_id: DefId,
853 coroutine_args: GenericArgsRef<'tcx>,
854 ) -> Ty<'tcx> {
855 tcx.debug_assert_args_compatible(def_id, coroutine_args);
856 let args =
865 ty::GenericArgs::for_item(tcx, tcx.typeck_root_def_id(def_id), |def, _| {
866 match def.kind {
867 ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(),
868 ty::GenericParamDefKind::Type { .. }
869 | ty::GenericParamDefKind::Const { .. } => coroutine_args[def.index as usize],
870 }
871 });
872 Ty::new_coroutine_witness(tcx, def_id, args)
873 }
874
875 #[inline]
878 pub fn new_static_str(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
879 Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, tcx.types.str_)
880 }
881
882 fn new_generic_adt(tcx: TyCtxt<'tcx>, wrapper_def_id: DefId, ty_param: Ty<'tcx>) -> Ty<'tcx> {
885 let adt_def = tcx.adt_def(wrapper_def_id);
886 let args = GenericArgs::for_item(tcx, wrapper_def_id, |param, args| match param.kind {
887 GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => bug!(),
888 GenericParamDefKind::Type { has_default, .. } => {
889 if param.index == 0 {
890 ty_param.into()
891 } else {
892 assert!(has_default);
893 tcx.type_of(param.def_id).instantiate(tcx, args).into()
894 }
895 }
896 });
897 Ty::new_adt(tcx, adt_def, args)
898 }
899
900 #[inline]
901 pub fn new_lang_item(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, item: LangItem) -> Option<Ty<'tcx>> {
902 let def_id = tcx.lang_items().get(item)?;
903 Some(Ty::new_generic_adt(tcx, def_id, ty))
904 }
905
906 #[inline]
907 pub fn new_diagnostic_item(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>, name: Symbol) -> Option<Ty<'tcx>> {
908 let def_id = tcx.get_diagnostic_item(name)?;
909 Some(Ty::new_generic_adt(tcx, def_id, ty))
910 }
911
912 #[inline]
913 pub fn new_box(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
914 let def_id = tcx.require_lang_item(LangItem::OwnedBox, DUMMY_SP);
915 Ty::new_generic_adt(tcx, def_id, ty)
916 }
917
918 #[inline]
919 pub fn new_option(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
920 let def_id = tcx.require_lang_item(LangItem::Option, DUMMY_SP);
921 Ty::new_generic_adt(tcx, def_id, ty)
922 }
923
924 #[inline]
925 pub fn new_maybe_uninit(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> {
926 let def_id = tcx.require_lang_item(LangItem::MaybeUninit, DUMMY_SP);
927 Ty::new_generic_adt(tcx, def_id, ty)
928 }
929
930 pub fn new_task_context(tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
932 let context_did = tcx.require_lang_item(LangItem::Context, DUMMY_SP);
933 let context_adt_ref = tcx.adt_def(context_did);
934 let context_args = tcx.mk_args(&[tcx.lifetimes.re_erased.into()]);
935 let context_ty = Ty::new_adt(tcx, context_adt_ref, context_args);
936 Ty::new_mut_ref(tcx, tcx.lifetimes.re_erased, context_ty)
937 }
938}
939
940impl<'tcx> rustc_type_ir::inherent::Ty<TyCtxt<'tcx>> for Ty<'tcx> {
941 fn new_bool(tcx: TyCtxt<'tcx>) -> Self {
942 tcx.types.bool
943 }
944
945 fn new_u8(tcx: TyCtxt<'tcx>) -> Self {
946 tcx.types.u8
947 }
948
949 fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferTy) -> Self {
950 Ty::new_infer(tcx, infer)
951 }
952
953 fn new_var(tcx: TyCtxt<'tcx>, vid: ty::TyVid) -> Self {
954 Ty::new_var(tcx, vid)
955 }
956
957 fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamTy) -> Self {
958 Ty::new_param(tcx, param.index, param.name)
959 }
960
961 fn new_placeholder(tcx: TyCtxt<'tcx>, placeholder: ty::PlaceholderType<'tcx>) -> Self {
962 Ty::new_placeholder(tcx, placeholder)
963 }
964
965 fn new_bound(interner: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundTy) -> Self {
966 Ty::new_bound(interner, debruijn, var)
967 }
968
969 fn new_anon_bound(tcx: TyCtxt<'tcx>, debruijn: ty::DebruijnIndex, var: ty::BoundVar) -> Self {
970 Ty::new_bound(tcx, debruijn, ty::BoundTy { var, kind: ty::BoundTyKind::Anon })
971 }
972
973 fn new_canonical_bound(tcx: TyCtxt<'tcx>, var: ty::BoundVar) -> Self {
974 Ty::new_canonical_bound(tcx, var)
975 }
976
977 fn new_alias(
978 interner: TyCtxt<'tcx>,
979 kind: ty::AliasTyKind,
980 alias_ty: ty::AliasTy<'tcx>,
981 ) -> Self {
982 Ty::new_alias(interner, kind, alias_ty)
983 }
984
985 fn new_error(interner: TyCtxt<'tcx>, guar: ErrorGuaranteed) -> Self {
986 Ty::new_error(interner, guar)
987 }
988
989 fn new_adt(
990 interner: TyCtxt<'tcx>,
991 adt_def: ty::AdtDef<'tcx>,
992 args: ty::GenericArgsRef<'tcx>,
993 ) -> Self {
994 Ty::new_adt(interner, adt_def, args)
995 }
996
997 fn new_foreign(interner: TyCtxt<'tcx>, def_id: DefId) -> Self {
998 Ty::new_foreign(interner, def_id)
999 }
1000
1001 fn new_dynamic(
1002 interner: TyCtxt<'tcx>,
1003 preds: &'tcx List<ty::PolyExistentialPredicate<'tcx>>,
1004 region: ty::Region<'tcx>,
1005 ) -> Self {
1006 Ty::new_dynamic(interner, preds, region)
1007 }
1008
1009 fn new_coroutine(
1010 interner: TyCtxt<'tcx>,
1011 def_id: DefId,
1012 args: ty::GenericArgsRef<'tcx>,
1013 ) -> Self {
1014 Ty::new_coroutine(interner, def_id, args)
1015 }
1016
1017 fn new_coroutine_closure(
1018 interner: TyCtxt<'tcx>,
1019 def_id: DefId,
1020 args: ty::GenericArgsRef<'tcx>,
1021 ) -> Self {
1022 Ty::new_coroutine_closure(interner, def_id, args)
1023 }
1024
1025 fn new_closure(interner: TyCtxt<'tcx>, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> Self {
1026 Ty::new_closure(interner, def_id, args)
1027 }
1028
1029 fn new_coroutine_witness(
1030 interner: TyCtxt<'tcx>,
1031 def_id: DefId,
1032 args: ty::GenericArgsRef<'tcx>,
1033 ) -> Self {
1034 Ty::new_coroutine_witness(interner, def_id, args)
1035 }
1036
1037 fn new_coroutine_witness_for_coroutine(
1038 interner: TyCtxt<'tcx>,
1039 def_id: DefId,
1040 coroutine_args: ty::GenericArgsRef<'tcx>,
1041 ) -> Self {
1042 Ty::new_coroutine_witness_for_coroutine(interner, def_id, coroutine_args)
1043 }
1044
1045 fn new_ptr(interner: TyCtxt<'tcx>, ty: Self, mutbl: hir::Mutability) -> Self {
1046 Ty::new_ptr(interner, ty, mutbl)
1047 }
1048
1049 fn new_ref(
1050 interner: TyCtxt<'tcx>,
1051 region: ty::Region<'tcx>,
1052 ty: Self,
1053 mutbl: hir::Mutability,
1054 ) -> Self {
1055 Ty::new_ref(interner, region, ty, mutbl)
1056 }
1057
1058 fn new_array_with_const_len(interner: TyCtxt<'tcx>, ty: Self, len: ty::Const<'tcx>) -> Self {
1059 Ty::new_array_with_const_len(interner, ty, len)
1060 }
1061
1062 fn new_slice(interner: TyCtxt<'tcx>, ty: Self) -> Self {
1063 Ty::new_slice(interner, ty)
1064 }
1065
1066 fn new_tup(interner: TyCtxt<'tcx>, tys: &[Ty<'tcx>]) -> Self {
1067 Ty::new_tup(interner, tys)
1068 }
1069
1070 fn new_tup_from_iter<It, T>(interner: TyCtxt<'tcx>, iter: It) -> T::Output
1071 where
1072 It: Iterator<Item = T>,
1073 T: CollectAndApply<Self, Self>,
1074 {
1075 Ty::new_tup_from_iter(interner, iter)
1076 }
1077
1078 fn tuple_fields(self) -> &'tcx ty::List<Ty<'tcx>> {
1079 self.tuple_fields()
1080 }
1081
1082 fn to_opt_closure_kind(self) -> Option<ty::ClosureKind> {
1083 self.to_opt_closure_kind()
1084 }
1085
1086 fn from_closure_kind(interner: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Self {
1087 Ty::from_closure_kind(interner, kind)
1088 }
1089
1090 fn from_coroutine_closure_kind(
1091 interner: TyCtxt<'tcx>,
1092 kind: rustc_type_ir::ClosureKind,
1093 ) -> Self {
1094 Ty::from_coroutine_closure_kind(interner, kind)
1095 }
1096
1097 fn new_fn_def(interner: TyCtxt<'tcx>, def_id: DefId, args: ty::GenericArgsRef<'tcx>) -> Self {
1098 Ty::new_fn_def(interner, def_id, args)
1099 }
1100
1101 fn new_fn_ptr(interner: TyCtxt<'tcx>, sig: ty::Binder<'tcx, ty::FnSig<'tcx>>) -> Self {
1102 Ty::new_fn_ptr(interner, sig)
1103 }
1104
1105 fn new_pat(interner: TyCtxt<'tcx>, ty: Self, pat: ty::Pattern<'tcx>) -> Self {
1106 Ty::new_pat(interner, ty, pat)
1107 }
1108
1109 fn new_unsafe_binder(interner: TyCtxt<'tcx>, ty: ty::Binder<'tcx, Ty<'tcx>>) -> Self {
1110 Ty::new_unsafe_binder(interner, ty)
1111 }
1112
1113 fn new_unit(interner: TyCtxt<'tcx>) -> Self {
1114 interner.types.unit
1115 }
1116
1117 fn new_usize(interner: TyCtxt<'tcx>) -> Self {
1118 interner.types.usize
1119 }
1120
1121 fn discriminant_ty(self, interner: TyCtxt<'tcx>) -> Ty<'tcx> {
1122 self.discriminant_ty(interner)
1123 }
1124
1125 fn has_unsafe_fields(self) -> bool {
1126 Ty::has_unsafe_fields(self)
1127 }
1128}
1129
1130impl<'tcx> Ty<'tcx> {
1132 #[inline(always)]
1137 pub fn kind(self) -> &'tcx TyKind<'tcx> {
1138 self.0.0
1139 }
1140
1141 #[inline(always)]
1143 pub fn flags(self) -> TypeFlags {
1144 self.0.0.flags
1145 }
1146
1147 #[inline]
1148 pub fn is_unit(self) -> bool {
1149 match self.kind() {
1150 Tuple(tys) => tys.is_empty(),
1151 _ => false,
1152 }
1153 }
1154
1155 #[inline]
1157 pub fn is_usize(self) -> bool {
1158 matches!(self.kind(), Uint(UintTy::Usize))
1159 }
1160
1161 #[inline]
1163 pub fn is_usize_like(self) -> bool {
1164 matches!(self.kind(), Uint(UintTy::Usize) | Infer(IntVar(_)))
1165 }
1166
1167 #[inline]
1168 pub fn is_never(self) -> bool {
1169 matches!(self.kind(), Never)
1170 }
1171
1172 #[inline]
1173 pub fn is_primitive(self) -> bool {
1174 matches!(self.kind(), Bool | Char | Int(_) | Uint(_) | Float(_))
1175 }
1176
1177 #[inline]
1178 pub fn is_adt(self) -> bool {
1179 matches!(self.kind(), Adt(..))
1180 }
1181
1182 #[inline]
1183 pub fn is_ref(self) -> bool {
1184 matches!(self.kind(), Ref(..))
1185 }
1186
1187 #[inline]
1188 pub fn is_ty_var(self) -> bool {
1189 matches!(self.kind(), Infer(TyVar(_)))
1190 }
1191
1192 #[inline]
1193 pub fn ty_vid(self) -> Option<ty::TyVid> {
1194 match self.kind() {
1195 &Infer(TyVar(vid)) => Some(vid),
1196 _ => None,
1197 }
1198 }
1199
1200 #[inline]
1201 pub fn is_ty_or_numeric_infer(self) -> bool {
1202 matches!(self.kind(), Infer(_))
1203 }
1204
1205 #[inline]
1206 pub fn is_phantom_data(self) -> bool {
1207 if let Adt(def, _) = self.kind() { def.is_phantom_data() } else { false }
1208 }
1209
1210 #[inline]
1211 pub fn is_bool(self) -> bool {
1212 *self.kind() == Bool
1213 }
1214
1215 #[inline]
1217 pub fn is_str(self) -> bool {
1218 *self.kind() == Str
1219 }
1220
1221 #[inline]
1223 pub fn is_imm_ref_str(self) -> bool {
1224 matches!(self.kind(), ty::Ref(_, inner, hir::Mutability::Not) if inner.is_str())
1225 }
1226
1227 #[inline]
1228 pub fn is_param(self, index: u32) -> bool {
1229 match self.kind() {
1230 ty::Param(data) => data.index == index,
1231 _ => false,
1232 }
1233 }
1234
1235 #[inline]
1236 pub fn is_slice(self) -> bool {
1237 matches!(self.kind(), Slice(_))
1238 }
1239
1240 #[inline]
1241 pub fn is_array_slice(self) -> bool {
1242 match self.kind() {
1243 Slice(_) => true,
1244 ty::RawPtr(ty, _) | Ref(_, ty, _) => matches!(ty.kind(), Slice(_)),
1245 _ => false,
1246 }
1247 }
1248
1249 #[inline]
1250 pub fn is_array(self) -> bool {
1251 matches!(self.kind(), Array(..))
1252 }
1253
1254 #[inline]
1255 pub fn is_simd(self) -> bool {
1256 match self.kind() {
1257 Adt(def, _) => def.repr().simd(),
1258 _ => false,
1259 }
1260 }
1261
1262 #[inline]
1263 pub fn is_scalable_vector(self) -> bool {
1264 match self.kind() {
1265 Adt(def, _) => def.repr().scalable(),
1266 _ => false,
1267 }
1268 }
1269
1270 pub fn sequence_element_type(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1271 match self.kind() {
1272 Array(ty, _) | Slice(ty) => *ty,
1273 Str => tcx.types.u8,
1274 _ => bug!("`sequence_element_type` called on non-sequence value: {}", self),
1275 }
1276 }
1277
1278 pub fn scalable_vector_element_count_and_type(self, tcx: TyCtxt<'tcx>) -> (u16, Ty<'tcx>) {
1279 let Adt(def, args) = self.kind() else {
1280 bug!("`scalable_vector_size_and_type` called on invalid type")
1281 };
1282 let Some(ScalableElt::ElementCount(element_count)) = def.repr().scalable else {
1283 bug!("`scalable_vector_size_and_type` called on non-scalable vector type");
1284 };
1285 let variant = def.non_enum_variant();
1286 assert_eq!(variant.fields.len(), 1);
1287 let field_ty = variant.fields[FieldIdx::ZERO].ty(tcx, args);
1288 (element_count, field_ty)
1289 }
1290
1291 pub fn simd_size_and_type(self, tcx: TyCtxt<'tcx>) -> (u64, Ty<'tcx>) {
1292 let Adt(def, args) = self.kind() else {
1293 bug!("`simd_size_and_type` called on invalid type")
1294 };
1295 assert!(def.repr().simd(), "`simd_size_and_type` called on non-SIMD type");
1296 let variant = def.non_enum_variant();
1297 assert_eq!(variant.fields.len(), 1);
1298 let field_ty = variant.fields[FieldIdx::ZERO].ty(tcx, args);
1299 let Array(f0_elem_ty, f0_len) = field_ty.kind() else {
1300 bug!("Simd type has non-array field type {field_ty:?}")
1301 };
1302 (
1307 f0_len
1308 .try_to_target_usize(tcx)
1309 .expect("expected SIMD field to have definite array size"),
1310 *f0_elem_ty,
1311 )
1312 }
1313
1314 #[inline]
1315 pub fn is_mutable_ptr(self) -> bool {
1316 matches!(self.kind(), RawPtr(_, hir::Mutability::Mut) | Ref(_, _, hir::Mutability::Mut))
1317 }
1318
1319 #[inline]
1321 pub fn ref_mutability(self) -> Option<hir::Mutability> {
1322 match self.kind() {
1323 Ref(_, _, mutability) => Some(*mutability),
1324 _ => None,
1325 }
1326 }
1327
1328 #[inline]
1329 pub fn is_raw_ptr(self) -> bool {
1330 matches!(self.kind(), RawPtr(_, _))
1331 }
1332
1333 #[inline]
1336 pub fn is_any_ptr(self) -> bool {
1337 self.is_ref() || self.is_raw_ptr() || self.is_fn_ptr()
1338 }
1339
1340 #[inline]
1341 pub fn is_box(self) -> bool {
1342 match self.kind() {
1343 Adt(def, _) => def.is_box(),
1344 _ => false,
1345 }
1346 }
1347
1348 #[inline]
1353 pub fn is_box_global(self, tcx: TyCtxt<'tcx>) -> bool {
1354 match self.kind() {
1355 Adt(def, args) if def.is_box() => {
1356 let Some(alloc) = args.get(1) else {
1357 return true;
1359 };
1360 alloc.expect_ty().ty_adt_def().is_some_and(|alloc_adt| {
1361 tcx.is_lang_item(alloc_adt.did(), LangItem::GlobalAlloc)
1362 })
1363 }
1364 _ => false,
1365 }
1366 }
1367
1368 pub fn boxed_ty(self) -> Option<Ty<'tcx>> {
1369 match self.kind() {
1370 Adt(def, args) if def.is_box() => Some(args.type_at(0)),
1371 _ => None,
1372 }
1373 }
1374
1375 pub fn pinned_ty(self) -> Option<Ty<'tcx>> {
1376 match self.kind() {
1377 Adt(def, args) if def.is_pin() => Some(args.type_at(0)),
1378 _ => None,
1379 }
1380 }
1381
1382 pub fn pinned_ref(self) -> Option<(Ty<'tcx>, ty::Mutability)> {
1383 if let Adt(def, args) = self.kind()
1384 && def.is_pin()
1385 && let &ty::Ref(_, ty, mutbl) = args.type_at(0).kind()
1386 {
1387 return Some((ty, mutbl));
1388 }
1389 None
1390 }
1391
1392 pub fn maybe_pinned_ref(self) -> Option<(Ty<'tcx>, ty::Pinnedness, ty::Mutability)> {
1393 match *self.kind() {
1394 Adt(def, args)
1395 if def.is_pin()
1396 && let ty::Ref(_, ty, mutbl) = *args.type_at(0).kind() =>
1397 {
1398 Some((ty, ty::Pinnedness::Pinned, mutbl))
1399 }
1400 ty::Ref(_, ty, mutbl) => Some((ty, ty::Pinnedness::Not, mutbl)),
1401 _ => None,
1402 }
1403 }
1404
1405 pub fn expect_boxed_ty(self) -> Ty<'tcx> {
1407 self.boxed_ty()
1408 .unwrap_or_else(|| bug!("`expect_boxed_ty` is called on non-box type {:?}", self))
1409 }
1410
1411 #[inline]
1415 pub fn is_scalar(self) -> bool {
1416 matches!(
1417 self.kind(),
1418 Bool | Char
1419 | Int(_)
1420 | Float(_)
1421 | Uint(_)
1422 | FnDef(..)
1423 | FnPtr(..)
1424 | RawPtr(_, _)
1425 | Infer(IntVar(_) | FloatVar(_))
1426 )
1427 }
1428
1429 #[inline]
1431 pub fn is_floating_point(self) -> bool {
1432 matches!(self.kind(), Float(_) | Infer(FloatVar(_)))
1433 }
1434
1435 #[inline]
1436 pub fn is_trait(self) -> bool {
1437 matches!(self.kind(), Dynamic(_, _))
1438 }
1439
1440 #[inline]
1441 pub fn is_enum(self) -> bool {
1442 matches!(self.kind(), Adt(adt_def, _) if adt_def.is_enum())
1443 }
1444
1445 #[inline]
1446 pub fn is_union(self) -> bool {
1447 matches!(self.kind(), Adt(adt_def, _) if adt_def.is_union())
1448 }
1449
1450 #[inline]
1451 pub fn is_closure(self) -> bool {
1452 matches!(self.kind(), Closure(..))
1453 }
1454
1455 #[inline]
1456 pub fn is_coroutine(self) -> bool {
1457 matches!(self.kind(), Coroutine(..))
1458 }
1459
1460 #[inline]
1461 pub fn is_coroutine_closure(self) -> bool {
1462 matches!(self.kind(), CoroutineClosure(..))
1463 }
1464
1465 #[inline]
1466 pub fn is_integral(self) -> bool {
1467 matches!(self.kind(), Infer(IntVar(_)) | Int(_) | Uint(_))
1468 }
1469
1470 #[inline]
1471 pub fn is_fresh_ty(self) -> bool {
1472 matches!(self.kind(), Infer(FreshTy(_)))
1473 }
1474
1475 #[inline]
1476 pub fn is_fresh(self) -> bool {
1477 matches!(self.kind(), Infer(FreshTy(_) | FreshIntTy(_) | FreshFloatTy(_)))
1478 }
1479
1480 #[inline]
1481 pub fn is_char(self) -> bool {
1482 matches!(self.kind(), Char)
1483 }
1484
1485 #[inline]
1486 pub fn is_numeric(self) -> bool {
1487 self.is_integral() || self.is_floating_point()
1488 }
1489
1490 #[inline]
1491 pub fn is_signed(self) -> bool {
1492 matches!(self.kind(), Int(_))
1493 }
1494
1495 #[inline]
1496 pub fn is_ptr_sized_integral(self) -> bool {
1497 matches!(self.kind(), Int(ty::IntTy::Isize) | Uint(ty::UintTy::Usize))
1498 }
1499
1500 #[inline]
1501 pub fn has_concrete_skeleton(self) -> bool {
1502 !matches!(self.kind(), Param(_) | Infer(_) | Error(_))
1503 }
1504
1505 pub fn contains(self, other: Ty<'tcx>) -> bool {
1509 struct ContainsTyVisitor<'tcx>(Ty<'tcx>);
1510
1511 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsTyVisitor<'tcx> {
1512 type Result = ControlFlow<()>;
1513
1514 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1515 if self.0 == t { ControlFlow::Break(()) } else { t.super_visit_with(self) }
1516 }
1517 }
1518
1519 let cf = self.visit_with(&mut ContainsTyVisitor(other));
1520 cf.is_break()
1521 }
1522
1523 pub fn contains_closure(self) -> bool {
1527 struct ContainsClosureVisitor;
1528
1529 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ContainsClosureVisitor {
1530 type Result = ControlFlow<()>;
1531
1532 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
1533 if let ty::Closure(..) = t.kind() {
1534 ControlFlow::Break(())
1535 } else {
1536 t.super_visit_with(self)
1537 }
1538 }
1539 }
1540
1541 let cf = self.visit_with(&mut ContainsClosureVisitor);
1542 cf.is_break()
1543 }
1544
1545 pub fn find_async_drop_impl_coroutine<F: FnMut(Ty<'tcx>)>(
1550 self,
1551 tcx: TyCtxt<'tcx>,
1552 mut f: F,
1553 ) -> Ty<'tcx> {
1554 assert!(self.is_coroutine());
1555 let mut cor_ty = self;
1556 let mut ty = cor_ty;
1557 loop {
1558 let ty::Coroutine(def_id, args) = ty.kind() else { return cor_ty };
1559 cor_ty = ty;
1560 f(ty);
1561 if !tcx.is_async_drop_in_place_coroutine(*def_id) {
1562 return cor_ty;
1563 }
1564 ty = args.first().unwrap().expect_ty();
1565 }
1566 }
1567
1568 pub fn builtin_deref(self, explicit: bool) -> Option<Ty<'tcx>> {
1573 match *self.kind() {
1574 _ if let Some(boxed) = self.boxed_ty() => Some(boxed),
1575 Ref(_, ty, _) => Some(ty),
1576 RawPtr(ty, _) if explicit => Some(ty),
1577 _ => None,
1578 }
1579 }
1580
1581 pub fn builtin_index(self) -> Option<Ty<'tcx>> {
1583 match self.kind() {
1584 Array(ty, _) | Slice(ty) => Some(*ty),
1585 _ => None,
1586 }
1587 }
1588
1589 #[tracing::instrument(level = "trace", skip(tcx))]
1590 pub fn fn_sig(self, tcx: TyCtxt<'tcx>) -> PolyFnSig<'tcx> {
1591 self.kind().fn_sig(tcx)
1592 }
1593
1594 #[inline]
1595 pub fn is_fn(self) -> bool {
1596 matches!(self.kind(), FnDef(..) | FnPtr(..))
1597 }
1598
1599 #[inline]
1600 pub fn is_fn_ptr(self) -> bool {
1601 matches!(self.kind(), FnPtr(..))
1602 }
1603
1604 #[inline]
1605 pub fn is_impl_trait(self) -> bool {
1606 matches!(self.kind(), Alias(ty::Opaque, ..))
1607 }
1608
1609 #[inline]
1610 pub fn ty_adt_def(self) -> Option<AdtDef<'tcx>> {
1611 match self.kind() {
1612 Adt(adt, _) => Some(*adt),
1613 _ => None,
1614 }
1615 }
1616
1617 #[inline]
1620 pub fn tuple_fields(self) -> &'tcx List<Ty<'tcx>> {
1621 match self.kind() {
1622 Tuple(args) => args,
1623 _ => bug!("tuple_fields called on non-tuple: {self:?}"),
1624 }
1625 }
1626
1627 #[inline]
1631 pub fn variant_range(self, tcx: TyCtxt<'tcx>) -> Option<Range<VariantIdx>> {
1632 match self.kind() {
1633 TyKind::Adt(adt, _) => Some(adt.variant_range()),
1634 TyKind::Coroutine(def_id, args) => {
1635 Some(args.as_coroutine().variant_range(*def_id, tcx))
1636 }
1637 _ => None,
1638 }
1639 }
1640
1641 #[inline]
1646 pub fn discriminant_for_variant(
1647 self,
1648 tcx: TyCtxt<'tcx>,
1649 variant_index: VariantIdx,
1650 ) -> Option<Discr<'tcx>> {
1651 match self.kind() {
1652 TyKind::Adt(adt, _) if adt.is_enum() => {
1653 Some(adt.discriminant_for_variant(tcx, variant_index))
1654 }
1655 TyKind::Coroutine(def_id, args) => {
1656 Some(args.as_coroutine().discriminant_for_variant(*def_id, tcx, variant_index))
1657 }
1658 _ => None,
1659 }
1660 }
1661
1662 pub fn discriminant_ty(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1664 match self.kind() {
1665 ty::Adt(adt, _) if adt.is_enum() => adt.repr().discr_type().to_ty(tcx),
1666 ty::Coroutine(_, args) => args.as_coroutine().discr_ty(tcx),
1667
1668 ty::Param(_) | ty::Alias(..) | ty::Infer(ty::TyVar(_)) => {
1669 let assoc_items = tcx.associated_item_def_ids(
1670 tcx.require_lang_item(hir::LangItem::DiscriminantKind, DUMMY_SP),
1671 );
1672 Ty::new_projection_from_args(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
1673 }
1674
1675 ty::Pat(ty, _) => ty.discriminant_ty(tcx),
1676
1677 ty::Bool
1678 | ty::Char
1679 | ty::Int(_)
1680 | ty::Uint(_)
1681 | ty::Float(_)
1682 | ty::Adt(..)
1683 | ty::Foreign(_)
1684 | ty::Str
1685 | ty::Array(..)
1686 | ty::Slice(_)
1687 | ty::RawPtr(_, _)
1688 | ty::Ref(..)
1689 | ty::FnDef(..)
1690 | ty::FnPtr(..)
1691 | ty::Dynamic(..)
1692 | ty::Closure(..)
1693 | ty::CoroutineClosure(..)
1694 | ty::CoroutineWitness(..)
1695 | ty::Never
1696 | ty::Tuple(_)
1697 | ty::UnsafeBinder(_)
1698 | ty::Error(_)
1699 | ty::Infer(IntVar(_) | FloatVar(_)) => tcx.types.u8,
1700
1701 ty::Bound(..)
1702 | ty::Placeholder(_)
1703 | ty::Infer(FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1704 bug!("`discriminant_ty` applied to unexpected type: {:?}", self)
1705 }
1706 }
1707 }
1708
1709 pub fn ptr_metadata_ty_or_tail(
1712 self,
1713 tcx: TyCtxt<'tcx>,
1714 normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
1715 ) -> Result<Ty<'tcx>, Ty<'tcx>> {
1716 let tail = tcx.struct_tail_raw(self, &ObligationCause::dummy(), normalize, || {});
1717 match tail.kind() {
1718 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1720 | ty::Uint(_)
1721 | ty::Int(_)
1722 | ty::Bool
1723 | ty::Float(_)
1724 | ty::FnDef(..)
1725 | ty::FnPtr(..)
1726 | ty::RawPtr(..)
1727 | ty::Char
1728 | ty::Ref(..)
1729 | ty::Coroutine(..)
1730 | ty::CoroutineWitness(..)
1731 | ty::Array(..)
1732 | ty::Closure(..)
1733 | ty::CoroutineClosure(..)
1734 | ty::Never
1735 | ty::Error(_)
1736 | ty::Foreign(..)
1738 | ty::Adt(..)
1741 | ty::Tuple(..) => Ok(tcx.types.unit),
1744
1745 ty::Str | ty::Slice(_) => Ok(tcx.types.usize),
1746
1747 ty::Dynamic(_, _) => {
1748 let dyn_metadata = tcx.require_lang_item(LangItem::DynMetadata, DUMMY_SP);
1749 Ok(tcx.type_of(dyn_metadata).instantiate(tcx, &[tail.into()]))
1750 }
1751
1752 ty::Param(_) | ty::Alias(..) => Err(tail),
1755
1756 | ty::UnsafeBinder(_) => todo!("FIXME(unsafe_binder)"),
1757
1758 ty::Infer(ty::TyVar(_))
1759 | ty::Pat(..)
1760 | ty::Bound(..)
1761 | ty::Placeholder(..)
1762 | ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => bug!(
1763 "`ptr_metadata_ty_or_tail` applied to unexpected type: {self:?} (tail = {tail:?})"
1764 ),
1765 }
1766 }
1767
1768 pub fn ptr_metadata_ty(
1771 self,
1772 tcx: TyCtxt<'tcx>,
1773 normalize: impl FnMut(Ty<'tcx>) -> Ty<'tcx>,
1774 ) -> Ty<'tcx> {
1775 match self.ptr_metadata_ty_or_tail(tcx, normalize) {
1776 Ok(metadata) => metadata,
1777 Err(tail) => bug!(
1778 "`ptr_metadata_ty` failed to get metadata for type: {self:?} (tail = {tail:?})"
1779 ),
1780 }
1781 }
1782
1783 #[track_caller]
1792 pub fn pointee_metadata_ty_or_projection(self, tcx: TyCtxt<'tcx>) -> Ty<'tcx> {
1793 let Some(pointee_ty) = self.builtin_deref(true) else {
1794 bug!("Type {self:?} is not a pointer or reference type")
1795 };
1796 if pointee_ty.has_trivial_sizedness(tcx, SizedTraitKind::Sized) {
1797 tcx.types.unit
1798 } else {
1799 match pointee_ty.ptr_metadata_ty_or_tail(tcx, |x| x) {
1800 Ok(metadata_ty) => metadata_ty,
1801 Err(tail_ty) => {
1802 let metadata_def_id = tcx.require_lang_item(LangItem::Metadata, DUMMY_SP);
1803 Ty::new_projection(tcx, metadata_def_id, [tail_ty])
1804 }
1805 }
1806 }
1807 }
1808
1809 pub fn to_opt_closure_kind(self) -> Option<ty::ClosureKind> {
1849 match self.kind() {
1850 Int(int_ty) => match int_ty {
1851 ty::IntTy::I8 => Some(ty::ClosureKind::Fn),
1852 ty::IntTy::I16 => Some(ty::ClosureKind::FnMut),
1853 ty::IntTy::I32 => Some(ty::ClosureKind::FnOnce),
1854 _ => bug!("cannot convert type `{:?}` to a closure kind", self),
1855 },
1856
1857 Bound(..) | Placeholder(_) | Param(_) | Infer(_) => None,
1861
1862 Error(_) => Some(ty::ClosureKind::Fn),
1863
1864 _ => bug!("cannot convert type `{:?}` to a closure kind", self),
1865 }
1866 }
1867
1868 pub fn from_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
1871 match kind {
1872 ty::ClosureKind::Fn => tcx.types.i8,
1873 ty::ClosureKind::FnMut => tcx.types.i16,
1874 ty::ClosureKind::FnOnce => tcx.types.i32,
1875 }
1876 }
1877
1878 pub fn from_coroutine_closure_kind(tcx: TyCtxt<'tcx>, kind: ty::ClosureKind) -> Ty<'tcx> {
1891 match kind {
1892 ty::ClosureKind::Fn | ty::ClosureKind::FnMut => tcx.types.i16,
1893 ty::ClosureKind::FnOnce => tcx.types.i32,
1894 }
1895 }
1896
1897 #[instrument(skip(tcx), level = "debug")]
1907 pub fn has_trivial_sizedness(self, tcx: TyCtxt<'tcx>, sizedness: SizedTraitKind) -> bool {
1908 match self.kind() {
1909 ty::Infer(ty::IntVar(_) | ty::FloatVar(_))
1910 | ty::Uint(_)
1911 | ty::Int(_)
1912 | ty::Bool
1913 | ty::Float(_)
1914 | ty::FnDef(..)
1915 | ty::FnPtr(..)
1916 | ty::UnsafeBinder(_)
1917 | ty::RawPtr(..)
1918 | ty::Char
1919 | ty::Ref(..)
1920 | ty::Coroutine(..)
1921 | ty::CoroutineWitness(..)
1922 | ty::Array(..)
1923 | ty::Pat(..)
1924 | ty::Closure(..)
1925 | ty::CoroutineClosure(..)
1926 | ty::Never
1927 | ty::Error(_) => true,
1928
1929 ty::Str | ty::Slice(_) | ty::Dynamic(_, _) => match sizedness {
1930 SizedTraitKind::Sized => false,
1931 SizedTraitKind::MetaSized => true,
1932 },
1933
1934 ty::Foreign(..) => match sizedness {
1935 SizedTraitKind::Sized | SizedTraitKind::MetaSized => false,
1936 },
1937
1938 ty::Tuple(tys) => tys.last().is_none_or(|ty| ty.has_trivial_sizedness(tcx, sizedness)),
1939
1940 ty::Adt(def, args) => def
1941 .sizedness_constraint(tcx, sizedness)
1942 .is_none_or(|ty| ty.instantiate(tcx, args).has_trivial_sizedness(tcx, sizedness)),
1943
1944 ty::Alias(..) | ty::Param(_) | ty::Placeholder(..) | ty::Bound(..) => false,
1945
1946 ty::Infer(ty::TyVar(_)) => false,
1947
1948 ty::Infer(ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_)) => {
1949 bug!("`has_trivial_sizedness` applied to unexpected type: {:?}", self)
1950 }
1951 }
1952 }
1953
1954 pub fn is_trivially_pure_clone_copy(self) -> bool {
1963 match self.kind() {
1964 ty::Bool | ty::Char | ty::Never => true,
1965
1966 ty::Str | ty::Slice(..) | ty::Foreign(..) | ty::Dynamic(..) => false,
1968
1969 ty::Infer(ty::InferTy::FloatVar(_) | ty::InferTy::IntVar(_))
1970 | ty::Int(..)
1971 | ty::Uint(..)
1972 | ty::Float(..) => true,
1973
1974 ty::FnDef(..) => true,
1976
1977 ty::Array(element_ty, _len) => element_ty.is_trivially_pure_clone_copy(),
1978
1979 ty::Tuple(field_tys) => {
1981 field_tys.len() <= 3 && field_tys.iter().all(Self::is_trivially_pure_clone_copy)
1982 }
1983
1984 ty::Pat(ty, _) => ty.is_trivially_pure_clone_copy(),
1985
1986 ty::FnPtr(..) => false,
1989
1990 ty::Ref(_, _, hir::Mutability::Mut) => false,
1992
1993 ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => true,
1996
1997 ty::Coroutine(..) | ty::CoroutineWitness(..) => false,
1998
1999 ty::Adt(..) | ty::Closure(..) | ty::CoroutineClosure(..) => false,
2001
2002 ty::UnsafeBinder(_) => false,
2003
2004 ty::Alias(..) => false,
2006
2007 ty::Param(..) | ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) | ty::Error(..) => {
2008 false
2009 }
2010 }
2011 }
2012
2013 pub fn is_trivially_wf(self, tcx: TyCtxt<'tcx>) -> bool {
2014 match *self.kind() {
2015 ty::Bool
2016 | ty::Char
2017 | ty::Int(_)
2018 | ty::Uint(_)
2019 | ty::Float(_)
2020 | ty::Str
2021 | ty::Never
2022 | ty::Param(_)
2023 | ty::Placeholder(_)
2024 | ty::Bound(..) => true,
2025
2026 ty::Slice(ty) => {
2027 ty.is_trivially_wf(tcx) && ty.has_trivial_sizedness(tcx, SizedTraitKind::Sized)
2028 }
2029 ty::RawPtr(ty, _) => ty.is_trivially_wf(tcx),
2030
2031 ty::FnPtr(sig_tys, _) => {
2032 sig_tys.skip_binder().inputs_and_output.iter().all(|ty| ty.is_trivially_wf(tcx))
2033 }
2034 ty::Ref(_, ty, _) => ty.is_global() && ty.is_trivially_wf(tcx),
2035
2036 ty::Infer(infer) => match infer {
2037 ty::TyVar(_) => false,
2038 ty::IntVar(_) | ty::FloatVar(_) => true,
2039 ty::FreshTy(_) | ty::FreshIntTy(_) | ty::FreshFloatTy(_) => true,
2040 },
2041
2042 ty::Adt(_, _)
2043 | ty::Tuple(_)
2044 | ty::Array(..)
2045 | ty::Foreign(_)
2046 | ty::Pat(_, _)
2047 | ty::FnDef(..)
2048 | ty::UnsafeBinder(..)
2049 | ty::Dynamic(..)
2050 | ty::Closure(..)
2051 | ty::CoroutineClosure(..)
2052 | ty::Coroutine(..)
2053 | ty::CoroutineWitness(..)
2054 | ty::Alias(..)
2055 | ty::Error(_) => false,
2056 }
2057 }
2058
2059 pub fn primitive_symbol(self) -> Option<Symbol> {
2061 match self.kind() {
2062 ty::Bool => Some(sym::bool),
2063 ty::Char => Some(sym::char),
2064 ty::Float(f) => match f {
2065 ty::FloatTy::F16 => Some(sym::f16),
2066 ty::FloatTy::F32 => Some(sym::f32),
2067 ty::FloatTy::F64 => Some(sym::f64),
2068 ty::FloatTy::F128 => Some(sym::f128),
2069 },
2070 ty::Int(f) => match f {
2071 ty::IntTy::Isize => Some(sym::isize),
2072 ty::IntTy::I8 => Some(sym::i8),
2073 ty::IntTy::I16 => Some(sym::i16),
2074 ty::IntTy::I32 => Some(sym::i32),
2075 ty::IntTy::I64 => Some(sym::i64),
2076 ty::IntTy::I128 => Some(sym::i128),
2077 },
2078 ty::Uint(f) => match f {
2079 ty::UintTy::Usize => Some(sym::usize),
2080 ty::UintTy::U8 => Some(sym::u8),
2081 ty::UintTy::U16 => Some(sym::u16),
2082 ty::UintTy::U32 => Some(sym::u32),
2083 ty::UintTy::U64 => Some(sym::u64),
2084 ty::UintTy::U128 => Some(sym::u128),
2085 },
2086 ty::Str => Some(sym::str),
2087 _ => None,
2088 }
2089 }
2090
2091 pub fn is_c_void(self, tcx: TyCtxt<'_>) -> bool {
2092 match self.kind() {
2093 ty::Adt(adt, _) => tcx.is_lang_item(adt.did(), LangItem::CVoid),
2094 _ => false,
2095 }
2096 }
2097
2098 pub fn is_async_drop_in_place_coroutine(self, tcx: TyCtxt<'_>) -> bool {
2099 match self.kind() {
2100 ty::Coroutine(def, ..) => tcx.is_async_drop_in_place_coroutine(*def),
2101 _ => false,
2102 }
2103 }
2104
2105 pub fn is_known_rigid(self) -> bool {
2111 self.kind().is_known_rigid()
2112 }
2113
2114 pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
2125 TypeWalker::new(self.into())
2126 }
2127}
2128
2129impl<'tcx> rustc_type_ir::inherent::Tys<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> {
2130 fn inputs(self) -> &'tcx [Ty<'tcx>] {
2131 self.split_last().unwrap().1
2132 }
2133
2134 fn output(self) -> Ty<'tcx> {
2135 *self.split_last().unwrap().0
2136 }
2137}
2138
2139#[cfg(target_pointer_width = "64")]
2141mod size_asserts {
2142 use rustc_data_structures::static_assert_size;
2143
2144 use super::*;
2145 static_assert_size!(TyKind<'_>, 24);
2147 static_assert_size!(ty::WithCachedTypeInfo<TyKind<'_>>, 48);
2148 }