]>
Commit | Line | Data |
---|---|---|
dfeec247 | 1 | use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags; |
dfeec247 | 2 | use crate::ty::print::{FmtPrinter, Printer}; |
9ffffee4 FG |
3 | use crate::ty::{self, Ty, TyCtxt, TypeFoldable, TypeSuperFoldable}; |
4 | use crate::ty::{EarlyBinder, InternalSubsts, SubstsRef, TypeVisitableExt}; | |
5e7ed085 | 5 | use rustc_errors::ErrorGuaranteed; |
dfeec247 XL |
6 | use rustc_hir::def::Namespace; |
7 | use rustc_hir::def_id::{CrateNum, DefId}; | |
3dfed10e | 8 | use rustc_hir::lang_items::LangItem; |
9c376795 | 9 | use rustc_index::bit_set::FiniteBitSet; |
532ac7d7 | 10 | use rustc_macros::HashStable; |
a2a8927a | 11 | use rustc_middle::ty::normalize_erasing_regions::NormalizationError; |
04454e1e | 12 | use rustc_span::Symbol; |
cc61c64b XL |
13 | |
14 | use std::fmt; | |
15 | ||
f035d41b XL |
16 | /// A monomorphized `InstanceDef`. |
17 | /// | |
18 | /// Monomorphization happens on-the-fly and no monomorphized MIR is ever created. Instead, this type | |
19 | /// simply couples a potentially generic `InstanceDef` with some substs, and codegen and const eval | |
20 | /// will do all required substitution as they run. | |
3dfed10e | 21 | #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, TyEncodable, TyDecodable)] |
f2b60f7d | 22 | #[derive(HashStable, Lift, TypeFoldable, TypeVisitable)] |
cc61c64b XL |
23 | pub struct Instance<'tcx> { |
24 | pub def: InstanceDef<'tcx>, | |
532ac7d7 | 25 | pub substs: SubstsRef<'tcx>, |
cc61c64b XL |
26 | } |
27 | ||
29967ef6 | 28 | #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)] |
f2b60f7d | 29 | #[derive(TyEncodable, TyDecodable, HashStable, TypeFoldable, TypeVisitable, Lift)] |
cc61c64b | 30 | pub enum InstanceDef<'tcx> { |
f035d41b XL |
31 | /// A user-defined callable item. |
32 | /// | |
33 | /// This includes: | |
34 | /// - `fn` items | |
35 | /// - closures | |
36 | /// - generators | |
3dfed10e | 37 | Item(ty::WithOptConstParam<DefId>), |
f035d41b XL |
38 | |
39 | /// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI). | |
40 | /// | |
41 | /// Alongside `Virtual`, this is the only `InstanceDef` that does not have its own callable MIR. | |
42 | /// Instead, codegen and const eval "magically" evaluate calls to intrinsics purely in the | |
43 | /// caller. | |
cc61c64b | 44 | Intrinsic(DefId), |
3b2f2976 | 45 | |
f035d41b XL |
46 | /// `<T as Trait>::method` where `method` receives unsizeable `self: Self` (part of the |
47 | /// `unsized_locals` feature). | |
48 | /// | |
49 | /// The generated shim will take `Self` via `*mut Self` - conceptually this is `&owned Self` - | |
50 | /// and dereference the argument to call the original function. | |
064997fb | 51 | VTableShim(DefId), |
a1dfa0c6 | 52 | |
e74abb32 XL |
53 | /// `fn()` pointer where the function itself cannot be turned into a pointer. |
54 | /// | |
60c5eb7d XL |
55 | /// One example is `<dyn Trait as Trait>::fn`, where the shim contains |
56 | /// a virtual call, which codegen supports only via a direct call to the | |
57 | /// `<dyn Trait as Trait>::fn` instance (an `InstanceDef::Virtual`). | |
58 | /// | |
59 | /// Another example is functions annotated with `#[track_caller]`, which | |
60 | /// must have their implicit caller location argument populated for a call. | |
61 | /// Because this is a required part of the function's ABI but can't be tracked | |
62 | /// as a property of the function pointer, we use a single "caller location" | |
63 | /// (the definition of the function itself). | |
e74abb32 XL |
64 | ReifyShim(DefId), |
65 | ||
f035d41b XL |
66 | /// `<fn() as FnTrait>::call_*` (generated `FnTrait` implementation for `fn()` pointers). |
67 | /// | |
60c5eb7d | 68 | /// `DefId` is `FnTrait::call_*`. |
cc61c64b | 69 | FnPtrShim(DefId, Ty<'tcx>), |
3b2f2976 | 70 | |
f035d41b | 71 | /// Dynamic dispatch to `<dyn Trait as Trait>::fn`. |
60c5eb7d | 72 | /// |
f035d41b XL |
73 | /// This `InstanceDef` does not have callable MIR. Calls to `Virtual` instances must be |
74 | /// codegen'd as virtual calls through the vtable. | |
75 | /// | |
76 | /// If this is reified to a `fn` pointer, a `ReifyShim` is used (see `ReifyShim` above for more | |
77 | /// details on that). | |
cc61c64b | 78 | Virtual(DefId, usize), |
3b2f2976 | 79 | |
f035d41b XL |
80 | /// `<[FnMut closure] as FnOnce>::call_once`. |
81 | /// | |
82 | /// The `DefId` is the ID of the `call_once` method in `FnOnce`. | |
c295e0f8 | 83 | ClosureOnceShim { call_once: DefId, track_caller: bool }, |
3b2f2976 | 84 | |
353b0b11 FG |
85 | /// Compiler-generated accessor for thread locals which returns a reference to the thread local |
86 | /// the `DefId` defines. This is used to export thread locals from dylibs on platforms lacking | |
87 | /// native support. | |
88 | ThreadLocalShim(DefId), | |
89 | ||
74b04a01 | 90 | /// `core::ptr::drop_in_place::<T>`. |
f035d41b | 91 | /// |
74b04a01 XL |
92 | /// The `DefId` is for `core::ptr::drop_in_place`. |
93 | /// The `Option<Ty<'tcx>>` is either `Some(T)`, or `None` for empty drop | |
94 | /// glue. | |
cc61c64b | 95 | DropGlue(DefId, Option<Ty<'tcx>>), |
3b2f2976 | 96 | |
f035d41b XL |
97 | /// Compiler-generated `<T as Clone>::clone` implementation. |
98 | /// | |
99 | /// For all types that automatically implement `Copy`, a trivial `Clone` impl is provided too. | |
100 | /// Additionally, arrays, tuples, and closures get a `Clone` shim even if they aren't `Copy`. | |
101 | /// | |
102 | /// The `DefId` is for `Clone::clone`, the `Ty` is the type `T` with the builtin `Clone` impl. | |
3b2f2976 | 103 | CloneShim(DefId, Ty<'tcx>), |
353b0b11 FG |
104 | |
105 | /// Compiler-generated `<T as FnPtr>::addr` implementation. | |
106 | /// | |
107 | /// Automatically generated for all potentially higher-ranked `fn(I) -> R` types. | |
108 | /// | |
109 | /// The `DefId` is for `FnPtr::addr`, the `Ty` is the type `T`. | |
110 | FnPtrAddrShim(DefId, Ty<'tcx>), | |
cc61c64b XL |
111 | } |
112 | ||
dc9dc135 | 113 | impl<'tcx> Instance<'tcx> { |
3dfed10e XL |
114 | /// Returns the `Ty` corresponding to this `Instance`, with generic substitutions applied and |
115 | /// lifetimes erased, allowing a `ParamEnv` to be specified for use during normalization. | |
116 | pub fn ty(&self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Ty<'tcx> { | |
ff7c6d11 | 117 | let ty = tcx.type_of(self.def.def_id()); |
9ffffee4 | 118 | tcx.subst_and_normalize_erasing_regions(self.substs, param_env, ty.skip_binder()) |
dfeec247 XL |
119 | } |
120 | ||
121 | /// Finds a crate that contains a monomorphization of this instance that | |
122 | /// can be linked to from the local crate. A return value of `None` means | |
123 | /// no upstream crate provides such an exported monomorphization. | |
124 | /// | |
125 | /// This method already takes into account the global `-Zshare-generics` | |
126 | /// setting, always returning `None` if `share-generics` is off. | |
127 | pub fn upstream_monomorphization(&self, tcx: TyCtxt<'tcx>) -> Option<CrateNum> { | |
128 | // If we are not in share generics mode, we don't link to upstream | |
129 | // monomorphizations but always instantiate our own internal versions | |
130 | // instead. | |
131 | if !tcx.sess.opts.share_generics() { | |
132 | return None; | |
133 | } | |
134 | ||
135 | // If this is an item that is defined in the local crate, no upstream | |
136 | // crate can know about it/provide a monomorphization. | |
137 | if self.def_id().is_local() { | |
138 | return None; | |
139 | } | |
140 | ||
141 | // If this a non-generic instance, it cannot be a shared monomorphization. | |
74b04a01 | 142 | self.substs.non_erasable_generics().next()?; |
dfeec247 XL |
143 | |
144 | match self.def { | |
3dfed10e XL |
145 | InstanceDef::Item(def) => tcx |
146 | .upstream_monomorphizations_for(def.did) | |
dfeec247 XL |
147 | .and_then(|monos| monos.get(&self.substs).cloned()), |
148 | InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs), | |
149 | _ => None, | |
150 | } | |
ff7c6d11 XL |
151 | } |
152 | } | |
153 | ||
cc61c64b XL |
154 | impl<'tcx> InstanceDef<'tcx> { |
155 | #[inline] | |
3dfed10e XL |
156 | pub fn def_id(self) -> DefId { |
157 | match self { | |
158 | InstanceDef::Item(def) => def.did, | |
064997fb | 159 | InstanceDef::VTableShim(def_id) |
dfeec247 XL |
160 | | InstanceDef::ReifyShim(def_id) |
161 | | InstanceDef::FnPtrShim(def_id, _) | |
162 | | InstanceDef::Virtual(def_id, _) | |
163 | | InstanceDef::Intrinsic(def_id) | |
353b0b11 | 164 | | InstanceDef::ThreadLocalShim(def_id) |
c295e0f8 | 165 | | InstanceDef::ClosureOnceShim { call_once: def_id, track_caller: _ } |
dfeec247 | 166 | | InstanceDef::DropGlue(def_id, _) |
353b0b11 FG |
167 | | InstanceDef::CloneShim(def_id, _) |
168 | | InstanceDef::FnPtrAddrShim(def_id, _) => def_id, | |
cc61c64b XL |
169 | } |
170 | } | |
171 | ||
c295e0f8 XL |
172 | /// Returns the `DefId` of instances which might not require codegen locally. |
173 | pub fn def_id_if_not_guaranteed_local_codegen(self) -> Option<DefId> { | |
174 | match self { | |
175 | ty::InstanceDef::Item(def) => Some(def.did), | |
353b0b11 FG |
176 | ty::InstanceDef::DropGlue(def_id, Some(_)) | InstanceDef::ThreadLocalShim(def_id) => { |
177 | Some(def_id) | |
178 | } | |
064997fb | 179 | InstanceDef::VTableShim(..) |
c295e0f8 XL |
180 | | InstanceDef::ReifyShim(..) |
181 | | InstanceDef::FnPtrShim(..) | |
182 | | InstanceDef::Virtual(..) | |
183 | | InstanceDef::Intrinsic(..) | |
184 | | InstanceDef::ClosureOnceShim { .. } | |
185 | | InstanceDef::DropGlue(..) | |
353b0b11 FG |
186 | | InstanceDef::CloneShim(..) |
187 | | InstanceDef::FnPtrAddrShim(..) => None, | |
c295e0f8 XL |
188 | } |
189 | } | |
190 | ||
3dfed10e XL |
191 | #[inline] |
192 | pub fn with_opt_param(self) -> ty::WithOptConstParam<DefId> { | |
193 | match self { | |
194 | InstanceDef::Item(def) => def, | |
064997fb | 195 | InstanceDef::VTableShim(def_id) |
3dfed10e XL |
196 | | InstanceDef::ReifyShim(def_id) |
197 | | InstanceDef::FnPtrShim(def_id, _) | |
198 | | InstanceDef::Virtual(def_id, _) | |
199 | | InstanceDef::Intrinsic(def_id) | |
c295e0f8 | 200 | | InstanceDef::ClosureOnceShim { call_once: def_id, track_caller: _ } |
3dfed10e | 201 | | InstanceDef::DropGlue(def_id, _) |
353b0b11 FG |
202 | | InstanceDef::CloneShim(def_id, _) |
203 | | InstanceDef::ThreadLocalShim(def_id) | |
204 | | InstanceDef::FnPtrAddrShim(def_id, _) => ty::WithOptConstParam::unknown(def_id), | |
3dfed10e XL |
205 | } |
206 | } | |
207 | ||
cc61c64b | 208 | #[inline] |
353b0b11 FG |
209 | pub fn get_attrs( |
210 | &self, | |
211 | tcx: TyCtxt<'tcx>, | |
212 | attr: Symbol, | |
213 | ) -> impl Iterator<Item = &'tcx rustc_ast::Attribute> { | |
04454e1e | 214 | tcx.get_attrs(self.def_id(), attr) |
cc61c64b | 215 | } |
ff7c6d11 | 216 | |
dfeec247 XL |
217 | /// Returns `true` if the LLVM version of this instance is unconditionally |
218 | /// marked with `inline`. This implies that a copy of this instance is | |
219 | /// generated in every codegen unit. | |
220 | /// Note that this is only a hint. See the documentation for | |
221 | /// `generates_cgu_internal_copy` for more information. | |
222 | pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool { | |
ba9703b0 | 223 | use rustc_hir::definitions::DefPathData; |
ff7c6d11 | 224 | let def_id = match *self { |
3dfed10e | 225 | ty::InstanceDef::Item(def) => def.did, |
ff7c6d11 | 226 | ty::InstanceDef::DropGlue(_, Some(_)) => return false, |
353b0b11 | 227 | ty::InstanceDef::ThreadLocalShim(_) => return false, |
dfeec247 | 228 | _ => return true, |
ff7c6d11 | 229 | }; |
29967ef6 XL |
230 | matches!( |
231 | tcx.def_key(def_id).disambiguated_data.data, | |
232 | DefPathData::Ctor | DefPathData::ClosureExpr | |
233 | ) | |
ff7c6d11 XL |
234 | } |
235 | ||
dfeec247 XL |
236 | /// Returns `true` if the machine code for this instance is instantiated in |
237 | /// each codegen unit that references it. | |
238 | /// Note that this is only a hint! The compiler can globally decide to *not* | |
239 | /// do this in order to speed up compilation. CGU-internal copies are | |
240 | /// only exist to enable inlining. If inlining is not performed (e.g. at | |
241 | /// `-Copt-level=0`) then the time for generating them is wasted and it's | |
242 | /// better to create a single copy with external linkage. | |
243 | pub fn generates_cgu_internal_copy(&self, tcx: TyCtxt<'tcx>) -> bool { | |
244 | if self.requires_inline(tcx) { | |
245 | return true; | |
ff7c6d11 | 246 | } |
74b04a01 XL |
247 | if let ty::InstanceDef::DropGlue(.., Some(ty)) = *self { |
248 | // Drop glue generally wants to be instantiated at every codegen | |
ff7c6d11 XL |
249 | // unit, but without an #[inline] hint. We should make this |
250 | // available to normal end-users. | |
74b04a01 XL |
251 | if tcx.sess.opts.incremental.is_none() { |
252 | return true; | |
253 | } | |
254 | // When compiling with incremental, we can generate a *lot* of | |
255 | // codegen units. Including drop glue into all of them has a | |
256 | // considerable compile time cost. | |
257 | // | |
258 | // We include enums without destructors to allow, say, optimizing | |
259 | // drops of `Option::None` before LTO. We also respect the intent of | |
260 | // `#[inline]` on `Drop::drop` implementations. | |
261 | return ty.ty_adt_def().map_or(true, |adt_def| { | |
cdc7bbd5 XL |
262 | adt_def.destructor(tcx).map_or_else( |
263 | || adt_def.is_enum(), | |
264 | |dtor| tcx.codegen_fn_attrs(dtor.did).requests_inline(), | |
265 | ) | |
74b04a01 | 266 | }); |
ff7c6d11 | 267 | } |
353b0b11 FG |
268 | if let ty::InstanceDef::ThreadLocalShim(..) = *self { |
269 | return false; | |
270 | } | |
a1dfa0c6 | 271 | tcx.codegen_fn_attrs(self.def_id()).requests_inline() |
ff7c6d11 | 272 | } |
60c5eb7d XL |
273 | |
274 | pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool { | |
dfeec247 | 275 | match *self { |
136023e0 XL |
276 | InstanceDef::Item(ty::WithOptConstParam { did: def_id, .. }) |
277 | | InstanceDef::Virtual(def_id, _) => { | |
04454e1e | 278 | tcx.body_codegen_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER) |
dfeec247 | 279 | } |
c295e0f8 | 280 | InstanceDef::ClosureOnceShim { call_once: _, track_caller } => track_caller, |
dfeec247 XL |
281 | _ => false, |
282 | } | |
60c5eb7d | 283 | } |
1b1a35ee XL |
284 | |
285 | /// Returns `true` when the MIR body associated with this instance should be monomorphized | |
286 | /// by its users (e.g. codegen or miri) by substituting the `substs` from `Instance` (see | |
287 | /// `Instance::substs_for_mir_body`). | |
288 | /// | |
289 | /// Otherwise, returns `false` only for some kinds of shims where the construction of the MIR | |
290 | /// body should perform necessary substitutions. | |
291 | pub fn has_polymorphic_mir_body(&self) -> bool { | |
292 | match *self { | |
293 | InstanceDef::CloneShim(..) | |
353b0b11 FG |
294 | | InstanceDef::ThreadLocalShim(..) |
295 | | InstanceDef::FnPtrAddrShim(..) | |
1b1a35ee XL |
296 | | InstanceDef::FnPtrShim(..) |
297 | | InstanceDef::DropGlue(_, Some(_)) => false, | |
298 | InstanceDef::ClosureOnceShim { .. } | |
299 | | InstanceDef::DropGlue(..) | |
300 | | InstanceDef::Item(_) | |
301 | | InstanceDef::Intrinsic(..) | |
302 | | InstanceDef::ReifyShim(..) | |
303 | | InstanceDef::Virtual(..) | |
064997fb | 304 | | InstanceDef::VTableShim(..) => true, |
1b1a35ee XL |
305 | } |
306 | } | |
cc61c64b XL |
307 | } |
308 | ||
487cf647 FG |
309 | fn fmt_instance( |
310 | f: &mut fmt::Formatter<'_>, | |
311 | instance: &Instance<'_>, | |
312 | type_length: rustc_session::Limit, | |
313 | ) -> fmt::Result { | |
314 | ty::tls::with(|tcx| { | |
315 | let substs = tcx.lift(instance.substs).expect("could not lift for printing"); | |
316 | ||
317 | let s = FmtPrinter::new_with_limit(tcx, Namespace::ValueNS, type_length) | |
318 | .print_def_path(instance.def_id(), substs)? | |
319 | .into_buffer(); | |
320 | f.write_str(&s) | |
321 | })?; | |
322 | ||
323 | match instance.def { | |
324 | InstanceDef::Item(_) => Ok(()), | |
325 | InstanceDef::VTableShim(_) => write!(f, " - shim(vtable)"), | |
326 | InstanceDef::ReifyShim(_) => write!(f, " - shim(reify)"), | |
353b0b11 | 327 | InstanceDef::ThreadLocalShim(_) => write!(f, " - shim(tls)"), |
487cf647 FG |
328 | InstanceDef::Intrinsic(_) => write!(f, " - intrinsic"), |
329 | InstanceDef::Virtual(_, num) => write!(f, " - virtual#{}", num), | |
330 | InstanceDef::FnPtrShim(_, ty) => write!(f, " - shim({})", ty), | |
331 | InstanceDef::ClosureOnceShim { .. } => write!(f, " - shim"), | |
332 | InstanceDef::DropGlue(_, None) => write!(f, " - shim(None)"), | |
333 | InstanceDef::DropGlue(_, Some(ty)) => write!(f, " - shim(Some({}))", ty), | |
334 | InstanceDef::CloneShim(_, ty) => write!(f, " - shim({})", ty), | |
353b0b11 | 335 | InstanceDef::FnPtrAddrShim(_, ty) => write!(f, " - shim({})", ty), |
487cf647 FG |
336 | } |
337 | } | |
338 | ||
339 | pub struct ShortInstance<'a, 'tcx>(pub &'a Instance<'tcx>, pub usize); | |
340 | ||
341 | impl<'a, 'tcx> fmt::Display for ShortInstance<'a, 'tcx> { | |
0bf4aa26 | 342 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
487cf647 FG |
343 | fmt_instance(f, self.0, rustc_session::Limit(self.1)) |
344 | } | |
345 | } | |
532ac7d7 | 346 | |
487cf647 FG |
347 | impl<'tcx> fmt::Display for Instance<'tcx> { |
348 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | |
349 | ty::tls::with(|tcx| fmt_instance(f, self, tcx.type_length_limit())) | |
cc61c64b XL |
350 | } |
351 | } | |
352 | ||
dc9dc135 | 353 | impl<'tcx> Instance<'tcx> { |
dfeec247 XL |
354 | pub fn new(def_id: DefId, substs: SubstsRef<'tcx>) -> Instance<'tcx> { |
355 | assert!( | |
356 | !substs.has_escaping_bound_vars(), | |
357 | "substs of instance {:?} not normalized for codegen: {:?}", | |
358 | def_id, | |
359 | substs | |
360 | ); | |
3dfed10e | 361 | Instance { def: InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), substs } |
cc61c64b XL |
362 | } |
363 | ||
dc9dc135 | 364 | pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> { |
29967ef6 XL |
365 | let substs = InternalSubsts::for_item(tcx, def_id, |param, _| match param.kind { |
366 | ty::GenericParamDefKind::Lifetime => tcx.lifetimes.re_erased.into(), | |
367 | ty::GenericParamDefKind::Type { .. } => { | |
368 | bug!("Instance::mono: {:?} has type parameters", def_id) | |
369 | } | |
370 | ty::GenericParamDefKind::Const { .. } => { | |
371 | bug!("Instance::mono: {:?} has const parameters", def_id) | |
372 | } | |
373 | }); | |
374 | ||
375 | Instance::new(def_id, substs) | |
cc61c64b XL |
376 | } |
377 | ||
378 | #[inline] | |
379 | pub fn def_id(&self) -> DefId { | |
380 | self.def.def_id() | |
381 | } | |
ea8adc8c | 382 | |
9fa01778 | 383 | /// Resolves a `(def_id, substs)` pair to an (optional) instance -- most commonly, |
ea8adc8c XL |
384 | /// this is used to find the precise code that will run for a trait method invocation, |
385 | /// if known. | |
386 | /// | |
f9f354fc | 387 | /// Returns `Ok(None)` if we cannot resolve `Instance` to a specific instance. |
ea8adc8c XL |
388 | /// For example, in a context like this, |
389 | /// | |
04454e1e | 390 | /// ```ignore (illustrative) |
ea8adc8c XL |
391 | /// fn foo<T: Debug>(t: T) { ... } |
392 | /// ``` | |
393 | /// | |
f9f354fc | 394 | /// trying to resolve `Debug::fmt` applied to `T` will yield `Ok(None)`, because we do not |
ea8adc8c XL |
395 | /// know what code ought to run. (Note that this setting is also affected by the |
396 | /// `RevealMode` in the parameter environment.) | |
397 | /// | |
398 | /// Presuming that coherence and type-check have succeeded, if this method is invoked | |
94b46f34 | 399 | /// in a monomorphic context (i.e., like during codegen), then it is guaranteed to return |
f9f354fc XL |
400 | /// `Ok(Some(instance))`. |
401 | /// | |
5e7ed085 | 402 | /// Returns `Err(ErrorGuaranteed)` when the `Instance` resolution process |
f9f354fc XL |
403 | /// couldn't complete due to errors elsewhere - this is distinct |
404 | /// from `Ok(None)` to avoid misleading diagnostics when an error | |
405 | /// has already been/will be emitted, for the original cause | |
dc9dc135 XL |
406 | pub fn resolve( |
407 | tcx: TyCtxt<'tcx>, | |
408 | param_env: ty::ParamEnv<'tcx>, | |
409 | def_id: DefId, | |
410 | substs: SubstsRef<'tcx>, | |
5e7ed085 | 411 | ) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> { |
3dfed10e XL |
412 | Instance::resolve_opt_const_arg( |
413 | tcx, | |
414 | param_env, | |
415 | ty::WithOptConstParam::unknown(def_id), | |
416 | substs, | |
417 | ) | |
418 | } | |
419 | ||
9c376795 FG |
420 | pub fn expect_resolve( |
421 | tcx: TyCtxt<'tcx>, | |
422 | param_env: ty::ParamEnv<'tcx>, | |
423 | def_id: DefId, | |
424 | substs: SubstsRef<'tcx>, | |
425 | ) -> Instance<'tcx> { | |
426 | match ty::Instance::resolve(tcx, param_env, def_id, substs) { | |
427 | Ok(Some(instance)) => instance, | |
428 | _ => bug!( | |
429 | "failed to resolve instance for {}", | |
430 | tcx.def_path_str_with_substs(def_id, substs) | |
431 | ), | |
432 | } | |
433 | } | |
434 | ||
3dfed10e XL |
435 | // This should be kept up to date with `resolve`. |
436 | pub fn resolve_opt_const_arg( | |
437 | tcx: TyCtxt<'tcx>, | |
438 | param_env: ty::ParamEnv<'tcx>, | |
439 | def: ty::WithOptConstParam<DefId>, | |
440 | substs: SubstsRef<'tcx>, | |
5e7ed085 | 441 | ) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> { |
ba9703b0 XL |
442 | // All regions in the result of this query are erased, so it's |
443 | // fine to erase all of the input regions. | |
f9f354fc XL |
444 | |
445 | // HACK(eddyb) erase regions in `substs` first, so that `param_env.and(...)` | |
446 | // below is more likely to ignore the bounds in scope (e.g. if the only | |
447 | // generic parameters mentioned by `substs` were lifetime ones). | |
fc512014 | 448 | let substs = tcx.erase_regions(substs); |
f9f354fc XL |
449 | |
450 | // FIXME(eddyb) should this always use `param_env.with_reveal_all()`? | |
3dfed10e XL |
451 | if let Some((did, param_did)) = def.as_const_arg() { |
452 | tcx.resolve_instance_of_const_arg( | |
fc512014 | 453 | tcx.erase_regions(param_env.and((did, param_did, substs))), |
3dfed10e XL |
454 | ) |
455 | } else { | |
fc512014 | 456 | tcx.resolve_instance(tcx.erase_regions(param_env.and((def.did, substs)))) |
3dfed10e | 457 | } |
ea8adc8c | 458 | } |
ea8adc8c | 459 | |
e74abb32 XL |
460 | pub fn resolve_for_fn_ptr( |
461 | tcx: TyCtxt<'tcx>, | |
462 | param_env: ty::ParamEnv<'tcx>, | |
463 | def_id: DefId, | |
464 | substs: SubstsRef<'tcx>, | |
465 | ) -> Option<Instance<'tcx>> { | |
466 | debug!("resolve(def_id={:?}, substs={:?})", def_id, substs); | |
c295e0f8 XL |
467 | // Use either `resolve_closure` or `resolve_for_vtable` |
468 | assert!(!tcx.is_closure(def_id), "Called `resolve_for_fn_ptr` on closure: {:?}", def_id); | |
f9f354fc | 469 | Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| { |
e74abb32 | 470 | match resolved.def { |
3dfed10e | 471 | InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => { |
e74abb32 | 472 | debug!(" => fn pointer created for function with #[track_caller]"); |
3dfed10e | 473 | resolved.def = InstanceDef::ReifyShim(def.did); |
60c5eb7d XL |
474 | } |
475 | InstanceDef::Virtual(def_id, _) => { | |
476 | debug!(" => fn pointer created for virtual call"); | |
477 | resolved.def = InstanceDef::ReifyShim(def_id); | |
478 | } | |
479 | _ => {} | |
e74abb32 | 480 | } |
60c5eb7d XL |
481 | |
482 | resolved | |
e74abb32 XL |
483 | }) |
484 | } | |
485 | ||
dc9dc135 XL |
486 | pub fn resolve_for_vtable( |
487 | tcx: TyCtxt<'tcx>, | |
488 | param_env: ty::ParamEnv<'tcx>, | |
489 | def_id: DefId, | |
490 | substs: SubstsRef<'tcx>, | |
491 | ) -> Option<Instance<'tcx>> { | |
136023e0 | 492 | debug!("resolve_for_vtable(def_id={:?}, substs={:?})", def_id, substs); |
9ffffee4 | 493 | let fn_sig = tcx.fn_sig(def_id).subst_identity(); |
74b04a01 | 494 | let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty() |
e1599b0c XL |
495 | && fn_sig.input(0).skip_binder().is_param(0) |
496 | && tcx.generics_of(def_id).has_self; | |
a1dfa0c6 XL |
497 | if is_vtable_shim { |
498 | debug!(" => associated item with unsizeable self: Self"); | |
064997fb | 499 | Some(Instance { def: InstanceDef::VTableShim(def_id), substs }) |
a1dfa0c6 | 500 | } else { |
136023e0 XL |
501 | Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| { |
502 | match resolved.def { | |
503 | InstanceDef::Item(def) => { | |
504 | // We need to generate a shim when we cannot guarantee that | |
505 | // the caller of a trait object method will be aware of | |
506 | // `#[track_caller]` - this ensures that the caller | |
507 | // and callee ABI will always match. | |
508 | // | |
509 | // The shim is generated when all of these conditions are met: | |
510 | // | |
511 | // 1) The underlying method expects a caller location parameter | |
512 | // in the ABI | |
513 | if resolved.def.requires_caller_location(tcx) | |
514 | // 2) The caller location parameter comes from having `#[track_caller]` | |
515 | // on the implementation, and *not* on the trait method. | |
516 | && !tcx.should_inherit_track_caller(def.did) | |
517 | // If the method implementation comes from the trait definition itself | |
518 | // (e.g. `trait Foo { #[track_caller] my_fn() { /* impl */ } }`), | |
519 | // then we don't need to generate a shim. This check is needed because | |
520 | // `should_inherit_track_caller` returns `false` if our method | |
521 | // implementation comes from the trait block, and not an impl block | |
522 | && !matches!( | |
523 | tcx.opt_associated_item(def.did), | |
524 | Some(ty::AssocItem { | |
064997fb | 525 | container: ty::AssocItemContainer::TraitContainer, |
136023e0 XL |
526 | .. |
527 | }) | |
528 | ) | |
529 | { | |
c295e0f8 XL |
530 | if tcx.is_closure(def.did) { |
531 | debug!(" => vtable fn pointer created for closure with #[track_caller]: {:?} for method {:?} {:?}", | |
532 | def.did, def_id, substs); | |
533 | ||
534 | // Create a shim for the `FnOnce/FnMut/Fn` method we are calling | |
535 | // - unlike functions, invoking a closure always goes through a | |
536 | // trait. | |
537 | resolved = Instance { def: InstanceDef::ReifyShim(def_id), substs }; | |
538 | } else { | |
539 | debug!( | |
540 | " => vtable fn pointer created for function with #[track_caller]: {:?}", def.did | |
541 | ); | |
542 | resolved.def = InstanceDef::ReifyShim(def.did); | |
543 | } | |
136023e0 XL |
544 | } |
545 | } | |
546 | InstanceDef::Virtual(def_id, _) => { | |
547 | debug!(" => vtable fn pointer created for virtual call"); | |
548 | resolved.def = InstanceDef::ReifyShim(def_id); | |
549 | } | |
550 | _ => {} | |
551 | } | |
552 | ||
553 | resolved | |
554 | }) | |
a1dfa0c6 XL |
555 | } |
556 | } | |
557 | ||
ff7c6d11 | 558 | pub fn resolve_closure( |
dc9dc135 | 559 | tcx: TyCtxt<'tcx>, |
0bf4aa26 | 560 | def_id: DefId, |
e74abb32 | 561 | substs: ty::SubstsRef<'tcx>, |
dc9dc135 | 562 | requested_kind: ty::ClosureKind, |
064997fb | 563 | ) -> Option<Instance<'tcx>> { |
ba9703b0 | 564 | let actual_kind = substs.as_closure().kind(); |
ea8adc8c | 565 | |
ff7c6d11 | 566 | match needs_fn_once_adapter_shim(actual_kind, requested_kind) { |
dc9dc135 | 567 | Ok(true) => Instance::fn_once_adapter_instance(tcx, def_id, substs), |
064997fb | 568 | _ => Some(Instance::new(def_id, substs)), |
ff7c6d11 | 569 | } |
ea8adc8c | 570 | } |
a1dfa0c6 | 571 | |
dc9dc135 | 572 | pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> { |
3dfed10e | 573 | let def_id = tcx.require_lang_item(LangItem::DropInPlace, None); |
9ffffee4 | 574 | let substs = tcx.mk_substs(&[ty.into()]); |
9c376795 | 575 | Instance::expect_resolve(tcx, ty::ParamEnv::reveal_all(), def_id, substs) |
dc9dc135 XL |
576 | } |
577 | ||
487cf647 | 578 | #[instrument(level = "debug", skip(tcx), ret)] |
dc9dc135 XL |
579 | pub fn fn_once_adapter_instance( |
580 | tcx: TyCtxt<'tcx>, | |
581 | closure_did: DefId, | |
e74abb32 | 582 | substs: ty::SubstsRef<'tcx>, |
064997fb | 583 | ) -> Option<Instance<'tcx>> { |
3dfed10e | 584 | let fn_once = tcx.require_lang_item(LangItem::FnOnce, None); |
dfeec247 XL |
585 | let call_once = tcx |
586 | .associated_items(fn_once) | |
74b04a01 | 587 | .in_definition_order() |
ba9703b0 | 588 | .find(|it| it.kind == ty::AssocKind::Fn) |
dfeec247 XL |
589 | .unwrap() |
590 | .def_id; | |
c295e0f8 XL |
591 | let track_caller = |
592 | tcx.codegen_fn_attrs(closure_did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER); | |
593 | let def = ty::InstanceDef::ClosureOnceShim { call_once, track_caller }; | |
dc9dc135 XL |
594 | |
595 | let self_ty = tcx.mk_closure(closure_did, substs); | |
596 | ||
ba9703b0 | 597 | let sig = substs.as_closure().sig(); |
064997fb FG |
598 | let sig = |
599 | tcx.try_normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), sig).ok()?; | |
dc9dc135 | 600 | assert_eq!(sig.inputs().len(), 1); |
487cf647 | 601 | let substs = tcx.mk_substs_trait(self_ty, [sig.inputs()[0].into()]); |
dc9dc135 | 602 | |
487cf647 | 603 | debug!(?self_ty, ?sig); |
064997fb | 604 | Some(Instance { def, substs }) |
dc9dc135 XL |
605 | } |
606 | ||
1b1a35ee | 607 | /// Depending on the kind of `InstanceDef`, the MIR body associated with an |
ba9703b0 XL |
608 | /// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other |
609 | /// cases the MIR body is expressed in terms of the types found in the substitution array. | |
610 | /// In the former case, we want to substitute those generic types and replace them with the | |
611 | /// values from the substs when monomorphizing the function body. But in the latter case, we | |
612 | /// don't want to do that substitution, since it has already been done effectively. | |
613 | /// | |
1b1a35ee | 614 | /// This function returns `Some(substs)` in the former case and `None` otherwise -- i.e., if |
ba9703b0 | 615 | /// this function returns `None`, then the MIR body does not require substitution during |
1b1a35ee | 616 | /// codegen. |
29967ef6 | 617 | fn substs_for_mir_body(&self) -> Option<SubstsRef<'tcx>> { |
9ffffee4 | 618 | self.def.has_polymorphic_mir_body().then_some(self.substs) |
a1dfa0c6 | 619 | } |
3dfed10e | 620 | |
29967ef6 XL |
621 | pub fn subst_mir<T>(&self, tcx: TyCtxt<'tcx>, v: &T) -> T |
622 | where | |
9ffffee4 | 623 | T: TypeFoldable<TyCtxt<'tcx>> + Copy, |
29967ef6 | 624 | { |
04454e1e FG |
625 | if let Some(substs) = self.substs_for_mir_body() { |
626 | EarlyBinder(*v).subst(tcx, substs) | |
627 | } else { | |
628 | *v | |
629 | } | |
29967ef6 XL |
630 | } |
631 | ||
cdc7bbd5 | 632 | #[inline(always)] |
29967ef6 XL |
633 | pub fn subst_mir_and_normalize_erasing_regions<T>( |
634 | &self, | |
635 | tcx: TyCtxt<'tcx>, | |
636 | param_env: ty::ParamEnv<'tcx>, | |
fc512014 | 637 | v: T, |
29967ef6 XL |
638 | ) -> T |
639 | where | |
9ffffee4 | 640 | T: TypeFoldable<TyCtxt<'tcx>> + Clone, |
29967ef6 XL |
641 | { |
642 | if let Some(substs) = self.substs_for_mir_body() { | |
643 | tcx.subst_and_normalize_erasing_regions(substs, param_env, v) | |
644 | } else { | |
fc512014 | 645 | tcx.normalize_erasing_regions(param_env, v) |
29967ef6 XL |
646 | } |
647 | } | |
648 | ||
a2a8927a XL |
649 | #[inline(always)] |
650 | pub fn try_subst_mir_and_normalize_erasing_regions<T>( | |
651 | &self, | |
652 | tcx: TyCtxt<'tcx>, | |
653 | param_env: ty::ParamEnv<'tcx>, | |
654 | v: T, | |
655 | ) -> Result<T, NormalizationError<'tcx>> | |
656 | where | |
9ffffee4 | 657 | T: TypeFoldable<TyCtxt<'tcx>> + Clone, |
a2a8927a XL |
658 | { |
659 | if let Some(substs) = self.substs_for_mir_body() { | |
660 | tcx.try_subst_and_normalize_erasing_regions(substs, param_env, v) | |
661 | } else { | |
662 | tcx.try_normalize_erasing_regions(param_env, v) | |
663 | } | |
664 | } | |
665 | ||
3dfed10e | 666 | /// Returns a new `Instance` where generic parameters in `instance.substs` are replaced by |
6a06907d | 667 | /// identity parameters if they are determined to be unused in `instance.def`. |
3dfed10e XL |
668 | pub fn polymorphize(self, tcx: TyCtxt<'tcx>) -> Self { |
669 | debug!("polymorphize: running polymorphization analysis"); | |
064997fb | 670 | if !tcx.sess.opts.unstable_opts.polymorphize { |
3dfed10e XL |
671 | return self; |
672 | } | |
673 | ||
c295e0f8 XL |
674 | let polymorphized_substs = polymorphize(tcx, self.def, self.substs); |
675 | debug!("polymorphize: self={:?} polymorphized_substs={:?}", self, polymorphized_substs); | |
676 | Self { def: self.def, substs: polymorphized_substs } | |
3dfed10e XL |
677 | } |
678 | } | |
679 | ||
680 | fn polymorphize<'tcx>( | |
681 | tcx: TyCtxt<'tcx>, | |
c295e0f8 | 682 | instance: ty::InstanceDef<'tcx>, |
3dfed10e XL |
683 | substs: SubstsRef<'tcx>, |
684 | ) -> SubstsRef<'tcx> { | |
c295e0f8 XL |
685 | debug!("polymorphize({:?}, {:?})", instance, substs); |
686 | let unused = tcx.unused_generic_params(instance); | |
3dfed10e XL |
687 | debug!("polymorphize: unused={:?}", unused); |
688 | ||
689 | // If this is a closure or generator then we need to handle the case where another closure | |
690 | // from the function is captured as an upvar and hasn't been polymorphized. In this case, | |
691 | // the unpolymorphized upvar closure would result in a polymorphized closure producing | |
692 | // multiple mono items (and eventually symbol clashes). | |
c295e0f8 | 693 | let def_id = instance.def_id(); |
3dfed10e XL |
694 | let upvars_ty = if tcx.is_closure(def_id) { |
695 | Some(substs.as_closure().tupled_upvars_ty()) | |
9ffffee4 | 696 | } else if tcx.type_of(def_id).skip_binder().is_generator() { |
3dfed10e XL |
697 | Some(substs.as_generator().tupled_upvars_ty()) |
698 | } else { | |
699 | None | |
700 | }; | |
5e7ed085 | 701 | let has_upvars = upvars_ty.map_or(false, |ty| !ty.tuple_fields().is_empty()); |
3dfed10e XL |
702 | debug!("polymorphize: upvars_ty={:?} has_upvars={:?}", upvars_ty, has_upvars); |
703 | ||
704 | struct PolymorphizationFolder<'tcx> { | |
705 | tcx: TyCtxt<'tcx>, | |
fc512014 | 706 | } |
3dfed10e | 707 | |
9ffffee4 FG |
708 | impl<'tcx> ty::TypeFolder<TyCtxt<'tcx>> for PolymorphizationFolder<'tcx> { |
709 | fn interner(&self) -> TyCtxt<'tcx> { | |
3dfed10e XL |
710 | self.tcx |
711 | } | |
712 | ||
713 | fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> { | |
714 | debug!("fold_ty: ty={:?}", ty); | |
5099ac24 | 715 | match *ty.kind() { |
3dfed10e | 716 | ty::Closure(def_id, substs) => { |
c295e0f8 XL |
717 | let polymorphized_substs = polymorphize( |
718 | self.tcx, | |
719 | ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), | |
720 | substs, | |
721 | ); | |
3dfed10e XL |
722 | if substs == polymorphized_substs { |
723 | ty | |
724 | } else { | |
725 | self.tcx.mk_closure(def_id, polymorphized_substs) | |
726 | } | |
727 | } | |
728 | ty::Generator(def_id, substs, movability) => { | |
c295e0f8 XL |
729 | let polymorphized_substs = polymorphize( |
730 | self.tcx, | |
731 | ty::InstanceDef::Item(ty::WithOptConstParam::unknown(def_id)), | |
732 | substs, | |
733 | ); | |
3dfed10e XL |
734 | if substs == polymorphized_substs { |
735 | ty | |
736 | } else { | |
737 | self.tcx.mk_generator(def_id, polymorphized_substs, movability) | |
738 | } | |
739 | } | |
740 | _ => ty.super_fold_with(self), | |
741 | } | |
742 | } | |
743 | } | |
744 | ||
745 | InternalSubsts::for_item(tcx, def_id, |param, _| { | |
9c376795 | 746 | let is_unused = unused.is_unused(param.index); |
3dfed10e XL |
747 | debug!("polymorphize: param={:?} is_unused={:?}", param, is_unused); |
748 | match param.kind { | |
749 | // Upvar case: If parameter is a type parameter.. | |
750 | ty::GenericParamDefKind::Type { .. } if | |
751 | // ..and has upvars.. | |
752 | has_upvars && | |
753 | // ..and this param has the same type as the tupled upvars.. | |
754 | upvars_ty == Some(substs[param.index as usize].expect_ty()) => { | |
755 | // ..then double-check that polymorphization marked it used.. | |
756 | debug_assert!(!is_unused); | |
757 | // ..and polymorphize any closures/generators captured as upvars. | |
758 | let upvars_ty = upvars_ty.unwrap(); | |
759 | let polymorphized_upvars_ty = upvars_ty.fold_with( | |
760 | &mut PolymorphizationFolder { tcx }); | |
761 | debug!("polymorphize: polymorphized_upvars_ty={:?}", polymorphized_upvars_ty); | |
762 | ty::GenericArg::from(polymorphized_upvars_ty) | |
763 | }, | |
764 | ||
765 | // Simple case: If parameter is a const or type parameter.. | |
cdc7bbd5 | 766 | ty::GenericParamDefKind::Const { .. } | ty::GenericParamDefKind::Type { .. } if |
3dfed10e | 767 | // ..and is within range and unused.. |
9c376795 | 768 | unused.is_unused(param.index) => |
3dfed10e XL |
769 | // ..then use the identity for this parameter. |
770 | tcx.mk_param_from_def(param), | |
771 | ||
772 | // Otherwise, use the parameter as before. | |
773 | _ => substs[param.index as usize], | |
774 | } | |
775 | }) | |
ea8adc8c XL |
776 | } |
777 | ||
dc9dc135 XL |
778 | fn needs_fn_once_adapter_shim( |
779 | actual_closure_kind: ty::ClosureKind, | |
780 | trait_closure_kind: ty::ClosureKind, | |
781 | ) -> Result<bool, ()> { | |
ea8adc8c | 782 | match (actual_closure_kind, trait_closure_kind) { |
dfeec247 XL |
783 | (ty::ClosureKind::Fn, ty::ClosureKind::Fn) |
784 | | (ty::ClosureKind::FnMut, ty::ClosureKind::FnMut) | |
785 | | (ty::ClosureKind::FnOnce, ty::ClosureKind::FnOnce) => { | |
786 | // No adapter needed. | |
787 | Ok(false) | |
788 | } | |
ea8adc8c | 789 | (ty::ClosureKind::Fn, ty::ClosureKind::FnMut) => { |
9c376795 | 790 | // The closure fn `llfn` is a `fn(&self, ...)`. We want a |
94b46f34 | 791 | // `fn(&mut self, ...)`. In fact, at codegen time, these are |
ea8adc8c XL |
792 | // basically the same thing, so we can just return llfn. |
793 | Ok(false) | |
794 | } | |
ba9703b0 | 795 | (ty::ClosureKind::Fn | ty::ClosureKind::FnMut, ty::ClosureKind::FnOnce) => { |
dfeec247 | 796 | // The closure fn `llfn` is a `fn(&self, ...)` or `fn(&mut |
9c376795 | 797 | // self, ...)`. We want a `fn(self, ...)`. We can produce |
dfeec247 XL |
798 | // this by doing something like: |
799 | // | |
800 | // fn call_once(self, ...) { call_mut(&self, ...) } | |
801 | // fn call_once(mut self, ...) { call_mut(&mut self, ...) } | |
802 | // | |
803 | // These are both the same at codegen time. | |
804 | Ok(true) | |
ea8adc8c | 805 | } |
ba9703b0 | 806 | (ty::ClosureKind::FnMut | ty::ClosureKind::FnOnce, _) => Err(()), |
ea8adc8c XL |
807 | } |
808 | } | |
9c376795 FG |
809 | |
810 | // Set bits represent unused generic parameters. | |
811 | // An empty set indicates that all parameters are used. | |
812 | #[derive(Debug, Copy, Clone, Eq, PartialEq, Decodable, Encodable, HashStable)] | |
813 | pub struct UnusedGenericParams(FiniteBitSet<u32>); | |
814 | ||
353b0b11 FG |
815 | impl Default for UnusedGenericParams { |
816 | fn default() -> Self { | |
817 | UnusedGenericParams::new_all_used() | |
818 | } | |
819 | } | |
820 | ||
9c376795 FG |
821 | impl UnusedGenericParams { |
822 | pub fn new_all_unused(amount: u32) -> Self { | |
823 | let mut bitset = FiniteBitSet::new_empty(); | |
824 | bitset.set_range(0..amount); | |
825 | Self(bitset) | |
826 | } | |
827 | ||
828 | pub fn new_all_used() -> Self { | |
829 | Self(FiniteBitSet::new_empty()) | |
830 | } | |
831 | ||
832 | pub fn mark_used(&mut self, idx: u32) { | |
833 | self.0.clear(idx); | |
834 | } | |
835 | ||
836 | pub fn is_unused(&self, idx: u32) -> bool { | |
837 | self.0.contains(idx).unwrap_or(false) | |
838 | } | |
839 | ||
840 | pub fn is_used(&self, idx: u32) -> bool { | |
841 | !self.is_unused(idx) | |
842 | } | |
843 | ||
844 | pub fn all_used(&self) -> bool { | |
845 | self.0.is_empty() | |
846 | } | |
353b0b11 FG |
847 | |
848 | pub fn bits(&self) -> u32 { | |
849 | self.0.0 | |
850 | } | |
851 | ||
852 | pub fn from_bits(bits: u32) -> UnusedGenericParams { | |
853 | UnusedGenericParams(FiniteBitSet(bits)) | |
854 | } | |
9c376795 | 855 | } |