Skip to content

Commit 0476408

Browse files
committedApr 11, 2024
Added documentation to certain types
1 parent 62cffee commit 0476408

File tree

4 files changed

+102
-9
lines changed

4 files changed

+102
-9
lines changed
 

‎compiler/rustc_middle/src/ty/instance.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ impl<'tcx> fmt::Display for Instance<'tcx> {
414414
}
415415

416416
impl<'tcx> Instance<'tcx> {
417+
/// Creates a new instance of type `InstanceDef::Item`, with appropiate `def_id` and `args` set.
417418
pub fn new(def_id: DefId, args: GenericArgsRef<'tcx>) -> Instance<'tcx> {
418419
assert!(
419420
!args.has_escaping_bound_vars(),
@@ -437,6 +438,7 @@ impl<'tcx> Instance<'tcx> {
437438
Instance::new(def_id, args)
438439
}
439440

441+
/// Returns the `def_id` of this instance.
440442
#[inline]
441443
pub fn def_id(&self) -> DefId {
442444
self.def.def_id()
@@ -482,6 +484,7 @@ impl<'tcx> Instance<'tcx> {
482484
tcx.resolve_instance(tcx.erase_regions(param_env.and((def_id, args))))
483485
}
484486

487+
/// Behaves exactly like [`Self::resolve`], but panics on error.
485488
pub fn expect_resolve(
486489
tcx: TyCtxt<'tcx>,
487490
param_env: ty::ParamEnv<'tcx>,
@@ -618,6 +621,7 @@ impl<'tcx> Instance<'tcx> {
618621
}
619622
}
620623

624+
/// Returns an instance representing closure of type `requested_kind` with `def_id` and subst set to `args`.
621625
pub fn resolve_closure(
622626
tcx: TyCtxt<'tcx>,
623627
def_id: DefId,
@@ -632,6 +636,7 @@ impl<'tcx> Instance<'tcx> {
632636
}
633637
}
634638

639+
/// Returns an instance representing the function [`core::ptr::drop_in_place`] with its generic argument set to `ty`.
635640
pub fn resolve_drop_in_place(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> ty::Instance<'tcx> {
636641
let def_id = tcx.require_lang_item(LangItem::DropInPlace, None);
637642
let args = tcx.mk_args(&[ty.into()]);
@@ -741,6 +746,10 @@ impl<'tcx> Instance<'tcx> {
741746
self.def.has_polymorphic_mir_body().then_some(self.args)
742747
}
743748

749+
/// Instantiates a generic value `v`(like `Vec<T>`), substituting its generic arguments and turning it into a concrete one(like `i32`, or `Vec<f32>`).
750+
/// If a value is not generic, this will do nothing.
751+
/// This function does not erase lifetimes, so a value like `&'a i32` will remain unchanged.
752+
/// For monomorphizing generics while also erasing lifetimes, try using [`Self::instantiate_mir_and_normalize_erasing_regions`].
744753
pub fn instantiate_mir<T>(&self, tcx: TyCtxt<'tcx>, v: EarlyBinder<&T>) -> T
745754
where
746755
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
@@ -753,6 +762,11 @@ impl<'tcx> Instance<'tcx> {
753762
}
754763
}
755764

765+
/// Instantiates a generic value `v`(like `Vec<T>`), substituting its generic arguments and turning it into a concrete one(like `i32`, or `Vec<f32>`).
766+
/// This function erases lifetimes, so a value like `&'a i32` will become `&ReErased i32`.
767+
/// If a value is not generic and has no lifetime info, this will do nothing.
768+
/// For monomorphizing generics while preserving lifetimes, use [`Self::instantiate_mir`].
769+
/// This function will panic if normalization fails. If you want to handle normalization errors, use [`Self::try_instantiate_mir_and_normalize_erasing_regions`]
756770
#[inline(always)]
757771
// Keep me in sync with try_instantiate_mir_and_normalize_erasing_regions
758772
pub fn instantiate_mir_and_normalize_erasing_regions<T>(
@@ -771,6 +785,7 @@ impl<'tcx> Instance<'tcx> {
771785
}
772786
}
773787

788+
/// A version of [`Self::instantiate_mir_and_normalize_erasing_regions`] which will returns a [`NormalizationError`] on normalization failure instead of panicking.
774789
#[inline(always)]
775790
// Keep me in sync with instantiate_mir_and_normalize_erasing_regions
776791
pub fn try_instantiate_mir_and_normalize_erasing_regions<T>(
@@ -939,6 +954,7 @@ fn needs_fn_once_adapter_shim(
939954

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

@@ -949,36 +965,44 @@ impl Default for UnusedGenericParams {
949965
}
950966

951967
impl UnusedGenericParams {
968+
/// Creates a new [`UnusedGenericParams`] where all generic pameters are set as unused.
952969
pub fn new_all_unused(amount: u32) -> Self {
953970
let mut bitset = FiniteBitSet::new_empty();
954971
bitset.set_range(0..amount);
955972
Self(bitset)
956973
}
957974

975+
/// Creates a new [`UnusedGenericParams`] where all generic pameters are set as used.
958976
pub fn new_all_used() -> Self {
959977
Self(FiniteBitSet::new_empty())
960978
}
961979

980+
/// Marks a generic paramenter at index `idx` as used.
962981
pub fn mark_used(&mut self, idx: u32) {
963982
self.0.clear(idx);
964983
}
965984

985+
/// Returns true if generic paramenter at index `idx` unused, and false otherwise.
966986
pub fn is_unused(&self, idx: u32) -> bool {
967987
self.0.contains(idx).unwrap_or(false)
968988
}
969989

990+
/// Returns true if generic paramenter at index `idx` used, and false otherwise.
970991
pub fn is_used(&self, idx: u32) -> bool {
971992
!self.is_unused(idx)
972993
}
973994

995+
/// Returns true if all generic parameters are used, and false otherwise.
974996
pub fn all_used(&self) -> bool {
975997
self.0.is_empty()
976998
}
977999

1000+
/// Turns a [`UnusedGenericParams`] into its underlying bit representation.
9781001
pub fn bits(&self) -> u32 {
9791002
self.0.0
9801003
}
9811004

1005+
/// Creates a [`UnusedGenericParams`] from its bit representation.
9821006
pub fn from_bits(bits: u32) -> UnusedGenericParams {
9831007
UnusedGenericParams(FiniteBitSet(bits))
9841008
}

0 commit comments

Comments
 (0)