Skip to content

Commit 0c57959

Browse files
committed
Added documentation to functions and types contained within rustc_middle::ty::instance
1 parent cdd4ff8 commit 0c57959

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
341341
}
342342

343343
impl<'tcx> Instance<'tcx> {
344+
/// Creates a new instance of type `InstanceDef::Item`, with appropiate `def_id` and `args` set.
344345
pub fn new(def_id: DefId, args: GenericArgsRef<'tcx>) -> Instance<'tcx> {
345346
assert!(
346347
!args.has_escaping_bound_vars(),
@@ -365,6 +366,7 @@ impl<'tcx> Instance<'tcx> {
365366
}
366367

367368
#[inline]
369+
/// Returns the `def_id` of this instance.
368370
pub fn def_id(&self) -> DefId {
369371
self.def.def_id()
370372
}
@@ -408,7 +410,7 @@ impl<'tcx> Instance<'tcx> {
408410
let args = tcx.erase_regions(args);
409411
tcx.resolve_instance(tcx.erase_regions(param_env.and((def_id, args))))
410412
}
411-
413+
/// Behaves exactly like [`resolve`], but panics on error.
412414
pub fn expect_resolve(
413415
tcx: TyCtxt<'tcx>,
414416
param_env: ty::ParamEnv<'tcx>,
@@ -524,7 +526,7 @@ impl<'tcx> Instance<'tcx> {
524526
})
525527
}
526528
}
527-
529+
/// Returns an instance representing closure of type `requested_kind` with `def_id` and subst set to `args`.
528530
pub fn resolve_closure(
529531
tcx: TyCtxt<'tcx>,
530532
def_id: DefId,
@@ -538,7 +540,7 @@ impl<'tcx> Instance<'tcx> {
538540
_ => Instance::new(def_id, args),
539541
}
540542
}
541-
543+
/// Returns an instance representing the function [`drop_in_place`] with its generic argument set to `ty`.
542544
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
543545
let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
544546
let args = tcx.mk_args(&[ty.into()]);
@@ -571,7 +573,6 @@ impl<'tcx> Instance<'tcx> {
571573
debug!(?self_ty, args=?tupled_inputs_ty.tuple_fields());
572574
Instance { def, args }
573575
}
574-
575576
pub fn try_resolve_item_for_coroutine(
576577
tcx: TyCtxt<'tcx>,
577578
trait_item_id: DefId,
@@ -619,7 +620,6 @@ impl<'tcx> Instance<'tcx> {
619620
Some(Instance::new(trait_item_id, rcvr_args))
620621
}
621622
}
622-
623623
/// Depending on the kind of `InstanceDef`, the MIR body associated with an
624624
/// instance is expressed in terms of the generic parameters of `self.def_id()`, and in other
625625
/// cases the MIR body is expressed in terms of the types found in the substitution array.
@@ -633,7 +633,10 @@ impl<'tcx> Instance<'tcx> {
633633
fn args_for_mir_body(&self) -> Option<GenericArgsRef<'tcx>> {
634634
self.def.has_polymorphic_mir_body().then_some(self.args)
635635
}
636-
636+
/// Instantiates a generic value `v`(like `Vec<T>`), substituting its generic arguments and turning it into a concrete one(like `i32`, or `Vec<f32>`).
637+
/// If a value is not generic, this will do nothing.
638+
/// This function does not erase lifetimes, so a value like `&'a i32` will remain unchanged.
639+
/// For monomorphizing generics while also erasing lifetimes, try using [`instantiate_mir_and_normalize_erasing_regions`].
637640
pub fn instantiate_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T
638641
where
639642
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
@@ -645,7 +648,11 @@ impl<'tcx> Instance<'tcx> {
645648
v.instantiate_identity()
646649
}
647650
}
648-
651+
/// Instantiates a generic value `v`(like `Vec<T>`), substituting its generic arguments and turning it into a concrete one(like `i32`, or `Vec<f32>`).
652+
/// This function erases lifetimes, so a value like `&'a i32` will become `&ReErased i32`.
653+
/// If a value is not generic and has no lifetime info, this will do nothing.
654+
/// For monomorphizing generics while preserving lifetimes, use [`instantiate_mir`].
655+
/// This function will panic if normalization fails. If you want to handle normalization errors, use [`try_instantiate_mir_and_normalize_erasing_regions`]
649656
#[inline(always)]
650657
pub fn instantiate_mir_and_normalize_erasing_regions<T>(
651658
&self,
@@ -662,7 +669,7 @@ impl<'tcx> Instance<'tcx> {
662669
tcx.normalize_erasing_regions(param_env, v.skip_binder())
663670
}
664671
}
665-
672+
/// A version of [`instantiate_mir_and_normalize_erasing_regions`] which will returns a [`NormalizationError`] on normalization failure instead of panicking.
666673
#[inline(always)]
667674
pub fn try_instantiate_mir_and_normalize_erasing_regions<T>(
668675
&self,
@@ -823,6 +830,7 @@ fn needs_fn_once_adapter_shim(
823830

824831
// Set bits represent unused generic parameters.
825832
// An empty set indicates that all parameters are used.
833+
/// A data structure used to store information about which generic parameters are in use, and which are not.
826834
#[derive(Debug, Copy, Clone, Eq, PartialEq, Decodable, Encodable, HashStable)]
827835
pub struct UnusedGenericParams(FiniteBitSet<u32>);
828836

@@ -833,36 +841,37 @@ impl Default for UnusedGenericParams {
833841
}
834842

835843
impl UnusedGenericParams {
844+
/// Creates a new [`UnusedGenericParams`] where all generic pameters are set as unused.
836845
pub fn new_all_unused(amount: u32) -> Self {
837846
let mut bitset = FiniteBitSet::new_empty();
838847
bitset.set_range(0..amount);
839848
Self(bitset)
840849
}
841-
850+
/// Creates a new [`UnusedGenericParams`] where all generic pameters are set as used.
842851
pub fn new_all_used() -> Self {
843852
Self(FiniteBitSet::new_empty())
844853
}
845-
854+
/// Marks a generic paramenter at index `idx` as used.
846855
pub fn mark_used(&mut self, idx: u32) {
847856
self.0.clear(idx);
848857
}
849-
858+
/// Returns true if generic paramenter at index `idx` unused, and false otherwise.
850859
pub fn is_unused(&self, idx: u32) -> bool {
851860
self.0.contains(idx).unwrap_or(false)
852861
}
853-
862+
/// Returns true if generic paramenter at index `idx` used, and false otherwise.
854863
pub fn is_used(&self, idx: u32) -> bool {
855864
!self.is_unused(idx)
856865
}
857-
866+
/// Returns true if all generic parameters are used, and false otherwise.
858867
pub fn all_used(&self) -> bool {
859868
self.0.is_empty()
860869
}
861-
870+
/// Turns a [`UnusedGenericParams`] into its underlying bit representation.
862871
pub fn bits(&self) -> u32 {
863872
self.0.0
864873
}
865-
874+
/// Creates a [`UnusedGenericParams`] from its bit representation.
866875
pub fn from_bits(bits: u32) -> UnusedGenericParams {
867876
UnusedGenericParams(FiniteBitSet(bits))
868877
}

0 commit comments

Comments
 (0)