1use std::ops::ControlFlow;
2
3use rustc_ast as ast;
4use rustc_data_structures::fx::FxHashMap;
5use rustc_hir::def_id::DefId;
6use rustc_macros::{StableHash, TyDecodable, TyEncodable};
7use rustc_span::{Span, Symbol, kw};
8use rustc_type_ir::{TypeSuperVisitable as _, TypeVisitable, TypeVisitor};
9use tracing::instrument;
10
11use super::{Clause, InstantiatedClauses, ParamConst, ParamTy, Ty, TyCtxt, Unnormalized};
12use crate::ty::{self, ClauseKind, EarlyBinder, GenericArgsRef, Region, RegionKind, TyKind};
13
14#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericParamDefKind {
#[inline]
fn clone(&self) -> GenericParamDefKind {
match self {
GenericParamDefKind::Lifetime => GenericParamDefKind::Lifetime,
GenericParamDefKind::Type {
has_default: __self_0, synthetic: __self_1 } =>
GenericParamDefKind::Type {
has_default: ::core::clone::Clone::clone(__self_0),
synthetic: ::core::clone::Clone::clone(__self_1),
},
GenericParamDefKind::Const { has_default: __self_0 } =>
GenericParamDefKind::Const {
has_default: ::core::clone::Clone::clone(__self_0),
},
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamDefKind {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
GenericParamDefKind::Lifetime =>
::core::fmt::Formatter::write_str(f, "Lifetime"),
GenericParamDefKind::Type {
has_default: __self_0, synthetic: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f, "Type",
"has_default", __self_0, "synthetic", &__self_1),
GenericParamDefKind::Const { has_default: __self_0 } =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Const",
"has_default", &__self_0),
}
}
}Debug, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for GenericParamDefKind {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
GenericParamDefKind::Lifetime => { 0usize }
GenericParamDefKind::Type {
has_default: ref __binding_0, synthetic: ref __binding_1 }
=> {
1usize
}
GenericParamDefKind::Const { has_default: ref __binding_0 }
=> {
2usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Type {
has_default: ref __binding_0, synthetic: ref __binding_1 }
=> {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
GenericParamDefKind::Const { has_default: ref __binding_0 }
=> {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for GenericParamDefKind {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { GenericParamDefKind::Lifetime }
1usize => {
GenericParamDefKind::Type {
has_default: ::rustc_serialize::Decodable::decode(__decoder),
synthetic: ::rustc_serialize::Decodable::decode(__decoder),
}
}
2usize => {
GenericParamDefKind::Const {
has_default: ::rustc_serialize::Decodable::decode(__decoder),
}
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `GenericParamDefKind`, expected 0..3, actual {0}",
n));
}
}
}
}
};TyDecodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for
GenericParamDefKind {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
::std::mem::discriminant(self).stable_hash(__hcx, __hasher);
match *self {
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Type {
has_default: ref __binding_0, synthetic: ref __binding_1 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
GenericParamDefKind::Const { has_default: ref __binding_0 }
=> {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
15pub enum GenericParamDefKind {
16 Lifetime,
17 Type { has_default: bool, synthetic: bool },
18 Const { has_default: bool },
19}
20
21impl GenericParamDefKind {
22 pub fn descr(&self) -> &'static str {
23 match self {
24 GenericParamDefKind::Lifetime => "lifetime",
25 GenericParamDefKind::Type { .. } => "type",
26 GenericParamDefKind::Const { .. } => "constant",
27 }
28 }
29 pub fn to_ord(&self) -> ast::ParamKindOrd {
30 match self {
31 GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
32 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
33 ast::ParamKindOrd::TypeOrConst
34 }
35 }
36 }
37
38 pub fn is_ty_or_const(&self) -> bool {
39 match self {
40 GenericParamDefKind::Lifetime => false,
41 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => true,
42 }
43 }
44
45 pub fn is_synthetic(&self) -> bool {
46 match self {
47 GenericParamDefKind::Type { synthetic, .. } => *synthetic,
48 _ => false,
49 }
50 }
51}
52
53#[derive(#[automatically_derived]
impl ::core::clone::Clone for GenericParamDef {
#[inline]
fn clone(&self) -> GenericParamDef {
GenericParamDef {
name: ::core::clone::Clone::clone(&self.name),
def_id: ::core::clone::Clone::clone(&self.def_id),
index: ::core::clone::Clone::clone(&self.index),
pure_wrt_drop: ::core::clone::Clone::clone(&self.pure_wrt_drop),
kind: ::core::clone::Clone::clone(&self.kind),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for GenericParamDef {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f,
"GenericParamDef", "name", &self.name, "def_id", &self.def_id,
"index", &self.index, "pure_wrt_drop", &self.pure_wrt_drop,
"kind", &&self.kind)
}
}Debug, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for GenericParamDef {
fn encode(&self, __encoder: &mut __E) {
let GenericParamDef {
name: ref __binding_0,
def_id: ref __binding_1,
index: ref __binding_2,
pure_wrt_drop: ref __binding_3,
kind: ref __binding_4 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_4,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for GenericParamDef {
fn decode(__decoder: &mut __D) -> Self {
GenericParamDef {
name: ::rustc_serialize::Decodable::decode(__decoder),
def_id: ::rustc_serialize::Decodable::decode(__decoder),
index: ::rustc_serialize::Decodable::decode(__decoder),
pure_wrt_drop: ::rustc_serialize::Decodable::decode(__decoder),
kind: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for
GenericParamDef {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
GenericParamDef {
name: ref __binding_0,
def_id: ref __binding_1,
index: ref __binding_2,
pure_wrt_drop: ref __binding_3,
kind: ref __binding_4 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
{ __binding_2.stable_hash(__hcx, __hasher); }
{ __binding_3.stable_hash(__hcx, __hasher); }
{ __binding_4.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
54pub struct GenericParamDef {
55 pub name: Symbol,
56 pub def_id: DefId,
57 pub index: u32,
58
59 pub pure_wrt_drop: bool,
63
64 pub kind: GenericParamDefKind,
65}
66
67impl GenericParamDef {
68 pub fn to_early_bound_region_data(&self) -> ty::EarlyParamRegion {
69 if let GenericParamDefKind::Lifetime = self.kind {
70 ty::EarlyParamRegion { index: self.index, name: self.name }
71 } else {
72 crate::util::bug::bug_fmt(format_args!("cannot convert a non-lifetime parameter def to an early bound region"))bug!("cannot convert a non-lifetime parameter def to an early bound region")
73 }
74 }
75
76 pub fn is_anonymous_lifetime(&self) -> bool {
77 match self.kind {
78 GenericParamDefKind::Lifetime => self.name == kw::UnderscoreLifetime,
79 _ => false,
80 }
81 }
82
83 pub fn default_value<'tcx>(
84 &self,
85 tcx: TyCtxt<'tcx>,
86 ) -> Option<EarlyBinder<'tcx, ty::GenericArg<'tcx>>> {
87 match self.kind {
88 GenericParamDefKind::Type { has_default: true, .. } => {
89 Some(tcx.type_of(self.def_id).map_bound(|t| t.into()))
90 }
91 GenericParamDefKind::Const { has_default: true, .. } => {
92 Some(tcx.const_param_default(self.def_id).map_bound(|c| c.into()))
93 }
94 _ => None,
95 }
96 }
97
98 pub fn to_error<'tcx>(&self, tcx: TyCtxt<'tcx>) -> ty::GenericArg<'tcx> {
99 match &self.kind {
100 ty::GenericParamDefKind::Lifetime => ty::Region::new_error_misc(tcx).into(),
101 ty::GenericParamDefKind::Type { .. } => Ty::new_misc_error(tcx).into(),
102 ty::GenericParamDefKind::Const { .. } => ty::Const::new_misc_error(tcx).into(),
103 }
104 }
105}
106
107#[derive(#[automatically_derived]
impl ::core::default::Default for GenericParamCount {
#[inline]
fn default() -> GenericParamCount {
GenericParamCount {
lifetimes: ::core::default::Default::default(),
types: ::core::default::Default::default(),
consts: ::core::default::Default::default(),
}
}
}Default)]
108pub struct GenericParamCount {
109 pub lifetimes: usize,
110 pub types: usize,
111 pub consts: usize,
112}
113
114#[derive(#[automatically_derived]
impl ::core::clone::Clone for Generics {
#[inline]
fn clone(&self) -> Generics {
Generics {
parent: ::core::clone::Clone::clone(&self.parent),
parent_count: ::core::clone::Clone::clone(&self.parent_count),
own_params: ::core::clone::Clone::clone(&self.own_params),
param_def_id_to_index: ::core::clone::Clone::clone(&self.param_def_id_to_index),
has_self: ::core::clone::Clone::clone(&self.has_self),
has_late_bound_regions: ::core::clone::Clone::clone(&self.has_late_bound_regions),
}
}
}Clone, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for Generics {
fn encode(&self, __encoder: &mut __E) {
let Generics {
parent: ref __binding_0,
parent_count: ref __binding_1,
own_params: ref __binding_2,
param_def_id_to_index: ref __binding_3,
has_self: ref __binding_4,
has_late_bound_regions: ref __binding_5 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_4,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_5,
__encoder);
}
}
};TyEncodable, const _: () =
{
impl<'tcx, __D: ::rustc_middle::ty::codec::TyDecoder<'tcx>>
::rustc_serialize::Decodable<__D> for Generics {
fn decode(__decoder: &mut __D) -> Self {
Generics {
parent: ::rustc_serialize::Decodable::decode(__decoder),
parent_count: ::rustc_serialize::Decodable::decode(__decoder),
own_params: ::rustc_serialize::Decodable::decode(__decoder),
param_def_id_to_index: ::rustc_serialize::Decodable::decode(__decoder),
has_self: ::rustc_serialize::Decodable::decode(__decoder),
has_late_bound_regions: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};TyDecodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for Generics {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Generics {
parent: ref __binding_0,
parent_count: ref __binding_1,
own_params: ref __binding_2,
param_def_id_to_index: ref __binding_3,
has_self: ref __binding_4,
has_late_bound_regions: ref __binding_5 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
{ __binding_2.stable_hash(__hcx, __hasher); }
{}
{ __binding_4.stable_hash(__hcx, __hasher); }
{ __binding_5.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
120pub struct Generics {
121 pub parent: Option<DefId>,
122 pub parent_count: usize,
123 pub own_params: Vec<GenericParamDef>,
124
125 #[stable_hash(ignore)]
127 pub param_def_id_to_index: FxHashMap<DefId, u32>,
128
129 pub has_self: bool,
130 pub has_late_bound_regions: Option<Span>,
131}
132
133impl std::fmt::Debug for Generics {
134 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
135 #[expect(rustc::potential_query_instability)]
137 let mut stabilized_hashmap = self.param_def_id_to_index.iter().collect::<Vec<_>>();
138 stabilized_hashmap.sort_by_key(|(_, v)| **v);
139 f.debug_struct("Generics")
140 .field("parent", &self.parent)
141 .field("parent_count", &self.parent_count)
142 .field("own_params", &self.own_params)
143 .field("param_def_id_to_index", &stabilized_hashmap)
144 .field("has_self", &self.has_self)
145 .field("has_late_bound_regions", &self.has_late_bound_regions)
146 .finish()
147 }
148}
149
150impl<'tcx> rustc_type_ir::inherent::GenericsOf<TyCtxt<'tcx>> for &'tcx Generics {
151 fn count(&self) -> usize {
152 self.parent_count + self.own_params.len()
153 }
154 fn param_region_def_id(self, tcx: TyCtxt<'tcx>, ebr: ty::EarlyParamRegion) -> DefId {
155 self.region_param(ebr, tcx).def_id
156 }
157}
158
159impl<'tcx> Generics {
160 pub fn param_def_id_to_index(&self, tcx: TyCtxt<'tcx>, def_id: DefId) -> Option<u32> {
165 if let Some(idx) = self.param_def_id_to_index.get(&def_id) {
166 Some(*idx)
167 } else if let Some(parent) = self.parent {
168 let parent = tcx.generics_of(parent);
169 parent.param_def_id_to_index(tcx, def_id)
170 } else {
171 None
172 }
173 }
174
175 #[inline]
176 pub fn count(&self) -> usize {
177 self.parent_count + self.own_params.len()
178 }
179
180 pub fn own_counts(&self) -> GenericParamCount {
181 let mut own_counts = GenericParamCount::default();
185
186 for param in &self.own_params {
187 match param.kind {
188 GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
189 GenericParamDefKind::Type { .. } => own_counts.types += 1,
190 GenericParamDefKind::Const { .. } => own_counts.consts += 1,
191 }
192 }
193
194 own_counts
195 }
196
197 pub fn own_defaults(&self) -> GenericParamCount {
198 let mut own_defaults = GenericParamCount::default();
199
200 for param in &self.own_params {
201 match param.kind {
202 GenericParamDefKind::Lifetime => (),
203 GenericParamDefKind::Type { has_default, .. } => {
204 own_defaults.types += has_default as usize;
205 }
206 GenericParamDefKind::Const { has_default, .. } => {
207 own_defaults.consts += has_default as usize;
208 }
209 }
210 }
211
212 own_defaults
213 }
214
215 pub fn requires_monomorphization(&self, tcx: TyCtxt<'tcx>) -> bool {
216 if self.own_requires_monomorphization() {
217 return true;
218 }
219
220 if let Some(parent_def_id) = self.parent {
221 let parent = tcx.generics_of(parent_def_id);
222 parent.requires_monomorphization(tcx)
223 } else {
224 false
225 }
226 }
227
228 pub fn own_requires_monomorphization(&self) -> bool {
229 for param in &self.own_params {
230 match param.kind {
231 GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
232 return true;
233 }
234 GenericParamDefKind::Lifetime => {}
235 }
236 }
237 false
238 }
239
240 pub fn param_at(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
242 if let Some(index) = param_index.checked_sub(self.parent_count) {
243 &self.own_params[index]
244 } else {
245 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
246 .param_at(param_index, tcx)
247 }
248 }
249
250 pub fn params_to(&'tcx self, param_index: usize, tcx: TyCtxt<'tcx>) -> &'tcx [GenericParamDef] {
251 if let Some(index) = param_index.checked_sub(self.parent_count) {
252 &self.own_params[..index]
253 } else {
254 tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
255 .params_to(param_index, tcx)
256 }
257 }
258
259 pub fn region_param(
261 &'tcx self,
262 param: ty::EarlyParamRegion,
263 tcx: TyCtxt<'tcx>,
264 ) -> &'tcx GenericParamDef {
265 let param = self.param_at(param.index as usize, tcx);
266 match param.kind {
267 GenericParamDefKind::Lifetime => param,
268 _ => {
269 crate::util::bug::bug_fmt(format_args!("expected lifetime parameter, but found another generic parameter: {0:#?}",
param))bug!("expected lifetime parameter, but found another generic parameter: {param:#?}")
270 }
271 }
272 }
273
274 pub fn type_param(&'tcx self, param: ParamTy, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
276 let param = self.param_at(param.index as usize, tcx);
277 match param.kind {
278 GenericParamDefKind::Type { .. } => param,
279 _ => crate::util::bug::bug_fmt(format_args!("expected type parameter, but found another generic parameter: {0:#?}",
param))bug!("expected type parameter, but found another generic parameter: {param:#?}"),
280 }
281 }
282
283 pub fn const_param(&'tcx self, param: ParamConst, tcx: TyCtxt<'tcx>) -> &'tcx GenericParamDef {
285 let param = self.param_at(param.index as usize, tcx);
286 match param.kind {
287 GenericParamDefKind::Const { .. } => param,
288 _ => crate::util::bug::bug_fmt(format_args!("expected const parameter, but found another generic parameter: {0:#?}",
param))bug!("expected const parameter, but found another generic parameter: {param:#?}"),
289 }
290 }
291
292 pub fn has_impl_trait(&'tcx self) -> bool {
294 self.own_params.iter().any(|param| {
295 #[allow(non_exhaustive_omitted_patterns)] match param.kind {
ty::GenericParamDefKind::Type { synthetic: true, .. } => true,
_ => false,
}matches!(param.kind, ty::GenericParamDefKind::Type { synthetic: true, .. })
296 })
297 }
298
299 pub fn own_synthetic_params_count(&'tcx self) -> usize {
300 self.own_params.iter().filter(|p| p.kind.is_synthetic()).count()
301 }
302
303 pub fn own_args_no_defaults<'a>(
308 &'tcx self,
309 tcx: TyCtxt<'tcx>,
310 args: &'a [ty::GenericArg<'tcx>],
311 ) -> &'a [ty::GenericArg<'tcx>] {
312 let mut own_params = self.parent_count..self.count();
313 if self.has_own_self() {
314 own_params.start = 1;
315 }
316
317 own_params.end -= self
324 .own_params
325 .iter()
326 .rev()
327 .take_while(|param| {
328 param.default_value(tcx).is_some_and(|default| {
329 default.instantiate(tcx, args).skip_norm_wip() == args[param.index as usize]
330 })
331 })
332 .count();
333
334 &args[own_params]
335 }
336
337 pub fn own_args(
341 &'tcx self,
342 args: &'tcx [ty::GenericArg<'tcx>],
343 ) -> &'tcx [ty::GenericArg<'tcx>] {
344 let own = &args[self.parent_count..][..self.own_params.len()];
345 if self.has_own_self() { &own[1..] } else { own }
346 }
347
348 pub fn check_concrete_type_after_default(
353 &'tcx self,
354 tcx: TyCtxt<'tcx>,
355 args: &'tcx [ty::GenericArg<'tcx>],
356 ) -> bool {
357 let mut default_param_seen = false;
358 for param in self.own_params.iter() {
359 if let Some(inst) = param
360 .default_value(tcx)
361 .map(|default| default.instantiate(tcx, args).skip_norm_wip())
362 {
363 if inst == args[param.index as usize] {
364 default_param_seen = true;
365 } else if default_param_seen {
366 return true;
367 }
368 }
369 }
370 false
371 }
372
373 pub fn is_empty(&'tcx self) -> bool {
374 self.count() == 0
375 }
376
377 pub fn is_own_empty(&'tcx self) -> bool {
378 self.own_params.is_empty()
379 }
380
381 pub fn has_own_self(&'tcx self) -> bool {
382 self.has_self && self.parent.is_none()
383 }
384}
385
386#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for GenericClauses<'tcx> { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for GenericClauses<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::clone::Clone for GenericClauses<'tcx> {
#[inline]
fn clone(&self) -> GenericClauses<'tcx> {
let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
let _:
::core::clone::AssertParamIsClone<&'tcx [(Clause<'tcx>,
Span)]>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for GenericClauses<'tcx> {
#[inline]
fn default() -> GenericClauses<'tcx> {
GenericClauses {
parent: ::core::default::Default::default(),
clauses: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for GenericClauses<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"GenericClauses", "parent", &self.parent, "clauses",
&&self.clauses)
}
}Debug, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for GenericClauses<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let GenericClauses {
parent: ref __binding_0, clauses: __binding_1 } = *self;
::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 GenericClauses<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
GenericClauses {
parent: ::rustc_serialize::Decodable::decode(__decoder),
clauses: ::rustc_middle::ty::codec::RefDecodable::decode(__decoder),
}
}
}
};TyDecodable, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
GenericClauses<'tcx> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
GenericClauses {
parent: ref __binding_0, clauses: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
388pub struct GenericClauses<'tcx> {
389 pub parent: Option<DefId>,
390 pub clauses: &'tcx [(Clause<'tcx>, Span)],
391}
392
393impl<'tcx> GenericClauses<'tcx> {
394 pub fn instantiate(
395 self,
396 tcx: TyCtxt<'tcx>,
397 args: GenericArgsRef<'tcx>,
398 ) -> InstantiatedClauses<'tcx> {
399 let mut instantiated = InstantiatedClauses::empty();
400 self.instantiate_into(tcx, &mut instantiated, args);
401 instantiated
402 }
403
404 pub fn instantiate_own(
405 self,
406 tcx: TyCtxt<'tcx>,
407 args: GenericArgsRef<'tcx>,
408 ) -> impl Iterator<Item = (Unnormalized<'tcx, Clause<'tcx>>, Span)>
409 + DoubleEndedIterator
410 + ExactSizeIterator
411 + Clone {
412 EarlyBinder::bind_iter(self.clauses).iter_instantiated_copied(tcx, args).map(|u| {
413 let (clause, span) = u.unzip();
414 (clause, span.skip_normalization())
415 })
416 }
417
418 pub fn instantiate_own_identity(
419 self,
420 ) -> impl Iterator<Item = (Unnormalized<'tcx, Clause<'tcx>>, Span)>
421 + DoubleEndedIterator
422 + ExactSizeIterator
423 + Clone {
424 EarlyBinder::bind_iter(self.clauses).iter_identity_copied().map(|u| {
425 let (clause, span) = u.unzip();
426 (clause, span.skip_normalization())
427 })
428 }
429
430 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("instantiate_into",
"rustc_middle::ty::generics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4aa1fbcf467cf38ce58abfa8eb9213a789c5381c/compiler/rustc_middle/src/ty/generics.rs"),
::tracing_core::__macro_support::Option::Some(430u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::generics"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("instantiated")
}> =
::tracing::__macro_support::FieldName::new("instantiated");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("args")
}> =
::tracing::__macro_support::FieldName::new("args");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instantiated)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&args)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if let Some(def_id) = self.parent {
tcx.clauses_of(def_id).instantiate_into(tcx, instantiated,
args);
}
instantiated.clauses.extend(self.clauses.iter().map(|(p, _)|
EarlyBinder::bind(tcx, *p).instantiate(tcx, args)));
instantiated.spans.extend(self.clauses.iter().map(|(_, sp)| *sp));
}
}
}#[instrument(level = "debug", skip(self, tcx))]
431 fn instantiate_into(
432 self,
433 tcx: TyCtxt<'tcx>,
434 instantiated: &mut InstantiatedClauses<'tcx>,
435 args: GenericArgsRef<'tcx>,
436 ) {
437 if let Some(def_id) = self.parent {
438 tcx.clauses_of(def_id).instantiate_into(tcx, instantiated, args);
439 }
440 instantiated.clauses.extend(
441 self.clauses.iter().map(|(p, _)| EarlyBinder::bind(tcx, *p).instantiate(tcx, args)),
442 );
443 instantiated.spans.extend(self.clauses.iter().map(|(_, sp)| *sp));
444 }
445
446 pub fn instantiate_identity(self, tcx: TyCtxt<'tcx>) -> InstantiatedClauses<'tcx> {
447 let mut instantiated = InstantiatedClauses::empty();
448 self.instantiate_identity_into(tcx, &mut instantiated);
449 instantiated
450 }
451
452 fn instantiate_identity_into(
453 self,
454 tcx: TyCtxt<'tcx>,
455 instantiated: &mut InstantiatedClauses<'tcx>,
456 ) {
457 if let Some(def_id) = self.parent {
458 tcx.clauses_of(def_id).instantiate_identity_into(tcx, instantiated);
459 }
460 instantiated.clauses.extend(self.clauses.iter().map(|(p, _)| Unnormalized::new(*p)));
461 instantiated.spans.extend(self.clauses.iter().map(|(_, s)| s));
462 }
463
464 pub fn is_fully_generic_for_reflection(self) -> bool {
473 struct ParamChecker;
474 impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ParamChecker {
475 type Result = ControlFlow<()>;
476 fn visit_region(&mut self, r: Region<'tcx>) -> Self::Result {
477 match r.kind() {
478 RegionKind::ReEarlyParam(_) | RegionKind::ReStatic | RegionKind::ReError(_) => {
479 ControlFlow::Break(())
480 }
481 RegionKind::ReVar(_)
482 | RegionKind::RePlaceholder(_)
483 | RegionKind::ReErased
484 | RegionKind::ReLateParam(_) => {
485 crate::util::bug::bug_fmt(format_args!("unexpected lifetime in impl: {0:?}",
r))bug!("unexpected lifetime in impl: {r:?}")
486 }
487 RegionKind::ReBound(..) => ControlFlow::Continue(()),
488 }
489 }
490
491 fn visit_ty(&mut self, t: Ty<'tcx>) -> Self::Result {
492 match t.kind() {
493 TyKind::Param(_p) => {
494 return ControlFlow::Break(());
496 }
497 TyKind::Alias(..) => return ControlFlow::Break(()),
498 _ => (),
499 }
500 t.super_visit_with(self)
501 }
502 }
503
504 self.clauses.iter().all(|(clause, _)| {
507 match clause.kind().skip_binder() {
508 ClauseKind::Trait(trait_predicate) => {
509 if #[allow(non_exhaustive_omitted_patterns)] match trait_predicate.self_ty().kind()
{
ty::Param(_) => true,
_ => false,
}matches!(trait_predicate.self_ty().kind(), ty::Param(_))
514 && trait_predicate.trait_ref.args[1..]
515 .iter()
516 .all(|arg| arg.visit_with(&mut ParamChecker).is_continue())
517 {
518 return true;
519 }
520 }
521 ClauseKind::RegionOutlives(_)
522 | ClauseKind::TypeOutlives(_)
523 | ClauseKind::Projection(_)
524 | ClauseKind::ConstArgHasType(_, _)
525 | ClauseKind::WellFormed(_)
526 | ClauseKind::ConstEvaluatable(_)
527 | ClauseKind::HostEffect(_)
528 | ClauseKind::UnstableFeature(_) => {}
529 }
530 clause.visit_with(&mut ParamChecker).is_continue()
531 })
532 }
533}
534
535#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for ConstConditions<'tcx> { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for ConstConditions<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::clone::Clone for ConstConditions<'tcx> {
#[inline]
fn clone(&self) -> ConstConditions<'tcx> {
let _: ::core::clone::AssertParamIsClone<Option<DefId>>;
let _:
::core::clone::AssertParamIsClone<&'tcx [(ty::PolyTraitRef<'tcx>,
Span)]>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::default::Default for ConstConditions<'tcx> {
#[inline]
fn default() -> ConstConditions<'tcx> {
ConstConditions {
parent: ::core::default::Default::default(),
clauses: ::core::default::Default::default(),
}
}
}Default, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for ConstConditions<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"ConstConditions", "parent", &self.parent, "clauses",
&&self.clauses)
}
}Debug, const _: () =
{
impl<'tcx, __E: ::rustc_middle::ty::codec::TyEncoder<'tcx>>
::rustc_serialize::Encodable<__E> for ConstConditions<'tcx> {
fn encode(&self, __encoder: &mut __E) {
let ConstConditions {
parent: ref __binding_0, clauses: __binding_1 } = *self;
::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 ConstConditions<'tcx> {
fn decode(__decoder: &mut __D) -> Self {
ConstConditions {
parent: ::rustc_serialize::Decodable::decode(__decoder),
clauses: ::rustc_middle::ty::codec::RefDecodable::decode(__decoder),
}
}
}
};TyDecodable, const _: () =
{
impl<'tcx> ::rustc_data_structures::stable_hash::StableHash for
ConstConditions<'tcx> {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
ConstConditions {
parent: ref __binding_0, clauses: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
540pub struct ConstConditions<'tcx> {
541 pub parent: Option<DefId>,
542 pub clauses: &'tcx [(ty::PolyTraitRef<'tcx>, Span)],
543}
544
545impl<'tcx> ConstConditions<'tcx> {
546 pub fn instantiate(
547 self,
548 tcx: TyCtxt<'tcx>,
549 args: GenericArgsRef<'tcx>,
550 ) -> Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)> {
551 let mut instantiated = ::alloc::vec::Vec::new()vec![];
552 self.instantiate_into(tcx, &mut instantiated, args);
553 instantiated
554 }
555
556 pub fn instantiate_own(
557 self,
558 tcx: TyCtxt<'tcx>,
559 args: GenericArgsRef<'tcx>,
560 ) -> impl Iterator<Item = (Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>
561 + DoubleEndedIterator
562 + ExactSizeIterator
563 + Clone {
564 EarlyBinder::bind_iter(self.clauses).iter_instantiated_copied(tcx, args).map(|u| {
565 let (trait_ref, span) = u.unzip();
566 (trait_ref, span.skip_normalization())
567 })
568 }
569
570 pub fn instantiate_own_identity(
571 self,
572 ) -> impl Iterator<Item = (Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>
573 + DoubleEndedIterator
574 + ExactSizeIterator
575 + Clone {
576 EarlyBinder::bind_iter(self.clauses).iter_identity_copied().map(|u| {
577 let (trait_ref, span) = u.unzip();
578 (trait_ref, span.skip_normalization())
579 })
580 }
581
582 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("instantiate_into",
"rustc_middle::ty::generics", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/4aa1fbcf467cf38ce58abfa8eb9213a789c5381c/compiler/rustc_middle/src/ty/generics.rs"),
::tracing_core::__macro_support::Option::Some(582u32),
::tracing_core::__macro_support::Option::Some("rustc_middle::ty::generics"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("instantiated")
}> =
::tracing::__macro_support::FieldName::new("instantiated");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("args")
}> =
::tracing::__macro_support::FieldName::new("args");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&instantiated)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&args)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
if let Some(def_id) = self.parent {
tcx.const_conditions(def_id).instantiate_into(tcx,
instantiated, args);
}
instantiated.extend(self.clauses.iter().map(|&(c, s)|
(EarlyBinder::bind(tcx, c).instantiate(tcx, args), s)));
}
}
}#[instrument(level = "debug", skip(self, tcx))]
583 fn instantiate_into(
584 self,
585 tcx: TyCtxt<'tcx>,
586 instantiated: &mut Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>,
587 args: GenericArgsRef<'tcx>,
588 ) {
589 if let Some(def_id) = self.parent {
590 tcx.const_conditions(def_id).instantiate_into(tcx, instantiated, args);
591 }
592 instantiated.extend(
593 self.clauses
594 .iter()
595 .map(|&(c, s)| (EarlyBinder::bind(tcx, c).instantiate(tcx, args), s)),
596 );
597 }
598
599 pub fn instantiate_identity(
600 self,
601 tcx: TyCtxt<'tcx>,
602 ) -> Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)> {
603 let mut instantiated = ::alloc::vec::Vec::new()vec![];
604 self.instantiate_identity_into(tcx, &mut instantiated);
605 instantiated
606 }
607
608 fn instantiate_identity_into(
609 self,
610 tcx: TyCtxt<'tcx>,
611 instantiated: &mut Vec<(Unnormalized<'tcx, ty::PolyTraitRef<'tcx>>, Span)>,
612 ) {
613 if let Some(def_id) = self.parent {
614 tcx.const_conditions(def_id).instantiate_identity_into(tcx, instantiated);
615 }
616 instantiated.extend(
617 self.clauses
618 .iter()
619 .copied()
620 .map(|(trait_ref, span)| (Unnormalized::new(trait_ref), span)),
621 );
622 }
623}