1use core::intrinsics;
4use std::marker::PhantomData;
5use std::num::NonZero;
6use std::ptr::NonNull;
7
8use rustc_data_structures::intern::Interned;
9use rustc_errors::{DiagArgValue, IntoDiagArg};
10use rustc_hir::def_id::DefId;
11use rustc_macros::{HashStable, TyDecodable, TyEncodable, extension};
12use rustc_serialize::{Decodable, Encodable};
13use rustc_type_ir::WithCachedTypeInfo;
14use rustc_type_ir::walk::TypeWalker;
15use smallvec::SmallVec;
16
17use crate::ty::codec::{TyDecoder, TyEncoder};
18use crate::ty::{
19 self, ClosureArgs, CoroutineArgs, CoroutineClosureArgs, FallibleTypeFolder, InlineConstArgs,
20 Lift, List, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeVisitable, TypeVisitor, VisitorResult,
21 walk_visitable_list,
22};
23
24pub type GenericArgKind<'tcx> = rustc_type_ir::GenericArgKind<TyCtxt<'tcx>>;
25pub type TermKind<'tcx> = rustc_type_ir::TermKind<TyCtxt<'tcx>>;
26
27#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for GenericArg<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for GenericArg<'tcx> {
#[inline]
fn clone(&self) -> GenericArg<'tcx> {
let _: ::core::clone::AssertParamIsClone<NonNull<()>>;
let _:
::core::clone::AssertParamIsClone<PhantomData<(Ty<'tcx>,
ty::Region<'tcx>, ty::Const<'tcx>)>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for GenericArg<'tcx> {
#[inline]
fn eq(&self, other: &GenericArg<'tcx>) -> bool {
self.ptr == other.ptr && self.marker == other.marker
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for GenericArg<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<NonNull<()>>;
let _:
::core::cmp::AssertParamIsEq<PhantomData<(Ty<'tcx>,
ty::Region<'tcx>, ty::Const<'tcx>)>>;
}
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for GenericArg<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.ptr, state);
::core::hash::Hash::hash(&self.marker, state)
}
}Hash)]
36pub struct GenericArg<'tcx> {
37 ptr: NonNull<()>,
38 marker: PhantomData<(Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>)>,
39}
40
41impl<'tcx> rustc_type_ir::inherent::GenericArg<TyCtxt<'tcx>> for GenericArg<'tcx> {}
42
43impl<'tcx> rustc_type_ir::inherent::GenericArgs<TyCtxt<'tcx>> for ty::GenericArgsRef<'tcx> {
44 fn rebase_onto(
45 self,
46 tcx: TyCtxt<'tcx>,
47 source_ancestor: DefId,
48 target_args: GenericArgsRef<'tcx>,
49 ) -> GenericArgsRef<'tcx> {
50 self.rebase_onto(tcx, source_ancestor, target_args)
51 }
52
53 #[track_caller]
54 fn type_at(self, i: usize) -> Ty<'tcx> {
55 self.type_at(i)
56 }
57
58 #[track_caller]
59 fn region_at(self, i: usize) -> ty::Region<'tcx> {
60 self.region_at(i)
61 }
62
63 #[track_caller]
64 fn const_at(self, i: usize) -> ty::Const<'tcx> {
65 self.const_at(i)
66 }
67
68 fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::GenericArgsRef<'tcx> {
69 GenericArgs::identity_for_item(tcx, def_id)
70 }
71
72 fn extend_with_error(
73 tcx: TyCtxt<'tcx>,
74 def_id: DefId,
75 original_args: &[ty::GenericArg<'tcx>],
76 ) -> ty::GenericArgsRef<'tcx> {
77 ty::GenericArgs::extend_with_error(tcx, def_id, original_args)
78 }
79
80 fn split_closure_args(self) -> ty::ClosureArgsParts<TyCtxt<'tcx>> {
81 match self[..] {
82 [ref parent_args @ .., closure_kind_ty, closure_sig_as_fn_ptr_ty, tupled_upvars_ty] => {
83 ty::ClosureArgsParts {
84 parent_args,
85 closure_kind_ty: closure_kind_ty.expect_ty(),
86 closure_sig_as_fn_ptr_ty: closure_sig_as_fn_ptr_ty.expect_ty(),
87 tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
88 }
89 }
90 _ => crate::util::bug::bug_fmt(format_args!("closure args missing synthetics"))bug!("closure args missing synthetics"),
91 }
92 }
93
94 fn split_coroutine_closure_args(self) -> ty::CoroutineClosureArgsParts<TyCtxt<'tcx>> {
95 match self[..] {
96 [
97 ref parent_args @ ..,
98 closure_kind_ty,
99 signature_parts_ty,
100 tupled_upvars_ty,
101 coroutine_captures_by_ref_ty,
102 ] => ty::CoroutineClosureArgsParts {
103 parent_args,
104 closure_kind_ty: closure_kind_ty.expect_ty(),
105 signature_parts_ty: signature_parts_ty.expect_ty(),
106 tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
107 coroutine_captures_by_ref_ty: coroutine_captures_by_ref_ty.expect_ty(),
108 },
109 _ => crate::util::bug::bug_fmt(format_args!("closure args missing synthetics"))bug!("closure args missing synthetics"),
110 }
111 }
112
113 fn split_coroutine_args(self) -> ty::CoroutineArgsParts<TyCtxt<'tcx>> {
114 match self[..] {
115 [ref parent_args @ .., kind_ty, resume_ty, yield_ty, return_ty, tupled_upvars_ty] => {
116 ty::CoroutineArgsParts {
117 parent_args,
118 kind_ty: kind_ty.expect_ty(),
119 resume_ty: resume_ty.expect_ty(),
120 yield_ty: yield_ty.expect_ty(),
121 return_ty: return_ty.expect_ty(),
122 tupled_upvars_ty: tupled_upvars_ty.expect_ty(),
123 }
124 }
125 _ => crate::util::bug::bug_fmt(format_args!("coroutine args missing synthetics"))bug!("coroutine args missing synthetics"),
126 }
127 }
128}
129
130impl<'tcx> rustc_type_ir::inherent::IntoKind for GenericArg<'tcx> {
131 type Kind = GenericArgKind<'tcx>;
132
133 fn kind(self) -> Self::Kind {
134 self.kind()
135 }
136}
137
138unsafe impl<'tcx> rustc_data_structures::sync::DynSend for GenericArg<'tcx> where
139 &'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): rustc_data_structures::sync::DynSend
140{
141}
142unsafe impl<'tcx> rustc_data_structures::sync::DynSync for GenericArg<'tcx> where
143 &'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): rustc_data_structures::sync::DynSync
144{
145}
146unsafe impl<'tcx> Send for GenericArg<'tcx> where
147 &'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): Send
148{
149}
150unsafe impl<'tcx> Sync for GenericArg<'tcx> where
151 &'tcx (Ty<'tcx>, ty::Region<'tcx>, ty::Const<'tcx>): Sync
152{
153}
154
155impl<'tcx> IntoDiagArg for GenericArg<'tcx> {
156 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> DiagArgValue {
157 self.to_string().into_diag_arg(&mut None)
158 }
159}
160
161const TAG_MASK: usize = 0b11;
162const TYPE_TAG: usize = 0b00;
163const REGION_TAG: usize = 0b01;
164const CONST_TAG: usize = 0b10;
165
166impl<'tcx> GenericArgPackExt<'tcx> for GenericArgKind<'tcx> {
#[inline]
fn pack(self) -> GenericArg<'tcx> {
let (tag, ptr) =
match self {
GenericArgKind::Lifetime(lt) => {
match (&(align_of_val(&*lt.0.0) & TAG_MASK), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
(REGION_TAG, NonNull::from(lt.0.0).cast())
}
GenericArgKind::Type(ty) => {
match (&(align_of_val(&*ty.0.0) & TAG_MASK), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
(TYPE_TAG, NonNull::from(ty.0.0).cast())
}
GenericArgKind::Const(ct) => {
match (&(align_of_val(&*ct.0.0) & TAG_MASK), &0) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
(CONST_TAG, NonNull::from(ct.0.0).cast())
}
};
GenericArg {
ptr: ptr.map_addr(|addr| addr | tag),
marker: PhantomData,
}
}
}#[extension(trait GenericArgPackExt<'tcx>)]
167impl<'tcx> GenericArgKind<'tcx> {
168 #[inline]
169 fn pack(self) -> GenericArg<'tcx> {
170 let (tag, ptr) = match self {
171 GenericArgKind::Lifetime(lt) => {
172 assert_eq!(align_of_val(&*lt.0.0) & TAG_MASK, 0);
174 (REGION_TAG, NonNull::from(lt.0.0).cast())
175 }
176 GenericArgKind::Type(ty) => {
177 assert_eq!(align_of_val(&*ty.0.0) & TAG_MASK, 0);
179 (TYPE_TAG, NonNull::from(ty.0.0).cast())
180 }
181 GenericArgKind::Const(ct) => {
182 assert_eq!(align_of_val(&*ct.0.0) & TAG_MASK, 0);
184 (CONST_TAG, NonNull::from(ct.0.0).cast())
185 }
186 };
187
188 GenericArg { ptr: ptr.map_addr(|addr| addr | tag), marker: PhantomData }
189 }
190}
191
192impl<'tcx> From<ty::Region<'tcx>> for GenericArg<'tcx> {
193 #[inline]
194 fn from(r: ty::Region<'tcx>) -> GenericArg<'tcx> {
195 GenericArgKind::Lifetime(r).pack()
196 }
197}
198
199impl<'tcx> From<Ty<'tcx>> for GenericArg<'tcx> {
200 #[inline]
201 fn from(ty: Ty<'tcx>) -> GenericArg<'tcx> {
202 GenericArgKind::Type(ty).pack()
203 }
204}
205
206impl<'tcx> From<ty::Const<'tcx>> for GenericArg<'tcx> {
207 #[inline]
208 fn from(c: ty::Const<'tcx>) -> GenericArg<'tcx> {
209 GenericArgKind::Const(c).pack()
210 }
211}
212
213impl<'tcx> From<ty::Term<'tcx>> for GenericArg<'tcx> {
214 fn from(value: ty::Term<'tcx>) -> Self {
215 match value.kind() {
216 ty::TermKind::Ty(t) => t.into(),
217 ty::TermKind::Const(c) => c.into(),
218 }
219 }
220}
221
222impl<'tcx> GenericArg<'tcx> {
223 #[inline]
224 pub fn kind(self) -> GenericArgKind<'tcx> {
225 let ptr =
226 unsafe { self.ptr.map_addr(|addr| NonZero::new_unchecked(addr.get() & !TAG_MASK)) };
227 unsafe {
231 match self.ptr.addr().get() & TAG_MASK {
232 REGION_TAG => GenericArgKind::Lifetime(ty::Region(Interned::new_unchecked(
233 ptr.cast::<ty::RegionKind<'tcx>>().as_ref(),
234 ))),
235 TYPE_TAG => GenericArgKind::Type(Ty(Interned::new_unchecked(
236 ptr.cast::<WithCachedTypeInfo<ty::TyKind<'tcx>>>().as_ref(),
237 ))),
238 CONST_TAG => GenericArgKind::Const(ty::Const(Interned::new_unchecked(
239 ptr.cast::<WithCachedTypeInfo<ty::ConstKind<'tcx>>>().as_ref(),
240 ))),
241 _ => intrinsics::unreachable(),
242 }
243 }
244 }
245
246 #[inline]
247 pub fn as_region(self) -> Option<ty::Region<'tcx>> {
248 match self.kind() {
249 GenericArgKind::Lifetime(re) => Some(re),
250 _ => None,
251 }
252 }
253
254 #[inline]
255 pub fn as_type(self) -> Option<Ty<'tcx>> {
256 match self.kind() {
257 GenericArgKind::Type(ty) => Some(ty),
258 _ => None,
259 }
260 }
261
262 #[inline]
263 pub fn as_const(self) -> Option<ty::Const<'tcx>> {
264 match self.kind() {
265 GenericArgKind::Const(ct) => Some(ct),
266 _ => None,
267 }
268 }
269
270 #[inline]
271 pub fn as_term(self) -> Option<ty::Term<'tcx>> {
272 match self.kind() {
273 GenericArgKind::Lifetime(_) => None,
274 GenericArgKind::Type(ty) => Some(ty.into()),
275 GenericArgKind::Const(ct) => Some(ct.into()),
276 }
277 }
278
279 pub fn expect_region(self) -> ty::Region<'tcx> {
281 self.as_region().unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("expected a region, but found another kind"))bug!("expected a region, but found another kind"))
282 }
283
284 pub fn expect_ty(self) -> Ty<'tcx> {
288 self.as_type().unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("expected a type, but found another kind"))bug!("expected a type, but found another kind"))
289 }
290
291 pub fn expect_const(self) -> ty::Const<'tcx> {
293 self.as_const().unwrap_or_else(|| crate::util::bug::bug_fmt(format_args!("expected a const, but found another kind"))bug!("expected a const, but found another kind"))
294 }
295
296 pub fn is_non_region_infer(self) -> bool {
297 match self.kind() {
298 GenericArgKind::Lifetime(_) => false,
299 GenericArgKind::Type(ty) => ty.is_ty_or_numeric_infer(),
301 GenericArgKind::Const(ct) => ct.is_ct_infer(),
302 }
303 }
304
305 pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
316 TypeWalker::new(self)
317 }
318}
319
320impl<'a, 'tcx> Lift<TyCtxt<'tcx>> for GenericArg<'a> {
321 type Lifted = GenericArg<'tcx>;
322
323 fn lift_to_interner(self, tcx: TyCtxt<'tcx>) -> Option<Self::Lifted> {
324 match self.kind() {
325 GenericArgKind::Lifetime(lt) => tcx.lift(lt).map(|lt| lt.into()),
326 GenericArgKind::Type(ty) => tcx.lift(ty).map(|ty| ty.into()),
327 GenericArgKind::Const(ct) => tcx.lift(ct).map(|ct| ct.into()),
328 }
329 }
330}
331
332impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArg<'tcx> {
333 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
334 self,
335 folder: &mut F,
336 ) -> Result<Self, F::Error> {
337 match self.kind() {
338 GenericArgKind::Lifetime(lt) => lt.try_fold_with(folder).map(Into::into),
339 GenericArgKind::Type(ty) => ty.try_fold_with(folder).map(Into::into),
340 GenericArgKind::Const(ct) => ct.try_fold_with(folder).map(Into::into),
341 }
342 }
343
344 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
345 match self.kind() {
346 GenericArgKind::Lifetime(lt) => lt.fold_with(folder).into(),
347 GenericArgKind::Type(ty) => ty.fold_with(folder).into(),
348 GenericArgKind::Const(ct) => ct.fold_with(folder).into(),
349 }
350 }
351}
352
353impl<'tcx> TypeVisitable<TyCtxt<'tcx>> for GenericArg<'tcx> {
354 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
355 match self.kind() {
356 GenericArgKind::Lifetime(lt) => lt.visit_with(visitor),
357 GenericArgKind::Type(ty) => ty.visit_with(visitor),
358 GenericArgKind::Const(ct) => ct.visit_with(visitor),
359 }
360 }
361}
362
363impl<'tcx, E: TyEncoder<'tcx>> Encodable<E> for GenericArg<'tcx> {
364 fn encode(&self, e: &mut E) {
365 self.kind().encode(e)
366 }
367}
368
369impl<'tcx, D: TyDecoder<'tcx>> Decodable<D> for GenericArg<'tcx> {
370 fn decode(d: &mut D) -> GenericArg<'tcx> {
371 GenericArgKind::decode(d).pack()
372 }
373}
374
375pub type GenericArgs<'tcx> = List<GenericArg<'tcx>>;
377
378pub type GenericArgsRef<'tcx> = &'tcx GenericArgs<'tcx>;
379
380impl<'tcx> GenericArgs<'tcx> {
381 pub fn into_type_list(&self, tcx: TyCtxt<'tcx>) -> &'tcx List<Ty<'tcx>> {
387 tcx.mk_type_list_from_iter(self.iter().map(|arg| match arg.kind() {
388 GenericArgKind::Type(ty) => ty,
389 _ => crate::util::bug::bug_fmt(format_args!("`into_type_list` called on generic arg with non-types"))bug!("`into_type_list` called on generic arg with non-types"),
390 }))
391 }
392
393 pub fn as_closure(&'tcx self) -> ClosureArgs<TyCtxt<'tcx>> {
398 ClosureArgs { args: self }
399 }
400
401 pub fn as_coroutine_closure(&'tcx self) -> CoroutineClosureArgs<TyCtxt<'tcx>> {
406 CoroutineClosureArgs { args: self }
407 }
408
409 pub fn as_coroutine(&'tcx self) -> CoroutineArgs<TyCtxt<'tcx>> {
414 CoroutineArgs { args: self }
415 }
416
417 pub fn as_inline_const(&'tcx self) -> InlineConstArgs<'tcx> {
422 InlineConstArgs { args: self }
423 }
424
425 pub fn identity_for_item(tcx: TyCtxt<'tcx>, def_id: impl Into<DefId>) -> GenericArgsRef<'tcx> {
427 Self::for_item(tcx, def_id.into(), |param, _| tcx.mk_param_from_def(param))
428 }
429
430 pub fn for_item<F>(tcx: TyCtxt<'tcx>, def_id: DefId, mut mk_kind: F) -> GenericArgsRef<'tcx>
436 where
437 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
438 {
439 let defs = tcx.generics_of(def_id);
440 let count = defs.count();
441 let mut args = SmallVec::with_capacity(count);
442 Self::fill_item(&mut args, tcx, defs, &mut mk_kind);
443 tcx.mk_args(&args)
444 }
445
446 pub fn extend_to<F>(
447 &self,
448 tcx: TyCtxt<'tcx>,
449 def_id: DefId,
450 mut mk_kind: F,
451 ) -> GenericArgsRef<'tcx>
452 where
453 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
454 {
455 Self::for_item(tcx, def_id, |param, args| {
456 self.get(param.index as usize).cloned().unwrap_or_else(|| mk_kind(param, args))
457 })
458 }
459
460 pub fn fill_item<F>(
461 args: &mut SmallVec<[GenericArg<'tcx>; 8]>,
462 tcx: TyCtxt<'tcx>,
463 defs: &ty::Generics,
464 mk_kind: &mut F,
465 ) where
466 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
467 {
468 if let Some(def_id) = defs.parent {
469 let parent_defs = tcx.generics_of(def_id);
470 Self::fill_item(args, tcx, parent_defs, mk_kind);
471 }
472 Self::fill_single(args, defs, mk_kind)
473 }
474
475 pub fn fill_single<F>(
476 args: &mut SmallVec<[GenericArg<'tcx>; 8]>,
477 defs: &ty::Generics,
478 mk_kind: &mut F,
479 ) where
480 F: FnMut(&ty::GenericParamDef, &[GenericArg<'tcx>]) -> GenericArg<'tcx>,
481 {
482 args.reserve(defs.own_params.len());
483 for param in &defs.own_params {
484 let kind = mk_kind(param, args);
485 match (&(param.index as usize), &args.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val, &*right_val,
::core::option::Option::Some(format_args!("{0:#?}, {1:#?}",
args, defs)));
}
}
};assert_eq!(param.index as usize, args.len(), "{args:#?}, {defs:#?}");
486 args.push(kind);
487 }
488 }
489
490 pub fn extend_with_error(
493 tcx: TyCtxt<'tcx>,
494 def_id: DefId,
495 original_args: &[GenericArg<'tcx>],
496 ) -> GenericArgsRef<'tcx> {
497 ty::GenericArgs::for_item(tcx, def_id, |def, _| {
498 if let Some(arg) = original_args.get(def.index as usize) {
499 *arg
500 } else {
501 def.to_error(tcx)
502 }
503 })
504 }
505
506 #[inline]
507 pub fn types(&self) -> impl DoubleEndedIterator<Item = Ty<'tcx>> {
508 self.iter().filter_map(|k| k.as_type())
509 }
510
511 #[inline]
512 pub fn regions(&self) -> impl DoubleEndedIterator<Item = ty::Region<'tcx>> {
513 self.iter().filter_map(|k| k.as_region())
514 }
515
516 #[inline]
517 pub fn consts(&self) -> impl DoubleEndedIterator<Item = ty::Const<'tcx>> {
518 self.iter().filter_map(|k| k.as_const())
519 }
520
521 #[inline]
523 pub fn non_erasable_generics(&self) -> impl DoubleEndedIterator<Item = GenericArgKind<'tcx>> {
524 self.iter().filter_map(|arg| match arg.kind() {
525 ty::GenericArgKind::Lifetime(_) => None,
526 generic => Some(generic),
527 })
528 }
529
530 #[inline]
531 #[track_caller]
532 pub fn type_at(&self, i: usize) -> Ty<'tcx> {
533 self[i].as_type().unwrap_or_else(
534 #[track_caller]
535 || crate::util::bug::bug_fmt(format_args!("expected type for param #{0} in {1:?}",
i, self))bug!("expected type for param #{} in {:?}", i, self),
536 )
537 }
538
539 #[inline]
540 #[track_caller]
541 pub fn region_at(&self, i: usize) -> ty::Region<'tcx> {
542 self[i].as_region().unwrap_or_else(
543 #[track_caller]
544 || crate::util::bug::bug_fmt(format_args!("expected region for param #{0} in {1:?}",
i, self))bug!("expected region for param #{} in {:?}", i, self),
545 )
546 }
547
548 #[inline]
549 #[track_caller]
550 pub fn const_at(&self, i: usize) -> ty::Const<'tcx> {
551 self[i].as_const().unwrap_or_else(
552 #[track_caller]
553 || crate::util::bug::bug_fmt(format_args!("expected const for param #{0} in {1:?}",
i, self))bug!("expected const for param #{} in {:?}", i, self),
554 )
555 }
556
557 #[inline]
558 #[track_caller]
559 pub fn type_for_def(&self, def: &ty::GenericParamDef) -> GenericArg<'tcx> {
560 self.type_at(def.index as usize).into()
561 }
562
563 pub fn rebase_onto(
582 &self,
583 tcx: TyCtxt<'tcx>,
584 source_ancestor: DefId,
585 target_args: GenericArgsRef<'tcx>,
586 ) -> GenericArgsRef<'tcx> {
587 let defs = tcx.generics_of(source_ancestor);
588 tcx.mk_args_from_iter(target_args.iter().chain(self.iter().skip(defs.count())))
589 }
590
591 pub fn truncate_to(&self, tcx: TyCtxt<'tcx>, generics: &ty::Generics) -> GenericArgsRef<'tcx> {
595 tcx.mk_args(&self[..generics.count()])
596 }
597
598 pub fn print_as_list(&self) -> String {
599 let v = self.iter().map(|arg| arg.to_string()).collect::<Vec<_>>();
600 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("[{0}]", v.join(", ")))
})format!("[{}]", v.join(", "))
601 }
602}
603
604impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for GenericArgsRef<'tcx> {
605 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
606 self,
607 folder: &mut F,
608 ) -> Result<Self, F::Error> {
609 match self.len() {
616 1 => {
617 let param0 = self[0].try_fold_with(folder)?;
618 if param0 == self[0] { Ok(self) } else { Ok(folder.cx().mk_args(&[param0])) }
619 }
620 2 => {
621 let param0 = self[0].try_fold_with(folder)?;
622 let param1 = self[1].try_fold_with(folder)?;
623 if param0 == self[0] && param1 == self[1] {
624 Ok(self)
625 } else {
626 Ok(folder.cx().mk_args(&[param0, param1]))
627 }
628 }
629 0 => Ok(self),
630 _ => ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_args(v)),
631 }
632 }
633
634 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
635 match self.len() {
637 1 => {
638 let param0 = self[0].fold_with(folder);
639 if param0 == self[0] { self } else { folder.cx().mk_args(&[param0]) }
640 }
641 2 => {
642 let param0 = self[0].fold_with(folder);
643 let param1 = self[1].fold_with(folder);
644 if param0 == self[0] && param1 == self[1] {
645 self
646 } else {
647 folder.cx().mk_args(&[param0, param1])
648 }
649 }
650 0 => self,
651 _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_args(v)),
652 }
653 }
654}
655
656impl<'tcx> TypeFoldable<TyCtxt<'tcx>> for &'tcx ty::List<Ty<'tcx>> {
657 fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
658 self,
659 folder: &mut F,
660 ) -> Result<Self, F::Error> {
661 match self.len() {
677 2 => {
678 let param0 = self[0].try_fold_with(folder)?;
679 let param1 = self[1].try_fold_with(folder)?;
680 if param0 == self[0] && param1 == self[1] {
681 Ok(self)
682 } else {
683 Ok(folder.cx().mk_type_list(&[param0, param1]))
684 }
685 }
686 _ => ty::util::try_fold_list(self, folder, |tcx, v| tcx.mk_type_list(v)),
687 }
688 }
689
690 fn fold_with<F: TypeFolder<TyCtxt<'tcx>>>(self, folder: &mut F) -> Self {
691 match self.len() {
693 2 => {
694 let param0 = self[0].fold_with(folder);
695 let param1 = self[1].fold_with(folder);
696 if param0 == self[0] && param1 == self[1] {
697 self
698 } else {
699 folder.cx().mk_type_list(&[param0, param1])
700 }
701 }
702 _ => ty::util::fold_list(self, folder, |tcx, v| tcx.mk_type_list(v)),
703 }
704 }
705}
706
707impl<'tcx, T: TypeVisitable<TyCtxt<'tcx>>> TypeVisitable<TyCtxt<'tcx>> for &'tcx ty::List<T> {
708 #[inline]
709 fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
710 for elem in self.iter() {
match ::rustc_ast_ir::visit::VisitorResult::branch(elem.visit_with(visitor))
{
core::ops::ControlFlow::Continue(()) =>
(),
#[allow(unreachable_code)]
core::ops::ControlFlow::Break(r) => {
return ::rustc_ast_ir::visit::VisitorResult::from_residual(r);
}
};
};walk_visitable_list!(visitor, self.iter());
711 V::Result::output()
712 }
713}
714
715#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for UserArgs<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for UserArgs<'tcx> {
#[inline]
fn clone(&self) -> UserArgs<'tcx> {
let _: ::core::clone::AssertParamIsClone<GenericArgsRef<'tcx>>;
let _: ::core::clone::AssertParamIsClone<Option<UserSelfTy<'tcx>>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UserArgs<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "UserArgs",
"args", &self.args, "user_self_ty", &&self.user_self_ty)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for UserArgs<'tcx> {
#[inline]
fn eq(&self, other: &UserArgs<'tcx>) -> bool {
self.args == other.args && self.user_self_ty == other.user_self_ty
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for UserArgs<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<GenericArgsRef<'tcx>>;
let _: ::core::cmp::AssertParamIsEq<Option<UserSelfTy<'tcx>>>;
}
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for UserArgs<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.args, state);
::core::hash::Hash::hash(&self.user_self_ty, state)
}
}Hash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for UserArgs<'tcx> {
fn encode(&self, __encoder: &mut __E) {
match *self {
UserArgs {
args: ref __binding_0, user_self_ty: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for UserArgs<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
UserArgs {
args: ::rustc_serialize::Decodable::decode(__decoder),
user_self_ty: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
718#[derive(const _: () =
{
impl<'tcx, '__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for UserArgs<'tcx> {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
UserArgs {
args: ref __binding_0, user_self_ty: ref __binding_1 } => {
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for UserArgs<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
UserArgs { args: __binding_0, user_self_ty: __binding_1 } =>
{
UserArgs {
args: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
user_self_ty: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
UserArgs { args: __binding_0, user_self_ty: __binding_1 } =>
{
UserArgs {
args: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
user_self_ty: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for UserArgs<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
UserArgs {
args: ref __binding_0, user_self_ty: ref __binding_1 } => {
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable)]
719pub struct UserArgs<'tcx> {
720 pub args: GenericArgsRef<'tcx>,
722
723 pub user_self_ty: Option<UserSelfTy<'tcx>>,
726}
727
728#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for UserSelfTy<'tcx> { }Copy, #[automatically_derived]
impl<'tcx> ::core::clone::Clone for UserSelfTy<'tcx> {
#[inline]
fn clone(&self) -> UserSelfTy<'tcx> {
let _: ::core::clone::AssertParamIsClone<DefId>;
let _: ::core::clone::AssertParamIsClone<Ty<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UserSelfTy<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "UserSelfTy",
"impl_def_id", &self.impl_def_id, "self_ty", &&self.self_ty)
}
}Debug, #[automatically_derived]
impl<'tcx> ::core::cmp::PartialEq for UserSelfTy<'tcx> {
#[inline]
fn eq(&self, other: &UserSelfTy<'tcx>) -> bool {
self.impl_def_id == other.impl_def_id && self.self_ty == other.self_ty
}
}PartialEq, #[automatically_derived]
impl<'tcx> ::core::cmp::Eq for UserSelfTy<'tcx> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<DefId>;
let _: ::core::cmp::AssertParamIsEq<Ty<'tcx>>;
}
}Eq, #[automatically_derived]
impl<'tcx> ::core::hash::Hash for UserSelfTy<'tcx> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.impl_def_id, state);
::core::hash::Hash::hash(&self.self_ty, state)
}
}Hash, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for UserSelfTy<'tcx> {
fn encode(&self, __encoder: &mut __E) {
match *self {
UserSelfTy {
impl_def_id: ref __binding_0, self_ty: ref __binding_1 } =>
{
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for UserSelfTy<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
UserSelfTy {
impl_def_id: ::rustc_serialize::Decodable::decode(__decoder),
self_ty: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable)]
745#[derive(const _: () =
{
impl<'tcx, '__ctx>
::rustc_data_structures::stable_hasher::HashStable<::rustc_middle::ich::StableHashingContext<'__ctx>>
for UserSelfTy<'tcx> {
#[inline]
fn hash_stable(&self,
__hcx: &mut ::rustc_middle::ich::StableHashingContext<'__ctx>,
__hasher:
&mut ::rustc_data_structures::stable_hasher::StableHasher) {
match *self {
UserSelfTy {
impl_def_id: ref __binding_0, self_ty: ref __binding_1 } =>
{
{ __binding_0.hash_stable(__hcx, __hasher); }
{ __binding_1.hash_stable(__hcx, __hasher); }
}
}
}
}
};HashStable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeFoldable<::rustc_middle::ty::TyCtxt<'tcx>>
for UserSelfTy<'tcx> {
fn try_fold_with<__F: ::rustc_middle::ty::FallibleTypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Result<Self, __F::Error> {
Ok(match self {
UserSelfTy { impl_def_id: __binding_0, self_ty: __binding_1
} => {
UserSelfTy {
impl_def_id: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_0,
__folder)?,
self_ty: ::rustc_middle::ty::TypeFoldable::try_fold_with(__binding_1,
__folder)?,
}
}
})
}
fn fold_with<__F: ::rustc_middle::ty::TypeFolder<::rustc_middle::ty::TyCtxt<'tcx>>>(self,
__folder: &mut __F) -> Self {
match self {
UserSelfTy { impl_def_id: __binding_0, self_ty: __binding_1
} => {
UserSelfTy {
impl_def_id: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_0,
__folder),
self_ty: ::rustc_middle::ty::TypeFoldable::fold_with(__binding_1,
__folder),
}
}
}
}
}
};TypeFoldable, const _: () =
{
impl<'tcx>
::rustc_middle::ty::TypeVisitable<::rustc_middle::ty::TyCtxt<'tcx>>
for UserSelfTy<'tcx> {
fn visit_with<__V: ::rustc_middle::ty::TypeVisitor<::rustc_middle::ty::TyCtxt<'tcx>>>(&self,
__visitor: &mut __V) -> __V::Result {
match *self {
UserSelfTy {
impl_def_id: ref __binding_0, self_ty: ref __binding_1 } =>
{
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_0,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
{
match ::rustc_middle::ty::VisitorResult::branch(::rustc_middle::ty::TypeVisitable::visit_with(__binding_1,
__visitor)) {
::core::ops::ControlFlow::Continue(()) => {}
::core::ops::ControlFlow::Break(r) => {
return ::rustc_middle::ty::VisitorResult::from_residual(r);
}
}
}
}
}
<__V::Result as ::rustc_middle::ty::VisitorResult>::output()
}
}
};TypeVisitable)]
746pub struct UserSelfTy<'tcx> {
747 pub impl_def_id: DefId,
748 pub self_ty: Ty<'tcx>,
749}